ImagineAI-Real commited on
Commit
4253f05
·
1 Parent(s): d051db3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -39
app.py CHANGED
@@ -1,47 +1,38 @@
1
  import gradio as gr
2
  import requests
 
 
3
 
4
- # Set up API endpoint
5
  url = "https://75739ca5-2942-4ef4-b25b-705dae6a0946.id.repl.co/connect_to_websocket"
6
 
7
- # Define function for bot response
8
- def get_bot_response(user_id, message):
9
- # Send input message to API
10
- response = requests.post(url, data={"inputmsg": message, "userid": user_id})
11
-
12
- # Get the bot's response
13
  bot_response = response.text
14
-
15
- # Return the bot's response
16
  return bot_response
17
 
18
- # Set up Gradio chatbot interface
19
- title = "Chatbot"
20
- description = "Enter your message and the bot will respond!"
21
- chatbot = gr.Chatbot(
22
- get_bot_response,
23
- username="User",
24
- placeholder="Enter your message here...",
25
- live=True,
26
- button_text="Send",
27
- label="Chatbot",
28
- xss_safe=True,
29
- max_messages=10,
30
- min_height="400px",
31
- allow_speech=True,
32
- )
33
-
34
- # Set up user ID input
35
- user_id_input = gr.inputs.Textbox(label="User ID", placeholder="Enter your user ID")
36
-
37
- # Set up Gradio interface
38
- interface = gr.Interface(
39
- fn=get_bot_response,
40
- inputs=[user_id_input, chatbot],
41
- outputs="text",
42
- title=title,
43
- description=description,
44
- )
45
-
46
- # Launch the interface
47
- interface.launch()
 
1
  import gradio as gr
2
  import requests
3
+ import random
4
+ import time
5
 
6
+ # Set up API URL
7
  url = "https://75739ca5-2942-4ef4-b25b-705dae6a0946.id.repl.co/connect_to_websocket"
8
 
9
+ # Define function to send message to API and get response
10
+ def get_bot_response(user_input, user_id):
11
+ response = requests.post(url, data={"inputmsg": user_input, "userid": user_id})
 
 
 
12
  bot_response = response.text
 
 
13
  return bot_response
14
 
15
+ with gr.Blocks() as imaginechat:
16
+ gr.HTML('<h1 style="text-align: center; margin-bottom: 1rem"><a href="https://huggingface.co/spaces/ImagineAI-Real/ImagineChat" target="_blank">ImagineChat</a></h1>')
17
+ gr.HTML('<p>Imagine a Chatbot</p>')
18
+ chatbot = gr.Chatbot()
19
+ msg = gr.inputs.Textbox()
20
+ user_id_input = gr.inputs.Textbox(label="Enter your user ID:")
21
+ clear_button = gr.Button("Clear")
22
+ chat_history = []
23
+
24
+ def respond(message, user_id):
25
+ bot_message = get_bot_response(message, user_id)
26
+ chat_history.append((message, bot_message))
27
+ time.sleep(1)
28
+ return "", chat_history
29
+
30
+ def submit(message):
31
+ user_id = user_id_input.value
32
+ return respond(message, user_id)
33
+
34
+ msg.submit(respond, [msg, user_id_input], [msg, chatbot])
35
+
36
+ clear_button.click(lambda: chat_history.clear(), [chatbot])
37
+
38
+ imaginechat.launch()