Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,17 +8,14 @@ def chatbot(user_input, history):
|
|
| 8 |
if history is None:
|
| 9 |
history = []
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
messages.append({"role": "assistant", "content": str(bot)})
|
| 19 |
-
|
| 20 |
-
# current message
|
| 21 |
-
messages.append({"role": "user", "content": str(user_input)})
|
| 22 |
|
| 23 |
try:
|
| 24 |
response = client.chat.completions.create(
|
|
@@ -31,17 +28,24 @@ def chatbot(user_input, history):
|
|
| 31 |
except Exception as e:
|
| 32 |
bot_reply = f"Error: {str(e)}"
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
return
|
| 37 |
|
| 38 |
|
| 39 |
with gr.Blocks() as demo:
|
| 40 |
gr.Markdown("## 🤖 Simple Groq Chatbot")
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
msg = gr.Textbox(placeholder="Type your message...")
|
| 44 |
state = gr.State([])
|
|
|
|
| 45 |
clear = gr.Button("Clear")
|
| 46 |
|
| 47 |
msg.submit(chatbot, [msg, state], [chatbot_ui, state])
|
|
|
|
| 8 |
if history is None:
|
| 9 |
history = []
|
| 10 |
|
| 11 |
+
# ✅ history is already in correct format now
|
| 12 |
+
messages = history.copy()
|
| 13 |
|
| 14 |
+
# add current user message
|
| 15 |
+
messages.append({
|
| 16 |
+
"role": "user",
|
| 17 |
+
"content": user_input
|
| 18 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
try:
|
| 21 |
response = client.chat.completions.create(
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
bot_reply = f"Error: {str(e)}"
|
| 30 |
|
| 31 |
+
# add assistant reply
|
| 32 |
+
messages.append({
|
| 33 |
+
"role": "assistant",
|
| 34 |
+
"content": bot_reply
|
| 35 |
+
})
|
| 36 |
|
| 37 |
+
return messages, messages
|
| 38 |
|
| 39 |
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
gr.Markdown("## 🤖 Simple Groq Chatbot")
|
| 42 |
|
| 43 |
+
# ✅ IMPORTANT CHANGE HERE
|
| 44 |
+
chatbot_ui = gr.Chatbot(type="messages")
|
| 45 |
+
|
| 46 |
msg = gr.Textbox(placeholder="Type your message...")
|
| 47 |
state = gr.State([])
|
| 48 |
+
|
| 49 |
clear = gr.Button("Clear")
|
| 50 |
|
| 51 |
msg.submit(chatbot, [msg, state], [chatbot_ui, state])
|