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