mdpatel2 commited on
Commit
577a154
·
verified ·
1 Parent(s): 4f9abb2

updated to use chatbot component

Browse files
Files changed (1) hide show
  1. app.py +25 -39
app.py CHANGED
@@ -1,46 +1,32 @@
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
 
 
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