import os os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True") import spaces # noqa: E402 (must precede torch) import concurrent.futures # noqa: E402 import html # noqa: E402 import json # noqa: E402 import sys # noqa: E402 import time # noqa: E402 import types # noqa: E402 import gradio as gr # noqa: E402 import torch # noqa: E402 from huggingface_hub import snapshot_download # noqa: E402 MODEL_ID = "Cloudflare/clairic" # --------------------------------------------------------------------------- # ZeroGPU compatibility shims # --------------------------------------------------------------------------- # The release runtime targets Hopper (sm_90) and calls DeepGEMM / FlashInfer # for a couple of BF16 GEMMs and Gemma-style RMSNorms. ZeroGPU runs on # Blackwell sm_120, where DeepGEMM has no kernels, so we provide numerically # equivalent pure-PyTorch implementations of exactly the functions it uses. def _install_shims() -> None: dg = types.ModuleType("deep_gemm") def bf16_gemm_nn(a, b, out): torch.matmul(a, b, out=out) dg.bf16_gemm_nn = bf16_gemm_nn dg.get_num_sms = lambda: torch.cuda.get_device_properties(0).multi_processor_count dg.get_tc_util = lambda: 100 dg.get_pdl = lambda: False sys.modules["deep_gemm"] = dg fi = types.ModuleType("flashinfer") norm = types.ModuleType("flashinfer.norm") def _gemma(x32, weight, eps): var = x32.pow(2).mean(-1, keepdim=True) return x32 * torch.rsqrt(var + eps) * (1.0 + weight.float()) def gemma_rmsnorm(x, weight, eps=1e-6): return _gemma(x.float(), weight, eps).to(x.dtype) def gemma_fused_add_rmsnorm(x, residual, weight, eps=1e-6): total = x.float() + residual.float() residual.copy_(total) x.copy_(_gemma(total, weight, eps)) norm.gemma_rmsnorm = gemma_rmsnorm norm.gemma_fused_add_rmsnorm = gemma_fused_add_rmsnorm fi.norm = norm sys.modules["flashinfer"] = fi sys.modules["flashinfer.norm"] = norm _install_shims() # --------------------------------------------------------------------------- # Model loading (module scope, eager .to("cuda")) # --------------------------------------------------------------------------- MODEL_PATH = snapshot_download(MODEL_ID) sys.path.insert(0, MODEL_PATH) import xgrammar as xgr # noqa: E402 from transformers import Qwen3_5ForConditionalGeneration # noqa: E402 from clairic_model import ClairicModel # noqa: E402 from clairic_runtime import native as _native # noqa: E402 from clairic_runtime.large import Clairic27BModel # noqa: E402 from clairic_runtime.math import install as _install_math # noqa: E402 from clairic_runtime.readout import load_head # noqa: E402 from clairic_runtime.settings import RuntimeSettings # noqa: E402 _TOKENIZER_INFO = {} def _zerogpu_engine_load(cls, path, tokenizer, device="cuda", dtype=None, batch_size=None, text_batch_size=None, batch_token_budget=None, **_): """Same as the release loader, minus Hopper-only FA3 and CUDA-graph capture.""" class _NativeQwen3_5ForConditionalGeneration(Qwen3_5ForConditionalGeneration): _keep_in_fp32_modules_strict = ["A_log"] backbone = _NativeQwen3_5ForConditionalGeneration.from_pretrained( path, dtype=torch.bfloat16, local_files_only=True, attn_implementation={"text_config": "sdpa", "vision_config": "sdpa"}, ) backbone.to("cuda") head = load_head(path, match_bfloat16=True).float().eval().to("cuda") engine = cls(backbone, head, tokenizer, batch_size=batch_size, text_batch_size=text_batch_size, batch_token_budget=batch_token_budget) _TOKENIZER_INFO["info"] = xgr.TokenizerInfo.from_huggingface( tokenizer, vocab_size=backbone.get_output_embeddings().weight.shape[0], stop_token_ids=[engine.eos], ) return engine _native.TransformersEngine.load = classmethod(_zerogpu_engine_load) _settings = RuntimeSettings(model_path=MODEL_PATH, version="1", model_id="clairic-27b", max_length=65_273, max_concurrency=1) RUNTIME = Clairic27BModel.load(_settings, device="cuda") MODEL = ClairicModel(RUNTIME, "clairic") print("Clairic loaded.", flush=True) _WORKER_PID = None def _prepare_worker() -> None: """Re-create thread pools / grammar compiler / fused math inside the GPU fork.""" global _WORKER_PID if _WORKER_PID == os.getpid(): return engine = RUNTIME.engine engine._worker = concurrent.futures.ThreadPoolExecutor(max_workers=1, thread_name_prefix="clairic-model") RUNTIME._encoder = concurrent.futures.ThreadPoolExecutor(max_workers=1, thread_name_prefix="clairic-encode") engine.compiler = xgr.GrammarCompiler(_TOKENIZER_INFO["info"], max_threads=2) if engine._restore_math is None: engine._restore_math = _install_math(engine.backbone, engine.head) _WORKER_PID = os.getpid() # --------------------------------------------------------------------------- # Presets (from the model card) and rendering helpers # --------------------------------------------------------------------------- INVOICE_STATE = ( "Invoice from Northwind Supply. Issued 2026-09-02. " "Items: Pens; Paper. Total: $1080.00." ) INVOICE_FIELDS = { "vendor": {"type": "text", "instructions": "The invoice vendor."}, "total": {"type": "number", "decimals": 2}, "issued": {"type": "date"}, "items": {"type": "list", "items": "text", "instructions": "The purchased item names, in order."}, } DEMAND_STATE = "Daily demand for the last seven days: 41, 45, 43, 47, 49, 46, 48." DEMAND_FIELDS = { "next_day": {"type": "estimate", "min": 0, "max": 100}, "next_three_days": {"type": "forecast", "min": 0, "max": 100, "horizon": 3, "interval": "day"}, } RECEIPT_STATE = "Read the attached receipt." RECEIPT_FIELDS = { "items": {"type": "list", "items": "text", "instructions": "The purchased item names, in order."}, "total": {"type": "number", "source_only": True, "instructions": "The receipt total."}, "paid_cash": {"type": "boolean", "instructions": "Whether the customer paid in cash."}, } TICKET_STATE = ( "Hi team, our checkout page has been returning 502 errors since about 14:20 UTC today. " "Roughly 30% of EU customers are affected and we're losing orders. Account: ACME-4471. " "Please escalate - Priya, Head of Ops" ) TICKET_FIELDS = { "category": {"type": "enum", "enum": ["billing", "outage", "feature_request", "security", "other"]}, "urgent": {"type": "boolean", "instructions": "Whether the issue needs immediate attention."}, "account_id": {"type": "text"}, "share_affected": {"type": "number", "unit": "percent", "instructions": "Share of customers affected."}, "reporter": {"type": "text", "instructions": "Name of the person reporting."}, } def _fmt(fields: dict) -> str: return json.dumps(fields, indent=2) def _esc(value) -> str: return html.escape(str(value)) def _pct(p) -> float: try: return max(0.0, min(1.0, float(p))) * 100.0 except (TypeError, ValueError): return 0.0 def _num(v) -> str: if isinstance(v, bool): return str(v).lower() if isinstance(v, float): return f"{v:,.2f}".rstrip("0").rstrip(".") if abs(v) < 1e6 else f"{v:,.0f}" if isinstance(v, int): return f"{v:,}" return str(v) def _value_inline(value) -> str: if isinstance(value, dict): if "value" in value: return _esc(_num(value["value"])) return _esc(json.dumps(value, ensure_ascii=False)) return _esc(_num(value)) def _value_html(value) -> str: if value is None: return 'null' if isinstance(value, bool): return f'{str(value).lower()}' if isinstance(value, list): if not value: return '[ ]' chips = "".join(f'{_value_inline(v)}' for v in value) return f'
{chips}
' if isinstance(value, dict): rows = "".join(f"
{_esc(k)} {_value_inline(v)}
" for k, v in value.items()) return f'
{rows}
' return f'{_esc(_num(value))}' def _floats(seq): out = [] for v in seq or []: try: out.append(float(v)) except (TypeError, ValueError): return [] return out def _spark(vals, lows=None, highs=None) -> str: vals = _floats(vals) lows, highs = _floats(lows), _floats(highs) if len(vals) < 2: return "" band = len(lows) == len(vals) == len(highs) every = vals + (lows + highs if band else []) lo, hi = min(every), max(every) span = (hi - lo) or 1.0 w, h = 240, 70 X = lambda i: 12 + i * (w - 24) / (len(vals) - 1) Y = lambda v: h - 10 - (v - lo) / span * (h - 20) poly = "" if band: top = " ".join(f"{X(i):.1f},{Y(v):.1f}" for i, v in enumerate(highs)) bot = " ".join(f"{X(i):.1f},{Y(v):.1f}" for i, v in reversed(list(enumerate(lows)))) poly = f'' line = " ".join(f"{X(i):.1f},{Y(v):.1f}" for i, v in enumerate(vals)) dots = "".join(f'' for i, v in enumerate(vals)) return f'{poly}{dots}' def render_answers(response: dict, elapsed: float) -> str: cards = [] for name, ans in response.get("answers", {}).items(): ans = ans if isinstance(ans, dict) else {"value": ans} kind = ans.get("type", "") value = ans.get("value") body = _value_html(value) extra = "" if kind == "forecast" and isinstance(value, list): extra += _spark(value, ans.get("p10"), ans.get("p90")) if isinstance(ans.get("p10"), list): extra += '
shaded band: 80% interval (p10–p90)
' elif "p10" in ans and "p90" in ans and not isinstance(ans["p10"], list): extra += (f'
p10 {_esc(_num(ans["p10"]))}' f'median {_esc(_num(ans.get("median", value)))}' f'p90 {_esc(_num(ans["p90"]))}
') meters = "" if "confidence" in ans: c = _pct(ans["confidence"]) meters += (f'
confidence
' f'
{c:.1f}%
') if "p_null" in ans: p = _pct(ans["p_null"]) meters += (f'
p(null)
' f'
{p:.1f}%
') flags = f'{_esc(kind)}' if kind else "" if ans.get("valid") is False: flags += 'invalid' if ans.get("complete") is False: flags += 'incomplete' cards.append( f'
{_esc(name)}{flags}
' f'
{body}{extra}
{meters}
' ) usage = response.get("usage", {}) stats = "".join( f'
{_esc(v)}{_esc(k.replace("_", " "))}
' for k, v in usage.items() ) trunc = ('input truncated' if response.get("truncated") else 'complete input') return ( f'
Structured answers' f'{trunc}{elapsed:.1f}s
' f'
{"".join(cards)}
{stats}
' ) EMPTY_HTML = ( '
{ }
' "

Pick a preset or describe your own fields, then hit Extract.

" "

Each field comes back typed, with a confidence score and a null probability.

" ) # --------------------------------------------------------------------------- # Inference # --------------------------------------------------------------------------- def _gpu_duration(state, fields_json, image=None, *args, **kwargs): # measured: <=22.4s text (cold worker incl. Triton JIT), <=13.9s single image return 40 if image is not None else 26 @spaces.GPU(duration=_gpu_duration, size="xlarge") def extract(state: str, fields_json: str, image=None, reference_date: str = "", include_quantiles: bool = False): """Extract typed, scored structured answers from text and/or an image with Clairic. Args: state: Source text (or JSON) to read from. May be empty when an image is provided. fields_json: JSON object mapping field names to typed field specs, e.g. {"total": {"type": "number", "decimals": 2}}. Supported types: text, number, date, time, datetime, duration, boolean, enum, object, list, compute, estimate, forecast. image: Optional image (e.g. a receipt or document photo). reference_date: Optional ISO date (YYYY-MM-DD) used for relative date instructions. include_quantiles: Return quantiles for estimate / forecast fields. Returns: A rendered HTML summary and the raw JSON response (answers, truncated, usage). """ try: fields = json.loads(fields_json) except json.JSONDecodeError as exc: raise gr.Error(f"Fields must be valid JSON: {exc}") if not isinstance(fields, dict) or not fields: raise gr.Error("Fields must be a non-empty JSON object of field specs.") request = {"state": state or "", "fields": fields, "options": {"include_quantiles": bool(include_quantiles)}} if image is not None: request["images"] = [image] if reference_date and reference_date.strip(): request["reference_date"] = reference_date.strip() if not request["state"].strip() and image is None: raise gr.Error("Provide some source text or an image.") _prepare_worker() t0 = time.perf_counter() try: response = MODEL.predict(request) except Exception as exc: # validation / request errors → readable message raise gr.Error(f"{type(exc).__name__}: {exc}") elapsed = time.perf_counter() - t0 print(f"[clairic] inference {elapsed:.2f}s fields={list(fields)} image={image is not None}", flush=True) response = json.loads(json.dumps(response, default=str)) return render_answers(response, elapsed), response # --------------------------------------------------------------------------- # UI # --------------------------------------------------------------------------- CF_LOGO = """ """ HEADER = f"""
{CF_LOGO}Cloudflare

