Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,12 @@ from pathlib import Path
|
|
| 10 |
import gradio as gr
|
| 11 |
import numpy as np
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# -----------------------------
|
| 14 |
# CONFIG
|
| 15 |
# -----------------------------
|
|
@@ -64,7 +70,7 @@ def load_retriever():
|
|
| 64 |
|
| 65 |
_log(f"[load] retriever: contexts={len(contexts)} pipe_ok=1")
|
| 66 |
return contexts, pipe
|
| 67 |
-
except Exception
|
| 68 |
_log("[error] failed to load retriever:")
|
| 69 |
_log("".join(traceback.format_exc()))
|
| 70 |
return [], None
|
|
@@ -106,7 +112,6 @@ def extract_span(qa, q: str, context: str, max_len: int):
|
|
| 106 |
try:
|
| 107 |
out = qa({"question": q, "context": context}, top_k=3, max_answer_len=int(max_len), truncation=True)
|
| 108 |
outs = [out] if isinstance(out, dict) else out
|
| 109 |
-
# pick best by score
|
| 110 |
best = max(outs, key=lambda x: x.get("score", 0.0))
|
| 111 |
ans = best.get("answer", "").strip()
|
| 112 |
s = best.get("start", -1)
|
|
@@ -179,7 +184,11 @@ def run(question: str, top_k: int, max_len: int, thresh: float, lang: str):
|
|
| 179 |
# UI
|
| 180 |
# -----------------------------
|
| 181 |
def build_ui():
|
| 182 |
-
with gr.Blocks(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
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>')
|
| 184 |
|
| 185 |
with gr.Row():
|
|
@@ -198,10 +207,12 @@ def build_ui():
|
|
| 198 |
tops = gr.Textbox(label="Calon jawapan (Top-3) / Top-3 candidates")
|
| 199 |
rets = gr.Textbox(label="Konteks terpilih / Selected passages")
|
| 200 |
|
|
|
|
| 201 |
ask_btn.click(
|
| 202 |
fn=run,
|
| 203 |
inputs=[q, top_k, maxlen, thresh, lang],
|
| 204 |
outputs=[best, sent, tops, rets],
|
|
|
|
| 205 |
)
|
| 206 |
|
| 207 |
gr.Examples(
|
|
@@ -214,26 +225,18 @@ def build_ui():
|
|
| 214 |
examples_per_page=3,
|
| 215 |
label="Contoh pantas / Quick examples",
|
| 216 |
)
|
| 217 |
-
# ↓ add both of these ↓
|
| 218 |
-
app = demo # helps HF Spaces auto-discover the Gradio Blocks
|
| 219 |
-
|
| 220 |
-
# If running on Spaces (SPACE_ID env is present), force-launch the server.
|
| 221 |
-
if os.environ.get("SPACE_ID"):
|
| 222 |
-
demo.queue(concurrency_count=2).launch(
|
| 223 |
-
server_name="0.0.0.0",
|
| 224 |
-
server_port=int(os.environ.get("PORT", 7860)),
|
| 225 |
-
show_api=False
|
| 226 |
-
)
|
| 227 |
-
|
| 228 |
-
# Also allow local `python app.py`
|
| 229 |
-
if __name__ == "__main__":
|
| 230 |
-
demo.queue(concurrency_count=2).launch(
|
| 231 |
-
server_name="0.0.0.0",
|
| 232 |
-
server_port=int(os.environ.get("PORT", 7860)),
|
| 233 |
-
show_api=False
|
| 234 |
-
)
|
| 235 |
|
| 236 |
return demo
|
| 237 |
|
| 238 |
-
#
|
| 239 |
-
demo = build_ui()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
import gradio as gr
|
| 11 |
import numpy as np
|
| 12 |
|
| 13 |
+
# Put caches in /tmp so the Space doesn't fill persistent storage
|
| 14 |
+
os.environ.setdefault("HF_HOME", "/tmp/hf")
|
| 15 |
+
os.environ.setdefault("TRANSFORMERS_CACHE", "/tmp/hf/transformers")
|
| 16 |
+
os.environ.setdefault("HF_DATASETS_CACHE", "/tmp/hf/datasets")
|
| 17 |
+
os.environ.setdefault("HUGGINGFACE_HUB_CACHE", "/tmp/hf/hub")
|
| 18 |
+
|
| 19 |
# -----------------------------
|
| 20 |
# CONFIG
|
| 21 |
# -----------------------------
|
|
|
|
| 70 |
|
| 71 |
_log(f"[load] retriever: contexts={len(contexts)} pipe_ok=1")
|
| 72 |
return contexts, pipe
|
| 73 |
+
except Exception:
|
| 74 |
_log("[error] failed to load retriever:")
|
| 75 |
_log("".join(traceback.format_exc()))
|
| 76 |
return [], None
|
|
|
|
| 112 |
try:
|
| 113 |
out = qa({"question": q, "context": context}, top_k=3, max_answer_len=int(max_len), truncation=True)
|
| 114 |
outs = [out] if isinstance(out, dict) else out
|
|
|
|
| 115 |
best = max(outs, key=lambda x: x.get("score", 0.0))
|
| 116 |
ans = best.get("answer", "").strip()
|
| 117 |
s = best.get("start", -1)
|
|
|
|
| 184 |
# UI
|
| 185 |
# -----------------------------
|
| 186 |
def build_ui():
|
| 187 |
+
with gr.Blocks(
|
| 188 |
+
title="Salatus QA — Question-Only",
|
| 189 |
+
css=BRAND_CSS,
|
| 190 |
+
theme=gr.themes.Soft(primary_hue="slate", secondary_hue="amber"),
|
| 191 |
+
) as demo:
|
| 192 |
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>')
|
| 193 |
|
| 194 |
with gr.Row():
|
|
|
|
| 207 |
tops = gr.Textbox(label="Calon jawapan (Top-3) / Top-3 candidates")
|
| 208 |
rets = gr.Textbox(label="Konteks terpilih / Selected passages")
|
| 209 |
|
| 210 |
+
# NEW: set concurrency on the event (replacement for deprecated concurrency_count)
|
| 211 |
ask_btn.click(
|
| 212 |
fn=run,
|
| 213 |
inputs=[q, top_k, maxlen, thresh, lang],
|
| 214 |
outputs=[best, sent, tops, rets],
|
| 215 |
+
concurrency_limit=2,
|
| 216 |
)
|
| 217 |
|
| 218 |
gr.Examples(
|
|
|
|
| 225 |
examples_per_page=3,
|
| 226 |
label="Contoh pantas / Quick examples",
|
| 227 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
return demo
|
| 230 |
|
| 231 |
+
# Build once at import time
|
| 232 |
+
demo = build_ui()
|
| 233 |
+
app = demo # alias for HF Spaces auto-discovery
|
| 234 |
+
|
| 235 |
+
# Launch on Spaces or locally (no deprecated queue args)
|
| 236 |
+
if __name__ == "__main__" or os.environ.get("SPACE_ID"):
|
| 237 |
+
demo.launch(
|
| 238 |
+
server_name="0.0.0.0",
|
| 239 |
+
server_port=int(os.environ.get("PORT", 7860)),
|
| 240 |
+
show_api=False,
|
| 241 |
+
max_threads=8, # worker pool size
|
| 242 |
+
)
|