MIYASAJID19 commited on
Commit
a4e86b7
·
verified ·
1 Parent(s): b5f154f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -63
app.py CHANGED
@@ -1,63 +1,65 @@
1
- import pickle
2
- from sentence_transformers import SentenceTransformer, util
3
- import torch
4
- import gradio as gr
5
- from fastapi import FastAPI, Query
6
- from fastapi.middleware.cors import CORSMiddleware
7
- from fastapi.responses import JSONResponse
8
- import nest_asyncio
9
- import threading
10
-
11
- # Load model
12
- with open("chatbot.pkl", "rb") as f:
13
- data = pickle.load(f)
14
-
15
- questions = data["questions"]
16
- answers = data["answers"]
17
- question_embeddings = data["embeddings"]
18
- model = SentenceTransformer('all-MiniLM-L6-v2')
19
-
20
- def chat(user_question, threshold=0.4, top_k=1):
21
- user_embedding = model.encode(user_question, convert_to_tensor=True)
22
- cos_scores = util.cos_sim(user_embedding, question_embeddings)[0]
23
- top_results = torch.topk(cos_scores, k=top_k)
24
-
25
- for score, idx in zip(top_results.values, top_results.indices):
26
- if score.item() >= threshold:
27
- return answers[idx]
28
-
29
- return "Sorry, I am not able to answer that."
30
-
31
- # --- FastAPI app for query params ---
32
- api = FastAPI()
33
- api.add_middleware(
34
- CORSMiddleware,
35
- allow_origins=["*"],
36
- allow_methods=["*"],
37
- allow_headers=["*"],
38
- )
39
-
40
- @api.get("/")
41
- def get_chat(question: str = Query(..., description="Your question here")):
42
- answer = chat(question)
43
- return JSONResponse({"answer": answer})
44
-
45
- # --- Gradio interface ---
46
- iface = gr.Interface(
47
- fn=chat,
48
- inputs=gr.Textbox(lines=2, placeholder="Ask a question..."),
49
- outputs=gr.Textbox(),
50
- title="FAQ Chatbot",
51
- description="Ask any question and get answers from the FAQ."
52
- )
53
-
54
- # To run both Gradio and FastAPI in the same Space
55
- def run_gradio():
56
- iface.launch(server_name="0.0.0.0", server_port=7860)
57
-
58
- import uvicorn, nest_asyncio
59
- nest_asyncio.apply()
60
- threading.Thread(target=run_gradio, daemon=True).start()
61
-
62
- # Run FastAPI
63
- uvicorn.run(api, host="0.0.0.0", port=8000)
 
 
 
1
+ import pickle
2
+ import torch
3
+ from sentence_transformers import SentenceTransformer, util
4
+ import gradio as gr
5
+ from fastapi import FastAPI, Query
6
+ from fastapi.middleware.cors import CORSMiddleware
7
+ from fastapi.responses import JSONResponse
8
+ import threading
9
+ import uvicorn
10
+ import nest_asyncio
11
+
12
+ # --------------- Load model & embeddings ---------------
13
+ with open("chatbot.pkl", "rb") as f:
14
+ data = pickle.load(f)
15
+
16
+ questions = data["questions"]
17
+ answers = data["answers"]
18
+ question_embeddings = data["embeddings"]
19
+
20
+ model = SentenceTransformer('all-MiniLM-L6-v2')
21
+
22
+ def chat(user_question, threshold=0.4, top_k=1):
23
+ """Return best answer or fallback if none found."""
24
+ user_embedding = model.encode(user_question, convert_to_tensor=True)
25
+ cos_scores = util.cos_sim(user_embedding, question_embeddings)[0]
26
+ top_results = torch.topk(cos_scores, k=top_k)
27
+
28
+ for score, idx in zip(top_results.values, top_results.indices):
29
+ if score.item() >= threshold:
30
+ return {"matched_question": questions[idx], "answer": answers[idx], "score": score.item()}
31
+
32
+ return {"matched_question": None, "answer": "Sorry, I am not able to answer that.", "score": None}
33
+
34
+ # --------------- FastAPI API ---------------
35
+ api = FastAPI()
36
+ api.add_middleware(
37
+ CORSMiddleware,
38
+ allow_origins=["*"],
39
+ allow_methods=["*"],
40
+ allow_headers=["*"],
41
+ )
42
+
43
+ @api.get("/")
44
+ def get_chat(question: str = Query(..., description="Your question here")):
45
+ response = chat(question)
46
+ return JSONResponse(response)
47
+
48
+ # --------------- Gradio UI ---------------
49
+ iface = gr.Interface(
50
+ fn=lambda q: chat(q)["answer"],
51
+ inputs=gr.Textbox(lines=2, placeholder="Ask a question..."),
52
+ outputs=gr.Textbox(),
53
+ title="FAQ Chatbot",
54
+ description="Ask any question and get answers from the FAQ."
55
+ )
56
+
57
+ def run_gradio():
58
+ iface.launch(server_name="0.0.0.0", server_port=7860)
59
+
60
+ # --------------- Run both FastAPI + Gradio ---------------
61
+ nest_asyncio.apply()
62
+ threading.Thread(target=run_gradio, daemon=True).start()
63
+
64
+ # Run FastAPI on port 8000
65
+ uvicorn.run(api, host="0.0.0.0", port=8000)