Clairic 27B

Turn unstructured text and images into typed, scored structured answers. Declare a schema of fields — text, numbers, dates, enums, lists, estimates, forecasts — and get back values with confidence and null probabilities.

""" CSS = """ :root { --cf-orange:#F38020; --cf-orange-dark:#E06A10; --cf-text:#222; --cf-muted:#595959; --cf-line:#E5E5E5; } .gradio-container { max-width: 1200px !important; margin: 0 auto !important; font-family: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; } .cf-hero { padding:28px 4px 24px; margin-bottom:16px; border-bottom:1px solid var(--cf-line); } .cf-brand { display:flex; align-items:center; gap:8px; margin-bottom:18px; font-size:15px; font-weight:600; color:var(--cf-text); } .cf-logo { width:34px; flex:none; } .cf-hero h1 { font-size:32px; line-height:1.2; margin:0 0 8px; font-weight:600; color:var(--cf-text); letter-spacing:-.01em; } .cf-hero h1 span { font-size:18px; font-weight:400; color:var(--cf-muted); margin-left:4px; } .cf-hero p { color:var(--cf-muted); max-width:760px; margin:0 0 14px; font-size:15px; line-height:1.6; } .cf-links { display:flex; gap:20px; flex-wrap:wrap; align-items:center; font-size:14px; } .cf-links a { color:var(--cf-orange) !important; text-decoration:none; font-weight:500; } .cf-links a:hover { text-decoration:underline; } .cf-meta { color:var(--cf-muted); font-size:13px; } .cf-panel { border:1px solid var(--cf-line) !important; } .cf-section { font-size:13px; font-weight:600; color:var(--cf-text); margin:2px 0 4px; } #cf-run { background:var(--cf-orange) !important; color:#fff !important; font-weight:600 !important; border:none !important; box-shadow:none !important; } #cf-run:hover { background:var(--cf-orange-dark) !important; } .cf-empty { text-align:center; padding:48px 20px; border:1px solid var(--cf-line); border-radius:6px; color:var(--cf-muted); } .cf-empty-icon { font-family: ui-monospace, monospace; font-size:24px; color:#999; margin-bottom:6px; } .cf-results-head { display:flex; justify-content:space-between; align-items:center; font-weight:600; margin-bottom:12px; font-size:15px; } .cf-time { display:flex; gap:12px; align-items:center; font-weight:400; font-size:13px; color:var(--cf-muted); } .cf-grid { display:grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap:12px; } .cf-card { border-radius:6px; padding:14px; background:var(--block-background-fill); border:1px solid var(--border-color-primary); } .cf-card-head { display:flex; justify-content:space-between; align-items:center; gap:6px; margin-bottom:8px; } .cf-card-head code { font-size:13px; font-weight:600; color:var(--body-text-color); background:none; padding:0; } .cf-card-body { min-height:30px; margin-bottom:10px; } .cf-val { font-size:18px; font-weight:500; word-break:break-word; } .cf-null { font-family: ui-monospace, monospace; color:var(--body-text-color-subdued); font-style:italic; } .cf-bool { font-family: ui-monospace, monospace; font-weight:500; } .cf-true { color:#1a7f37; } .cf-false { color:#b42318; } .cf-chips { display:flex; flex-wrap:wrap; gap:6px; } .cf-chip { font-size:13px; padding:3px 8px; border-radius:4px; border:1px solid var(--border-color-primary); } .cf-obj div { font-size:13px; } .cf-sub { font-size:12px; color:var(--body-text-color-subdued); margin-top:6px; } .cf-spark { width:100%; height:54px; margin-top:6px; } .cf-spark polyline { fill:none; stroke:var(--cf-orange); stroke-width:1.5; stroke-linejoin:round; } .cf-spark .cf-band { fill:rgba(243,128,32,.10); stroke:none; } .cf-spark circle { fill:var(--cf-orange); stroke:none; r:2.5; } .cf-meter { display:grid; grid-template-columns: 70px 1fr 50px; align-items:center; gap:8px; font-size:11px; color:var(--body-text-color-subdued); margin-top:4px; } .cf-meter em { font-style:normal; text-align:right; font-weight:500; color:var(--body-text-color); } .cf-bar { height:4px; border-radius:2px; background:var(--border-color-primary); overflow:hidden; } .cf-bar i { display:block; height:100%; background:var(--cf-orange); } .cf-meter-null .cf-bar i { background:#8c8c8c; } .cf-type { font-size:11px; color:var(--body-text-color-subdued); margin-left:6px; } .cf-range { display:flex; justify-content:space-between; font-size:12px; margin-top:8px; color:var(--body-text-color-subdued); } .cf-range span:nth-child(2) { color:var(--body-text-color); font-weight:500; } .cf-flag { margin-left:6px; font-size:11px; font-weight:500; color:#b42318; } .cf-ok { color:var(--body-text-color-subdued); } .cf-stats { display:flex; flex-wrap:wrap; gap:24px; margin-top:16px; padding-top:12px; border-top:1px solid var(--border-color-primary); } .cf-stat b { display:block; font-size:15px; font-weight:500; } .cf-stat span { font-size:11px; color:var(--body-text-color-subdued); text-transform:capitalize; } .cf-footer { text-align:center; font-size:12px; color:var(--body-text-color-subdued); margin-top:16px; padding-top:16px; border-top:1px solid var(--cf-line); } .cf-footer a { color:var(--cf-orange) !important; text-decoration:none; } """ theme = gr.themes.Base( primary_hue=gr.themes.Color( c50="#fff6ed", c100="#ffead4", c200="#fed1a8", c300="#fdb070", c400="#fbad41", c500="#f38020", c600="#e06a10", c700="#b9520f", c800="#934114", c900="#773714", c950="#401a08", ), secondary_hue="neutral", neutral_hue="neutral", radius_size="sm", font=[gr.themes.GoogleFont("Inter"), "system-ui", "sans-serif"], font_mono=["ui-monospace", "SFMono-Regular", "Menlo", "monospace"], ).set( body_background_fill="#ffffff", button_primary_background_fill="#F38020", button_primary_background_fill_hover="#E06A10", button_primary_text_color="#ffffff", button_primary_border_color="#F38020", block_shadow="none", block_title_text_weight="500", block_label_text_weight="500", input_border_color_focus="#F38020", slider_color="#F38020", checkbox_background_color_selected="#F38020", link_text_color="#F38020", ) # Keep the light, neutral palette regardless of the visitor's OS dark-mode setting. FORCE_LIGHT = """ () => { const url = new URL(window.location); if (url.searchParams.get('__theme') !== 'light') { url.searchParams.set('__theme', 'light'); window.location.replace(url.href); } } """ PRESETS = { "Invoice": (INVOICE_STATE, _fmt(INVOICE_FIELDS)), "Forecast": (DEMAND_STATE, _fmt(DEMAND_FIELDS)), "Support ticket": (TICKET_STATE, _fmt(TICKET_FIELDS)), "Receipt image": (RECEIPT_STATE, _fmt(RECEIPT_FIELDS)), } with gr.Blocks(title="Clairic · Cloudflare") as demo: gr.HTML(HEADER) with gr.Row(equal_height=False): with gr.Column(scale=5): with gr.Group(elem_classes="cf-panel"): gr.HTML('
Source
') state = gr.Textbox(label="State (text or JSON)", lines=5, value=INVOICE_STATE, placeholder="Paste an email, invoice, log line, table, JSON…") image = gr.Image(label="Image (optional)", type="pil", height=240, sources=["upload", "clipboard"]) with gr.Group(elem_classes="cf-panel"): gr.HTML('
Schema
') fields = gr.Code(label="Fields (JSON)", language="json", value=_fmt(INVOICE_FIELDS), lines=12) with gr.Row(elem_classes="cf-presets"): preset_buttons = [gr.Button(name, size="sm", variant="secondary") for name in PRESETS] with gr.Accordion("Advanced options", open=False): reference_date = gr.Textbox(label="Reference date (ISO, optional)", placeholder="2026-11-18") include_quantiles = gr.Checkbox(label="Include quantiles for estimates / forecasts", value=False) run = gr.Button("Extract", variant="primary", elem_id="cf-run", size="lg") with gr.Column(scale=6): gr.HTML('
Answers
') out_html = gr.HTML(EMPTY_HTML) with gr.Accordion("Raw JSON response", open=False): out_json = gr.JSON(label="Response") for btn, (name, (st, fl)) in zip(preset_buttons, PRESETS.items()): btn.click(lambda s=st, f=fl: (s, f, None), outputs=[state, fields, image], api_visibility="private") run.click(extract, inputs=[state, fields, image, reference_date, include_quantiles], outputs=[out_html, out_json], api_name="extract", concurrency_limit=1) gr.Markdown("### Examples") gr.Examples( label="Text", examples=[ [INVOICE_STATE, _fmt(INVOICE_FIELDS)], [DEMAND_STATE, _fmt(DEMAND_FIELDS)], [TICKET_STATE, _fmt(TICKET_FIELDS)], ], inputs=[state, fields], outputs=[out_html, out_json], fn=extract, cache_examples=True, cache_mode="lazy", ) gr.Examples( label="Images (receipts from CORD-v2, CC-BY-4.0)", examples=[ [RECEIPT_STATE, _fmt(RECEIPT_FIELDS), "examples/receipt_clear.jpg"], [RECEIPT_STATE, _fmt(RECEIPT_FIELDS), "examples/receipt_crumpled.jpg"], ], inputs=[state, fields, image], outputs=[out_html, out_json], fn=extract, cache_examples=True, cache_mode="lazy", ) gr.HTML('') if __name__ == "__main__": demo.launch(theme=theme, css=CSS, js=FORCE_LIGHT, mcp_server=True)