File size: 12,080 Bytes
7dd18c1
10adc20
 
 
7dd18c1
10adc20
7dd18c1
 
 
d648b89
 
 
10adc20
 
 
7dd18c1
10adc20
7dd18c1
 
 
10adc20
7dd18c1
10adc20
7dd18c1
 
 
7364bf5
 
 
7dd18c1
 
7364bf5
10adc20
 
7364bf5
 
 
10adc20
 
 
 
 
 
 
7364bf5
 
 
10adc20
 
 
 
 
 
7dd18c1
10adc20
7dd18c1
 
10adc20
 
7dd18c1
7364bf5
d648b89
 
10adc20
04b52b6
d648b89
 
7364bf5
04b52b6
7364bf5
04b52b6
 
 
 
 
7364bf5
d648b89
04b52b6
 
7dd18c1
04b52b6
7dd18c1
10adc20
04b52b6
7dd18c1
 
10adc20
7364bf5
10adc20
 
7dd18c1
10adc20
 
 
7dd18c1
10adc20
 
 
 
 
 
 
7dd18c1
10adc20
 
 
7dd18c1
10adc20
7dd18c1
 
10adc20
7364bf5
10adc20
7364bf5
7dd18c1
10adc20
 
7dd18c1
 
 
 
10adc20
7dd18c1
 
 
10adc20
7dd18c1
 
10adc20
7dd18c1
7364bf5
10adc20
 
 
7dd18c1
10adc20
 
7dd18c1
 
10adc20
7364bf5
7dd18c1
10adc20
 
 
7dd18c1
10adc20
7dd18c1
10adc20
7dd18c1
10adc20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7dd18c1
10adc20
 
 
 
 
 
7dd18c1
 
10adc20
7dd18c1
 
7364bf5
10adc20
7364bf5
 
10adc20
 
 
7dd18c1
 
 
10adc20
 
 
 
7dd18c1
10adc20
 
 
 
 
 
7dd18c1
10adc20
7dd18c1
10adc20
 
 
 
 
7dd18c1
 
10adc20
 
 
7dd18c1
 
7364bf5
 
 
 
10adc20
 
7dd18c1
10adc20
7dd18c1
 
 
10adc20
7dd18c1
 
 
 
10adc20
7dd18c1
 
 
 
 
 
10adc20
 
7dd18c1
10adc20
7dd18c1
10adc20
 
 
 
 
 
7dd18c1
10adc20
 
 
7dd18c1
 
10adc20
7364bf5
10adc20
 
 
 
 
 
 
 
7dd18c1
10adc20
 
 
7dd18c1
 
 
 
 
10adc20
7dd18c1
 
 
10adc20
7dd18c1
cfaedf1
 
 
 
 
 
 
 
 
 
 
10adc20
 
 
7dd18c1
10adc20
 
7dd18c1
10adc20
 
 
7dd18c1
10adc20
 
7dd18c1
10adc20
7dd18c1
10adc20
7364bf5
10adc20
 
 
7dd18c1
10adc20
 
 
7dd18c1
 
 
 
10adc20
 
 
7dd18c1
 
10adc20
 
7dd18c1
10adc20
 
 
 
 
 
7dd18c1
10adc20
 
 
 
 
 
 
 
 
 
7dd18c1
10adc20
7dd18c1
 
 
10adc20
7364bf5
7dd18c1
 
cfaedf1
10adc20
cfaedf1
 
 
10adc20
7dd18c1
cfaedf1
7dd18c1
10adc20
7dd18c1
7364bf5
7dd18c1
 
cfaedf1
 
 
 
87b1efa
 
cfaedf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7364bf5
cfaedf1
10adc20
cfaedf1
 
 
 
 
 
 
10adc20
cfaedf1
 
 
87b1efa
cfaedf1
 
87b1efa
cfaedf1
 
 
 
 
 
 
 
601ec7c
cfaedf1
 
 
 
 
10adc20
cfaedf1
10adc20
7dd18c1
 
87b1efa
24f296d
7dd18c1
 
 
 
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"

