"""Gradio front end for the fact-checking prototype on a Hugging Face ZeroGPU Space. The models are placed on the GPU when the Space starts (ZeroGPU wants that at module level). A GPU is attached only while a claim is being checked, inside ``check_on_gpu``. Run locally with ``python app.py``; without ZeroGPU the ``spaces`` decorator does nothing and the models stay on CPU or a local GPU. On the Space the models run on the CPU by default, because ZeroGPU kept failing to allocate GPUs. Set the Space variable PROTOTYPE_DEVICE=gpu to use ZeroGPU again. """ from __future__ import annotations import os try: # imported before torch, as ZeroGPU requires import spaces except ImportError: # local run without the spaces package class spaces: # noqa: N801 @staticmethod def GPU(*args, **kwargs): # noqa: N802 if args and callable(args[0]): return args[0] return lambda fn: fn import html import sys import time from pathlib import Path import gradio as gr HERE = Path(__file__).resolve().parent # On the Space app.py sits next to prototype/ and src/. In the repository (a # local run or Google Colab) it lives in deploy/hf_space/, two levels down. ROOT = HERE if (HERE / "prototype").is_dir() else HERE.parents[1] if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) os.chdir(ROOT) # the AVeriTeC loader reads data/AVeriTeC relative to here from prototype.config import DENSE_MODEL, DISCRIMINATIVE_MODEL, resolve_device # noqa: E402 from prototype.index_cache import cache_directory, load_or_build_controller # noqa: E402 from prototype.pipeline import get_runtime, run_prototype # noqa: E402 from prototype.web import to_api_result # noqa: E402 from src.retrieval.corpus import load_chunks_for_cli # noqa: E402 ON_ZEROGPU = bool(os.environ.get("SPACES_ZERO_GPU")) # On the Space every model runs on the CPU by default and ZeroGPU is never # asked for a GPU: GPU allocation kept failing ("No CUDA GPUs are available"), # and failed attempts use up visitors' GPU allowance. Slower, but reliable. # Set the Space variable PROTOTYPE_DEVICE=gpu to use ZeroGPU again. REQUESTED_DEVICE = os.environ.get("PROTOTYPE_DEVICE", "").strip().lower() USE_ZEROGPU = ON_ZEROGPU and REQUESTED_DEVICE in ("gpu", "zerogpu", "cuda") if USE_ZEROGPU: DEVICE = "cuda" elif ON_ZEROGPU: DEVICE = "cpu" elif REQUESTED_DEVICE in ("cpu", "cuda"): DEVICE = REQUESTED_DEVICE else: DEVICE = resolve_device("auto") LANGS = { "en": "English", "nso": "Sepedi (Northern Sotho)", "tn": "Setswana", "ts": "Xitsonga", "zu": "isiZulu", } MAX_CLAIM_CHARS = 600 def _startup() -> None: """Embed the evidence once on CPU if no saved index came with the upload, then load every model onto DEVICE before the first visitor arrives.""" chunks = load_chunks_for_cli(averitec=True) if not cache_directory(chunks, DENSE_MODEL).exists(): print("Embedding the AVeriTeC evidence on CPU (first start only)...", flush=True) load_or_build_controller(chunks, model_name=DENSE_MODEL, device="cpu") started = time.perf_counter() get_runtime(averitec=True, device=DEVICE) # LaBSE, the index, explanation checker from src.utils.l1_bridge import get_nllb_translator from src.verification.discriminative import _load_model from src.verification.schemas import VerifierConfig get_nllb_translator(device=DEVICE) _load_model(VerifierConfig(discriminative_model=DISCRIMINATIVE_MODEL, device=DEVICE)) mode = "ZeroGPU" if USE_ZEROGPU else "CPU only" print(f"Models ready on {DEVICE} ({mode}) in {time.perf_counter() - started:.0f}s.", flush=True) _startup() def run_check(text: str, language: str) -> dict: started = time.perf_counter() record = run_prototype(text, language=language, averitec=True, device=DEVICE) result = to_api_result(record, device=DEVICE) result["seconds"] = round(time.perf_counter() - started, 1) return result # A ZeroGPU Space must define at least one @spaces.GPU function, even in CPU # mode, or it refuses to start. In CPU mode it is defined but never called. check_on_gpu = spaces.GPU(duration=60)(run_check) def check_claim(text: str, language: str) -> dict: return check_on_gpu(text, language) if USE_ZEROGPU else run_check(text, language) # ---------------------------------------------------------------- rendering _COLOURS = {"Supported": "#1a7f4b", "Refuted": "#c0392b", "Misleading": "#b9770e"} _CARD = "border:1px solid #e4e7ec;border-radius:12px;padding:16px;margin-bottom:12px;background:#fff;color:#1d2430" _MUTED = "color:#667085;font-size:14px" _LINK = "color:#2457d6;font-weight:600" def _e(value) -> str: return html.escape(str(value or "")) def _badge(verdict: str) -> str: colour = next((c for k, c in _COLOURS.items() if (verdict or "").startswith(k)), "#667085") return (f'{_e(verdict)}') def _evidence_html(items: list[dict]) -> str: rows = [] for i, ev in enumerate(items, start=1): row = f'
{i}. {_e(ev.get("text"))}' url = ev.get("url") if url: host = url.split("/")[2] if url.count("/") >= 2 else url archive = ev.get("archive_url") or f"https://web.archive.org/web/*/{url}" row += (f'
' f'Open source: {_e(host)} · Archived copy' f'
{_e(url)}
') elif ev.get("source"): row += f'
{_e(ev["source"])} · no public page to open
' rows.append(row + "
") return "".join(rows) or f'
No evidence was retrieved.
' def render(r: dict) -> str: resp, eng = r.get("response") or {}, r.get("english") or {} out = [f'
', _badge(r.get("verdict")), f'  confidence {float(r.get("confidence") or 0):.2f} · ' f'{_e(r.get("language_name"))} · {_e(r.get("seconds"))}s', f'
{_e(resp.get("verdict_statement"))}
'] if resp.get("explanation"): out.append(f"

