Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,288 +1,273 @@
|
|
| 1 |
-
# app.py — Salatus QA (Question-only RAG
|
| 2 |
-
import os,
|
| 3 |
from pathlib import Path
|
|
|
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import gradio as gr
|
| 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 |
if not s: return ""
|
| 60 |
-
s = s.replace("\u200b","").replace("\u200c","")
|
| 61 |
for k,v in APOS.items(): s = s.replace(k,v)
|
| 62 |
-
return re.sub(r"\s+",
|
| 63 |
-
|
| 64 |
-
def cosine_topk(query:
|
| 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 |
-
if BAD_WHO.search(ans): bonus -= 0.40
|
| 102 |
-
if len(ans) <= 3 and len(ans.split())==1: bonus -= 0.05
|
| 103 |
-
rescored.append((score+bonus, c))
|
| 104 |
-
rescored.sort(key=lambda x:x[0], reverse=True)
|
| 105 |
-
return [c for _,c in rescored]
|
| 106 |
-
|
| 107 |
-
def soft_trim_words(s: str, max_words: int):
|
| 108 |
-
w = s.split()
|
| 109 |
-
if len(w) <= max_words: return s
|
| 110 |
-
return " ".join(w[:max_words])
|
| 111 |
-
|
| 112 |
-
# --------------------------- Core inference ---------------------------
|
| 113 |
-
__MARRY__PAT = re.compile(
|
| 114 |
-
r"(dikahwinkan(?:\s+dengan)?|dikahwini|diperisterikan|beristeri|married(?:\s+to)?)",
|
| 115 |
-
re.I,
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
def _fallback_name(ctx: str, q: str):
|
| 119 |
-
"""Heuristic: if QA returns nothing, try to pick a plausible person name,
|
| 120 |
-
prioritizing names near 'dikahwinkan/ married' keywords."""
|
| 121 |
-
m = __MARRY__PAT.search(ctx)
|
| 122 |
if m:
|
| 123 |
-
|
| 124 |
-
seg = ctx[m.end(): m.end() + 120]
|
| 125 |
n = NAME_RE.search(seg)
|
| 126 |
if n:
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
return
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
q = norm(q)
|
| 140 |
if not q:
|
| 141 |
-
return
|
|
|
|
|
|
|
| 142 |
|
| 143 |
try:
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
if not filt:
|
| 148 |
-
filt = pairs[:
|
| 149 |
-
|
| 150 |
|
| 151 |
answers = []
|
| 152 |
-
|
| 153 |
-
for idx, sim in
|
| 154 |
ctx = CONTEXTS[idx]
|
| 155 |
-
#
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
except Exception as e:
|
| 159 |
-
# last-resort: treat as no answer from model
|
| 160 |
-
out = None
|
| 161 |
-
|
| 162 |
if isinstance(out, dict) and out.get("answer"):
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
| 168 |
else:
|
| 169 |
-
# fallback
|
| 170 |
-
|
| 171 |
-
if
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
fb["sim"] = float(sim)
|
| 175 |
-
answers.append(fb)
|
| 176 |
|
| 177 |
preview = ctx[:160].replace("\n"," ") + ("…" if len(ctx)>160 else "")
|
| 178 |
-
|
| 179 |
|
| 180 |
if not answers:
|
| 181 |
-
msg = "Tiada jawapan ditemui." if lang=="ms" else
|
| 182 |
-
|
|
|
|
| 183 |
|
| 184 |
-
|
|
|
|
| 185 |
best = answers[0]
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
ctx = CONTEXTS[idx]
|
| 190 |
-
snip = snippet_around(ctx, best.get("start",-1), best.get("end",-1))
|
| 191 |
|
| 192 |
-
|
| 193 |
-
f"- {c.get('answer','').strip()} (confidence≈{float(c.get('score',0.0)):.3f})"
|
| 194 |
-
for c in answers[:3]
|
| 195 |
-
])
|
| 196 |
-
|
| 197 |
-
return ans_text, snip, tops, "\n".join(selected)
|
| 198 |
|
| 199 |
except Exception as e:
|
| 200 |
-
print("ERROR
|
| 201 |
traceback.print_exc()
|
| 202 |
-
msg = "Ralat dalaman
|
| 203 |
return "", msg, "", ""
|
| 204 |
-
def run_ui_callback(q, top_k, max_len, thresh, lang):
|
| 205 |
-
# Ensure proper numeric types from sliders/text
|
| 206 |
-
try:
|
| 207 |
-
return run_question_only(q, int(top_k), int(max_len), float(thresh), lang)
|
| 208 |
-
except Exception as e:
|
| 209 |
-
print("ERROR in run_ui_callback:", repr(e))
|
| 210 |
-
traceback.print_exc()
|
| 211 |
-
return "", "Internal error.", "", ""
|
| 212 |
-
|
| 213 |
-
# --------------------------- UI ---------------------------------------
|
| 214 |
-
BRAND_PRIMARY = "#0f172a"
|
| 215 |
-
BRAND_ACCENT = "#f59e0b"
|
| 216 |
-
BRAND_BG = "#f8fafc"
|
| 217 |
-
HILITE_BG = "#fff59d"
|
| 218 |
-
|
| 219 |
-
CSS = f"""
|
| 220 |
-
:root {{
|
| 221 |
-
--brand-primary: {BRAND_PRIMARY};
|
| 222 |
-
--brand-accent: {BRAND_ACCENT};
|
| 223 |
-
--brand-bg: {BRAND_BG};
|
| 224 |
-
--hilite-bg: {HILITE_BG};
|
| 225 |
-
}}
|
| 226 |
-
.gradio-container {{ background: var(--brand-bg); }}
|
| 227 |
-
#hero {{
|
| 228 |
-
background: linear-gradient(135deg, #1e293b, #0f172a 70%);
|
| 229 |
-
color: white; border-radius: 14px; padding: 16px 20px; display:flex; align-items:center; gap:12px;
|
| 230 |
-
}}
|
| 231 |
-
#hero .title {{ font-weight:800; font-size:1.15rem; }}
|
| 232 |
-
button, .gr-button {{
|
| 233 |
-
background: var(--brand-accent) !important; color:#111 !important; border:0 !important; border-radius:12px !important; font-weight:700 !important;
|
| 234 |
-
}}
|
| 235 |
-
mark {{ background: var(--hilite-bg); }}
|
| 236 |
-
"""
|
| 237 |
|
|
|
|
|
|
|
|
|
|
| 238 |
def build_ui():
|
| 239 |
-
with gr.Blocks(title="Salatus QA — Question-Only",
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
gr.HTML("""<div id="hero">
|
| 243 |
<div style="font-size:1.6rem">🏰</div>
|
| 244 |
-
<div>
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
with gr.
|
| 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 |
return demo
|
| 284 |
|
| 285 |
demo = build_ui()
|
| 286 |
|
| 287 |
-
if
|
| 288 |
demo.queue().launch()
|
|
|
|
| 1 |
+
# app.py — Salatus QA (Question-only, RAG + robust fallbacks)
|
| 2 |
+
import os, re, json, traceback
|
| 3 |
from pathlib import Path
|
| 4 |
+
|
| 5 |
import numpy as np
|
| 6 |
+
import joblib
|
| 7 |
+
from scipy.sparse import csr_matrix
|
| 8 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering
|
| 9 |
+
|
| 10 |
import gradio as gr
|
| 11 |
+
|
| 12 |
+
# -------------------------------
|
| 13 |
+
# Config
|
| 14 |
+
# -------------------------------
|
| 15 |
+
HERE = Path(_file_).parent
|
| 16 |
+
REPO = os.environ.get("MODEL_REPO", "ChogwuCY/salatus-qa-xlmrb-base") # <— change if needed
|
| 17 |
+
|
| 18 |
+
RETR_DIR = HERE / "retriever"
|
| 19 |
+
CTX_JSON = RETR_DIR / "contexts.json"
|
| 20 |
+
TFIDF_PIP = RETR_DIR / "tfidf_pipe.joblib"
|
| 21 |
+
|
| 22 |
+
DEFAULT_TOPK = 5
|
| 23 |
+
DEFAULT_MAXLEN = 12
|
| 24 |
+
DEFAULT_THRESH = 0.05 # be permissive so we always return something
|
| 25 |
+
|
| 26 |
+
# Colors
|
| 27 |
+
BRAND_PRIMARY = "#0f172a" # blue-ish slate
|
| 28 |
+
BRAND_ACCENT = "#f59e0b" # amber
|
| 29 |
+
BRAND_BG = "#f8fafc"
|
| 30 |
+
HILITE_BG = "#fff59d"
|
| 31 |
+
|
| 32 |
+
CSS = f"""
|
| 33 |
+
:root {{
|
| 34 |
+
--brand-primary:{BRAND_PRIMARY};
|
| 35 |
+
--brand-accent:{BRAND_ACCENT};
|
| 36 |
+
--brand-bg:{BRAND_BG};
|
| 37 |
+
--hilite-bg:{HILITE_BG};
|
| 38 |
+
}}
|
| 39 |
+
.gradio-container {{
|
| 40 |
+
background: var(--brand-bg);
|
| 41 |
+
}}
|
| 42 |
+
#hero {{
|
| 43 |
+
background: linear-gradient(135deg, #1d2a52, #0f172a 70%);
|
| 44 |
+
color: #fff;
|
| 45 |
+
border-radius: 14px;
|
| 46 |
+
padding: 18px 20px;
|
| 47 |
+
margin-bottom: 10px;
|
| 48 |
+
display:flex; align-items:center; gap:12px;
|
| 49 |
+
}}
|
| 50 |
+
#hero .title {{ font-weight:800; font-size:1.25rem; }}
|
| 51 |
+
#hero .sub {{ opacity:.9; font-size:.95rem; }}
|
| 52 |
+
button, .gr-button {{
|
| 53 |
+
background: var(--brand-accent) !important; color:#111 !important;
|
| 54 |
+
border-radius: 12px !important; font-weight:700 !important; border:0 !important;
|
| 55 |
+
}}
|
| 56 |
+
.markdown-body mark, mark {{
|
| 57 |
+
background: var(--hilite-bg); padding:0 .15em; border-radius:.2em;
|
| 58 |
+
}}
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
# -------------------------------
|
| 62 |
+
# Load retriever
|
| 63 |
+
# -------------------------------
|
| 64 |
+
def load_retriever():
|
| 65 |
+
if not CTX_JSON.exists() or not TFIDF_PIP.exists():
|
| 66 |
+
return None, None
|
| 67 |
+
contexts = json.loads(CTX_JSON.read_text(encoding="utf-8"))
|
| 68 |
+
pipe = joblib.load(TFIDF_PIP) # (TfidfVectorizer -> Normalizer) pipeline
|
| 69 |
+
return contexts, pipe
|
| 70 |
+
|
| 71 |
+
CONTEXTS, TFIDF = load_retriever()
|
| 72 |
+
|
| 73 |
+
# -------------------------------
|
| 74 |
+
# Model / pipeline
|
| 75 |
+
# -------------------------------
|
| 76 |
+
device = 0 if os.environ.get("CUDA_VISIBLE_DEVICES") else -1 # Space will set CPU → -1
|
| 77 |
+
tokenizer = AutoTokenizer.from_pretrained(REPO)
|
| 78 |
+
model = AutoModelForQuestionAnswering.from_pretrained(REPO)
|
| 79 |
+
qa = pipeline("question-answering", model=model, tokenizer=tokenizer, device=device)
|
| 80 |
+
|
| 81 |
+
# -------------------------------
|
| 82 |
+
# Utilities
|
| 83 |
+
# -------------------------------
|
| 84 |
+
APOS = {"’": "'", "‘": "'", "“": '"', "”": '"', "\u200b":"", "\u200c":""}
|
| 85 |
+
def norm(s:str)->str:
|
| 86 |
if not s: return ""
|
|
|
|
| 87 |
for k,v in APOS.items(): s = s.replace(k,v)
|
| 88 |
+
return re.sub(r"\s+"," ", s).strip()
|
| 89 |
+
|
| 90 |
+
def cosine_topk(query:str, k:int=5):
|
| 91 |
+
"""Return list[(idx, sim)] by TF-IDF cosine. Keep k even if threshold used later."""
|
| 92 |
+
if TFIDF is None or CONTEXTS is None or not CONTEXTS:
|
| 93 |
+
return []
|
| 94 |
+
qv = TFIDF.transform([query])
|
| 95 |
+
# Project into the Normalized space (last step in pipeline already Normalizer)
|
| 96 |
+
# Equivalent of dot-product cosine.
|
| 97 |
+
# We can get raw matrix by re-fitting; instead, just score each doc via vectorizer vocabulary overlap.
|
| 98 |
+
# Faster: build a TFIDF matrix once? For simplicity use transform+pairwise with contexts? Not feasible.
|
| 99 |
+
# Minimal hack: select candidates by simple token overlap and then re-score with vectorizer:
|
| 100 |
+
|
| 101 |
+
# Lightweight filter: term overlap
|
| 102 |
+
toks = set(norm(query).lower().split())
|
| 103 |
+
prelim = []
|
| 104 |
+
# sample a subset cheaply for speed; or score all but not feasible here
|
| 105 |
+
# We'll just scan a windowed subset using simple overlap rank and take top 200 to vectorize properly.
|
| 106 |
+
# But we don't have vectors for all docs; we only can transform texts. Transforming all at startup is heavy.
|
| 107 |
+
# So: do a cheap overlap pass, pick top 200 indices, then compute real tfidf cosine on those.
|
| 108 |
+
for i, c in enumerate(CONTEXTS):
|
| 109 |
+
s = c.lower()
|
| 110 |
+
overlap = sum(1 for t in toks if t in s)
|
| 111 |
+
if overlap:
|
| 112 |
+
prelim.append((i, overlap))
|
| 113 |
+
prelim.sort(key=lambda x: x[1], reverse=True)
|
| 114 |
+
cand_idx = [i for i,_ in prelim[:200]] if prelim else list(range(min(200, len(CONTEXTS))))
|
| 115 |
+
cand_texts = [CONTEXTS[i] for i in cand_idx]
|
| 116 |
+
Cv = TFIDF.transform(cand_texts)
|
| 117 |
+
sims = (qv @ Cv.T).A[0] # dense array
|
| 118 |
+
pairs = sorted(zip(cand_idx, sims), key=lambda x: x[1], reverse=True)
|
| 119 |
+
return pairs[:max(1,k)]
|
| 120 |
+
|
| 121 |
+
# Proper-name (Malay / English) for last-resort
|
| 122 |
+
NAME_RE = re.compile(r"\b([A-Z][a-z]+(?:\s+[A-Z][a-z]+){0,3})\b")
|
| 123 |
+
MARRY_RE = re.compile(r"(dikahwinkan(?:\s+dengan)?|dikahwini|diperisterikan|beristeri|married(?:\s+to)?)", re.I)
|
| 124 |
+
|
| 125 |
+
def fallback_person(ctx:str):
|
| 126 |
+
m = MARRY_RE.search(ctx)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
if m:
|
| 128 |
+
seg = ctx[m.end():m.end()+120]
|
|
|
|
| 129 |
n = NAME_RE.search(seg)
|
| 130 |
if n:
|
| 131 |
+
return n.group(0), m.end()+n.start(), m.end()+n.end()
|
| 132 |
+
n = NAME_RE.search(ctx)
|
| 133 |
+
if n:
|
| 134 |
+
return n.group(0), n.start(), n.end()
|
| 135 |
+
return "", -1, -1
|
| 136 |
+
|
| 137 |
+
def soft_trim_words(s: str, max_tokens:int):
|
| 138 |
+
if not s: return s
|
| 139 |
+
toks = s.split()
|
| 140 |
+
if len(toks) <= max_tokens: return s
|
| 141 |
+
return " ".join(toks[:max_tokens])
|
| 142 |
+
|
| 143 |
+
def snippet_around(ctx:str, s:int, e:int):
|
| 144 |
+
if isinstance(s,int) and isinstance(e,int) and 0 <= s < e <= len(ctx):
|
| 145 |
+
pre = max(0, s-140)
|
| 146 |
+
post = min(len(ctx), e+140)
|
| 147 |
+
return ctx[pre:s] + "<mark>" + ctx[s:e] + "</mark>" + ctx[e:post]
|
| 148 |
+
# fallback: just show a shortened ctx
|
| 149 |
+
cut = ctx[:280].replace("\n"," ")
|
| 150 |
+
return cut + ("…" if len(ctx)>280 else "")
|
| 151 |
+
|
| 152 |
+
# -------------------------------
|
| 153 |
+
# Core runner
|
| 154 |
+
# -------------------------------
|
| 155 |
+
def run(q: str, top_k: int, max_len: int, thresh: float, lang: str):
|
| 156 |
q = norm(q)
|
| 157 |
if not q:
|
| 158 |
+
return ("",
|
| 159 |
+
"Sila masukkan soalan." if lang=="ms" else "Please enter a question.",
|
| 160 |
+
"", "")
|
| 161 |
|
| 162 |
try:
|
| 163 |
+
pairs = cosine_topk(q, k=max(1, int(top_k)))
|
| 164 |
+
if not pairs:
|
| 165 |
+
msg = "Aset retriever tiada atau kosong." if lang=="ms" else "Retriever assets missing or empty."
|
| 166 |
+
return "", msg, "", ""
|
| 167 |
+
|
| 168 |
+
# filter by threshold but always keep at least one
|
| 169 |
+
filt = [(i,s) for (i,s) in pairs if s >= float(thresh)]
|
| 170 |
if not filt:
|
| 171 |
+
filt = pairs[:1]
|
| 172 |
+
sel = filt[:max(1, int(top_k))]
|
| 173 |
|
| 174 |
answers = []
|
| 175 |
+
chosen = []
|
| 176 |
+
for idx, sim in sel:
|
| 177 |
ctx = CONTEXTS[idx]
|
| 178 |
+
# robust QA call (works across versions)
|
| 179 |
+
out = qa({"question": q, "context": ctx})
|
| 180 |
+
best = None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
if isinstance(out, dict) and out.get("answer"):
|
| 182 |
+
best = out
|
| 183 |
+
elif isinstance(out, list) and out:
|
| 184 |
+
best = out[0]
|
| 185 |
+
|
| 186 |
+
if best and str(best.get("answer","")).strip():
|
| 187 |
+
ans = soft_trim_words(best["answer"].strip(), int(max_len))
|
| 188 |
+
s = int(best.get("start", -1)); e = int(best.get("end", -1))
|
| 189 |
+
answers.append({"answer": ans, "start": s, "end": e, "score": float(best.get("score",0.0)), "sim": float(sim), "idx": idx})
|
| 190 |
else:
|
| 191 |
+
# fallback person name
|
| 192 |
+
name, s, e = fallback_person(ctx)
|
| 193 |
+
if name:
|
| 194 |
+
answers.append({"answer": soft_trim_words(name, int(max_len)),
|
| 195 |
+
"start": s, "end": e, "score": 0.0, "sim": float(sim), "idx": idx})
|
|
|
|
|
|
|
| 196 |
|
| 197 |
preview = ctx[:160].replace("\n"," ") + ("…" if len(ctx)>160 else "")
|
| 198 |
+
chosen.append(f"{idx} (sim={sim:.3f}) {preview}")
|
| 199 |
|
| 200 |
if not answers:
|
| 201 |
+
msg = "Tiada jawapan ditemui. Cuba turunkan ambang persamaan." if lang=="ms" else \
|
| 202 |
+
"No answer found. Try lowering the similarity threshold."
|
| 203 |
+
return "", msg, "", "\n".join(chosen)
|
| 204 |
|
| 205 |
+
# pick by (score then sim)
|
| 206 |
+
answers.sort(key=lambda x: (x["score"], x["sim"]), reverse=True)
|
| 207 |
best = answers[0]
|
| 208 |
+
ctx = CONTEXTS[best["idx"]]
|
| 209 |
+
sent = snippet_around(ctx, best["start"], best["end"])
|
| 210 |
+
tops = "\n".join([f"- {a['answer']} (confidence≈{a['score']:.3f})" for a in answers[:3]])
|
|
|
|
|
|
|
| 211 |
|
| 212 |
+
return best["answer"], sent, tops, "\n".join(chosen)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
except Exception as e:
|
| 215 |
+
print("ERROR:", repr(e))
|
| 216 |
traceback.print_exc()
|
| 217 |
+
msg = "Ralat dalaman." if lang=="ms" else "Internal error."
|
| 218 |
return "", msg, "", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
+
# -------------------------------
|
| 221 |
+
# UI
|
| 222 |
+
# -------------------------------
|
| 223 |
def build_ui():
|
| 224 |
+
with gr.Blocks(title="Salatus QA — Question-Only", theme=gr.themes.Soft(primary_hue="slate", secondary_hue="amber"), css=CSS) as demo:
|
| 225 |
+
gr.HTML(f"""
|
| 226 |
+
<div id="hero">
|
|
|
|
| 227 |
<div style="font-size:1.6rem">🏰</div>
|
| 228 |
+
<div>
|
| 229 |
+
<div class="title">Salatus QA — Question-Only</div>
|
| 230 |
+
<div class="sub">Masukkan soalan sahaja. Aplikasi memilih konteks terbaik, kemudian mengekstrak jawapan.</div>
|
| 231 |
+
</div>
|
| 232 |
+
<div style="margin-left:auto;opacity:.9">RAG Demo</div>
|
| 233 |
+
</div>
|
| 234 |
+
""")
|
| 235 |
+
|
| 236 |
+
with gr.Tab("Demo"):
|
| 237 |
+
with gr.Row():
|
| 238 |
+
lang = gr.Radio(choices=["ms","en"], value="ms", label="Bahasa / Language (ms/en)")
|
| 239 |
+
q = gr.Textbox(label="Soalan / Question", placeholder="Tulis soalan di sini…")
|
| 240 |
+
|
| 241 |
+
with gr.Row():
|
| 242 |
+
top_k = gr.Slider(1, 12, value=DEFAULT_TOPK, step=1, label="Bilangan konteks dicadang (top_k) / Number of candidate passages")
|
| 243 |
+
maxlen = gr.Slider(6, 32, value=DEFAULT_MAXLEN, step=1, label="Maks panjang jawapan (token) / Max answer length (tokens)")
|
| 244 |
+
thresh = gr.Slider(0.00, 0.50, value=DEFAULT_THRESH, step=0.01, label="Ambang persamaan (retrieval) / Similarity threshold")
|
| 245 |
+
|
| 246 |
+
ask_btn = gr.Button("Tanya / Ask", size="lg")
|
| 247 |
+
|
| 248 |
+
best = gr.Textbox(label="Jawapan terbaik / Best answer")
|
| 249 |
+
sent = gr.HTML(label="Jawapan (ayat) / Answer (sentence)")
|
| 250 |
+
tops = gr.Textbox(label="Calon jawapan (Top-3) / Top-3 candidates")
|
| 251 |
+
rets = gr.Textbox(label="Konteks terpilih / Selected passages")
|
| 252 |
+
|
| 253 |
+
ask_btn.click(fn=run, inputs=[q, top_k, maxlen, thresh, lang], outputs=[best, sent, tops, rets])
|
| 254 |
+
|
| 255 |
+
gr.Examples(
|
| 256 |
+
examples=[
|
| 257 |
+
["Siapakah yang dikahwinkan dengan Tuan Puteri Mahtabu’l-Bahri?", DEFAULT_TOPK, DEFAULT_MAXLEN, DEFAULT_THRESH, "ms"],
|
| 258 |
+
["Siapakah yang memerintahkan pihak Sultan Alau'd-Din?", DEFAULT_TOPK, DEFAULT_MAXLEN, DEFAULT_THRESH, "ms"],
|
| 259 |
+
["Who married Tuan Puteri Mahtabu’l-Bahri?", DEFAULT_TOPK, DEFAULT_MAXLEN, DEFAULT_THRESH, "en"],
|
| 260 |
+
],
|
| 261 |
+
inputs=[q, top_k, maxlen, thresh, lang],
|
| 262 |
+
fn=run,
|
| 263 |
+
outputs=[best, sent, tops, rets],
|
| 264 |
+
label="Contoh pantas / Quick examples",
|
| 265 |
+
cache_examples=False
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
return demo
|
| 269 |
|
| 270 |
demo = build_ui()
|
| 271 |
|
| 272 |
+
if __name__name_ == "__main__":
|
| 273 |
demo.queue().launch()
|