Spaces:
Build error
Build error
Commit
·
382301a
1
Parent(s):
e5576ba
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,34 @@
|
|
| 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
|
| 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 |
-
#
|
| 16 |
-
def
|
| 17 |
chat_history = []
|
| 18 |
-
|
| 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, user_id=user_id)
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
interface = gr.Interface(fn=chat, inputs=inputs, outputs=outputs, title="Chatbot", theme="compact")
|
| 49 |
|
| 50 |
-
return
|
| 51 |
|
| 52 |
-
# Launch interface
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
| 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", key_label="User", value_label="Bot")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|