nsomabalint commited on
Commit
8bd4cad
·
verified ·
1 Parent(s): 26a0ac9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -15
app.py CHANGED
@@ -1,8 +1,13 @@
1
  import gradio as gr
2
  import requests
3
  import json
 
4
 
5
- def respond(message, history, rasa_api_url):
 
 
 
 
6
  """
7
  Send a message to a Rasa chatbot API and get the response.
8
 
@@ -10,6 +15,7 @@ def respond(message, history, rasa_api_url):
10
  message (str): The user's message
11
  history (list): Chat history
12
  rasa_api_url (str): URL of the Rasa API endpoint
 
13
  """
14
  # Process the Rasa API URL
15
  if not rasa_api_url:
@@ -24,10 +30,13 @@ def respond(message, history, rasa_api_url):
24
  if "webhooks/rest/webhook" not in rasa_api_url:
25
  rasa_api_url = rasa_api_url + "webhooks/rest/webhook"
26
 
 
 
 
27
  try:
28
  # Prepare the payload
29
  payload = {
30
- "sender": "user",
31
  "message": message
32
  }
33
 
@@ -53,7 +62,6 @@ def respond(message, history, rasa_api_url):
53
  return "No response from the bot."
54
  else:
55
  return f"Error: Received status code {response.status_code} from the Rasa server."
56
-
57
  except requests.exceptions.RequestException as e:
58
  return f"Connection error: {str(e)}"
59
  except json.JSONDecodeError:
@@ -61,19 +69,44 @@ def respond(message, history, rasa_api_url):
61
  except Exception as e:
62
  return f"An unexpected error occurred: {str(e)}"
63
 
 
 
 
64
  # Create the Gradio interface
65
- demo = gr.ChatInterface(
66
- respond,
67
- additional_inputs=[
68
- gr.Textbox(
69
- value="",
70
- placeholder="e.g., http://your-rasa-server.com:5005/webhooks/rest/webhook",
71
- label="Rasa API URL"
72
- ),
73
- ],
74
- title="Rasa Chatbot Interface",
75
- description="Enter the URL of your Rasa chatbot API and start chatting."
76
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  if __name__ == "__main__":
79
  demo.launch()
 
1
  import gradio as gr
2
  import requests
3
  import json
4
+ import uuid
5
 
6
+ def generate_random_id():
7
+ """Generate a random sender ID if none is provided"""
8
+ return str(uuid.uuid4())[:8]
9
+
10
+ def respond(message, history, rasa_api_url, nickname):
11
  """
12
  Send a message to a Rasa chatbot API and get the response.
13
 
 
15
  message (str): The user's message
16
  history (list): Chat history
17
  rasa_api_url (str): URL of the Rasa API endpoint
18
+ nickname (str): User's nickname to be used as sender ID
19
  """
20
  # Process the Rasa API URL
21
  if not rasa_api_url:
 
30
  if "webhooks/rest/webhook" not in rasa_api_url:
31
  rasa_api_url = rasa_api_url + "webhooks/rest/webhook"
32
 
33
+ # Determine sender ID - use nickname if provided, otherwise use the session ID
34
+ sender_id = nickname if nickname else gr.State(generate_random_id()).value
35
+
36
  try:
37
  # Prepare the payload
38
  payload = {
39
+ "sender": sender_id,
40
  "message": message
41
  }
42
 
 
62
  return "No response from the bot."
63
  else:
64
  return f"Error: Received status code {response.status_code} from the Rasa server."
 
65
  except requests.exceptions.RequestException as e:
66
  return f"Connection error: {str(e)}"
67
  except json.JSONDecodeError:
 
69
  except Exception as e:
70
  return f"An unexpected error occurred: {str(e)}"
71
 
72
+ # State to store the random ID for this session
73
+ session_id = gr.State(generate_random_id)
74
+
75
  # Create the Gradio interface
76
+ with gr.Blocks(title="Rasa Chatbot Interface") as demo:
77
+ gr.Markdown("# Rasa Chatbot Interface")
78
+ gr.Markdown("Enter the URL of your Rasa chatbot API, optionally provide a nickname, and start chatting.")
79
+
80
+ with gr.Row():
81
+ with gr.Column(scale=3):
82
+ rasa_api_url = gr.Textbox(
83
+ value="",
84
+ placeholder="e.g., http://your-rasa-server.com:5005/webhooks/rest/webhook",
85
+ label="Rasa API URL"
86
+ )
87
+ with gr.Column(scale=1):
88
+ nickname = gr.Textbox(
89
+ value="",
90
+ placeholder="Enter a nickname (optional)",
91
+ label="Your Nickname"
92
+ )
93
+
94
+ chatbot = gr.Chatbot()
95
+ msg = gr.Textbox(placeholder="Type your message here...", label="Message")
96
+ clear = gr.Button("Clear")
97
+
98
+ # Set up event handlers
99
+ msg.submit(
100
+ respond,
101
+ [msg, chatbot, rasa_api_url, nickname],
102
+ [chatbot]
103
+ ).then(
104
+ lambda: "",
105
+ None,
106
+ [msg]
107
+ )
108
+
109
+ clear.click(lambda: None, None, chatbot)
110
 
111
  if __name__ == "__main__":
112
  demo.launch()