File size: 4,831 Bytes
4590062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import os
import faiss
import numpy as np
import gradio as gr
from pypdf import PdfReader
from sentence_transformers import SentenceTransformer
from groq import Groq

# -----------------------------
# Initialize Models
# -----------------------------
embedder = SentenceTransformer("all-MiniLM-L6-v2")

# Safely load API key
GROQ_API_KEY = os.getenv("Rag")
client = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None

# -----------------------------
# Global Storage
# -----------------------------
index = None
documents = []

# -----------------------------
# PDF Processing
# -----------------------------
def read_pdf(file):
    try:
        reader = PdfReader(file.name)  # FIX for Hugging Face
        text = ""
        for page in reader.pages:
            if page.extract_text():
                text += page.extract_text()
        return text
    except Exception as e:
        return f"Error reading PDF: {str(e)}"


def chunk_text(text, chunk_size=500, overlap=100):
    chunks = []
    start = 0

    while start < len(text):
        end = start + chunk_size
        chunks.append(text[start:end])
        start += chunk_size - overlap

    return chunks


# -----------------------------
# Create FAISS Index
# -----------------------------
def create_index(chunks):
    global index, documents

    documents = chunks
    embeddings = embedder.encode(chunks)

    embeddings = np.array(embeddings).astype("float32")

    dimension = embeddings.shape[1]
    index = faiss.IndexFlatL2(dimension)
    index.add(embeddings)


# -----------------------------
# Retrieval
# -----------------------------
def retrieve(query, k=3, threshold=1.2):
    global index

    if index is None:
        return [], None

    query_embedding = embedder.encode([query])
    query_embedding = np.array(query_embedding).astype("float32")

    distances, indices = index.search(query_embedding, k)

    relevant_chunks = []
    valid_distances = []

    for i, dist in zip(indices[0], distances[0]):
        if i < len(documents) and dist < threshold:
            relevant_chunks.append(documents[i])
            valid_distances.append(dist)

    # Confidence
    confidence = None
    if valid_distances:
        avg = np.mean(valid_distances)
        if avg < 0.5:
            confidence = "High"
        elif avg < 1.0:
            confidence = "Medium"
        else:
            confidence = "Low"

    return relevant_chunks, confidence


# -----------------------------
# LLM (Groq)
# -----------------------------
def ask_groq(context_chunks, question):
    if client is None:
        return "Error: GROQ_API_KEY not set in Hugging Face Secrets."

    context = "\n".join(context_chunks)

    prompt = f"""
You are an intelligent assistant.

Rules:
1. If answer is clearly in context → answer normally.
2. If related but not exact → say:
   "This is not explicitly mentioned in the document, but based on related context..."
3. If irrelevant → say:
   "The document does not contain information related to this question."

Context:
{context}

Question:
{question}
"""

    try:
        response = client.chat.completions.create(
            messages=[{"role": "user", "content": prompt}],
            model="llama-3.3-70b-versatile",
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"Groq API Error: {str(e)}"


# -----------------------------
# Main Functions
# -----------------------------
def process_pdf(file):
    if file is None:
        return "Please upload a PDF."

    text = read_pdf(file)

    if not text or "Error" in text:
        return text

    chunks = chunk_text(text)
    create_index(chunks)

    return f"✅ PDF processed! Chunks: {len(chunks)}"


def answer_question(question):
    if index is None:
        return "Please upload and process a PDF first."

    context_chunks, confidence = retrieve(question)

    if not context_chunks:
        return "The document does not contain information related to this question."

    answer = ask_groq(context_chunks, question)

    if confidence:
        answer = f"(Confidence: {confidence})\n\n{answer}"

    return answer


# -----------------------------
# Gradio UI
# -----------------------------
with gr.Blocks() as demo:
    gr.Markdown("## 📄 RAG PDF Q&A (Groq + FAISS)")

    file_input = gr.File(label="Upload PDF")
    upload_btn = gr.Button("Process PDF")
    status = gr.Textbox(label="Status")

    question = gr.Textbox(label="Ask a question")
    answer = gr.Textbox(label="Answer")

    upload_btn.click(process_pdf, inputs=file_input, outputs=status)
    question.submit(answer_question, inputs=question, outputs=answer)

# -----------------------------
# Launch
# -----------------------------
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)