architk47 commited on
Commit
00ddfc7
Β·
verified Β·
1 Parent(s): 3dacbe7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +216 -149
app.py CHANGED
@@ -1,195 +1,262 @@
1
  # app.py
2
  import os
3
- import tempfile
 
4
  import numpy as np
5
  import faiss
6
  import pdfplumber
7
  import docx
8
- from sentence_transformers import SentenceTransformer
9
  import gradio as gr
10
 
11
- # ---------------------------
12
- # Globals (in-memory store)
13
- # ---------------------------
14
- documents = [] # list of text chunks
15
- metadata = [] # list of source filenames (same length as documents)
16
- index = None # FAISS index
17
- dim = None
18
- model = None
 
 
19
 
20
- # ---------------------------
21
  # Config
22
- # ---------------------------
23
- EMBEDDING_MODEL_NAME = "all-MiniLM-L6-v2"
24
- CHUNK_SIZE = 200
25
- OVERLAP = 50
26
-
27
- # ---------------------------
28
- # Helpers: file loaders
29
- # ---------------------------
30
- def load_pdf(path):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  text = ""
32
- with pdfplumber.open(path) as pdf:
33
  for page in pdf.pages:
34
  t = page.extract_text()
35
  if t:
36
  text += t + "\n"
37
  return text
38
 
39
- def load_docx(path):
40
- d = docx.Document(path)
41
  return "\n".join(p.text for p in d.paragraphs)
42
 
43
- def load_txt(path):
44
- with open(path, "r", encoding="utf-8", errors="ignore") as f:
45
- return f.read()
 
 
46
 
47
- def chunk_text(text, chunk_size=CHUNK_SIZE, overlap=OVERLAP):
48
- words = text.split()
 
 
 
 
 
49
  chunks = []
50
- start = 0
51
- while start < len(words):
52
- end = start + chunk_size
53
- chunks.append(" ".join(words[start:end]))
54
- start += chunk_size - overlap
55
- return [c for c in chunks if c.strip()]
56
-
57
- # ---------------------------
58
- # Embedding / FAISS helpers
59
- # ---------------------------
60
- def ensure_model():
61
- global model, dim
62
- if model is None:
63
- # force CPU to avoid meta-tensor issues
64
- model = SentenceTransformer(EMBEDDING_MODEL_NAME, device="cpu")
65
- # compute dim by encoding a tiny sample
66
- v = model.encode(["hello"], convert_to_numpy=True)
67
- dim = v.shape[1]
68
 
 
 
 
69
  def ensure_index():
70
- global index, dim
71
  if index is None:
72
- # use inner product on normalized vectors to approximate cosine
73
- index = faiss.IndexFlatIP(dim)
74
 
75
- def add_embeddings_and_store(chunks, source_name):
76
- global index, documents, metadata
77
- ensure_model()
78
  ensure_index()
79
- # encode
80
- embs = model.encode(chunks, convert_to_numpy=True, show_progress_bar=False)
81
- # normalize to unit vectors for cosine via inner product
82
- norms = np.linalg.norm(embs, axis=1, keepdims=True)
83
  norms[norms == 0] = 1e-9
84
- embs = embs / norms
85
- embs = embs.astype("float32")
86
- index.add(embs)
87
  for c in chunks:
88
  documents.append(c)
89
- metadata.append(source_name)
90
 
91
- # ---------------------------
92
- # Ingest uploaded files
93
- # ---------------------------
94
- def ingest_uploaded_files(files):
95
- # files: list of temporary file objects from Gradio
 
96
  if not files:
97
- return "No files provided."
98
  count = 0
99
  for f in files:
