File size: 8,862 Bytes
1bfb382
 
 
 
 
 
 
 
 
 
376e7ad
1bfb382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376e7ad
 
1bfb382
376e7ad
 
1bfb382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
RAG Document Q&A Assistant
Upload documents, ask questions, get answers with source citations.
"""

import os
import tempfile
from typing import Optional

import chromadb
from pypdf import PdfReader  # PyMuPDF
import gradio as gr
from chromadb.utils import embedding_functions
from openai import OpenAI

# Initialize OpenAI client
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

# Initialize embedding function
embedding_func = embedding_functions.SentenceTransformerEmbeddingFunction(
    model_name="all-MiniLM-L6-v2"
)

# Global state for the current session
chroma_client = None
collection = None
current_chunks = []


def extract_text_from_pdf(file_path: str) -> str:
    """Extract text from PDF using pypdf."""
    reader = PdfReader(file_path)
    text = ""
    for page in reader.pages:
        text += page.extract_text() or ""
    return text


def extract_text_from_txt(file_path: str) -> str:
    """Extract text from TXT file."""
    with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
        return f.read()


def chunk_fixed_size(text: str, chunk_size: int = 500, overlap: int = 100) -> list[dict]:
    """Split text into fixed-size chunks with overlap."""
    chunks = []
    start = 0
    chunk_id = 0
    
    while start < len(text):
        end = start + chunk_size
        chunk_text = text[start:end].strip()
        
        if chunk_text:
            chunks.append({
                "id": f"chunk_{chunk_id}",
                "text": chunk_text,
                "start": start,
                "end": end
            })
            chunk_id += 1
        
        start = end - overlap
    
    return chunks


def chunk_by_paragraph(text: str) -> list[dict]:
    """Split text by paragraphs (double newlines)."""
    paragraphs = [p.strip() for p in text.split("\n\n") if p.strip()]
    
    chunks = []
    for i, para in enumerate(paragraphs):
        if len(para) > 50:
            chunks.append({
                "id": f"chunk_{i}",
                "text": para,
                "start": 0,
                "end": 0
            })
    
    return chunks


def process_document(file, chunking_strategy: str) -> str:
    """Process uploaded document and store in vector DB."""
    global chroma_client, collection, current_chunks
    
    if file is None:
        return "❌ Please upload a document first."
    
    file_path = file.name
    file_ext = os.path.splitext(file_path)[1].lower()
    
    try:
        if file_ext == ".pdf":
            text = extract_text_from_pdf(file_path)
        elif file_ext in [".txt", ".md"]:
            text = extract_text_from_txt(file_path)
        else:
            return f"❌ Unsupported file type: {file_ext}. Please upload PDF or TXT."
    except Exception as e:
        return f"❌ Error reading file: {str(e)}"
    
    if not text.strip():
        return "❌ No text could be extracted from the document."
    
    if chunking_strategy == "Fixed-size (500 chars)":
        current_chunks = chunk_fixed_size(text, chunk_size=500, overlap=100)
    else:
        current_chunks = chunk_by_paragraph(text)
    
    if not current_chunks:
        return "❌ No chunks could be created from the document."
    
    # Initialize fresh Chroma client and collection
    chroma_client = chromadb.Client()
    try:
        chroma_client.delete_collection(name="documents")
    except:
        pass
    collection = chroma_client.create_collection(
        name="documents",
        embedding_function=embedding_func
    )
    
    collection.add(
        documents=[c["text"] for c in current_chunks],
        ids=[c["id"] for c in current_chunks]
    )
    
    return f"βœ… Document processed successfully!\n\nπŸ“Š **Stats:**\n- Characters: {len(text):,}\n- Chunks created: {len(current_chunks)}\n- Chunking strategy: {chunking_strategy}"


def retrieve_context(query: str, top_k: int = 3) -> list[dict]:
    """Retrieve relevant chunks for the query."""
    if collection is None:
        return []
    
    results = collection.query(
        query_texts=[query],
        n_results=top_k
    )
    
    retrieved = []
    for i, (doc, distance) in enumerate(zip(
        results["documents"][0],
        results["distances"][0]
    )):
        similarity = 1 / (1 + distance)
        retrieved.append({
            "text": doc,
            "similarity": similarity,
            "rank": i + 1
        })
    
    return retrieved


def generate_answer(query: str, context_docs: list[dict]) -> str:
    """Generate answer using OpenAI with retrieved context."""
    if not context_docs:
        return "I don't have any context to answer this question. Please upload a document first."
    
    context = "\n\n".join([
        f"[Source {doc['rank']}] (relevance: {doc['similarity']:.0%})\n{doc['text']}"
        for doc in context_docs
    ])
    
    prompt = f"""Answer the question based on the provided context. 