import urllib.request
import tempfile
import re
import requests
import trafilatura
from bs4 import BeautifulSoup
import fitz
from PIL import Image
import io
import gradio as gr
import numpy as np
from io import StringIO
from sklearn.neighbors import NearestNeighbors
from sentence_transformers import SentenceTransformer
import torch
from transformers import (
    AutoTokenizer,
    AutoModelForCausalLM,
    NllbTokenizer,
    M2M100ForConditionalGeneration,
)

# CONFIG

EMBED_MODEL = "intfloat/e5-small-v2"         
LLM_MODEL = "Qwen/Qwen2.5-0.5B-Instruct"      
TRANS_MODEL_ID = "facebook/nllb-200-distilled-600M"

WORD_CHUNK_SIZE = 150     
TOP_K_CHUNKS = 5


# TEXT CLEANING

def preprocess(text: str) -> str:
    """Simple whitespace normalization (keeps content)."""
    text = text.replace("\r", " ").replace("\n", " ")
    text = re.sub(r"\s+", " ", text)
    return text.strip()



#  (PyMuPDF + urlretrieve)

def safe_download_pdf(url: str) -> str:
    """
    Safely download a PDF from URL using urllib.
    Works well for research sites (ScienceDirect, Euclid, etc.).
    Returns path to a temporary PDF file.
    """
    tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
    tmp_path = tmp.name
    tmp.close()

    urllib.request.urlretrieve(url, tmp_path)
    return tmp_path



def pdf_to_text(path):
    doc = fitz.open(path)
    pages = []

    for page in doc:
        
        text = page.get_text("text")
        
        if len(text.strip()) < 50 or "<<" in text or "/Obj" in text:
            try:
                text = page.get_textpage().extractText()
            except:
                pass
                
        if len(text.strip()) < 50:
            blocks = page.get_text("blocks")
            text = " ".join(b[4] for b in blocks if isinstance(b[4], str))

        pages.append(text)

    doc.close()
    return pages


def text_to_chunks(texts, word_length=WORD_CHUNK_SIZE, start_page=1):
    """Convert a list of page texts into overlapping word chunks """
    text_toks = [t.split(" ") for t in texts]
    chunks = []

    for idx, words in enumerate(text_toks):
        for i in range(0, len(words), word_length):
            chunk_words = words[i : i + word_length]

            if (
                (i + word_length) > len(words)
                and (len(chunk_words) < word_length)
                and (len(text_toks) != (idx + 1))
            ):
                text_toks[idx + 1] = chunk_words + text_toks[idx + 1]
                continue

            chunk = " ".join(chunk_words).strip()
            chunk = f'[Page no. {idx + start_page}] "{chunk}"'
            chunks.append(chunk)

    return chunks


# HTML ARTICLE INGEST (for non-PDF URLs)

def extract_html_text(url: str) -> str:
    #Extract main text from an article URL (non-PDF)- Uses trafilatura first, then BeautifulSoup fallback
    headers = {
        "User-Agent": "Mozilla/5.0 (compatible; RAG-bot/1.0)",
        "Accept": "text/html,application/xhtml+xml",
    }

    try:
        resp = requests.get(url, headers=headers, timeout=20)
        html = resp.text
    except Exception as e:
        return f"Error loading HTML: {e}"

    # Try trafilatura
    extracted = trafilatura.extract(html)
    if extracted and len(extracted) > 200:
        return preprocess(extracted)

    # Fallback to raw text via BeautifulSoup
    soup = BeautifulSoup(html, "html.parser")
    for bad in soup(["script", "style", "noscript"]):
        bad.decompose()

    text = soup.get_text(" ", strip=True)
    return preprocess(text)


# SEMANTIC SEARCH

