Spaces:
Runtime error
Runtime error
updated to use chatbot component
Browse files
app.py
CHANGED
|
@@ -1,46 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
rasa_response = response.json()
|
| 24 |
-
|
| 25 |
-
# Check if response is not empty
|
| 26 |
-
if rasa_response and len(rasa_response) > 0:
|
| 27 |
-
# Extract text from the first element of the list
|
| 28 |
-
response_text = rasa_response[0].get("text", "")
|
| 29 |
-
chat_history += f"Bot: {response_text}\n"
|
| 30 |
else:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
return chat_history
|
| 36 |
|
| 37 |
-
|
| 38 |
-
gr.Markdown("## Rasa Chatbot")
|
| 39 |
-
gr.Markdown("This is a simple chat interface for a Rasa chatbot.")
|
| 40 |
-
chat_history = gr.Textbox(label="Chat History", value="", lines=12, interactive=False)
|
| 41 |
-
user_input = gr.Textbox(label="Your Message", lines=2, placeholder="Type your message here...")
|
| 42 |
-
submit_button = gr.Button("Send")
|
| 43 |
-
submit_button.click(fn=interact_with_rasa, inputs=[chat_history, user_input], outputs=chat_history)
|
| 44 |
|
| 45 |
-
|
|
|
|
| 46 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
+
with gr.Blocks() as demo:
|
| 5 |
+
chatbot = gr.Chatbot()
|
| 6 |
+
msg = gr.Textbox()
|
| 7 |
+
clear = gr.ClearButton([msg, chatbot])
|
| 8 |
+
|
| 9 |
+
def interact_with_rasa(user_input, chat_history):
|
| 10 |
+
rasa_url = "https://clairify.ai/webhooks/rest/webhook"
|
| 11 |
+
payload = {"sender": "user", "message": user_input}
|
| 12 |
+
response = requests.post(rasa_url, json=payload)
|
| 13 |
+
|
| 14 |
+
if response.status_code == 200:
|
| 15 |
+
rasa_response = response.json()
|
| 16 |
+
if rasa_response and len(rasa_response) > 0:
|
| 17 |
+
bot_responses = [msg.get("text", "") for msg in rasa_response]
|
| 18 |
+
chat_history.append((user_input, bot_responses[0]))
|
| 19 |
+
for response in bot_responses[1:]:
|
| 20 |
+
chat_history.append((None, response))
|
| 21 |
+
else:
|
| 22 |
+
bot_responses = ["Error: Empty response from Rasa server."]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
else:
|
| 24 |
+
bot_responses = ["Error: Unable to connect to Rasa server."]
|
| 25 |
+
|
| 26 |
+
return "", chat_history
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
msg.submit(interact_with_rasa, [msg, chatbot], [msg, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
if __name__ == "__main__":
|
| 31 |
+
demo.launch()
|
| 32 |
|