# app.py — Salatus QA (Question-only, with retriever), robust start-up import os import json import math import traceback from functools import lru_cache from pathlib import Path import gradio as gr import numpy as np # Put caches in /tmp so the Space doesn't fill persistent storage os.environ.setdefault("HF_HOME", "/tmp/hf") os.environ.setdefault("TRANSFORMERS_CACHE", "/tmp/hf/transformers") os.environ.setdefault("HF_DATASETS_CACHE", "/tmp/hf/datasets") os.environ.setdefault("HUGGINGFACE_HUB_CACHE", "/tmp/hf/hub") # ----------------------------- # CONFIG # ----------------------------- MODEL_REPO = "ChogwuCY/salatus-qa-xlmrb-base" # <-- keep quotes! RETR_DIR = Path("retriever") # expects contexts.json + tfidf_pipe.joblib (+ optional X.npz) BRAND_CSS = """ :root { --brand-primary: #0f172a; /* slate-900 */ --brand-accent: #f59e0b; /* amber-500 */ } .gradio-container { font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial; } #bar { background: linear-gradient(135deg, #0ea5e9, #0f172a 70%); color: white; padding: 16px 20px; border-radius: 12px; } button, .gr-button { background: var(--brand-accent) !important; color: #111 !important; border-radius: 12px !important; font-weight: 700 !important; } textarea, input, .gr-textbox, .gr-box { border-radius: 12px !important; } mark { background: #fff59d; padding: 0 .15em; border-radius: .2em; } """ # ----------------------------- # LIGHTWEIGHT UTILS # ----------------------------- def _log(msg: str): print(msg, flush=True) def _norm(s: str) -> str: if not s: return "" s = s.replace("\u200b","").replace("\u200c","").replace("\xa0"," ") s = s.replace("’", "'").replace("‘", "'").replace("“", '"').replace("”", '"') return " ".join(s.split()) # ----------------------------- # RETRIEVER (TF-IDF) # ----------------------------- @lru_cache(maxsize=1) def load_retriever(): """ Returns (contexts, pipe) or ([], None) if not found. """ try: ctx_path = RETR_DIR / "contexts.json" pipe_path = RETR_DIR / "tfidf_pipe.joblib" if not ctx_path.exists() or not pipe_path.exists(): _log("[warn] retriever assets not found — running without retrieval.") return [], None contexts = json.loads(ctx_path.read_text(encoding="utf-8")) # lazy import to avoid import cost if not present import joblib pipe = joblib.load(pipe_path) _log(f"[load] retriever: contexts={len(contexts)} pipe_ok=1") return contexts, pipe except Exception: _log("[error] failed to load retriever:") _log("".join(traceback.format_exc())) return [], None def retrieve(question: str, top_k: int, thresh: float): contexts, pipe = load_retriever() if not contexts or pipe is None or not question.strip(): return [] try: Q = [question] qv = pipe.transform(Q) # L2-normalized Cv = pipe.transform(contexts) sims = (qv @ Cv.T).toarray()[0] # cosine sims because of Normalizer idxs = np.argsort(-sims)[: max(1, top_k)] out = [] for i in idxs: s = float(sims[i]) if s >= thresh: out.append((int(i), s, contexts[i])) return out except Exception: _log("[error] retrieve() crashed:") _log("".join(traceback.format_exc())) return [] # ----------------------------- # QA PIPELINE # ----------------------------- @lru_cache(maxsize=1) def load_qa(): from transformers import pipeline device = 0 if os.environ.get("CUDA_VISIBLE_DEVICES") else -1 _log(f"[load] QA model: {MODEL_REPO} device={'cuda:0' if device==0 else 'cpu'}") qa = pipeline("question-answering", model=MODEL_REPO, tokenizer=MODEL_REPO, device=device) return qa def extract_span(qa, q: str, context: str, max_len: int): try: out = qa({"question": q, "context": context}, top_k=3, max_answer_len=int(max_len), truncation=True) outs = [out] if isinstance(out, dict) else out best = max(outs, key=lambda x: x.get("score", 0.0)) ans = best.get("answer", "").strip() s = best.get("start", -1) e = best.get("end", -1) return ans, s, e, outs except Exception: _log("[error] extract_span crashed:") _log("".join(traceback.format_exc())) return "", -1, -1, [] def run(question: str, top_k: int, max_len: int, thresh: float, lang: str): """ Returns four strings: best_ans, sent_ans, top3_str, selected_passages_str """ try: q = _norm(question) if not q: return "—", "", "", "" # 1) Retrieve candidate contexts cands = retrieve(q, max(1, int(top_k)), float(thresh)) if not cands: msg_ms = "Tiada konteks ditemui (cuba rendahkan ambang persamaan atau naikkan top_k)." msg_en = "No passage found (try lowering the similarity threshold or increasing top_k)." return ("—", msg_ms if lang == "ms" else msg_en, "", "") qa = load_qa() # 2) Ask QA per context and keep the best best_score = -1.0 best_ans = "" best_sent = "" tops_list = [] chosen_list = [] for idx, sim, ctx in cands: ans, s, e, outs = extract_span(qa, q, ctx, int(max_len)) chosen_list.append(f"[{idx}] sim={sim:.3f} {ctx[:260]}…") if not ans: continue score = float(outs[0].get("score", 0.0)) tops_list.append(f"- {ans} (confidence≈{score*100:.1f}%) • sim={sim:.3f}") if score > best_score: best_score = score best_ans = ans if isinstance(s, int) and isinstance(e, int) and 0 <= s < e <= len(ctx): best_sent = ctx[:s] + "" + ctx[s:e] + "" + ctx[e:] else: best_sent = ctx.replace(ans, f"{ans}", 1) if not best_ans: msg_ms = "Model tidak menemui jawapan yang yakin dalam petikan terpilih." msg_en = "Model did not find a confident answer in the selected passages." return ("—", msg_ms if lang == "ms" else msg_en, "\n".join(tops_list[:3]), "\n".join(chosen_list[:5])) top3_str = "\n".join(tops_list[:3]) chosen = "\n".join(chosen_list[:5]) return best_ans, best_sent, top3_str, chosen except Exception: _log("[error] run() crashed:") _log("".join(traceback.format_exc())) return "—", "Ralat dalaman / Internal error.", "", "" # ----------------------------- # UI # ----------------------------- def build_ui(): with gr.Blocks( title="Salatus QA — Question-Only", css=BRAND_CSS, theme=gr.themes.Soft(primary_hue="slate", secondary_hue="amber"), ) as demo: gr.HTML('