Spaces:
Sleeping
Sleeping
[Winsmano Ambani] commited on
Commit ·
fa01e10
1
Parent(s): 650f31e
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import faiss
|
| 3 |
import torch
|
| 4 |
-
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
-
import os
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# Load QA model
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
index.add(embeddings)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
answer = qa_tokenizer.decode(inputs["input_ids"][0][start:end+1])
|
| 39 |
-
return f"Answer: {answer}\n\n(Context: {context})"
|
| 40 |
|
|
|
|
| 41 |
# Gradio UI
|
|
|
|
| 42 |
iface = gr.Interface(
|
| 43 |
fn=answer_question,
|
| 44 |
inputs="text",
|
| 45 |
outputs="text",
|
| 46 |
title="Retrieval-Augmented QA",
|
| 47 |
-
description="Ask a question
|
| 48 |
)
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
import faiss
|
| 6 |
import torch
|
| 7 |
+
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
|
| 8 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 9 |
|
| 10 |
+
# ----------------------------
|
| 11 |
+
# Paths (repo-relative)
|
| 12 |
+
# ----------------------------
|
| 13 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) if "__file__" in globals() else os.getcwd()
|
| 14 |
+
MODEL_DIR = os.path.join(BASE_DIR, "QA_model")
|
| 15 |
+
KB_PATH = os.path.join(BASE_DIR, "data", "SalatusSalatin.txt")
|
| 16 |
+
|
| 17 |
+
# ----------------------------
|
| 18 |
# Load QA model
|
| 19 |
+
# ----------------------------
|
| 20 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
+
qa_tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
|
| 22 |
+
qa_model = AutoModelForQuestionAnswering.from_pretrained(MODEL_DIR).to(device).eval()
|
| 23 |
+
|
| 24 |
+
# ----------------------------
|
| 25 |
+
# Embedding model
|
| 26 |
+
# ----------------------------
|
| 27 |
+
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2", device=device)
|
| 28 |
+
|
| 29 |
+
# ----------------------------
|
| 30 |
+
# Chunking helpers
|
| 31 |
+
# ----------------------------
|
| 32 |
+
def split_into_paragraphs(text: str):
|
| 33 |
+
# keep it simple: split on double newlines
|
| 34 |
+
return [p.strip() for p in re.split(r"\n\s*\n", text) if p.strip()]
|
| 35 |
|
| 36 |
+
def chunk_by_tokens(text: str, tokenizer, max_tokens=220, stride_tokens=40):
|
| 37 |
+
"""
|
| 38 |
+
Break a text into overlapping token windows to improve retrieval granularity.
|
| 39 |
+
"""
|
| 40 |
+
tokens = tokenizer.tokenize(text)
|
| 41 |
+
chunks = []
|
| 42 |
+
start = 0
|
| 43 |
+
while start < len(tokens):
|
| 44 |
+
end = min(start + max_tokens, len(tokens))
|
| 45 |
+
chunk_text = tokenizer.convert_tokens_to_string(tokens[start:end]).strip()
|
| 46 |
+
if chunk_text:
|
| 47 |
+
chunks.append(chunk_text)
|
| 48 |
+
if end == len(tokens):
|
| 49 |
+
break
|
| 50 |
+
start = max(0, end - stride_tokens)
|
| 51 |
+
return chunks
|
| 52 |
|
| 53 |
+
# ----------------------------
|
| 54 |
+
# Load and prepare knowledge base
|
| 55 |
+
# ----------------------------
|
| 56 |
+
with open(KB_PATH, "r", encoding="utf-8") as f:
|
| 57 |
+
raw_text = f.read()
|
| 58 |
|
| 59 |
+
paragraphs = split_into_paragraphs(raw_text)
|
| 60 |
+
|
| 61 |
+
# Pre-chunk paragraphs for better recall
|
| 62 |
+
CHUNK_TOKENS = 220
|
| 63 |
+
CHUNK_STRIDE = 40
|
| 64 |
+
kb_chunks = []
|
| 65 |
+
for p in paragraphs:
|
| 66 |
+
kb_chunks.extend(chunk_by_tokens(p, qa_tokenizer, max_tokens=CHUNK_TOKENS, stride_tokens=CHUNK_STRIDE))
|
| 67 |
+
|
| 68 |
+
# ----------------------------
|
| 69 |
+
# Build FAISS index (cosine sim)
|
| 70 |
+
# ----------------------------
|
| 71 |
+
embeddings = embedder.encode(kb_chunks, convert_to_numpy=True, show_progress_bar=True, batch_size=64)
|
| 72 |
+
embeddings = embeddings.astype("float32")
|
| 73 |
+
faiss.normalize_L2(embeddings) # cosine via inner product on normalized vectors
|
| 74 |
+
|
| 75 |
+
d = embeddings.shape[1]
|
| 76 |
+
index = faiss.IndexFlatIP(d)
|
| 77 |
index.add(embeddings)
|
| 78 |
|
| 79 |
+
# ----------------------------
|
| 80 |
+
# QA over (possibly long) context with chunking
|
| 81 |
+
# ----------------------------
|
| 82 |
+
def qa_on_context(question: str, context: str, max_length=384, stride=128):
|
| 83 |
+
"""
|
| 84 |
+
Run QA model on long context by splitting into overlapping token windows;
|
| 85 |
+
return (best_answer, best_score).
|
| 86 |
+
"""
|
| 87 |
+
inputs = qa_tokenizer(
|
| 88 |
+
question,
|
| 89 |
+
context,
|
| 90 |
+
return_overflowing_tokens=True,
|
| 91 |
+
max_length=max_length,
|
| 92 |
+
stride=stride,
|
| 93 |
+
truncation=True,
|
| 94 |
+
return_tensors="pt",
|
| 95 |
+
padding="max_length"
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
best_answer = ""
|
| 99 |
+
best_score = -1e30
|
| 100 |
+
|
| 101 |
+
n = inputs["input_ids"].shape[0]
|
| 102 |
+
for i in range(n):
|
| 103 |
+
input_ids = inputs["input_ids"][i].unsqueeze(0).to(device)
|
| 104 |
+
attention_mask = inputs["attention_mask"][i].unsqueeze(0).to(device)
|
| 105 |
+
token_type_ids = inputs.get("token_type_ids")
|
| 106 |
+
if token_type_ids is not None:
|
| 107 |
+
token_type_ids = token_type_ids[i].unsqueeze(0).to(device)
|
| 108 |
+
|
| 109 |
+
with torch.inference_mode():
|
| 110 |
+
outputs = qa_model(
|
| 111 |
+
input_ids=input_ids,
|
| 112 |
+
attention_mask=attention_mask,
|
| 113 |
+
token_type_ids=token_type_ids
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
start_scores = outputs.start_logits[0]
|
| 117 |
+
end_scores = outputs.end_logits[0]
|
| 118 |
+
|
| 119 |
+
# Get best span within valid range
|
| 120 |
+
start_idx = int(torch.argmax(start_scores))
|
| 121 |
+
end_idx = int(torch.argmax(end_scores))
|
| 122 |
+
if end_idx < start_idx:
|
| 123 |
+
continue
|
| 124 |
+
|
| 125 |
+
score = start_scores[start_idx].item() + end_scores[end_idx].item()
|
| 126 |
+
answer = qa_tokenizer.decode(input_ids[0][start_idx:end_idx + 1], skip_special_tokens=True).strip()
|
| 127 |
+
|
| 128 |
+
if answer and score > best_score:
|
| 129 |
+
best_answer = answer
|
| 130 |
+
best_score = score
|
| 131 |
+
|
| 132 |
+
return best_answer, best_score
|
| 133 |
+
|
| 134 |
+
# ----------------------------
|
| 135 |
+
# End-to-end QA with top-k retrieval + re-scoring
|
| 136 |
+
# ----------------------------
|
| 137 |
+
TOP_K = 5
|
| 138 |
+
|
| 139 |
+
def answer_question(question: str):
|
| 140 |
+
# 1) Retrieve top-k chunks by cosine similarity
|
| 141 |
+
q_emb = embedder.encode([question], convert_to_numpy=True).astype("float32")
|
| 142 |
+
faiss.normalize_L2(q_emb)
|
| 143 |
+
D, I = index.search(q_emb, k=min(TOP_K, len(kb_chunks)))
|
| 144 |
+
cand_idxs = I[0].tolist()
|
| 145 |
+
|
| 146 |
+
# 2) Run QA on each retrieved chunk; pick the best by logit score
|
| 147 |
+
best = {"answer": "", "score": -1e30, "context": ""}
|
| 148 |
+
for idx in cand_idxs:
|
| 149 |
+
ctx = kb_chunks[idx]
|
| 150 |
+
ans, sc = qa_on_context(question, ctx)
|
| 151 |
+
if sc > best["score"] and ans:
|
| 152 |
+
best.update({"answer": ans, "score": sc, "context": ctx})
|
| 153 |
|
| 154 |
+
# 3) Fallback if nothing found
|
| 155 |
+
if not best["answer"]:
|
| 156 |
+
return "Answer: (no good answer found)\n\n(Context: —)"
|
| 157 |
|
| 158 |
+
# 4) Return best answer + the chunk it came from
|
| 159 |
+
return f"Answer: {best['answer']}\n\n(Context: {best['context']})"
|
|
|
|
|
|
|
| 160 |
|
| 161 |
+
# ----------------------------
|
| 162 |
# Gradio UI
|
| 163 |
+
# ----------------------------
|
| 164 |
iface = gr.Interface(
|
| 165 |
fn=answer_question,
|
| 166 |
inputs="text",
|
| 167 |
outputs="text",
|
| 168 |
title="Retrieval-Augmented QA",
|
| 169 |
+
description="Ask a question; we retrieve the most relevant chunk from the knowledge base and extract the answer."
|
| 170 |
)
|
| 171 |
|
| 172 |
if __name__ == "__main__":
|