File size: 4,050 Bytes
2deda75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5baecb
2deda75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import os
import gradio as gr
import numpy as np
import faiss

from groq import Groq
from pypdf import PdfReader
from sentence_transformers import SentenceTransformer
from langchain_text_splitters import RecursiveCharacterTextSplitter

# =====================================================
# Configuration
# =====================================================
RELEVANCE_THRESHOLD = 1.2   # lower = stricter relevance

# =====================================================
# Initialize Groq Client
# =====================================================
client = Groq(api_key=os.environ.get("RAG-GROQ"))
# =====================================================
# Load Embedding Model
# =====================================================
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")

# =====================================================
# Global Vector Store
# =====================================================
vector_store = None
stored_chunks = []

# =====================================================
# PDF Processing Function
# =====================================================
def process_pdf(pdf_file):
    global vector_store, stored_chunks

    reader = PdfReader(pdf_file)
    full_text = ""

    for page in reader.pages:
        if page.extract_text():
            full_text += page.extract_text() + "\n"

    splitter = RecursiveCharacterTextSplitter(
        chunk_size=500,
        chunk_overlap=100
    )
    chunks = splitter.split_text(full_text)

    embeddings = embedding_model.encode(chunks)

    dimension = embeddings.shape[1]
    vector_store = faiss.IndexFlatL2(dimension)
    vector_store.add(np.array(embeddings))

    stored_chunks = chunks

    return "✅ PDF processed successfully. You can now ask questions."

# =====================================================
# Question Answering Function
# =====================================================
def answer_question(question):
    if vector_store is None:
        return "⚠️ Please upload and process a PDF first."

    question_embedding = embedding_model.encode([question])
    distances, indices = vector_store.search(
        np.array(question_embedding), k=3
    )

    avg_distance = distances[0].mean()

    context = ""
    for idx in indices[0]:
        context += stored_chunks[idx] + "\n"

    # Relevance feedback
    if avg_distance > RELEVANCE_THRESHOLD:
        relevance_note = (
            "⚠️ **Note:** This question is not directly answered in the document.\n"
            "The response below is based on loosely related context.\n\n"
        )
    else:
        relevance_note = ""

    prompt = f"""
You are an honest and careful AI assistant.

Instructions:
- Answer ONLY using the provided context.
- If the answer is not explicitly stated, say:
  "This is not directly mentioned in the document, but based on related context..."

Context:
{context}

Question:
{question}
"""

    response = client.chat.completions.create(
        model="llama-3.3-70b-versatile",
        messages=[
            {"role": "user", "content": prompt}
        ]
    )

    return relevance_note + response.choices[0].message.content

# =====================================================
# Gradio UI
# =====================================================
with gr.Blocks() as app:
    gr.Markdown("## 📄 RAG-based PDF Question Answering (Groq + FAISS)")
    gr.Markdown(
        "Upload a PDF and ask questions. "
        "The system will clearly tell you if an answer is not directly mentioned."
    )

    pdf_file = gr.File(label="Upload PDF", file_types=[".pdf"])
    process_btn = gr.Button("Process PDF")
    status_box = gr.Textbox(label="Status", interactive=False)

    question_box = gr.Textbox(label="Ask a Question")
    answer_box = gr.Textbox(label="Answer", lines=8)

    process_btn.click(
        process_pdf,
        inputs=pdf_file,
        outputs=status_box
    )

    question_box.submit(
        answer_question,
        inputs=question_box,
        outputs=answer_box
    )

app.launch()