Spaces:
Sleeping
Sleeping
Commit
·
2b26969
1
Parent(s):
b104675
Update app.py
Browse files
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
|
8 |
def get_bot_response(user_input, user_id):
|
9 |
response = requests.post(url, data={"inputmsg": user_input, "userid": user_id})
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
def
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|