Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import os | |
| import tempfile | |
| from langchain_community.document_loaders import PyPDFLoader | |
| from langchain.text_splitter import RecursiveCharacterTextSplitter | |
| from langchain_community.vectorstores import FAISS | |
| from langchain.chains import RetrievalQA | |
| from langchain_community.embeddings import HuggingFaceEmbeddings | |
| from langchain_huggingface import HuggingFaceEndpoint | |
| # Fix Streamlit config in Docker | |
| os.environ["STREAMLIT_HOME"] = "/tmp/.streamlit" | |
| # Get your Hugging Face API token from Secrets | |
| HF_TOKEN = os.environ.get("HUGGINGFACEHUB_API_TOKEN") | |
| if HF_TOKEN is None: | |
| st.error("โ ๏ธ Hugging Face API token not set. Add it in Settings โ Secrets.") | |
| st.stop() | |
| st.title("๐ DocuQuery - Free RAG App with HF Models") | |
| # Upload PDF | |
| uploaded_file = st.file_uploader("Upload your PDF", type="pdf") | |
| if uploaded_file: | |
| st.info("Processing document...") | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file: | |
| tmp_file.write(uploaded_file.read()) | |
| file_path = tmp_file.name | |
| # Load and split PDF | |
| loader = PyPDFLoader(file_path) | |
| documents = loader.load() | |
| st.write(f"Loaded {len(documents)} document(s)") | |
| text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50) | |
| docs = text_splitter.split_documents(documents) | |
| st.write(f"Split into {len(docs)} chunks") | |
| # Create embeddings + vectorstore | |
| embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") | |
| vectorstore = FAISS.from_documents(docs, embeddings) | |
| retriever = vectorstore.as_retriever() | |
| # Use HuggingFaceEndpoint (replaces deprecated HuggingFaceHub) | |
| llm = HuggingFaceEndpoint( | |
| endpoint_url="https://api-inference.huggingface.co/models/google/flan-t5-small", | |
| huggingfacehub_api_token=HF_TOKEN, | |
| task="text2text-generation" | |
| ) | |
| # Create QA chain | |
| qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever) | |
| st.success("Document processed! You can now ask questions.") | |
| # Question input | |
| query = st.text_input("Ask a question about your document:") | |
| if query: | |
| with st.spinner("Generating answer..."): | |
| answer = qa.run(query) | |
| st.markdown(f"**Answer:** {answer}") | |