Spaces:
Runtime error
Runtime error
refactor with new endpoint also
Browse files
app.py
CHANGED
|
@@ -1,42 +1,46 @@
|
|
| 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 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
else:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="red", secondary_hue="pink")) as demo:
|
| 34 |
gr.Markdown("## Rasa Chatbot")
|
| 35 |
gr.Markdown("This is a simple chat interface for a Rasa chatbot.")
|
| 36 |
-
|
| 37 |
-
|
| 38 |
submit_button = gr.Button("Send")
|
| 39 |
-
submit_button.click(fn=
|
| 40 |
|
| 41 |
demo.launch()
|
| 42 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
+
# Define the function to interact with Rasa
|
| 5 |
+
def interact_with_rasa(chat_history, user_input):
|
| 6 |
+
# Concatenate user input to chat history
|
| 7 |
+
chat_history += f"You: {user_input}\n"
|
| 8 |
+
|
| 9 |
+
# URL of your Rasa server
|
| 10 |
+
rasa_url = "https://clairify.ai/webhooks/rest/webhook" # Update the URL
|
| 11 |
+
|
| 12 |
+
# Payload to send to Rasa
|
| 13 |
+
payload = {
|
| 14 |
+
"sender": "user",
|
| 15 |
+
"message": user_input
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
# Send request to Rasa server
|
| 19 |
+
response = requests.post(rasa_url, json=payload)
|
| 20 |
+
|
| 21 |
+
# Parse Rasa response
|
| 22 |
+
if response.status_code == 200:
|
| 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 |
+
chat_history += "Bot: Error: Empty response from Rasa server.\n"
|
| 32 |
+
else:
|
| 33 |
+
chat_history += "Bot: Error: Unable to connect to Rasa server.\n"
|
| 34 |
+
|
| 35 |
+
return chat_history
|
| 36 |
|
| 37 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="red", secondary_hue="pink")) as demo:
|
| 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 |
demo.launch()
|
| 46 |
|