Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,195 +1,262 @@
|
|
| 1 |
# app.py
|
| 2 |
import os
|
| 3 |
-
import
|
|
|
|
| 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 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
# -------------------------
|
| 21 |
# Config
|
| 22 |
-
# -------------------------
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# -
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
text = ""
|
| 32 |
-
with pdfplumber.open(
|
| 33 |
for page in pdf.pages:
|
| 34 |
t = page.extract_text()
|
| 35 |
if t:
|
| 36 |
text += t + "\n"
|
| 37 |
return text
|
| 38 |
|
| 39 |
-
def
|
| 40 |
-
d = docx.Document(
|
| 41 |
return "\n".join(p.text for p in d.paragraphs)
|
| 42 |
|
| 43 |
-
def
|
| 44 |
-
|
| 45 |
-
return
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
chunks = []
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 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
|
| 71 |
if index is None:
|
| 72 |
-
|
| 73 |
-
index = faiss.IndexFlatIP(dim)
|
| 74 |
|
| 75 |
-
def
|
| 76 |
-
|
| 77 |
-
ensure_model()
|
| 78 |
ensure_index()
|
| 79 |
-
|
| 80 |
-
|
| 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 |
-
|
| 85 |
-
|
| 86 |
-
index.add(
|
| 87 |
for c in chunks:
|
| 88 |
documents.append(c)
|
| 89 |
-
|
| 90 |
|
| 91 |
-
# -------------------------
|
| 92 |
-
# Ingest
|
| 93 |
-
# -------------------------
|
| 94 |
-
def
|
| 95 |
-
|
|
|
|
| 96 |
if not files:
|
| 97 |
-
return "No files
|
| 98 |
count = 0
|
| 99 |
for f in files:
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
continue
|
| 124 |
-
|
| 125 |
chunks = chunk_text(text)
|
| 126 |
if chunks:
|
| 127 |
-
|
| 128 |
count += 1
|
| 129 |
-
return f"Ingested {count}
|
| 130 |
|
| 131 |
-
# -------------------------
|
| 132 |
-
#
|
| 133 |
-
# -------------------------
|
| 134 |
-
def
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 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
|
| 144 |
-
if
|
| 145 |
-
results.append({"
|
| 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 |
status = gr.Textbox(label="Status", interactive=False)
|
| 184 |
gr.Markdown("---")
|
| 185 |
-
|
| 186 |
-
|
| 187 |
ask_btn = gr.Button("Get Answer")
|
| 188 |
-
|
| 189 |
-
|
| 190 |
|
| 191 |
-
|
| 192 |
-
ask_btn.click(
|
| 193 |
|
| 194 |
if __name__ == "__main__":
|
| 195 |
-
demo.launch(
|
|
|
|
| 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()
|