Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,65 @@
|
|
| 1 |
-
import pickle
|
| 2 |
-
|
| 3 |
-
import
|
| 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
|
| 9 |
-
import
|
| 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 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
| 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)
|