Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ state = ConversationState()
|
|
| 12 |
|
| 13 |
def chat_with_groq(message, history):
|
| 14 |
messages = [{"role": "user" if i % 2 == 0 else "assistant", "content": msg}
|
| 15 |
-
for i, msg in enumerate(history + [message])]
|
| 16 |
|
| 17 |
payload = {"messages": messages}
|
| 18 |
if state.conversation_id:
|
|
@@ -25,33 +25,36 @@ def chat_with_groq(message, history):
|
|
| 25 |
return response_data["response"]
|
| 26 |
|
| 27 |
def load_conversation(conversation_id):
|
|
|
|
|
|
|
| 28 |
response = requests.get(f"{BACKEND_URL}/conversations/{conversation_id}")
|
| 29 |
if response.status_code == 200:
|
| 30 |
conversation_data = response.json()
|
| 31 |
state.conversation_id = conversation_id
|
| 32 |
-
|
|
|
|
| 33 |
else:
|
| 34 |
return []
|
| 35 |
|
| 36 |
-
def respond(message, chat_history):
|
| 37 |
-
bot_message = chat_with_groq(message, chat_history)
|
| 38 |
-
chat_history.append((message, bot_message))
|
| 39 |
-
return "", chat_history
|
| 40 |
-
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
gr.Markdown("# Groq Chatbot with Conversation Persistence")
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
load_button = gr.Button("Load Conversation")
|
| 47 |
|
| 48 |
chatbot = gr.Chatbot()
|
| 49 |
msg = gr.Textbox()
|
| 50 |
clear = gr.Button("Clear")
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 53 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 54 |
-
|
| 55 |
load_button.click(load_conversation, inputs=[conversation_id_input], outputs=[chatbot])
|
| 56 |
|
|
|
|
|
|
|
| 57 |
demo.launch()
|
|
|
|
| 12 |
|
| 13 |
def chat_with_groq(message, history):
|
| 14 |
messages = [{"role": "user" if i % 2 == 0 else "assistant", "content": msg}
|
| 15 |
+
for i, msg in enumerate([item for sublist in history for item in sublist] + [message])]
|
| 16 |
|
| 17 |
payload = {"messages": messages}
|
| 18 |
if state.conversation_id:
|
|
|
|
| 25 |
return response_data["response"]
|
| 26 |
|
| 27 |
def load_conversation(conversation_id):
|
| 28 |
+
if not conversation_id:
|
| 29 |
+
return []
|
| 30 |
response = requests.get(f"{BACKEND_URL}/conversations/{conversation_id}")
|
| 31 |
if response.status_code == 200:
|
| 32 |
conversation_data = response.json()
|
| 33 |
state.conversation_id = conversation_id
|
| 34 |
+
messages = conversation_data["messages"]
|
| 35 |
+
return [(msg["content"], messages[i+1]["content"]) for i, msg in enumerate(messages[::2])]
|
| 36 |
else:
|
| 37 |
return []
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
with gr.Blocks() as demo:
|
| 40 |
gr.Markdown("# Groq Chatbot with Conversation Persistence")
|
| 41 |
|
| 42 |
+
conversation_id_input = gr.Textbox(label="Conversation ID (optional)")
|
| 43 |
+
load_button = gr.Button("Load Conversation")
|
|
|
|
| 44 |
|
| 45 |
chatbot = gr.Chatbot()
|
| 46 |
msg = gr.Textbox()
|
| 47 |
clear = gr.Button("Clear")
|
| 48 |
|
| 49 |
+
def respond(message, chat_history):
|
| 50 |
+
bot_message = chat_with_groq(message, chat_history)
|
| 51 |
+
chat_history.append((message, bot_message))
|
| 52 |
+
return "", chat_history
|
| 53 |
+
|
| 54 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 55 |
clear.click(lambda: None, None, chatbot, queue=False)
|
|
|
|
| 56 |
load_button.click(load_conversation, inputs=[conversation_id_input], outputs=[chatbot])
|
| 57 |
|
| 58 |
+
gr.Markdown(f"## Current Conversation ID: {state.conversation_id}")
|
| 59 |
+
|
| 60 |
demo.launch()
|