lantzmurray commited on
Commit
35efd38
·
verified ·
1 Parent(s): fa04cd6

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +53 -71
src/streamlit_app.py CHANGED
@@ -1,82 +1,64 @@
1
- # app_hf_space.py (Iteration)
2
  import streamlit as st
 
 
 
 
 
 
 
3
 
4
- from langchain_huggingface import HuggingFaceEmbeddingsceEmbeddings
5
- from langchain_community.vectorstores.faiss import FAISS
6
- from langchain.chains import RetrievalQA
7
- from langchain_huggingface.llms import HuggingFacePipeline
 
 
 
 
 
 
8
 
9
- # --- Configuration ---
10
- # Use Hugging Face inference API via the `pipeline` from langchain-huggingface
11
- EMBED_MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
12
- LLM_MODEL_ID = "google/flan-t5-small"
13
- INDEX_DIR = "faiss_index"
14
 
15
- # 1. Initialize embeddings via HF Inference API
16
- embeddings = HuggingFaceEmbeddings(
17
- model_name=EMBED_MODEL_ID,
18
- cache_dir=".hf_cache"
19
- )
 
20
 
21
- # 2. Preload & Ingest (optional)
22
- import os, zipfile
23
- from streamlit import sidebar
24
 
25
- # Auto-extract preloaded zip if present
26
- docs_dir = "docs"
27
- zip_path = "preloaded_docs.zip"
28
- if os.path.exists(zip_path):
29
- with zipfile.ZipFile(zip_path, "r") as z:
30
- z.extractall(docs_dir)
31
- sidebar.success(f"Extracted {zip_path} to {docs_dir}/")
32
 
33
- # Sidebar button to re-ingest docs and rebuild index
34
- if sidebar.button("Re-ingest docs & rebuild index"):
35
- from ingest import load_documents, text_splitter, embeddings as ingest_embeddings
36
- docs = load_documents(docs_dir)
37
- chunks = text_splitter.split_documents(docs)
38
- FAISS.from_documents(chunks, ingest_embeddings).save_local(INDEX_DIR)
39
- sidebar.success("Re-ingestion complete and index rebuilt.")
40
 
41
- # 3. Load FAISS index
42
- store = FAISS.load_local(
43
- INDEX_DIR,
44
- embeddings
45
- )
46
 
47
- # 4. Initialize HF LLM via pipeline (inference API)
48
- # ...
49
- # 5. Build RetrievalQA chain
50
- # ...
51
- # 6. Streamlit UI
52
- # ... Initialize HF LLM via pipeline (inference API)
53
- llm = HuggingFacePipeline.from_model_id(
54
- model_id=LLM_MODEL_ID,
55
- task="text2text-generation",
56
- pipeline_kwargs={
57
- # Device mapping for inference
58
- "device": -1,
59
- # Cache directory for model weights
60
- "cache_dir": ".hf_cache"
61
- }
62
- )
63
-
64
- # 4. Build RetrievalQA chain
65
- aqa_chain = RetrievalQA.from_chain_type(
66
- llm=llm,
67
- chain_type="stuff",
68
- retriever=store.as_retriever()
69
- )
70
-
71
- # 5. Streamlit UI
72
- def main():
73
- st.title("🦜🔗 RAG App via HF Spaces")
74
- query = st.text_input("Ask a question about your docs:")
75
 
 
 
 
76
  if query:
77
- with st.spinner("Generating answer via HF Space..."):
78
- answer = aqa_chain.run(query)
79
- st.markdown(f"**Answer:** {answer}")
80
-
81
- if __name__ == "__main__":
82
- main()
 
 
 
1
  import streamlit as st
2
+ import zipfile, io, os
3
+ from langchain.document_loaders import PyPDFLoader
4
+ from langchain.text_splitter import CharacterTextSplitter
5
+ from langchain.schema import Document
6
+ from langchain.embeddings import SentenceTransformerEmbeddings
7
+ from langchain.vectorstores import FAISS
8
+ from transformers import pipeline
9
 
10
+ # Cache the QA initialization so ingestion runs once per session
11
+ @st.cache_resource
12
+ def init_qa(zip_bytes):
13
+ tmp_dir = "tmp_pdfs"
14
+ # Clean up or create temp folder
15
+ if os.path.exists(tmp_dir):
16
+ for f in os.listdir(tmp_dir):
17
+ os.remove(os.path.join(tmp_dir, f))
18
+ else:
19
+ os.makedirs(tmp_dir)
20
 
21
+ # Extract uploaded ZIP
22
+ with zipfile.ZipFile(io.BytesIO(zip_bytes)) as z:
23
+ z.extractall(tmp_dir)
 
 
24
 
25
+ # Load all PDFs
26
+ docs = []
27
+ for fname in os.listdir(tmp_dir):
28
+ if fname.lower().endswith(".pdf"):
29
+ loader = PyPDFLoader(os.path.join(tmp_dir, fname))
30
+ docs.extend(loader.load())
31
 
32
+ # Split into manageable chunks
33
+ splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
34
+ split_docs = splitter.split_documents(docs)
35
 
36
+ # Build vector store
37
+ embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
38
+ vector_store = FAISS.from_documents(split_docs, embeddings)
 
 
 
 
39
 
40
+ # Load the RAG model
41
+ generator = pipeline(
42
+ "text2text-generation",
43
+ model="PleIAs/Pleias-RAG-350M",
44
+ tokenizer="PleIAs/Pleias-RAG-350M"
45
+ )
 
46
 
47
+ return vector_store, generator
 
 
 
 
48
 
49
+ # Streamlit UI
50
+ st.title("Pleias-RAG 350M Streamlit App")
51
+ st.write("Upload a ZIP of PDFs to initialize the RAG engine.")
52
+ zip_file = st.file_uploader("ZIP file", type=["zip"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
+ if zip_file:
55
+ vector_store, generator = init_qa(zip_file.read())
56
+ query = st.text_input("Ask a question:")
57
  if query:
58
+ docs = vector_store.similarity_search(query, k=4)
59
+ context = "\n\n".join([doc.page_content for doc in docs])
60
+ prompt = f"question: {query}\ncontext: {context}"
61
+ answer = generator(prompt, max_length=512, do_sample=False)[0]["generated_text"]
62
+ st.write(answer)
63
+ else:
64
+ st.info("Awaiting ZIP upload.")