100
- # Gradio passes a tuple-like file object; we write to temp and read
101
- # f.name exists for local path; otherwise read bytes
102
- try:
103
- path = f.name
104
- # If path exists (when running locally) use it
105
- if not os.path.exists(path):
106
- raise Exception("missing path")
107
- except Exception:
108
- # write bytes to temp file
109
- tmp = tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(f.orig_name)[1])
110
- tmp.write(f.read())
111
- tmp.flush()
112
- path = tmp.name
113
-
114
- ext = os.path.splitext(path)[1].lower().lstrip(".")
115
- if ext == "pdf":
116
- text = load_pdf(path)
117
- elif ext in ("docx", "doc"):
118
- text = load_docx(path)
119
- elif ext in ("txt", "md"):
120
- text = load_txt(path)
121
  else:
122
- # ignore unsupported types but continue
 
 
 
 
 
 
 
123
  continue
124
-
125
  chunks = chunk_text(text)
126
  if chunks:
127
- add_embeddings_and_store(chunks, os.path.basename(path))
128
  count += 1
129
- return f"Ingested {count} file(s)."
130
 
131
- # ---------------------------
132
- # Search + Answer
133
- # ---------------------------
134
- def search_and_answer(question, top_k=3):
135
- if index is None or len(documents) == 0:
136
- return "No documents ingested yet. Upload and process files first.", []
137
- ensure_model()
138
- q_emb = model.encode([question], convert_to_numpy=True, show_progress_bar=False)
139
- q_emb = q_emb / (np.linalg.norm(q_emb, axis=1, keepdims=True) + 1e-9)
140
- q_emb = q_emb.astype("float32")
141
- D, I = index.search(q_emb, top_k) # D = inner products (cosine-like), I = indices
142
  results = []
143
- for score, idx in zip(D[0], I[0]):
144
- if idx < len(documents):
145
- results.append({"chunk": documents[idx], "source": metadata[idx], "score": float(score)})
146
- if not results:
147
- return "Answer not found in the uploaded documents.", []
148
- # Build short answer by concatenating top 1-2 chunks
149
- top_texts = [r["chunk"] for r in results[:2]]
150
- answer = "\n\n".join(top_texts)
151
- return answer, results
152
-
153
- # ---------------------------
154
- # Gradio UI functions
155
- # ---------------------------
156
- def handle_process(files):
157
- msg = ingest_uploaded_files(files)
158
- return msg
159
-
160
- def handle_query(question, top_k):
161
- answer, matches = search_and_answer(question, top_k)
162
- # prepare display for matches: show score and source and snippet
163
- if matches:
164
- md = []
165
- for m in matches:
166
- snippet = m["chunk"]
167
- src = m["source"]
168
- sc = round(m["score"], 4)
169
- md.append(f"**Source:** {src} \n**Score:** {sc} \n\n{snippet}")
170
- matches_str = "\n\n---\n\n".join(md)
171
- else:
172
- matches_str = ""
173
- return answer, matches_str
174
-
175
- # ---------------------------
176
- # Launch Gradio app
177
- # ---------------------------
178
- with gr.Blocks(title="Mini Document Q&A (Gradio)") as demo:
179
- gr.Markdown("# Q&A\nUpload documents and ask questions **only based on their content**.")
180
- with gr.Row():
181
- file_input = gr.File(label="Upload documents (PDF, DOCX, TXT)", file_count="multiple")
182
- process_btn = gr.Button("Process Documents")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  status = gr.Textbox(label="Status", interactive=False)
184
  gr.Markdown("---")
185
- question = gr.Textbox(label="Ask a question", placeholder="Enter a question based on uploaded docs...")
186
- top_k = gr.Slider(1, 5, value=3, step=1, label="Top K chunks to retrieve")
187
  ask_btn = gr.Button("Get Answer")
188
- answer_out = gr.Textbox(label="Answer (retrieved text)", interactive=False)
189
- matches_out = gr.Markdown(label="Matched sections (evidence)")
190
 
191
- process_btn.click(fn=handle_process, inputs=file_input, outputs=status)
192
- ask_btn.click(fn=handle_query, inputs=[question, top_k], outputs=[answer_out, matches_out])
193
 
194
  if __name__ == "__main__":
195
- demo.launch(server_name="0.0.0.0", share=True)
 
