Spaces:
Sleeping

File size: 3,235 Bytes
18f3c81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# pdf_qa_app.py
import os
import gradio as gr
from pathlib import Path
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_ollama import ChatOllama  # You can swap with ChatOpenAI, ChatGroq, etc.

# Set up vector store directory
persist_dir = "chroma_pdf"
llm = ChatOllama(model="llama3.1:8b", temperature=0.1)
parser = StrOutputParser()

PROMPT = ChatPromptTemplate.from_template(
    """You are an assistant that answers using ONLY the context provided.
If the answer is not present, say "I don't know."

Context:
{ctx}

Question: {q}
Answer with citations like [#] referencing the context.
"""
)

def process_pdfs(uploaded_files):
    docs = []
    for f in uploaded_files:
        temp_path = Path("uploads") / f.name
        temp_path.parent.mkdir(exist_ok=True)
        with open(temp_path, "wb") as out:
            out.write(f.read())
        loader = PyPDFLoader(str(temp_path))
        docs.extend(loader.load())
    splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
    chunks = splitter.split_documents(docs)
    embedder = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
    db = Chroma.from_documents(chunks, embedding=embedder, persist_directory=persist_dir)
    db.persist()
    return "Index built successfully!", True

def ask_question(q):
    embedder = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
    db = Chroma(persist_directory=persist_dir, embedding_function=embedder)
    docs = db.similarity_search(q, k=4)
    ctx = "\n\n".join(f"[{i+1}] {d.page_content.strip()} (p{d.metadata.get('page', 0)+1})" for i, d in enumerate(docs))
    prompt = PROMPT.invoke({"q": q, "ctx": ctx})
    return (llm | parser).invoke(prompt)

def reset_index():
    if Path(persist_dir).exists():
        for file in Path(persist_dir).glob("**/*"):
            file.unlink()
        Path(persist_dir).rmdir()
    return "Index reset. Please upload and rebuild.", False

def guarded_ask_question(q, ready):
    if not ready:
        return "Please upload PDFs and click 'Build Index' first."
    return ask_question(q)

with gr.Blocks() as demo:
    gr.Markdown("""# 📄🧠 PDF Q&A Agent
Upload PDF(s), build an index, and ask questions with grounded, cited answers.
""")

    uploaded_files = gr.File(file_types=[".pdf"], file_count="multiple", label="Upload PDFs")
    with gr.Row():
        build_btn = gr.Button("Build Index")
        reset_btn = gr.Button("Reset Index")
    status = gr.Textbox(label="Status")

    q = gr.Textbox(label="Ask a question")
    submit = gr.Button("Submit")
    answer = gr.Textbox(label="Answer")

    index_ready = gr.State(False)

    build_btn.click(fn=process_pdfs, inputs=[uploaded_files], outputs=[status, index_ready])
    reset_btn.click(fn=reset_index, outputs=[status, index_ready])
    submit.click(fn=guarded_ask_question, inputs=[q, index_ready], outputs=answer)

demo.launch()