Spaces:
Build error
Build error
Commit
·
ba4186b
1
Parent(s):
e239898
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,55 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
url = "https://75739ca5-2942-4ef4-b25b-705dae6a0946.id.repl.co/connect_to_websocket"
|
| 6 |
|
| 7 |
-
# Define user
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
chat_window = gr.outputs.HTML(html="")
|
| 15 |
-
|
| 16 |
-
# Define function to send user input to API
|
| 17 |
-
def send_input(inputmsg, userid):
|
| 18 |
-
response = requests.post(url, data={"inputmsg": inputmsg, "userid": userid})
|
| 19 |
-
return response.text
|
| 20 |
-
|
| 21 |
-
# Define function to update chat window with new message
|
| 22 |
-
def update_chat_window():
|
| 23 |
-
# Get user input and user ID
|
| 24 |
-
inputmsg = input_msg.value
|
| 25 |
-
userid = user_id.value
|
| 26 |
-
|
| 27 |
-
# Send input message to API
|
| 28 |
-
bot_response = send_input(inputmsg, userid)
|
| 29 |
-
|
| 30 |
-
# Update chat window with bot response
|
| 31 |
-
chat_window.html += f"<p><strong>You:</strong> {inputmsg}</p>"
|
| 32 |
-
chat_window.html += f"<p><strong>Bot:</strong> {bot_response}</p>"
|
| 33 |
-
|
| 34 |
-
# Clear input message textbox
|
| 35 |
-
input_msg.update("")
|
| 36 |
|
| 37 |
# Define Gradio interface
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
+
import gradio as gr
|
| 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 user input to API and get bot response
|
| 8 |
+
def get_bot_response(user_input, user_id):
|
| 9 |
+
# Send user input to API
|
| 10 |
+
response = requests.post(url, data={"inputmsg": user_input, "userid": user_id})
|
| 11 |
+
# Get bot's response
|
| 12 |
+
bot_response = response.text
|
| 13 |
+
return bot_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Define Gradio interface
|
| 16 |
+
def chat_interface(user_id):
|
| 17 |
+
chat_history = []
|
| 18 |
+
chatbot = gr.Chatbot(get_bot_response, args=[user_id])
|
| 19 |
+
|
| 20 |
+
# Define function to handle user input and bot response
|
| 21 |
+
def chat(input_text):
|
| 22 |
+
# Add user input to chat history
|
| 23 |
+
chat_history.append(("You", input_text))
|
| 24 |
+
# Get bot response and add to chat history
|
| 25 |
+
bot_response = chatbot.get_response(input_text)
|
| 26 |
+
chat_history.append(("Bot", bot_response))
|
| 27 |
+
# Format chat history as string
|
| 28 |
+
chat_history_str = "\n".join([f"{name}: {msg}" for name, msg in chat_history])
|
| 29 |
+
return chat_history_str
|
| 30 |
+
|
| 31 |
+
# Define interface components
|
| 32 |
+
user_id_input = gr.TextInput(label="User ID", default=user_id)
|
| 33 |
+
chat_input = gr.TextInput(label="Chat", placeholder="Type your message here")
|
| 34 |
+
chat_output = gr.Textbox(label="Chat history", placeholder="Conversation history will appear here", output_readonly=True)
|
| 35 |
+
submit_button = gr.Button(label="Send")
|
| 36 |
+
|
| 37 |
+
# Define function to clear chat history
|
| 38 |
+
def clear_chat():
|
| 39 |
+
nonlocal chat_history
|
| 40 |
+
chat_history = []
|
| 41 |
+
chat_output.update("")
|
| 42 |
+
|
| 43 |
+
clear_button = gr.Button(label="Clear chat", callback=clear_chat)
|
| 44 |
+
|
| 45 |
+
# Define interface layout
|
| 46 |
+
inputs = [user_id_input, chat_input, submit_button, clear_button]
|
| 47 |
+
outputs = [chat_output]
|
| 48 |
+
interface = gr.Interface(fn=chat, inputs=inputs, outputs=outputs, title="Chatbot", theme="compact")
|
| 49 |
+
|
| 50 |
+
return interface
|
| 51 |
+
|
| 52 |
+
# Launch interface
|
| 53 |
+
gradio_user_id = "user123"
|
| 54 |
+
interface = chat_interface(gradio_user_id)
|
| 55 |
+
interface.launch()
|