ImagineAI-Real commited on
Commit
2b26969
·
1 Parent(s): b104675

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -27
app.py CHANGED
@@ -1,34 +1,61 @@
1
  import gradio as gr
2
  import requests
 
 
3
 
4
- # Set up API URL
5
  url = "https://75739ca5-2942-4ef4-b25b-705dae6a0946.id.repl.co/connect_to_websocket"
6
 
7
- # Define function to send message to API and get response
8
  def get_bot_response(user_input, user_id):
9
  response = requests.post(url, data={"inputmsg": user_input, "userid": user_id})
10
- bot_response = response.text
11
- return bot_response
12
-
13
- # Set up gradio interface
14
- def chatbot_interface(user_id):
15
- chat_history = []
16
- chatbot_output = gr.outputs.Textbox()
17
- chat_window = gr.outputs.Textbox(label="Chat History:", type="key_value")
18
-
19
- def update_chatbot_response(message):
20
- bot_response = get_bot_response(message, user_id)
21
- chat_history.append((message, bot_response))
22
- chatbot_output.update(bot_response)
23
- chat_window.update(chat_history)
24
-
25
- chat_input = gr.inputs.Textbox(label="Enter your message here:", placeholder="Type here...")
26
- chat_panel = gr.Interface(update_chatbot_response, inputs=chat_input, outputs=[chatbot_output, chat_window],
27
- title="Chatbot", theme="compact", layout="vertical", live=True)
28
-
29
- return chat_panel
30
-
31
- # Launch gradio interface
32
- if __name__ == '__main__':
33
- gr.Interface(fn=chatbot_interface, inputs="text", outputs="text",
34
- title="Chatbot", description="Enter your user ID to start the chat.").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import requests
3
+ import random
4
+ import time
5
 
6
+ # Set up API endpoint URL
7
  url = "https://75739ca5-2942-4ef4-b25b-705dae6a0946.id.repl.co/connect_to_websocket"
8
 
9
+ # Define the chatbot's response function
10
  def get_bot_response(user_input, user_id):
11
  response = requests.post(url, data={"inputmsg": user_input, "userid": user_id})
12
+ return response.text
13
+
14
+ # Define the Gradio interface
15
+ def chatbot_interface():
16
+ # Set up chatbot and message input components
17
+ chatbot = gr.Chatbot()
18
+ message = gr.TextInput(label="Message")
19
+
20
+ # Define function for responding to user input
21
+ def chatbot_response(msg, chat_history):
22
+ # Get the user's message and user ID
23
+ user_message = msg.lower()
24
+ user_id = chatbot.session_id
25
+
26
+ # Send message to API to get bot's response
27
+ bot_message = get_bot_response(user_message, user_id)
28
+
29
+ # Add messages to chat history
30
+ chat_history.append(('User', user_message))
31
+ chat_history.append(('Chatbot', bot_message))
32
+
33
+ # Return chat history
34
+ return chat_history[-10:]
35
+
36
+ # Create chat history component
37
+ chat_history = gr.MultiText(lines=10)
38
+
39
+ # Create submit button to send messages
40
+ submit_button = gr.Button(text="Send")
41
+
42
+ # Define function to clear chat history
43
+ def clear_chat_history():
44
+ chat_history.clear()
45
+
46
+ # Create clear button to clear chat history
47
+ clear_button = gr.Button(text="Clear Chat", onclick=clear_chat_history)
48
+
49
+ # Define and return the interface
50
+ interface = gr.Interface(fn=chatbot_response, inputs=message, outputs=chat_history, title="Chatbot",
51
+ description="Talk to the chatbot", live=True, theme="compact",
52
+ examples=[["Hi"], ["What is your name?"], ["How are you doing?"], ["Goodbye"]],
53
+ layout="vertical",
54
+ allow_flagging=False,
55
+ allow_screenshot=False,
56
+ allow_download=False)
57
+ interface.test_launch()
58
+ return interface
59
+
60
+ # Launch the interface
61
+ chatbot_interface()