Spaces:
Runtime error
Runtime error
Mehul Patel commited on
Commit ·
454bd37
1
Parent(s): 159b02a
removed global state
Browse files
app.py
CHANGED
|
@@ -90,39 +90,34 @@ theme = gr.themes.Default(primary_hue="cyan", secondary_hue="violet")
|
|
| 90 |
|
| 91 |
with gr.Blocks(theme=theme) as demo:
|
| 92 |
|
| 93 |
-
chat_history = gr.State([])
|
| 94 |
rasa_client = RasaSocketIOClient(rasa_io_url)
|
| 95 |
|
| 96 |
-
async def interact_with_rasa(user_input):
|
| 97 |
logger.important(f"User input received: {user_input}")
|
| 98 |
-
|
| 99 |
if not rasa_client.sio.connected:
|
| 100 |
logger.important("Rasa client not connected, attempting to connect...")
|
| 101 |
await rasa_client.connect()
|
| 102 |
-
|
| 103 |
logger.important(f"Sending payload to Rasa: {user_input}")
|
| 104 |
-
|
| 105 |
is_first_message = True
|
| 106 |
-
|
| 107 |
async for message in rasa_client.send_message(user_input):
|
| 108 |
-
|
| 109 |
logger.important(f"Processing messages: {message}")
|
| 110 |
-
|
| 111 |
if is_first_message:
|
| 112 |
new_entry = (user_input, message.get('text', ''))
|
| 113 |
is_first_message = False
|
| 114 |
else:
|
| 115 |
new_entry = (None, message.get('text', ''))
|
| 116 |
-
|
| 117 |
-
chat_history
|
| 118 |
-
|
| 119 |
-
yield "", chat_history.value
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
chatbot = gr.Chatbot(label="Conversation")
|
| 122 |
msg = gr.Textbox(placeholder="Say Hi! to wake me up", show_label=False)
|
| 123 |
-
clear = gr.
|
| 124 |
-
|
| 125 |
-
msg.submit(interact_with_rasa, inputs=[msg], outputs=[msg, chatbot])
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
logger.important("Launching Gradio demo...")
|
|
|
|
| 90 |
|
| 91 |
with gr.Blocks(theme=theme) as demo:
|
| 92 |
|
|
|
|
| 93 |
rasa_client = RasaSocketIOClient(rasa_io_url)
|
| 94 |
|
| 95 |
+
async def interact_with_rasa(user_input, chat_history):
|
| 96 |
logger.important(f"User input received: {user_input}")
|
|
|
|
| 97 |
if not rasa_client.sio.connected:
|
| 98 |
logger.important("Rasa client not connected, attempting to connect...")
|
| 99 |
await rasa_client.connect()
|
|
|
|
| 100 |
logger.important(f"Sending payload to Rasa: {user_input}")
|
|
|
|
| 101 |
is_first_message = True
|
|
|
|
| 102 |
async for message in rasa_client.send_message(user_input):
|
|
|
|
| 103 |
logger.important(f"Processing messages: {message}")
|
|
|
|
| 104 |
if is_first_message:
|
| 105 |
new_entry = (user_input, message.get('text', ''))
|
| 106 |
is_first_message = False
|
| 107 |
else:
|
| 108 |
new_entry = (None, message.get('text', ''))
|
| 109 |
+
chat_history.append(new_entry)
|
| 110 |
+
yield "", chat_history
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
async def handle_clear_button():
|
| 113 |
+
await rasa_client.disconnect()
|
| 114 |
+
return "", None
|
| 115 |
+
|
| 116 |
chatbot = gr.Chatbot(label="Conversation")
|
| 117 |
msg = gr.Textbox(placeholder="Say Hi! to wake me up", show_label=False)
|
| 118 |
+
clear = gr.Button("Clear")
|
| 119 |
+
clear.click(handle_clear_button, inputs=[], outputs=[msg, chatbot])
|
| 120 |
+
msg.submit(interact_with_rasa, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
logger.important("Launching Gradio demo...")
|