{_e(resp['explanation'])}

") if resp.get("needs_review"): out.append('
The translation quality for this answer is low. Please rely on the English text below.
') if resp.get("disclaimer"): out.append(f'

{_e(resp["disclaimer"])}

') out.append("
") if resp.get("language") and resp["language"] != "en": out.append(f'
Show English (authoritative)' f'
{_e(eng.get("verdict_statement"))}
' f'

{_e(eng.get("explanation"))}

') evidence = r.get("evidence") or [] out.append(f'
' f'Evidence retrieved ({len(evidence)}){_evidence_html(evidence)}
') return "".join(out) _TD = "padding:4px 6px;border:1px solid #e4e7ec;text-align:left;vertical-align:top" def _cell(value) -> str: text = str(value if value is not None else "") if text.startswith(("http://", "https://")): host = text.split("/")[2] return f'{_e(host)}' return _e(text) def render_trace(layers: list) -> str: """The pipeline view: a strip of the six layers, then each layer's details.""" if not layers: return "" steps = " → ".join( f'' f'{_e(L["layer"])} {_e(L["title"])}' f'{" · " + _e(L["seconds"]) + "s" if L.get("seconds") is not None else ""}' for L in layers) out = [f'
' f'How the pipeline handled this claim
{steps}
'] for L in layers: took = f' ({_e(L["seconds"])}s)' if L.get("seconds") is not None else "" out.append(f'
' f'{_e(L["layer"])} · {_e(L["title"])}{took}' f'
{_e(L["what"])}
') if L.get("fields"): out.append('') for k, v in L["fields"]: out.append(f'') out.append("
{_e(k)}{_e(v)}
") for T in L.get("tables") or []: out.append(f'
{_e(T["title"])}
' '
' + "".join(f'' for c in T["columns"]) + "") for row in T["rows"]: out.append("" + "".join(f'' for c in row) + "") out.append("
{_e(c)}
{_cell(c)}
") out += [f'
• {_e(n)}
' for n in L.get("notes") or []] out.append("
") out.append("
") return "".join(out) def show(result: dict | None, show_trace: bool) -> str: if not isinstance(result, dict) or not result: return "" return render(result) + (render_trace(result.get("trace") or []) if show_trace is True else "") def _message(text: str) -> str: return f'
{_e(text)}
' def check(text: str, language: str, show_trace: bool): """Bad input gets a message in the result area, not an exception. Automated requests to the Space API (a "ping" sent as every input) hit this function too. Raising gr.Error for them printed a long traceback in the Space logs each time; returning a message keeps the logs clean and never reaches the GPU. """ text = str(text or "").strip() language = str(language or "").strip().lower() if language not in LANGS: return _message("Please choose the language of the claim from the menu."), None if not text: return _message("Please enter a claim."), None if len(text) > MAX_CLAIM_CHARS: return _message(f"Please keep the claim under {MAX_CLAIM_CHARS} characters."), None result, error = _check_with_retry(text, language) if result is None: detail = f"{type(error).__name__}: {error}" if "quota" in detail.lower(): return _message("Today's free GPU allowance has been used up. Sign in to Hugging Face " "for a larger allowance, or try again later."), None if _gpu_attach_failed(error): return _message("The shared GPU could not be attached just now. Please try again in a " "few minutes."), None return _message(f"Something went wrong while checking this claim ({detail})."), None return show(result, show_trace), result def _gpu_attach_failed(exc: BaseException) -> bool: text = f"{type(exc).__name__}: {exc}" return any(s in text for s in ("No CUDA GPUs", "ZeroGPU worker error", "CUDA error", "'RuntimeError'")) def _check_with_retry(text: str, language: str, attempts: int = 1): """ZeroGPU sometimes fails to attach a GPU to the worker ("No CUDA GPUs are available" in worker_init). That is on the platform side. Failed attempts are reported to use up the visitor's GPU allowance, so the default is one attempt, and the Space runs on CPU unless PROTOTYPE_DEVICE=gpu. Other errors (a used quota, a bug in the pipeline) are never retried.""" error = None for attempt in range(1, attempts + 1): try: return check_claim(text, language), None except Exception as exc: # noqa: BLE001 - the spaces wrapper raises several types error = exc print(f"GPU attempt {attempt} failed: {type(exc).__name__}: {exc}", flush=True) if not _gpu_attach_failed(exc) or attempt == attempts: break time.sleep(3) return None, error with gr.Blocks(title="Multilingual Fact-Checker") as demo: gr.Markdown( "# Multilingual Fact-Checker\n" "Ask in English, Sepedi, Setswana, Xitsonga or isiZulu. The answer comes back in the same " "language. Evidence comes from the AVeriTeC fact-checking dataset; each passage links to its source." ) claim = gr.Textbox(label="Claim or question", lines=4, placeholder="Type or paste a claim ...") # allow_custom_value: automated requests to the Space API (for example a # "ping" health check) send values outside the menu. Gradio would raise # inside its own code and log a long traceback; check() rejects them # instead with a short message and without using the GPU. language = gr.Dropdown(choices=[(v, k) for k, v in LANGS.items()], value="en", label="Language of the claim", allow_custom_value=True) show_trace = gr.Checkbox(label="Show how the pipeline works (every layer, step by step)", value=False) button = gr.Button("Check claim", variant="primary") last = gr.State(None) # the last result, so the switch redraws without checking again result = gr.HTML() button.click(check, inputs=[claim, language, show_trace], outputs=[result, last]) show_trace.change(show, inputs=[last, show_trace], outputs=result) if __name__ == "__main__": demo.queue(max_size=20).launch()