Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- README.md +23 -19
- app.py +51 -0
- requirements.txt +7 -3
README.md
CHANGED
|
@@ -1,19 +1,23 @@
|
|
| 1 |
-
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π DocuQuery - Free Document Q&A with LangChain + Hugging Face
|
| 2 |
+
|
| 3 |
+
DocuQuery lets you **upload a PDF and ask questions** about it.
|
| 4 |
+
This version uses **free Hugging Face models** (no OpenAI required).
|
| 5 |
+
|
| 6 |
+
## π Features
|
| 7 |
+
- Upload a PDF
|
| 8 |
+
- Ask questions in natural language
|
| 9 |
+
- Uses **sentence-transformers for embeddings** + **Flan-T5 for answering**
|
| 10 |
+
- Runs free via Hugging Face Hub
|
| 11 |
+
|
| 12 |
+
## π οΈ Tech Stack
|
| 13 |
+
- Python, Streamlit
|
| 14 |
+
- LangChain, FAISS
|
| 15 |
+
- Hugging Face Hub (Flan-T5, MiniLM embeddings)
|
| 16 |
+
|
| 17 |
+
## βΆοΈ Run Locally
|
| 18 |
+
```bash
|
| 19 |
+
git clone https://github.com/<your-username>/docuquery.git
|
| 20 |
+
cd docuquery
|
| 21 |
+
pip install -r requirements.txt
|
| 22 |
+
export HUGGINGFACEHUB_API_TOKEN="your_hf_token_here"
|
| 23 |
+
streamlit run app.py
|
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
+
from langchain_community.vectorstores import FAISS
|
| 5 |
+
from langchain.chains import RetrievalQA
|
| 6 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
from langchain_community.llms import HuggingFaceHub
|
| 8 |
+
import tempfile
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# Hugging Face API token (free β just create an account on huggingface.co)
|
| 12 |
+
HF_TOKEN = os.getenv("HuggingfacehubAPIToken")
|
| 13 |
+
|
| 14 |
+
st.set_page_config(page_title="DocuQuery (Free)", page_icon="π", layout="wide")
|
| 15 |
+
st.title("π DocuQuery - Free RAG App with Hugging Face Models")
|
| 16 |
+
|
| 17 |
+
# Upload PDF
|
| 18 |
+
uploaded_file = st.file_uploader("Upload your PDF file", type="pdf")
|
| 19 |
+
|
| 20 |
+
if uploaded_file:
|
| 21 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
| 22 |
+
tmp_file.write(uploaded_file.read())
|
| 23 |
+
file_path = tmp_file.name
|
| 24 |
+
|
| 25 |
+
# Load PDF and split text
|
| 26 |
+
loader = PyPDFLoader(file_path)
|
| 27 |
+
documents = loader.load()
|
| 28 |
+
|
| 29 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 30 |
+
docs = text_splitter.split_documents(documents)
|
| 31 |
+
|
| 32 |
+
# Create embeddings with free Hugging Face model
|
| 33 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 34 |
+
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 35 |
+
|
| 36 |
+
retriever = vectorstore.as_retriever()
|
| 37 |
+
|
| 38 |
+
# Use a free Hugging Face LLM (lightweight for Q&A)
|
| 39 |
+
llm = HuggingFaceHub(
|
| 40 |
+
repo_id="google/flan-t5-base",
|
| 41 |
+
huggingfacehub_api_token=HF_TOKEN,
|
| 42 |
+
model_kwargs={"temperature":0, "max_length":512}
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
|
| 46 |
+
|
| 47 |
+
query = st.text_input("π Ask a question about your document:")
|
| 48 |
+
|
| 49 |
+
if query:
|
| 50 |
+
answer = qa.run(query)
|
| 51 |
+
st.markdown(f"**Answer:** {answer}")
|
requirements.txt
CHANGED
|
@@ -1,3 +1,7 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
langchain
|
| 3 |
+
langchain-community
|
| 4 |
+
faiss-cpu
|
| 5 |
+
sentence-transformers
|
| 6 |
+
pypdf
|
| 7 |
+
huggingface_hub
|