Vinit710 commited on
Commit
487faee
Β·
verified Β·
1 Parent(s): 71caa7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -41
app.py CHANGED
@@ -1,41 +1,55 @@
1
- import streamlit as st
2
- from langchain.vectorstores import Chroma
3
- from langchain.embeddings import HuggingFaceEmbeddings
4
- from langchain.chains import RetrievalQA
5
- from langchain_community.llms import HuggingFaceHub
6
- from update_vector_db import update_vector_db # custom update logic
7
-
8
- st.set_page_config(page_title="🧠 RAG Chatbot", layout="centered")
9
- st.title("πŸ’¬ Ask Me Anything - Tech RAG Chatbot")
10
-
11
- # Load embeddings + chroma
12
- @st.cache_resource
13
- def load_chain():
14
- embed = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
15
- db = Chroma(persist_directory="chroma_store", embedding_function=embed)
16
-
17
- llm = HuggingFaceHub(
18
- repo_id="mistralai/Mistral-7B-Instruct-v0.1",
19
- model_kwargs={"temperature": 0.5, "max_new_tokens": 256},
20
- )
21
-
22
- qa = RetrievalQA.from_chain_type(llm=llm, retriever=db.as_retriever(), return_source_documents=True)
23
- return qa
24
-
25
- qa_chain = load_chain()
26
-
27
- # Chat interface
28
- user_query = st.text_input("πŸ”Ž Ask your question:")
29
- if user_query:
30
- with st.spinner("Thinking..."):
31
- result = qa_chain.invoke({"query": user_query})
32
- st.markdown("### πŸ“’ Answer:")
33
- st.write(result['result'])
34
- st.markdown("### πŸ“„ Sources:")
35
- for doc in result['source_documents']:
36
- st.write("β€’", doc.metadata.get("source", "Unknown"))
37
-
38
- # Button to trigger DB update
39
- if st.button("πŸ”„ Update Vector Database"):
40
- update_vector_db("new_data", "chroma_store")
41
- st.success("Vector DB updated!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)