1
  # app.py
2
  import os
3
+ import io
4
+ import re
5
  import numpy as np
6
  import faiss
7
  import pdfplumber
8
  import docx
 
9
  import gradio as gr
10
 
11
+ from sentence_transformers import SentenceTransformer
12
+ from transformers import pipeline
13
+
14
+ # -------------------------
15
+ # In-memory store
16
+ # -------------------------
17
+ documents = []
18
+ sources = []
19
+ index = None
20
+ embed_dim = None
21
 
22
+ # -------------------------
23
  # Config
24
+ # -------------------------
25
+ EMBED_MODEL_NAME = "all-MiniLM-L6-v2"
26
+ LLM_MODEL_NAME = "google/flan-t5-base" # small, CPU-friendly
27
+ CHUNK_SIZE = 120
28
+ OVERLAP = 30
29
+ TOP_K_DEFAULT = 3
30
+
31
+ SIMILARITY_THRESHOLD = 0.30 # chunk-level gate (keep)
32
+ SENTENCE_THRESHOLD = 0.34 # sentence-level gate before LLM
33
+ SENTENCE_CLOSE_DELTA = 0.05 # include additional sentence if score within delta of top
34
+
35
+ # -------------------------
36
+ # Lazy models
37
+ # -------------------------
38
+ embedder = None
39
+ llm = None
40
+
41
+ def load_embedder():
42
+ global embedder, embed_dim
43
+ if embedder is None:
44
+ embedder = SentenceTransformer(EMBED_MODEL_NAME, device="cpu")
45
+ embed_dim = int(embedder.encode(["_"], convert_to_numpy=True).shape[1])
46
+
47
+ def load_llm():
48
+ global llm
49
+ if llm is None:
50
+ # device=-1 => CPU (Hugging Face Spaces)
51
+ llm = pipeline("text2text-generation", model=LLM_MODEL_NAME, device=-1)
52
+
53
+ # -------------------------
54
+ # Byte-safe file loaders
55
+ # -------------------------
56
+ def load_pdf_bytes(b: bytes) -> str:
57
  text = ""
58
+ with pdfplumber.open(io.BytesIO(b)) as pdf:
59
  for page in pdf.pages:
60
  t = page.extract_text()
61
  if t:
62
  text += t + "\n"
63
  return text
64
 
65
+ def load_docx_bytes(b: bytes) -> str:
66
+ d = docx.Document(io.BytesIO(b))
67
  return "\n".join(p.text for p in d.paragraphs)
68
 
69
+ def load_txt_bytes(b: bytes) -> str:
70
+ try:
71
+ return b.decode("utf-8")
72
+ except Exception:
73
+ return b.decode("latin-1", errors="ignore")
74
 
75
+ # -------------------------
76
+ # Chunking (sentence-aware)
77
+ # -------------------------
78
+ def chunk_text(text: str):
79
+ # simple sentence-splitting then group to target token/word count
80
+ text = text.replace("\r", " ").replace("\n", " ")
81
+ sentences = re.split(r'(?<=[\.\?\!])\s+', text)
82
  chunks = []
83
+ current = []
84
+ for s in sentences:
85
+ if not s.strip():
86
+ continue
87
+ current.append(s.strip())
88
+ if len(" ".join(current).split()) >= CHUNK_SIZE:
89
+ chunks.append(" ".join(current))
90
+ # keep overlap sentences from end
91
+ current = current[-OVERLAP:]
92
+ if current:
93
+ chunks.append(" ".join(current))
94
+ return [c.strip() for c in chunks if c.strip()]
 
 
 
 
 
 
95
 
96
+ # -------------------------
97
+ # Index helpers
98
+ # -------------------------
99
  def ensure_index():
100
+ global index
101
  if index is None:
102
+ index = faiss.IndexFlatIP(embed_dim)
 
103
 
104
+ def add_chunks(chunks, source_name):
105
+ load_embedder()
 
106
  ensure_index()
