Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
|
|
|
|
|
|
| 30 |
llm = HuggingFaceHub(
|
| 31 |
-
repo_id="google/flan-t5-base",
|
| 32 |
model_kwargs={"temperature": 0},
|
| 33 |
-
huggingfacehub_api_token=
|
| 34 |
)
|
| 35 |
|
| 36 |
qa = ConversationalRetrievalChain.from_llm(
|
|
@@ -38,15 +40,17 @@ qa = ConversationalRetrievalChain.from_llm(
|
|
| 38 |
retriever=retriever
|
| 39 |
)
|
| 40 |
|
| 41 |
-
# ---
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
# ---
|
| 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 |
-
# ---
|
| 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 |
|