class SemanticSearch:
    def __init__(self, model_name=EMBED_MODEL):
        self.embedder = SentenceTransformer(model_name)
        self.fitted = False
        self.chunks = []
        self.nn = None

    def fit(self, chunks, batch_size=512, n_neighbors=TOP_K_CHUNKS):
        self.chunks = chunks
        emb = self.embedder.encode(
            chunks,
            batch_size=batch_size,
            convert_to_numpy=True,
            show_progress_bar=False,
        ).astype("float32")

        n_neighbors = min(n_neighbors, len(emb))
        self.nn = NearestNeighbors(n_neighbors=n_neighbors, metric="cosine")
        self.nn.fit(emb)
        self.fitted = True

    def search(self, query, k=TOP_K_CHUNKS):
        if not self.fitted:
            raise ValueError("Vector store not ready. Load a document first.")
        k = min(k, len(self.chunks))
        q_emb = self.embedder.encode([query], convert_to_numpy=True).astype("float32")
        dist, idx = self.nn.kneighbors(q_emb, n_neighbors=k)
        idx = idx[0]
        dist = dist[0]
        results = [(self.chunks[i], float(dist[j])) for j, i in enumerate(idx)]
        return results


vs = SemanticSearch()



# LOAD QWEN LLM


q_tokenizer = AutoTokenizer.from_pretrained(LLM_MODEL)
q_model = AutoModelForCausalLM.from_pretrained(LLM_MODEL).to("cpu")
q_model.eval()


@torch.no_grad()
def run_llm(system_prompt: str, user_prompt: str, max_new_tokens: int = 256) -> str:
    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": user_prompt},
    ]
    text = q_tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True,
    )
    inputs = q_tokenizer(text, return_tensors="pt").to("cpu")

    outputs = q_model.generate(
        **inputs,
        max_new_tokens=max_new_tokens,
        do_sample=False,
        temperature=0.2,
        top_p=0.9,
        eos_token_id=q_tokenizer.eos_token_id,
    )

    gen = outputs[0][inputs["input_ids"].shape[1] :]
    out = q_tokenizer.decode(gen, skip_special_tokens=True)
    return out.strip()



# FOR TRANSLATION (NLLB-200)


t_tokenizer = NllbTokenizer.from_pretrained(TRANS_MODEL_ID)
t_model = M2M100ForConditionalGeneration.from_pretrained(TRANS_MODEL_ID).to("cpu")

LANG_CODES = {
    "English": "eng_Latn",
    "Hindi": "hin_Deva",
    "Telugu": "tel_Telu",
    "Tamil": "tam_Taml",
    "Kannada": "kan_Knda",
    "Malayalam": "mal_Mlym",
    "Bengali": "ben_Beng",
    "Marathi": "mar_Deva",
    "Gujarati": "guj_Gujr",
    "Odia": "ory_Orya",
    "Punjabi": "pan_Guru",
    "Assamese": "asm_Beng",
}


def translate_answer(text: str, lang: str) -> str:
    if lang == "auto" or lang == "English":
        return text

    try:
        tgt = LANG_CODES[lang]
        inputs = t_tokenizer(text, return_tensors="pt").to("cpu")
        outputs = t_model.generate(
            **inputs,
            forced_bos_token_id=t_tokenizer.convert_tokens_to_ids(tgt),
            max_length=400,
        )
        return t_tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
    except Exception as e:
        print("Translation error:", e)
        return text

# RAG PROMPT + ANSWERING

def build_prompt(question: str, retrieved):
    ctx = "\n\n".join([c for c, _ in retrieved])
    prompt = f"""
You are a precise and factual RAG assistant.

Use ONLY the context below to answer the question.
If the context does not contain the answer, say:
"I don't know based on this document."

When possible:
- Answer in short, clear points.
- Refer to evidence using the [Page no. X] tags in the text.

CONTEXT:
{ctx}

QUESTION:
{question}

Answer in English:
""".strip()
    return prompt

def highlight_sources(retrieved):
    html = "<h4>πŸ“š Source Passages</h4>"
    for i, (chunk, score) in enumerate(retrieved):
        html += f"""
        <div style='padding:10px; background:#eef6ff; margin:8px 0;'>
            <b>[{i+1}] Score = {1-score:.3f}</b><br>
            {chunk[:400]}...
        </div>
        """
    return html
    
