mdpatel2 commited on
Commit
4f9abb2
·
verified ·
1 Parent(s): 600d7b0

refactor with new endpoint also

Browse files
Files changed (1) hide show
  1. app.py +34 -30
app.py CHANGED
@@ -1,42 +1,46 @@
1
  import gradio as gr
2
  import requests
3
 
4
- theme = gr.themes.Base(
5
- primary_hue="cyan",
6
- secondary_hue="blue",
7
- )
8
-
9
- def chat_with_rasa(user_message, history):
10
- response = requests.post(
11
- "http://clairify.ai/webhooks/rest/webhook",
12
- json={"sender": "user", "message": user_message}
13
- )
14
-
15
- try:
16
- # Get the raw response content
17
- raw_response = response.content.decode("utf-8")
18
- chat_log = history
19
- chat_log += f"\n\nRaw Response:\n{raw_response}"
20
-
21
- # Attempt to parse JSON response
22
- bot_messages = response.json()
23
- if isinstance(bot_messages, list):
24
- chat_log += f"\n\nYou: {user_message}"
25
- for msg in bot_messages:
26
- chat_log += f"\nBot: {msg.get('text', 'No response')}"
 
 
 
27
  else:
28
- chat_log += "\n\nError: Unexpected response type from Rasa."
29
- return chat_log
30
- except ValueError as e:
31
- return f"Error decoding JSON response: {str(e)}"
 
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
- history = gr.Textbox(label="Chat History", value="", lines=12, interactive=False)
37
- user_message = gr.Textbox(label="Your Message", lines=2, placeholder="Type your message here...")
38
  submit_button = gr.Button("Send")
39
- submit_button.click(fn=chat_with_rasa, inputs=[user_message, history], outputs=history)
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