File size: 9,294 Bytes
9938856
 
 
 
 
 
 
a86bc8c
9938856
6769e97
9938856
6accb39
99b1e76
 
 
 
 
 
9938856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99b1e76
9938856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345dddc
9938856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73a6287
9938856
 
73a6287
552003c
9938856
73a6287
9938856
73a6287
9938856
 
 
bde0271
9938856
 
 
 
 
 
 
 
 
 
bde0271
9938856
 
 
bde0271
 
9938856
73a6287
a86bc8c
9938856
bde0271
 
9938856
 
 
 
bde0271
9938856
bde0271
9938856
 
 
 
bde0271
9938856
 
bde0271
9938856
 
 
 
 
 
 
 
 
 
5456b8b
99b1e76
 
 
 
 
9938856
6accb39
 
9938856
6accb39
9938856
6accb39
9938856
6accb39
9938856
6accb39
9938856
6accb39
9938856
6accb39
9938856
 
6accb39
99b1e76
9938856
 
 
 
99b1e76
9938856
6accb39
 
 
9938856
 
 
6accb39
 
9938856
6accb39
 
345dddc
5456b8b
 
99b1e76
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
60
61
62
63
64
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# 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] + "<mark>" + ctx[s:e] + "</mark>" + ctx[e:]
                else:
                    best_sent = ctx.replace(ans, f"<mark>{ans}</mark>", 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('<div id="bar"><b>Salatus QA — Question-Only</b><br><span style="opacity:.85">Masukkan soalan sahaja. Aplikasi memilih konteks terbaik, kemudian mengekstrak jawapan.</span></div>')

        with gr.Row():
            lang = gr.Radio(choices=["ms", "en"], value="ms", label="Bahasa / Language (ms/en)")

        q = gr.Textbox(label="Soalan / Question", placeholder="Tulis soalan di sini…")
        with gr.Row():
            top_k  = gr.Slider(1, 10, value=5, step=1, label="Bilangan konteks dicadang (top_k) / Number of candidate passages")
            maxlen = gr.Slider(6, 24, value=12, step=1, label="Maks panjang jawapan (token) / Max answer length (tokens)")
            thresh = gr.Slider(0.01, 0.25, value=0.12, step=0.01, label="Ambang persamaan (retrieval) / Similarity threshold")

        ask_btn = gr.Button("Tanya / Ask")

        best = gr.Textbox(label="Jawapan terbaik / Best answer")
        sent = gr.HTML(label="Jawapan (ayat) / Answer (sentence)")
        tops = gr.Textbox(label="Calon jawapan (Top-3) / Top-3 candidates")
        rets = gr.Textbox(label="Konteks terpilih / Selected passages")

        # NEW: set concurrency on the event (replacement for deprecated concurrency_count)
        ask_btn.click(
            fn=run,
            inputs=[q, top_k, maxlen, thresh, lang],
            outputs=[best, sent, tops, rets],
            concurrency_limit=2,
        )

        gr.Examples(
            examples=[
                ["Siapakah yang dikahwinkan dengan Tuan Puteri Mahtabu’l-Bahri?", 5, 12, 0.12, "ms"],
                ["Siapakah yang memerintahkan pihak Sultan Alau'd-Din?",         5, 12, 0.12, "ms"],
                ["Who married Tuan Puteri Mahtabu’l-Bahri?",                     5, 12, 0.12, "en"],
            ],
            inputs=[q, top_k, maxlen, thresh, lang],
            examples_per_page=3,
            label="Contoh pantas / Quick examples",
        )

    return demo

# Build once at import time
demo = build_ui()
app = demo  # alias for HF Spaces auto-discovery

# Launch on Spaces or locally (no deprecated queue args)
if __name__ == "__main__" or os.environ.get("SPACE_ID"):
    demo.launch(
        server_name="0.0.0.0",
        server_port=int(os.environ.get("PORT", 7860)),
        show_api=False,
        max_threads=8,  # worker pool size
    )