107
+ vecs = embedder.encode(chunks, convert_to_numpy=True)
108
+ norms = np.linalg.norm(vecs, axis=1, keepdims=True)
 
 
109
  norms[norms == 0] = 1e-9
110
+ vecs = vecs / norms
111
+ vecs = vecs.astype("float32")
112
+ index.add(vecs)
113
  for c in chunks:
114
  documents.append(c)
115
+ sources.append(source_name)
116
 
117
+ # -------------------------
118
+ # Ingest
119
+ # -------------------------
120
+ def ingest_files(files):
121
+ global documents, sources, index
122
+ documents, sources, index = [], [], None
123
  if not files:
124
+ return "No files uploaded."
125
  count = 0
126
  for f in files:
127
+ data = f.read()
128
+ name = (f.name or "").lower()
129
+ if name.endswith(".pdf"):
130
+ text = load_pdf_bytes(data)
131
+ elif name.endswith(".docx") or name.endswith(".doc"):
132
+ text = load_docx_bytes(data)
133
+ elif name.endswith(".txt") or name.endswith(".md"):
134
+ text = load_txt_bytes(data)
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  else:
136
+ # try detect
137
+ if data[:4].lower().startswith(b'%pdf'):
138
+ text = load_pdf_bytes(data)
139
+ elif data[:2] == b'PK':
140
+ text = load_docx_bytes(data)
141
+ else:
142
+ text = load_txt_bytes(data)
143
+ if not text or not text.strip():
144
  continue
 
145
  chunks = chunk_text(text)
146
  if chunks:
147
+ add_chunks(chunks, os.path.basename(name) or "uploaded")
148
  count += 1
149
+ return f"Ingested {count} document(s)."
150
 
151
+ # -------------------------
152
+ # Retrieval
153
+ # -------------------------
154
+ def retrieve(question, top_k):
155
+ load_embedder()
156
+ qv = embedder.encode([question], convert_to_numpy=True)
157
+ qv = qv / (np.linalg.norm(qv, axis=1, keepdims=True) + 1e-9)
158
+ qv = qv.astype("float32")
159
+ scores, ids = index.search(qv, top_k)
 
 
160
  results = []
