Spaces:
Sleeping
Sleeping
Download app.py from Tshego235/multilingual-fact-checker: direct link, hf CLI and curl.
- Browser
- Download file 14.7 kB
-
https://huggingface.co/spaces/Tshego235/multilingual-fact-checker/resolve/main/app.py
- Command line
-
hf download hf://spaces/Tshego235/multilingual-fact-checker/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Tshego235/multilingual-fact-checker/resolve/main/app.py
14.7 kB
| """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 | |
| 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'<span style="display:inline-block;padding:4px 12px;border-radius:999px;' | |
| f'font-weight:700;color:#fff;background:{colour}">{_e(verdict)}</span>') | |
| def _evidence_html(items: list[dict]) -> str: | |
| rows = [] | |
| for i, ev in enumerate(items, start=1): | |
| row = f'<div style="border-top:1px solid #e4e7ec;padding-top:10px;margin-top:10px;font-size:14px">{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'<div style="margin-top:6px"><a style="{_LINK}" href="{_e(url)}" target="_blank" rel="noopener noreferrer">' | |
| f'Open source: {_e(host)}</a> · <a style="{_LINK}" href="{_e(archive)}" target="_blank" ' | |
| f'rel="noopener noreferrer">Archived copy</a>' | |
| f'<div style="{_MUTED};font-size:12px;word-break:break-all">{_e(url)}</div></div>') | |
| elif ev.get("source"): | |
| row += f'<div style="{_MUTED}">{_e(ev["source"])} · no public page to open</div>' | |
| rows.append(row + "</div>") | |
| return "".join(rows) or f'<div style="{_MUTED}">No evidence was retrieved.</div>' | |
| def render(r: dict) -> str: | |
| resp, eng = r.get("response") or {}, r.get("english") or {} | |
| out = [f'<div style="{_CARD}">', _badge(r.get("verdict")), | |
| f'<span style="{_MUTED}"> confidence {float(r.get("confidence") or 0):.2f} · ' | |
| f'{_e(r.get("language_name"))} · {_e(r.get("seconds"))}s</span>', | |
| f'<div style="font-size:20px;font-weight:600;margin:12px 0 8px">{_e(resp.get("verdict_statement"))}</div>'] | |
| if resp.get("explanation"): | |
| out.append(f"<p>{_e(resp['explanation'])}</p>") | |
| if resp.get("needs_review"): | |
| out.append('<div style="background:#fff8e6;border:1px solid #f0d78c;border-radius:8px;padding:8px 12px;' | |
| 'font-size:14px">The translation quality for this answer is low. Please rely on the English text below.</div>') | |
| if resp.get("disclaimer"): | |
| out.append(f'<p style="{_MUTED}">{_e(resp["disclaimer"])}</p>') | |
| out.append("</div>") | |
| if resp.get("language") and resp["language"] != "en": | |
| out.append(f'<details style="{_CARD}"><summary style="{_LINK};cursor:pointer">Show English (authoritative)</summary>' | |
| f'<div style="font-size:18px;font-weight:600;margin:10px 0">{_e(eng.get("verdict_statement"))}</div>' | |
| f'<p>{_e(eng.get("explanation"))}</p></details>') | |
| evidence = r.get("evidence") or [] | |
| out.append(f'<details open style="{_CARD}"><summary style="{_LINK};cursor:pointer">' | |
| f'Evidence retrieved ({len(evidence)})</summary>{_evidence_html(evidence)}</details>') | |
| 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'<a style="{_LINK}" href="{_e(text)}" target="_blank" rel="noopener noreferrer">{_e(host)}</a>' | |
| 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 = " <span style='color:#667085'>→</span> ".join( | |
| f'<span style="background:#eef3ff;border:1px solid #c9d7fb;border-radius:8px;padding:4px 8px;font-size:13px">' | |
| f'<b style="color:#2457d6">{_e(L["layer"])}</b> {_e(L["title"])}' | |
| f'{" · " + _e(L["seconds"]) + "s" if L.get("seconds") is not None else ""}</span>' | |
| for L in layers) | |
| out = [f'<div style="{_CARD}"><div style="font-size:20px;font-weight:600;margin-bottom:8px">' | |
| f'How the pipeline handled this claim</div><div style="line-height:2.2">{steps}</div>'] | |
| for L in layers: | |
| took = f' ({_e(L["seconds"])}s)' if L.get("seconds") is not None else "" | |
| out.append(f'<details open style="border-top:1px solid #e4e7ec;margin-top:10px;padding-top:8px">' | |
| f'<summary style="{_LINK};cursor:pointer;font-size:16px">{_e(L["layer"])} · {_e(L["title"])}{took}</summary>' | |
| f'<div style="{_MUTED};margin:6px 0">{_e(L["what"])}</div>') | |
| if L.get("fields"): | |
| out.append('<table style="border-collapse:collapse;width:100%;font-size:13px">') | |
| for k, v in L["fields"]: | |
| out.append(f'<tr><td style="{_TD};color:#667085;width:28%">{_e(k)}</td><td style="{_TD}">{_e(v)}</td></tr>') | |
| out.append("</table>") | |
| for T in L.get("tables") or []: | |
| out.append(f'<div style="font-weight:600;font-size:13px;margin-top:8px">{_e(T["title"])}</div>' | |
| '<div style="overflow-x:auto"><table style="border-collapse:collapse;width:100%;font-size:13px"><tr>' | |
| + "".join(f'<th style="{_TD};background:#fafbfc">{_e(c)}</th>' for c in T["columns"]) + "</tr>") | |
| for row in T["rows"]: | |
| out.append("<tr>" + "".join(f'<td style="{_TD}">{_cell(c)}</td>' for c in row) + "</tr>") | |
| out.append("</table></div>") | |
| out += [f'<div style="{_MUTED};font-size:13px;margin:4px 0">• {_e(n)}</div>' for n in L.get("notes") or []] | |
| out.append("</details>") | |
| out.append("</div>") | |
| 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'<div style="{_CARD};border-color:#f0d78c;background:#fff8e6">{_e(text)}</div>' | |
| 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() | |