If the context doesn't contain enough information to answer fully, say so.
Always reference which source(s) you used.

CONTEXT:
{context}

QUESTION: {query}

ANSWER:"""

    try:
        response = openai_client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[
                {"role": "system", "content": "You are a helpful assistant that answers questions based on provided document context. Be concise and cite your sources."},
                {"role": "user", "content": prompt}
            ],
            temperature=0.3,
            max_tokens=500
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"❌ Error generating answer: {str(e)}"


def ask_question(query: str) -> tuple[str, str]:
    """Main function to handle user questions."""
    if not query.strip():
        return "Please enter a question.", ""
    
    if collection is None:
        return "Please upload and process a document first.", ""
    
    retrieved = retrieve_context(query, top_k=3)
    answer = generate_answer(query, retrieved)
    
    sources = "\n\n---\n\n**πŸ“š Retrieved Sources:**\n\n"
    for doc in retrieved:
        sources += f"**[Source {doc['rank']}]** (relevance: {doc['similarity']:.0%})\n"
        sources += f"```\n{doc['text'][:300]}{'...' if len(doc['text']) > 300 else ''}\n```\n\n"
    
    return answer, sources


# Build Gradio interface
with gr.Blocks(title="RAG Document Q&A", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # πŸ“„ RAG Document Q&A Assistant
    
    Upload a document (PDF or TXT), choose a chunking strategy, and ask questions!
    
    **How it works:**
    1. Your document is split into chunks using the selected strategy
    2. Chunks are embedded using Sentence Transformers (all-MiniLM-L6-v2)
    3. When you ask a question, relevant chunks are retrieved using semantic search
    4. GPT-4o-mini generates an answer based on the retrieved context
    
    ---
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### πŸ“€ Step 1: Upload Document")
            file_input = gr.File(
                label="Upload PDF or TXT",
                file_types=[".pdf", ".txt", ".md"]
            )
            chunking_dropdown = gr.Dropdown(
                choices=["Fixed-size (500 chars)", "Paragraph-based"],
                value="Paragraph-based",
                label="Chunking Strategy"
            )
            process_btn = gr.Button("Process Document", variant="primary")
            process_output = gr.Markdown(label="Processing Status")
        
        with gr.Column(scale=2):
            gr.Markdown("### πŸ’¬ Step 2: Ask Questions")
            question_input = gr.Textbox(
                label="Your Question",
                placeholder="What is this document about?",
                lines=2
            )
            ask_btn = gr.Button("Ask", variant="primary")
            
            answer_output = gr.Markdown(label="Answer")
            sources_output = gr.Markdown(label="Sources")
    
    gr.Markdown("""
    ---
    
    **πŸ“š References:**
    - [RAG Original Paper (Lewis et al., 2020)](https://arxiv.org/abs/2005.11401)
    - [RAG Survey (Gao et al., 2023)](https://arxiv.org/pdf/2312.10997)
    - [Chunking Strategies for RAG (Merola & Singh, 2025)](https://arxiv.org/abs/2504.19754)
    
    Built as part of an AI/ML Engineering portfolio project.
    """)
    
    process_btn.click(
        fn=process_document,
        inputs=[file_input, chunking_dropdown],
        outputs=[process_output]
    )
    
    ask_btn.click(
        fn=ask_question,
        inputs=[question_input],
        outputs=[answer_output, sources_output]
    )
    
    question_input.submit(
        fn=ask_question,
        inputs=[question_input],
        outputs=[answer_output, sources_output]
    )


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