Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,57 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
from langchain_community.
|
| 5 |
-
from langchain.
|
| 6 |
-
from langchain_community.
|
| 7 |
-
from
|
| 8 |
-
import
|
| 9 |
-
import
|
| 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 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain_community.vectorstores import FAISS
|
| 7 |
+
from langchain.chains import RetrievalQA
|
| 8 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 9 |
+
from langchain_community.llms import HuggingFaceHub
|
| 10 |
+
|
| 11 |
+
# Fix Streamlit config in Docker
|
| 12 |
+
os.environ["STREAMLIT_HOME"] = "/tmp/.streamlit"
|
| 13 |
+
|
| 14 |
+
HF_TOKEN = os.environ.get("HuggingfacehubAPIToken")
|
| 15 |
+
if HF_TOKEN is None:
|
| 16 |
+
st.error("β οΈ Hugging Face API token not set. Add it in Settings β Secrets.")
|
| 17 |
+
st.stop()
|
| 18 |
+
|
| 19 |
+
st.title("π DocuQuery - Free RAG App")
|
| 20 |
+
|
| 21 |
+
# Upload PDF
|
| 22 |
+
uploaded_file = st.file_uploader("Upload your PDF", type="pdf")
|
| 23 |
+
|
| 24 |
+
if uploaded_file:
|
| 25 |
+
st.info("Processing document...")
|
| 26 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
| 27 |
+
tmp_file.write(uploaded_file.read())
|
| 28 |
+
file_path = tmp_file.name
|
| 29 |
+
|
| 30 |
+
# Load and split PDF
|
| 31 |
+
loader = PyPDFLoader(file_path)
|
| 32 |
+
documents = loader.load()
|
| 33 |
+
st.write(f"Loaded {len(documents)} document(s)")
|
| 34 |
+
|
| 35 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 36 |
+
docs = text_splitter.split_documents(documents)
|
| 37 |
+
st.write(f"Split into {len(docs)} chunks")
|
| 38 |
+
|
| 39 |
+
# Embeddings and vectorstore
|
| 40 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 41 |
+
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 42 |
+
retriever = vectorstore.as_retriever()
|
| 43 |
+
|
| 44 |
+
# LLM
|
| 45 |
+
llm = HuggingFaceHub(repo_id="google/flan-t5-base",
|
| 46 |
+
huggingfacehub_api_token=HF_TOKEN,
|
| 47 |
+
model_kwargs={"temperature": 0, "max_length": 512})
|
| 48 |
+
|
| 49 |
+
qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
|
| 50 |
+
st.success("Document processed! You can now ask questions.")
|
| 51 |
+
|
| 52 |
+
# Question input
|
| 53 |
+
query = st.text_input("Ask a question about your document:")
|
| 54 |
+
if query:
|
| 55 |
+
with st.spinner("Generating answer..."):
|
| 56 |
+
answer = qa.run(query)
|
| 57 |
+
st.markdown(f"**Answer:** {answer}")
|