Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,55 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
st.
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import chromadb
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# HF model to use (small + free)
|
| 7 |
+
MODEL_ID = "google/flan-t5-base"
|
| 8 |
+
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
| 9 |
+
API_TOKEN = st.secrets["HUGGINGFACEHUB_API_TOKEN"]
|
| 10 |
+
|
| 11 |
+
# Setup headers
|
| 12 |
+
headers = {
|
| 13 |
+
"Authorization": f"Bearer {API_TOKEN}"
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
# Load Chroma DB
|
| 17 |
+
chroma_client = chromadb.PersistentClient(path="chroma_store")
|
| 18 |
+
collection = chroma_client.get_or_create_collection(name="tech_docs")
|
| 19 |
+
|
| 20 |
+
# HF API call
|
| 21 |
+
def query_huggingface(prompt):
|
| 22 |
+
payload = {
|
| 23 |
+
"inputs": prompt,
|
| 24 |
+
"options": {"wait_for_model": True}
|
| 25 |
+
}
|
| 26 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 27 |
+
return response.json()[0]['generated_text']
|
| 28 |
+
|
| 29 |
+
# UI
|
| 30 |
+
st.title("π¬ Ask Me Anything - Tech RAG Chatbot")
|
| 31 |
+
|
| 32 |
+
user_query = st.text_input("π Ask your question:")
|
| 33 |
+
|
| 34 |
+
if user_query:
|
| 35 |
+
# Retrieve top 3 matching docs from vector DB
|
| 36 |
+
results = collection.query(query_texts=[user_query], n_results=3)
|
| 37 |
+
context = "\n".join(results["documents"][0]) if results["documents"] else ""
|
| 38 |
+
|
| 39 |
+
# Build prompt
|
| 40 |
+
prompt = f"""Answer the question using the context below:
|
| 41 |
+
|
| 42 |
+
Context:
|
| 43 |
+
{context}
|
| 44 |
+
|
| 45 |
+
Question:
|
| 46 |
+
{user_query}
|
| 47 |
+
|
| 48 |
+
Answer:"""
|
| 49 |
+
|
| 50 |
+
# Send to HF API
|
| 51 |
+
with st.spinner("Thinking..."):
|
| 52 |
+
answer = query_huggingface(prompt)
|
| 53 |
+
|
| 54 |
+
st.markdown("### π’ Answer:")
|
| 55 |
+
st.write(answer)
|