mirxakamran893 commited on
Commit
01817d1
Β·
verified Β·
1 Parent(s): a77771e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -23,13 +23,19 @@ MODEL = "qwen/qwq-32b:free"
23
  # βœ… FastAPI app
24
  app = FastAPI()
25
 
26
- # βœ… Context Retriever
27
- def get_context(query, top_k=5):
28
  query_vec = embed_model.encode([query])
29
  D, I = index.search(np.array(query_vec), top_k)
30
- return "\n".join([texts[i] for i in I[0]])
31
 
32
- # βœ… Chat Function
 
 
 
 
 
 
 
33
  def chat_fn(message, history):
34
  headers = {
35
  "Authorization": f"Bearer {API_KEY}",
@@ -38,8 +44,15 @@ def chat_fn(message, history):
38
 
39
  context = get_context(message)
40
 
 
 
 
 
41
  messages = [
42
- {"role": "system", "content": "You are a helpful assistant. Use the following context to answer: " + context}
 
 
 
43
  ]
44
 
45
  for user, assistant in history:
@@ -79,7 +92,7 @@ demo = gr.ChatInterface(
79
  # βœ… Mount Gradio at root
80
  app = gr.mount_gradio_app(app, demo, path="/")
81
 
82
- # βœ… For local dev (optional on Hugging Face)
83
  if __name__ == "__main__":
84
  nest_asyncio.apply()
85
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
23
  # βœ… FastAPI app
24
  app = FastAPI()
25
 
26
+ # βœ… Context Retriever with threshold
27
+ def get_context(query, top_k=5, threshold=0.3):
28
  query_vec = embed_model.encode([query])
29
  D, I = index.search(np.array(query_vec), top_k)
 
30
 
31
+ # Filter based on similarity distance threshold
32
+ results = [(texts[i], d) for i, d in zip(I[0], D[0]) if d >= threshold]
33
+ if not results:
34
+ return ""
35
+
36
+ return "\n".join([text for text, _ in results])
37
+
38
+ # βœ… Chat Function (RAG-aware + fallback)
39
  def chat_fn(message, history):
40
  headers = {
41
  "Authorization": f"Bearer {API_KEY}",
 
44
 
45
  context = get_context(message)
46
 
47
+ # No valid context? Respond with fallback message
48
+ if not context.strip():
49
+ return "❌ Sorry, I can't answer that."
50
+
51
  messages = [
52
+ {
53
+ "role": "system",
54
+ "content": "You are a helpful assistant. Only use the context below to answer. If you can't find an answer in it, reply: 'Sorry, I can't answer that.'\n\nContext:\n" + context
55
+ }
56
  ]
57
 
58
  for user, assistant in history:
 
92
  # βœ… Mount Gradio at root
93
  app = gr.mount_gradio_app(app, demo, path="/")
94
 
95
+ # βœ… For local development
96
  if __name__ == "__main__":
97
  nest_asyncio.apply()
98
  uvicorn.run(app, host="0.0.0.0", port=7860)