Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,34 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from chatbot_functions import DataProcessor, Chatbot
|
| 3 |
|
| 4 |
-
# Initialize
|
| 5 |
-
data_processor = DataProcessor()
|
| 6 |
chatbot = Chatbot(data_processor)
|
| 7 |
|
| 8 |
-
|
| 9 |
-
if not message.strip():
|
| 10 |
-
return "Please enter a question."
|
| 11 |
-
|
| 12 |
-
try:
|
| 13 |
-
best_question, response, source = chatbot.get_response(message)
|
| 14 |
-
if best_question == "UNKNOWN":
|
| 15 |
-
return response
|
| 16 |
-
else:
|
| 17 |
-
return f"Matched question: {best_question}\nAnswer: {response}\nSource: {source}"
|
| 18 |
-
except Exception as e:
|
| 19 |
-
return f"Error: {str(e)}"
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
iface = gr.ChatInterface(
|
| 23 |
-
fn=
|
| 24 |
-
title="Care Companion Chatbot"
|
| 25 |
-
description="Ask health-related questions and get reliable answers from Care Companion."
|
| 26 |
)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
import gradio as gr
|
| 3 |
from chatbot_functions import DataProcessor, Chatbot
|
| 4 |
|
| 5 |
+
# Initialize
|
| 6 |
+
data_processor = DataProcessor()
|
| 7 |
chatbot = Chatbot(data_processor)
|
| 8 |
|
| 9 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
@app.post("/api/chat")
|
| 12 |
+
async def chat(request: Request):
|
| 13 |
+
data = await request.json()
|
| 14 |
+
message = data.get("message", "").strip()
|
| 15 |
+
|
| 16 |
+
if not message:
|
| 17 |
+
return {"reply": "Please enter a valid question."}
|
| 18 |
+
|
| 19 |
+
best_question, response, source = chatbot.get_response(message)
|
| 20 |
+
if best_question == "UNKNOWN":
|
| 21 |
+
return {"reply": response}
|
| 22 |
+
else:
|
| 23 |
+
return {
|
| 24 |
+
"reply": f"Matched question: {best_question}\nAnswer: {response}\nSource: {source}"
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# Gradio UI (optional for Hugging Face Space)
|
| 28 |
iface = gr.ChatInterface(
|
| 29 |
+
fn=lambda msg, history=None: chatbot.get_response(msg)[1],
|
| 30 |
+
title="Care Companion Chatbot"
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
+
import threading
|
| 34 |
+
threading.Thread(target=lambda: iface.launch(server_name="0.0.0.0", server_port=7860, share=False)).start()
|