Spaces:
Sleeping
Sleeping
upload gradio_chat.py
Browse files- gradio_chat.py +31 -0
gradio_chat.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# gradio_chat.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
# Define the FastAPI URL
|
| 6 |
+
FASTAPI_URL = "http://127.0.0.1:8000/chat/"
|
| 7 |
+
|
| 8 |
+
# Function to call the FastAPI endpoint
|
| 9 |
+
def ask_question(question):
|
| 10 |
+
response = requests.get(FASTAPI_URL, params={"str1": question})
|
| 11 |
+
# response = requests.post(FASTAPI_URL, json={"question": question})
|
| 12 |
+
return response.json().get("message", "No response")
|
| 13 |
+
|
| 14 |
+
# Define the Gradio chat interface
|
| 15 |
+
with gr.Blocks() as demo:
|
| 16 |
+
chatbot = gr.Chatbot()
|
| 17 |
+
msg = gr.Textbox(show_label=False, placeholder="Type your question here...")
|
| 18 |
+
submit = gr.Button("Submit")
|
| 19 |
+
|
| 20 |
+
def respond(message, history):
|
| 21 |
+
response = ask_question(message)
|
| 22 |
+
history.append((message, response))
|
| 23 |
+
return history, ""
|
| 24 |
+
|
| 25 |
+
submit.click(respond, [msg, chatbot], [chatbot, msg])
|
| 26 |
+
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
| 27 |
+
|
| 28 |
+
# Launch the Gradio interface
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
demo.launch()
|
| 31 |
+
|