hema05core commited on
Commit
2cffa1d
·
verified ·
1 Parent(s): 804b383

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -26,11 +26,13 @@ db = FAISS.from_documents(texts, embeddings)
26
  # --- 4️⃣ Build retriever-based chatbot ---
27
  retriever = db.as_retriever(search_kwargs={"k": 3})
28
 
29
- # --- Hugging Face Hub LLM setup with secret token ---
 
 
30
  llm = HuggingFaceHub(
31
- repo_id="google/flan-t5-base", # smaller for faster response
32
  model_kwargs={"temperature": 0},
33
- huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
34
  )
35
 
36
  qa = ConversationalRetrievalChain.from_llm(
@@ -38,15 +40,17 @@ qa = ConversationalRetrievalChain.from_llm(
38
  retriever=retriever
39
  )
40
 
41
- # --- 5️⃣ Respond function ---
42
- def respond(message, chat_history):
43
- chat_history = chat_history or []
44
- chat_history = chat_history[-6:] # keep last 3 exchanges
45
- result = qa({"question": message, "chat_history": chat_history})
46
- chat_history.append((message, result["answer"]))
47
- return chat_history, chat_history
 
 
48
 
49
- # --- 6️⃣ Gradio UI with Entry Warning ---
50
  with gr.Blocks() as demo:
51
  with gr.Column():
52
  # Warning message
@@ -92,6 +96,7 @@ with gr.Blocks() as demo:
92
  gr.update(visible=False) # exit_btn
93
  )
94
 
 
95
  enter_btn.click(
96
  enter_case,
97
  inputs=None,
@@ -110,7 +115,7 @@ with gr.Blocks() as demo:
110
  outputs=[chatbot, chatbot]
111
  )
112
 
113
- # --- 7️⃣ Launch ---
114
  if __name__ == "__main__":
115
  demo.launch(share=True, enable_queue=True)
116
 
 
26
  # --- 4️⃣ Build retriever-based chatbot ---
27
  retriever = db.as_retriever(search_kwargs={"k": 3})
28
 
29
+ # --- 5️⃣ Hugging Face LLM setup using secret ---
30
+ hf_token = os.environ["HUGGINGFACEHUB_API_TOKEN"] # Must be set in Space Secrets
31
+
32
  llm = HuggingFaceHub(
33
+ repo_id="google/flan-t5-base",
34
  model_kwargs={"temperature": 0},
35
+ huggingfacehub_api_token=hf_token
36
  )
37
 
38
  qa = ConversationalRetrievalChain.from_llm(
 
40
  retriever=retriever
41
  )
42
 
43
+ # --- 6️⃣ Chat history ---
44
+ chat_history = []
45
+
46
+ # --- 7️⃣ Respond function ---
47
+ def respond(message, history):
48
+ history = history[-6:] # Keep last 3 exchanges
49
+ result = qa({"question": message, "chat_history": history})
50
+ history.append((message, result["answer"]))
51
+ return history, history
52
 
53
+ # --- 8️⃣ Gradio UI with Entry Warning ---
54
  with gr.Blocks() as demo:
55
  with gr.Column():
56
  # Warning message
 
96
  gr.update(visible=False) # exit_btn
97
  )
98
 
99
+ # Connect buttons
100
  enter_btn.click(
101
  enter_case,
102
  inputs=None,
 
115
  outputs=[chatbot, chatbot]
116
  )
117
 
118
+ # --- 9️⃣ Launch ---
119
  if __name__ == "__main__":
120
  demo.launch(share=True, enable_queue=True)
121