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()