161
+ for s, i in zip(scores[0], ids[0]):
162
+ if i < len(documents):
163
+ results.append({"score": float(s), "chunk": documents[i], "source": sources[i]})
164
+ return results
165
+
166
+ # -------------------------
167
+ # Sentence utilities
168
+ # -------------------------
169
+ def split_into_sentences(text: str):
170
+ # conservative split using punctuation
171
+ sents = re.split(r'(?<=[\.\?\!])\s+', text.replace("\n", " ").strip())
172
+ return [s.strip() for s in sents if s.strip()]
173
+
174
+ # -------------------------
175
+ # Core: gated per-sentence extraction + LLM paraphrase
176
+ # -------------------------
177
+ def answer_question(question, top_k):
178
+ if index is None or len(documents) == 0:
179
+ return "No documents processed yet.", ""
180
+ # retrieve chunk-level
181
+ hits = retrieve(question, top_k)
182
+ if not hits:
183
+ return "Answer not found in the uploaded documents.", ""
184
+ # top chunk-level score check
185
+ if hits[0]["score"] < SIMILARITY_THRESHOLD:
186
+ return "Answer not found in the uploaded documents.", ""
187
+ # build candidate sentences from top-k chunks
188
+ candidate_sentences = []
189
+ for h in hits:
190
+ sents = split_into_sentences(h["chunk"])
191
+ for sent in sents:
192
+ candidate_sentences.append({"text": sent, "source": h["source"]})
193
+ if not candidate_sentences:
194
+ return "Answer not found in the uploaded documents.", ""
195
+ # embed question and candidate sentences
196
+ load_embedder()
197
+ qv = embedder.encode([question], convert_to_numpy=True)
198
+ qv = qv / (np.linalg.norm(qv, axis=1, keepdims=True) + 1e-9)
199
+ s_texts = [c["text"] for c in candidate_sentences]
200
+ s_vecs = embedder.encode(s_texts, convert_to_numpy=True)
201
+ norms = np.linalg.norm(s_vecs, axis=1, keepdims=True)
202
+ norms[norms == 0] = 1e-9
203
+ s_vecs = s_vecs / norms
204
+ # cosine scores via dot product (q normalized)
205
+ scores = (s_vecs @ qv.T).squeeze() # shape (num_sentences,)
206
+ # pick top sentence(s)
207
+ top_idx = int(np.argmax(scores))
208
+ top_score = float(scores[top_idx])
209
+ if top_score < SENTENCE_THRESHOLD:
210
+ return "Answer not found in the uploaded documents.", ""
211
+ # include any other sentence close to top (helps when answer spans two short sentences)
212
+ chosen_idxs = [top_idx]
213
+ for i, sc in enumerate(scores):
214
+ if i != top_idx and (top_score - float(sc)) <= SENTENCE_CLOSE_DELTA and float(sc) >= SENTENCE_THRESHOLD:
215
+ chosen_idxs.append(i)
216
+ chosen_texts = [s_texts[i] for i in chosen_idxs]
217
+ chosen_sources = [candidate_sentences[i]["source"] for i in chosen_idxs]
218
+ # Now call LLM for a concise paraphrase (one-line direct answer)
219
+ load_llm()
220
+ # Build strict prompt
221
+ context = "\n".join(chosen_texts)
222
+ prompt = f"""You are given only the following sentence(s) extracted from documents. Answer the question in ONE short, direct sentence (no extra commentary). Use only information present. If the sentence(s) do not contain the answer, reply exactly: "Answer not found in the uploaded documents."
223
+
224
+ SENTENCE(S):
225
+ {context}
226
+
227
+ QUESTION:
228
+ {question}
229
+
230
+ SHORT ANSWER:
231
+ """
232
+ out = llm(prompt, max_new_tokens=80, do_sample=False)[0]["generated_text"].strip()
233
+ # If model repeats the entire sentence, it's fine β€” still constrained. But ensure if model hallucinates we keep guard:
234
+ if out.lower().startswith("answer not found"):
235
+ return "Answer not found in the uploaded documents.", ""
236
+ # Prepare evidence display
237
+ evidence_lines = []
238
+ for txt, src, sc in zip(chosen_texts, chosen_sources, [float(scores[i]) for i in chosen_idxs]):
239
+ evidence_lines.append(f"Source: {src} | SentenceScore: {round(sc,3)}\n{txt}")
240
+ evidence = "\n\n---\n\n".join(evidence_lines)
241
+ return out, evidence
242
+
243
+ # -------------------------
244
+ # Gradio UI
245
+ # -------------------------
246
+ with gr.Blocks(title="Grounded RAG Q&A β€” Direct Answers") as demo:
247
+ gr.Markdown("## πŸ“„ Grounded RAG Q&A β€” Direct, concise answers (no hallucination)")
248
+ file_input = gr.File(file_count="multiple", label="Upload documents (PDF, DOCX, TXT)")
249
+ proc_btn = gr.Button("Process Documents")
250
  status = gr.Textbox(label="Status", interactive=False)
251
  gr.Markdown("---")
252
+ q = gr.Textbox(label="Ask a question", placeholder="e.g. How many privilege leaves do I have?")
253
+ topk = gr.Slider(1, 5, value=TOP_K_DEFAULT, step=1, label="Top K chunks to retrieve")
254
  ask_btn = gr.Button("Get Answer")
255
+ answer_box = gr.Textbox(label="Answer (direct)", interactive=False)
256
+ evidence_box = gr.Markdown(label="Evidence (selected sentences)")
257
 
258
+ proc_btn.click(ingest_files, file_input, status)
259
+ ask_btn.click(answer_question, [q, topk], [answer_box, evidence_box])
260
 
261
  if __name__ == "__main__":
262
+ demo.launch()