DataDrivenAlex commited on
Commit
6dbd1b0
·
verified ·
1 Parent(s): be2a36d

Add RAG Document Q&A app

Browse files
Files changed (2) hide show
  1. README.md +15 -6
  2. index.html +98 -19
README.md CHANGED
@@ -1,10 +1,19 @@
1
  ---
2
- title: Rag Document Qa
3
- emoji: 🌖
4
- colorFrom: yellow
5
- colorTo: blue
6
  sdk: static
7
- pinned: false
 
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
1
  ---
2
+ title: RAG Document Q&A
3
+ emoji: 📚
4
+ colorFrom: blue
5
+ colorTo: indigo
6
  sdk: static
7
+ pinned: true
8
+ license: mit
9
+ short_description: "In-browser RAG: ask questions about your own PDFs"
10
+ tags: [rag, retrieval, question-answering, transformers.js, semantic-search]
11
  ---
12
 
13
+ # 📚 RAG Document Q&A
14
+
15
+ Retrieval-Augmented Generation pipeline that runs entirely in the browser: document loading (PDF via pdf.js, TXT, pasted text), chunking with overlap, sentence embeddings (Xenova/all-MiniLM-L6-v2), cosine-similarity vector search and extractive answering (Xenova/distilbert-base-cased-distilled-squad). Retrieved passages are shown with similarity scores so every answer is traceable. Documents never leave the user's device.
16
+
17
+ Runs fully client-side with [Transformers.js](https://huggingface.co/docs/transformers.js) (ONNX Runtime Web) — no server, no API keys.
18
+
19
+ **Author:** [Alexandru Cojocari](https://www.linkedin.com/in/alexdataanalyst/) · [GitHub](https://github.com/DatadrivenAlex) — Junior AI Developer (Generative AI, RAG, Agentic AI, Secure AI)
index.html CHANGED
@@ -1,19 +1,98 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
3
+ <title>RAG Document Q&amp;A</title><link rel="stylesheet" href="style.css"></head>
4
+ <body><main>
5
+ <h1>📚 RAG Document Q&amp;A</h1>
6
+ <p class="sub">Retrieval-Augmented question answering over your own documents, running 100% in your browser.
7
+ Pipeline: load (PDF/TXT/paste) → chunk with overlap → embed (all-MiniLM-L6-v2) → cosine vector search → extractive answer (DistilBERT-SQuAD), with sources.</p>
8
+
9
+ <div class="card">
10
+ <div class="row">
11
+ <div><label>Documents (PDF, TXT, MD)</label><input id="files" type="file" multiple accept=".pdf,.txt,.md">
12
+ <p class="muted">Files never leave your device.</p></div>
13
+ <div><label>…or paste text</label><textarea id="pasted"></textarea></div>
14
+ </div>
15
+ <button id="build">1. Build vector index</button>
16
+ <p id="status" class="muted"></p><progress id="prog" value="0" max="100" hidden></progress>
17
+ </div>
18
+
19
+ <div class="card">
20
+ <label>2. Ask a question</label><input id="q" type="text" value="Why does RAG reduce hallucinations?">
21
+ <label>Passages to retrieve (top-k): <span id="kv">3</span></label><input id="k" type="range" min="1" max="8" value="3">
22
+ <br><button id="ask" disabled>Ask</button>
23
+ <div id="answer"></div><div id="sources"></div>
24
+ </div>
25
+
26
+ <footer>Stack: Transformers.js · ONNX Runtime Web · pdf.js · vanilla JS. Built by
27
+ <a href="https://www.linkedin.com/in/alexdataanalyst/" target="_blank">Alexandru Cojocari</a> ·
28
+ <a href="https://github.com/DatadrivenAlex" target="_blank">GitHub</a></footer>
29
+ </main>
30
+ <script type="module">
31
+ import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3';
32
+ import * as pdfjs from 'https://cdn.jsdelivr.net/npm/pdfjs-dist@4.10.38/build/pdf.min.mjs';
33
+ pdfjs.GlobalWorkerOptions.workerSrc = 'https://cdn.jsdelivr.net/npm/pdfjs-dist@4.10.38/build/pdf.worker.min.mjs';
34
+
35
+ const $ = id => document.getElementById(id);
36
+ $('pasted').value = `Retrieval-Augmented Generation (RAG) combines a retriever and a generator.
37
+ Documents are split into chunks, converted into embeddings and stored in a vector database such as FAISS or ChromaDB.
38
+ At query time the question is embedded, the most similar chunks are retrieved and passed to a language model as context.
39
+ RAG reduces hallucinations because answers are grounded in retrieved sources, and it lets a model use private or recent data without fine-tuning.
40
+ Typical frameworks for building RAG pipelines are LangChain, LlamaIndex and LangGraph for agentic workflows.`;
41
+ $('k').oninput = e => $('kv').textContent = e.target.value;
42
+
43
+ let embedder, qa, index = null;
44
+ const progress = info => { if (info.status === 'progress') { $('prog').hidden = false; $('prog').value = info.progress; } };
45
+ const esc = s => s.replace(/[&<>]/g, c => ({'&':'&amp;','<':'&lt;','>':'&gt;'}[c]));
46
+
47
+ async function readFile(f) {
48
+ if (f.name.toLowerCase().endsWith('.pdf')) {
49
+ const pdf = await pdfjs.getDocument({ data: await f.arrayBuffer() }).promise;
50
+ let out = '';
51
+ for (let i = 1; i <= pdf.numPages; i++) out += (await (await pdf.getPage(i)).getTextContent()).items.map(t => t.str).join(' ') + '\n';
52
+ return out;
53
+ }
54
+ return await f.text();
55
+ }
56
+ function chunk(text, size = 500, overlap = 100) {
57
+ text = text.replace(/\s+/g, ' ').trim(); const out = [];
58
+ for (let s = 0; s < text.length; s += size - overlap) out.push(text.slice(s, s + size));
59
+ return out.filter(c => c.length > 30);
60
+ }
61
+ const dot = (a, b) => a.reduce((s, v, i) => s + v * b[i], 0);
62
+
63
+ $('build').onclick = async () => {
64
+ $('build').disabled = true;
65
+ try {
66
+ let text = $('pasted').value;
67
+ for (const f of $('files').files) text += '\n\n' + await readFile(f);
68
+ const chunks = chunk(text);
69
+ if (!chunks.length) { $('status').textContent = 'Please add a document or text.'; return; }
70
+ $('status').textContent = 'Loading embedding model…';
71
+ embedder ??= await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2', { progress_callback: progress });
72
+ $('status').textContent = `Embedding ${chunks.length} chunks…`;
73
+ const vecs = (await embedder(chunks, { pooling: 'mean', normalize: true })).tolist();
74
+ index = chunks.map((c, i) => ({ c, v: vecs[i] }));
75
+ $('status').textContent = 'Loading QA model…';
76
+ qa ??= await pipeline('question-answering', 'Xenova/distilbert-base-cased-distilled-squad', { progress_callback: progress });
77
+ $('prog').hidden = true;
78
+ $('status').innerHTML = `<span class="ok">✔ Indexed ${chunks.length} chunks.</span> Ask a question below.`;
79
+ $('ask').disabled = false;
80
+ } catch (e) { $('status').innerHTML = `<span class="bad">Error: ${esc(e.message)}</span>`; }
81
+ finally { $('build').disabled = false; }
82
+ };
83
+
84
+ $('ask').onclick = async () => {
85
+ const q = $('q').value.trim(); if (!q || !index) return;
86
+ $('ask').disabled = true; $('answer').textContent = 'Thinking…';
87
+ try {
88
+ const qv = (await embedder(q, { pooling: 'mean', normalize: true })).tolist()[0];
89
+ const hits = index.map(x => ({ ...x, s: dot(qv, x.v) })).sort((a, b) => b.s - a.s).slice(0, +$('k').value);
90
+ const res = await qa(q, hits.map(h => h.c).join(' '));
91
+ $('answer').innerHTML = `<h3>Answer</h3><p style="font-size:18px"><b>${esc(res.answer || '—')}</b>
92
+ <span class="muted">(confidence ${(res.score ?? 0).toFixed(2)})</span></p>`;
93
+ $('sources').innerHTML = '<h3>Sources</h3>' + hits.map((h, i) =>
94
+ `<p class="muted">[${i + 1}] cosine similarity ${h.s.toFixed(2)}</p><blockquote>${esc(h.c)}</blockquote>`).join('');
95
+ } catch (e) { $('answer').innerHTML = `<span class="bad">Error: ${esc(e.message)}</span>`; }
96
+ finally { $('ask').disabled = false; }
97
+ };
98
+ </script></body></html>