Aadityaramrame commited on
Commit
e8e3235
·
verified ·
1 Parent(s): 5a6cb79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -20
app.py CHANGED
@@ -1,29 +1,34 @@
 
1
  import gradio as gr
2
  from chatbot_functions import DataProcessor, Chatbot
3
 
4
- # Initialize data and chatbot
5
- data_processor = DataProcessor() # loads cleaned_medquad.csv
6
  chatbot = Chatbot(data_processor)
7
 
8
- def chat_with_backend(message, history=None):
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
- # Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  iface = gr.ChatInterface(
23
- fn=chat_with_backend,
24
- title="Care Companion Chatbot",
25
- description="Ask health-related questions and get reliable answers from Care Companion."
26
  )
27
 
28
- if __name__ == "__main__":
29
- iface.launch(server_name="0.0.0.0", server_port=7860, share=True, debug=True)
 
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()