def answer_question(question: str, language: str):
    if question.strip() == "":
        return "Please enter a question.", ""

    if not vs.fitted:
        return "Please load a PDF or URL first.", ""

    retrieved = vs.search(question, k=TOP_K_CHUNKS)
    prompt = build_prompt(question, retrieved)
    system = "You are a reliable RAG assistant for academic PDFs and web articles."

    english_answer = run_llm(system, prompt)
    final_answer = translate_answer(english_answer, language)

    return final_answer, highlight_sources(retrieved)

# LOADERS

def load_pdf_ui(pdf_file, language):
    if pdf_file is None:
        return "Upload a PDF first."

    texts = pdf_to_text(pdf_file.name)
    chunks = text_to_chunks(texts)
    vs.fit(chunks)

    return f"PDF loaded with {len(chunks)} chunks."


def load_url_ui(url: str, language: str):
    url = (url or "").strip()
    if url == "":
        return "Enter a URL."

    # If URL looks like a direct PDF link β†’ use safe downloader
    lower = url.lower().split("?")[0]
    try:
        if lower.endswith(".pdf"):
            pdf_path = safe_download_pdf(url)
            texts = pdf_to_text(pdf_path)
            chunks = text_to_chunks(texts)
            vs.fit(chunks)
            return f"PDF URL loaded with {len(chunks)} chunks."
        else:
            # Treat as HTML article
            text = extract_html_text(url)
            if text.startswith("Error loading HTML:"):
                vs.fitted = False
                return text

            texts = [text]  # one big "page"
            chunks = text_to_chunks(texts, start_page=1)
            vs.fit(chunks)
            return f"URL article loaded with {len(chunks)} chunks."
    except Exception as e:
        vs.fitted = False
        return f"Error loading URL: {e}"


# GRADIO UI

def create_app():
    with gr.Blocks() as demo:
        gr.Markdown("<h1 style='text-align:center;'> Multilingual Chat with PDF / URL</h1>")
        gr.Markdown(
            "Upload a PDF or paste a URL (PDF or article). "
            "The app creates embeddings, retrieves the most relevant chunks, "
            "and answers using a small local LLM."
        )

        # Language selector
        lang = gr.Dropdown(
            ["auto"] + list(LANG_CODES.keys()),
            value="auto",
            label="Answer Language",
        )

        # ---------------------------
        #   DOCUMENT LOADING AREA
        # ---------------------------
        gr.Markdown("### Load PDF or Article")

        with gr.Row():
            pdf = gr.File(
                label="Upload PDF",
                file_types=[".pdf"],
                height=70   # smaller box
            )
            pdf_status = gr.HTML()

        # Auto load PDF on upload
        pdf.upload(load_pdf_ui, [pdf, lang], pdf_status)

        gr.Markdown("---")

        # URL input
        url = gr.Textbox(
            label="Enter URL",
            placeholder="https://example.com/article.pdf",
        )
        url_status = gr.HTML()

        # Auto load URL on pressing Enter
        url.submit(load_url_ui, [url, lang], url_status)

        # ---------------------------
        #   CHAT AREA
        # ---------------------------
        gr.Markdown("### Ask Questions About the Loaded Document")

        q = gr.Textbox(label="Your Question")
        a = gr.HTML(label="Answer")
        cits = gr.HTML(label="Source Passages")

        ask_btn = gr.Button("Ask", variant="primary")
        ask_btn.click(answer_question, [q, lang], [a, cits])

        # Pre-set example question buttons
        gr.Markdown("### Example Questions")
        with gr.Row():
            b1 = gr.Button("Summarize the document")
            b2 = gr.Button("What are the key findings?")
            b3 = gr.Button("Explain the methodology used")
            b4 = gr.Button("What are the main limitations?")
            b5 = gr.Button("What is the conclusion of this paper?")

        b1.click(lambda: "Summarize the document.", None, q)
        b2.click(lambda: "What are the key findings?", None, q)
        b3.click(lambda: "Explain the methodology used in this study.", None, q)
        b4.click(lambda: "What are the main limitations of this study?", None, q)
        b5.click(lambda: "What is the conclusion of this paper?", None, q)

        return demo





demo = create_app()

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