Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,50 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
|
| 3 |
-
from langchain_groq import ChatGroq
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
gr.
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
response, history = chatbot_response(input_text)
|
| 51 |
-
return response, history
|
| 52 |
-
|
| 53 |
-
submit_btn.click(fn=handle_submit, inputs=user_input, outputs=[response_output, chat_history])
|
| 54 |
-
|
| 55 |
-
# Launch app
|
| 56 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
|
| 5 |
+
# Session-wide context
|
| 6 |
+
conversation_history = [SystemMessage(content="You are a comedian AI assistant")]
|
| 7 |
+
|
| 8 |
+
def chatbot_response(api_key, user_input):
|
| 9 |
+
if not api_key:
|
| 10 |
+
return "Please enter your Groq API key.", ""
|
| 11 |
+
|
| 12 |
+
# Pass API key to ChatGroq
|
| 13 |
+
chat = ChatGroq(model="gemma2-9b-it", temperature=0.5, api_key=api_key)
|
| 14 |
+
|
| 15 |
+
# Append user input and get response
|
| 16 |
+
conversation_history.append(HumanMessage(content=user_input))
|
| 17 |
+
response = chat.invoke(conversation_history)
|
| 18 |
+
conversation_history.append(AIMessage(content=response.content))
|
| 19 |
+
|
| 20 |
+
# Format chat history
|
| 21 |
+
chat_log = ""
|
| 22 |
+
for msg in conversation_history:
|
| 23 |
+
if isinstance(msg, SystemMessage):
|
| 24 |
+
chat_log += f"βοΈ *System Role*: {msg.content}\n\n"
|
| 25 |
+
elif isinstance(msg, HumanMessage):
|
| 26 |
+
chat_log += f"π€ **User**: {msg.content}\n\n"
|
| 27 |
+
elif isinstance(msg, AIMessage):
|
| 28 |
+
chat_log += f"π€ **Bot**: {msg.content}\n\n"
|
| 29 |
+
|
| 30 |
+
return response.content, chat_log
|
| 31 |
+
|
| 32 |
+
# Gradio UI
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown("# π€ Conversational Q&A Chatbot")
|
| 35 |
+
gr.Markdown("Enter your Groq API key and chat below!")
|
| 36 |
+
|
| 37 |
+
api_key_box = gr.Textbox(label="π Groq API Key", type="password")
|
| 38 |
+
user_input = gr.Textbox(label="π¬ Your Question")
|
| 39 |
+
submit_btn = gr.Button("Ask")
|
| 40 |
+
|
| 41 |
+
response_output = gr.Textbox(label="π§ Response", interactive=False)
|
| 42 |
+
chat_history = gr.Markdown()
|
| 43 |
+
|
| 44 |
+
submit_btn.click(
|
| 45 |
+
fn=chatbot_response,
|
| 46 |
+
inputs=[api_key_box, user_input],
|
| 47 |
+
outputs=[response_output, chat_history]
|
| 48 |
+
)
|
| 49 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
demo.launch()
|