File size: 2,285 Bytes
2c57db1
 
 
 
 
 
 
 
5c553f4
2c57db1
 
 
 
5c553f4
bd2b023
2c57db1
 
 
 
5c553f4
2c57db1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c553f4
2c57db1
 
 
 
5c553f4
 
 
 
 
 
2c57db1
5c553f4
2c57db1
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
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
52
53
54
55
56
57
58
59
60
61
62
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}")