Spaces:
Sleeping

File size: 2,967 Bytes
02d5a1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
QueryVault - Gradio Web Interface
Run this file to launch the QA bot in your browser.

Usage:
    python app.py
"""

import gradio as gr
from dotenv import load_dotenv
from src.qabot import process_pdf_and_query

# Load environment variables from .env file
load_dotenv()


# ---------------------------------------------------------------------------
# Gradio Interface
# ---------------------------------------------------------------------------

def answer_question(pdf_file, question: str) -> str:
    """
    Wrapper called by Gradio on each user interaction.

    Args:
        pdf_file: Gradio file object (has a .name attribute with the temp path).
        question: User's question string.

    Returns:
        Answer string to display in the output box.
    """
    if pdf_file is None:
        return "⚠️  Please upload a PDF document before asking a question."

    if isinstance(pdf_file, str):
        file_path = pdf_file
    elif isinstance(pdf_file, dict):
        file_path = pdf_file.get("path") or pdf_file.get("name")
    else:
        file_path = getattr(pdf_file, "name", str(pdf_file))
    return process_pdf_and_query(file_path, question)


# ---------------------------------------------------------------------------
# UI Layout
# ---------------------------------------------------------------------------

with gr.Blocks(
    title="QueryVault – PDF QA Bot",
    theme=gr.themes.Soft()
) as demo:

    gr.Markdown(
        """
        # πŸ“„ QueryVault β€” PDF Question Answering Bot
        Upload a PDF document, ask any question about its content, and get an
        intelligent answer powered by **Groq API (Llama-3.3 70B)** and **LangChain RAG**.
        """
    )

    with gr.Row():
        with gr.Column(scale=1):
            pdf_input = gr.File(
                label="πŸ“ Upload PDF",
                file_types=[".pdf"],
                type="filepath",
            )
            question_input = gr.Textbox(
                label="❓ Your Question",
                placeholder="e.g. What are the main findings of this document?",
                lines=3,
            )
            submit_btn = gr.Button("πŸ” Get Answer", variant="primary")

        with gr.Column(scale=1):
            answer_output = gr.Textbox(
                label="πŸ’‘ Answer",
                lines=12,
                interactive=False,
            )

    submit_btn.click(
        fn=answer_question,
        inputs=[pdf_input, question_input],
        outputs=answer_output,
    )

    gr.Markdown(
        """
        ---
        **How it works:**
        1. Your PDF is parsed and split into chunks.
        2. Each chunk is converted to a semantic embedding via HuggingFace Local Embeddings (`all-MiniLM-L6-v2`).
        3. The most relevant chunks are retrieved using ChromaDB.
        4. Llama-3.3 70B reads the context and generates a precise answer.
        """
    )


if __name__ == "__main__":
    demo.launch(share=True)