Spaces:
Build error
Build error
Commit
·
e239898
1
Parent(s):
5ae2b4f
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# Define API URL
|
| 5 |
+
url = "https://75739ca5-2942-4ef4-b25b-705dae6a0946.id.repl.co/connect_to_websocket"
|
| 6 |
+
|
| 7 |
+
# Define user ID
|
| 8 |
+
user_id = gr.inputs.Textbox(label="User ID")
|
| 9 |
+
|
| 10 |
+
# Define input message textbox
|
| 11 |
+
input_msg = gr.inputs.Textbox(label="You")
|
| 12 |
+
|
| 13 |
+
# Define chat window
|
| 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 |
+
iface = gr.Interface(
|
| 39 |
+
fn=None,
|
| 40 |
+
inputs=[user_id, input_msg],
|
| 41 |
+
outputs=chat_window,
|
| 42 |
+
title="Chatbot",
|
| 43 |
+
live=True,
|
| 44 |
+
allow_flagging=False,
|
| 45 |
+
layout="vertical"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Replace the default interface function with the update_chat_window function
|
| 49 |
+
iface.interface_function = update_chat_window
|
| 50 |
+
|
| 51 |
+
# Run the interface
|
| 52 |
+
iface.launch()
|