lantzmurray commited on
Commit
66a3ddd
·
verified ·
1 Parent(s): cce75d3

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +44 -12
src/streamlit_app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app_hf_space.py
2
  import streamlit as st
3
  from langchain_huggingface import HuggingFaceEmbeddings
4
  from langchain_community.vectorstores.faiss import FAISS
@@ -17,16 +17,47 @@ embeddings = HuggingFaceEmbeddings(
17
  cache_dir=".hf_cache"
18
  )
19
 
20
- # 2. Load FAISS index
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  store = FAISS.load_local(
22
  INDEX_DIR,
23
  embeddings
24
  )
25
 
26
- # 3. Initialize HF LLM via pipeline (inference API)
 
 
 
 
 
27
  llm = HuggingFacePipeline.from_model_id(
28
  model_id=LLM_MODEL_ID,
29
- task="text2text-generation"
 
 
 
 
 
 
30
  )
31
 
32
  # 4. Build RetrievalQA chain
@@ -37,13 +68,14 @@ aqa_chain = RetrievalQA.from_chain_type(
37
  )
38
 
39
  # 5. Streamlit UI
40
- st.title("🦜🔗 RAG App via HF Spaces")
41
- query = st.text_input("Ask a question about your docs:")
 
42
 
43
- if query:
44
- with st.spinner("Generating answer via HF Space..."):
45
- answer = aqa_chain.run(query)
46
- st.markdown(f"**Answer:** {answer}")
47
 
48
- # NOTE: Deploy this to Hugging Face Spaces for fully-managed hosting.
49
- # Just push this file to your repo on HF and enable Streamlit space.
 
1
+ # app_hf_space.py (Iteration)
2
  import streamlit as st
3
  from langchain_huggingface import HuggingFaceEmbeddings
4
  from langchain_community.vectorstores.faiss import FAISS
 
17
  cache_dir=".hf_cache"
18
  )
19
 
20
+ # 2. Preload & Ingest (optional)
21
+ import os, zipfile
22
+ from streamlit import sidebar
23
+
24
+ # Auto-extract preloaded zip if present
25
+ docs_dir = "docs"
26
+ zip_path = "preloaded_docs.zip"
27
+ if os.path.exists(zip_path):
28
+ with zipfile.ZipFile(zip_path, "r") as z:
29
+ z.extractall(docs_dir)
30
+ sidebar.success(f"Extracted {zip_path} to {docs_dir}/")
31
+
32
+ # Sidebar button to re-ingest docs and rebuild index
33
+ if sidebar.button("Re-ingest docs & rebuild index"):
34
+ from ingest import load_documents, text_splitter, embeddings as ingest_embeddings
35
+ docs = load_documents(docs_dir)
36
+ chunks = text_splitter.split_documents(docs)
37
+ FAISS.from_documents(chunks, ingest_embeddings).save_local(INDEX_DIR)
38
+ sidebar.success("Re-ingestion complete and index rebuilt.")
39
+
40
+ # 3. Load FAISS index
41
  store = FAISS.load_local(
42
  INDEX_DIR,
43
  embeddings
44
  )
45
 
46
+ # 4. Initialize HF LLM via pipeline (inference API)
47
+ # ...
48
+ # 5. Build RetrievalQA chain
49
+ # ...
50
+ # 6. Streamlit UI
51
+ # ... Initialize HF LLM via pipeline (inference API)
52
  llm = HuggingFacePipeline.from_model_id(
53
  model_id=LLM_MODEL_ID,
54
+ task="text2text-generation",
55
+ pipeline_kwargs={
56
+ # Device mapping for inference
57
+ "device": -1,
58
+ # Cache directory for model weights
59
+ "cache_dir": ".hf_cache"
60
+ }
61
  )
62
 
63
  # 4. Build RetrievalQA chain
 
68
  )
69
 
70
  # 5. Streamlit UI
71
+ def main():
72
+ st.title("🦜🔗 RAG App via HF Spaces")
73
+ query = st.text_input("Ask a question about your docs:")
74
 
75
+ if query:
76
+ with st.spinner("Generating answer via HF Space..."):
77
+ answer = aqa_chain.run(query)
78
+ st.markdown(f"**Answer:** {answer}")
79
 
80
+ if __name__ == "__main__":
81
+ main()