Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- 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 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
LLM_MODEL_ID = "google/flan-t5-small"
|
| 13 |
-
INDEX_DIR = "faiss_index"
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
)
|
|
|
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 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 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
sidebar.success("Re-ingestion complete and index rebuilt.")
|
| 40 |
|
| 41 |
-
|
| 42 |
-
store = FAISS.load_local(
|
| 43 |
-
INDEX_DIR,
|
| 44 |
-
embeddings
|
| 45 |
-
)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 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 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
| 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.")
|