File size: 29,295 Bytes
e938d78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a65eeb
196d84d
 
e938d78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
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-flash"


# ---------------------------------------------------------------------------
# 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.backend import ClairicFlashModel  # 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-flash",
                            max_length=65_273, max_concurrency=1)
RUNTIME = ClairicFlashModel.load(_settings, device="cuda")
MODEL = ClairicModel(RUNTIME, "clairic-flash")
print("Clairic Flash 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 '<span class="cf-null">null</span>'
    if isinstance(value, bool):
        return f'<span class="cf-bool cf-{str(value).lower()}">{str(value).lower()}</span>'
    if isinstance(value, list):
        if not value:
            return '<span class="cf-null">[ ]</span>'
        chips = "".join(f'<span class="cf-chip">{_value_inline(v)}</span>' for v in value)
        return f'<div class="cf-chips">{chips}</div>'
    if isinstance(value, dict):
        rows = "".join(f"<div><b>{_esc(k)}</b> {_value_inline(v)}</div>" for k, v in value.items())
        return f'<div class="cf-obj">{rows}</div>'
    return f'<span class="cf-val">{_esc(_num(value))}</span>'


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'<polygon class="cf-band" points="{top} {bot}"/>'
    line = " ".join(f"{X(i):.1f},{Y(v):.1f}" for i, v in enumerate(vals))
    dots = "".join(f'<circle cx="{X(i):.1f}" cy="{Y(v):.1f}" r="3.5"/>' for i, v in enumerate(vals))
    return f'<svg class="cf-spark" viewBox="0 0 {w} {h}">{poly}<polyline points="{line}"/>{dots}</svg>'


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 += '<div class="cf-sub">shaded band: 80% interval (p10–p90)</div>'
        elif "p10" in ans and "p90" in ans and not isinstance(ans["p10"], list):
            extra += (f'<div class="cf-range"><span>p10 {_esc(_num(ans["p10"]))}</span>'
                      f'<span>median {_esc(_num(ans.get("median", value)))}</span>'
                      f'<span>p90 {_esc(_num(ans["p90"]))}</span></div>')
        meters = ""
        if "confidence" in ans:
            c = _pct(ans["confidence"])
            meters += (f'<div class="cf-meter"><span>confidence</span><div class="cf-bar">'
                       f'<i style="width:{c:.1f}%"></i></div><em>{c:.1f}%</em></div>')
        if "p_null" in ans:
            p = _pct(ans["p_null"])
            meters += (f'<div class="cf-meter cf-meter-null"><span>p(null)</span><div class="cf-bar">'
                       f'<i style="width:{p:.1f}%"></i></div><em>{p:.1f}%</em></div>')
        flags = f'<span class="cf-type">{_esc(kind)}</span>' if kind else ""
        if ans.get("valid") is False:
            flags += '<span class="cf-flag">invalid</span>'
        if ans.get("complete") is False:
            flags += '<span class="cf-flag">incomplete</span>'
        cards.append(
            f'<div class="cf-card"><div class="cf-card-head"><code>{_esc(name)}</code><span>{flags}</span></div>'
            f'<div class="cf-card-body">{body}{extra}</div>{meters}</div>'
        )
    usage = response.get("usage", {})
    stats = "".join(
        f'<div class="cf-stat"><b>{_esc(v)}</b><span>{_esc(k.replace("_", " "))}</span></div>'
        for k, v in usage.items()
    )
    trunc = ('<span class="cf-flag">input truncated</span>' if response.get("truncated") else
             '<span class="cf-ok">● complete input</span>')
    return (
        f'<div class="cf-results"><div class="cf-results-head"><span>Structured answers</span>'
        f'<span class="cf-time">{trunc}<span>⚡ {elapsed:.1f}s</span></span></div>'
        f'<div class="cf-grid">{"".join(cards)}</div><div class="cf-stats">{stats}</div></div>'
    )


FIELD_TYPES = ["text", "number", "date", "time", "datetime", "duration", "boolean", "enum",
               "list", "estimate", "forecast"]


def add_field(fields_json: str, name: str, kind: str, instructions: str, options: str) -> str:
    """Append a typed field spec to the schema JSON (UI helper)."""
    name = (name or "").strip()
    if not name:
        raise gr.Error("Give the field a name.")
    try:
        fields = json.loads(fields_json or "{}")
        if not isinstance(fields, dict):
            raise ValueError
    except ValueError:
        raise gr.Error("The current schema is not a valid JSON object.")
    spec = {"type": kind}
    if kind == "list":
        spec["items"] = "text"
    elif kind == "enum":
        values = [v.strip() for v in (options or "").split(",") if v.strip()]
        if not values:
            raise gr.Error("Enum fields need at least one option.")
        spec["enum"] = values
    elif kind in ("estimate", "forecast"):
        spec.update({"min": 0, "max": 100})
        if kind == "forecast":
            spec.update({"horizon": 3, "interval": "day"})
    if instructions and instructions.strip():
        spec["instructions"] = instructions.strip()
    fields[name] = spec
    return _fmt(fields)


EMPTY_HTML = (
    '<div class="cf-empty"><div class="cf-empty-icon">{ }</div>'
    "<p>Pick a preset or describe your own fields, then hit <b>Extract</b>.</p>"
    "<p class='cf-sub'>Each field comes back typed, with a confidence score and a null probability.</p></div>"
)


# ---------------------------------------------------------------------------
# Inference
# ---------------------------------------------------------------------------
def _gpu_duration(state, fields_json, image=None, *args, **kwargs):
    # measured on this Space: 21.4s text on a cold worker (incl. Triton JIT), 4.3s warm text,
    # 24.1s single image on a cold worker, 9-11s warm
    return 28 if image is not None else 25


@spaces.GPU(duration=_gpu_duration)
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 Flash.

    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-flash] 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 = """
<svg class="cf-logo" viewBox="0 0 120 56" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
  <defs><linearGradient id="cfg" x1="0" x2="1"><stop offset="0" stop-color="#FBAD41"/><stop offset="1" stop-color="#F38020"/></linearGradient></defs>
  <path fill="url(#cfg)" d="M93 50H20.5a14 14 0 0 1-2.7-27.7A22 22 0 0 1 59 13.6 17 17 0 0 1 88.6 24 13 13 0 0 1 106 37a13 13 0 0 1-13 13z"/>
  <path fill="#fff" fill-opacity=".9" d="M38 36h46a2 2 0 0 1 0 4H38a2 2 0 0 1 0-4zm8-8h30a2 2 0 0 1 0 4H46a2 2 0 0 1 0-4z"/>
</svg>
"""

HEADER = f"""
<div class="cf-hero">
  <div class="cf-hero-glow"></div>
  <div class="cf-hero-row">
    {CF_LOGO}
    <div>
      <div class="cf-eyebrow">CLOUDFLARE · SYSTEM ONE: EXTRACT</div>
      <h1>Clairic Flash <span>9B</span></h1>
      <p>Turn messy text and images into <b>typed, scored</b> structured answers. Declare a schema of fields —
      text, numbers, dates, enums, lists, estimates, forecasts — and get back values with confidence and null probabilities.</p>
      <div class="cf-links">
        <a href="https://huggingface.co/Cloudflare/clairic-flash" target="_blank">Model card ↗</a>
        <a href="https://huggingface.co/Cloudflare/clairic" target="_blank">Clairic 27B ↗</a>
        <span class="cf-pill">Apache-2.0</span><span class="cf-pill">Multimodal</span><span class="cf-pill">Structured output</span>
      </div>
    </div>
  </div>
</div>
"""

CSS = """
:root { --cf-orange:#F38020; --cf-amber:#FBAD41; --cf-ink:#0f1115; --cf-ink2:#1b1e24; --cf-line:rgba(243,128,32,.28); }
.gradio-container { max-width: 1240px !important; margin: 0 auto !important; font-family: 'Inter', system-ui, sans-serif; }
.dark .gradio-container { color: var(--body-text-color); }
.cf-hero { position:relative; overflow:hidden; border-radius:20px; padding:30px 34px; margin-bottom:10px;
  background: radial-gradient(1200px 300px at 85% -20%, rgba(243,128,32,.55), transparent 60%),
              linear-gradient(135deg, #0f1115 0%, #1b1e24 60%, #2a1b10 100%);
  border:1px solid var(--cf-line); color:#f5f5f5; }
.cf-hero-glow { position:absolute; right:-80px; bottom:-120px; width:340px; height:340px; border-radius:50%;
  background: radial-gradient(circle, rgba(251,173,65,.35), transparent 70%); filter: blur(10px); }
.cf-hero-row { position:relative; display:flex; gap:26px; align-items:center; }
.cf-logo { width:120px; flex:none; filter: drop-shadow(0 6px 24px rgba(243,128,32,.55)); }
.cf-eyebrow { font-size:12px; letter-spacing:.18em; color:var(--cf-amber); font-weight:700; }
.cf-hero h1 { font-size:46px; line-height:1.05; margin:6px 0 8px; color:#fff; font-weight:800; letter-spacing:-.02em; }
.cf-hero h1 span { font-size:20px; vertical-align:middle; padding:3px 10px; border-radius:999px; margin-left:6px;
  background: linear-gradient(90deg, var(--cf-amber), var(--cf-orange)); color:#1a0e03; font-weight:800; }
.cf-hero p { color:#d6d8de; max-width:760px; margin:0 0 12px; font-size:15px; }
.cf-hero p b { color:#fff; }
.cf-links { display:flex; gap:10px; flex-wrap:wrap; align-items:center; }
.cf-links a { color:#fff !important; text-decoration:none; font-weight:600; font-size:13px; padding:6px 12px; border-radius:10px;
  background: rgba(243,128,32,.18); border:1px solid rgba(243,128,32,.5); }
.cf-links a:hover { background: rgba(243,128,32,.35); }
.cf-pill { font-size:12px; color:#c9ccd3; padding:5px 10px; border-radius:999px; border:1px solid rgba(255,255,255,.15); }
@media (max-width: 720px) { .cf-hero-row { flex-direction:column; align-items:flex-start; } .cf-hero h1 { font-size:36px; } }

.cf-panel { border-radius:16px !important; border:1px solid var(--cf-line) !important; }
.cf-section { font-size:12px; letter-spacing:.14em; font-weight:800; color:var(--cf-orange); margin:2px 0 4px; }
#cf-run { background: linear-gradient(90deg, var(--cf-amber), var(--cf-orange)) !important; color:#1a0e03 !important;
  font-weight:800 !important; font-size:16px !important; border:none !important; box-shadow: 0 8px 24px rgba(243,128,32,.35); }
#cf-run:hover { filter: brightness(1.06); transform: translateY(-1px); }
.cf-presets button { border-radius:999px !important; }

.cf-empty { text-align:center; padding:56px 20px; border:1.5px dashed var(--cf-line); border-radius:16px; color: var(--body-text-color-subdued); }
.cf-empty-icon { font-family: ui-monospace, monospace; font-size:42px; font-weight:800; color: var(--cf-orange); }
.cf-results-head { display:flex; justify-content:space-between; align-items:center; font-weight:800; margin-bottom:10px; font-size:15px; }
.cf-time { display:flex; gap:10px; align-items:center; font-weight:600; font-size:13px; color: var(--body-text-color-subdued); }
.cf-grid { display:grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap:12px; }
.cf-card { border-radius:14px; padding:14px 14px 12px; background: var(--block-background-fill);
  border:1px solid var(--border-color-primary); border-top:3px solid var(--cf-orange);
  box-shadow: 0 1px 2px rgba(0,0,0,.05); animation: cfIn .35s ease both; }
@keyframes cfIn { from { opacity:0; transform: translateY(6px);} to { opacity:1; transform:none; } }
.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:700; color: var(--cf-orange); background: rgba(243,128,32,.10); padding:2px 8px; border-radius:6px; }
.cf-card-body { min-height:30px; margin-bottom:10px; }
.cf-val { font-size:20px; font-weight:700; 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:800; padding:2px 10px; border-radius:8px; }
.cf-true { background: rgba(34,197,94,.15); color:#16a34a; } .cf-false { background: rgba(239,68,68,.12); color:#dc2626; }
.cf-chips { display:flex; flex-wrap:wrap; gap:6px; }
.cf-chip { font-size:13px; font-weight:600; padding:4px 10px; border-radius:999px; background: rgba(243,128,32,.12); border:1px solid rgba(243,128,32,.35); }
.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:2.5; stroke-linejoin:round; }
.cf-spark .cf-band { fill: rgba(243,128,32,.16); stroke:none; }
.cf-spark circle { fill: var(--cf-amber); stroke: var(--cf-orange); stroke-width:1.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:700; color: var(--body-text-color); }
.cf-bar { height:7px; border-radius:99px; background: var(--border-color-primary); overflow:hidden; }
.cf-bar i { display:block; height:100%; border-radius:99px; background: linear-gradient(90deg, var(--cf-amber), var(--cf-orange)); }
.cf-meter-null .cf-bar i { background: linear-gradient(90deg, #9aa0aa, #5f6670); }
.cf-type { font-size:10px; font-weight:700; text-transform:uppercase; letter-spacing:.08em; padding:2px 7px; border-radius:6px; border:1px solid var(--border-color-primary); color: var(--body-text-color-subdued); margin-left:4px; }
.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(--cf-orange); font-weight:700; }
.cf-flag { margin-left:4px;  font-size:11px; font-weight:700; padding:2px 8px; border-radius:6px; background: rgba(239,68,68,.12); color:#dc2626; }
.cf-ok { color:#16a34a; }
.cf-stats { display:flex; flex-wrap:wrap; gap:8px; margin-top:14px; }
.cf-stat { flex:1; min-width:90px; padding:8px 10px; border-radius:10px; background: rgba(243,128,32,.07); border:1px solid rgba(243,128,32,.2); }
.cf-stat b { display:block; font-size:16px; } .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:10px; }
.cf-footer a { color: var(--cf-orange) !important; }
"""

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 Flash · 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('<div class="cf-section" style="padding:10px 12px 0">① SOURCE</div>')
                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('<div class="cf-section" style="padding:10px 12px 0">② SCHEMA</div>')
                fields = gr.Code(label="Fields (JSON)", language="json", value=_fmt(INVOICE_FIELDS), lines=12)
                with gr.Accordion("➕ Add a field", open=False):
                    with gr.Row():
                        new_name = gr.Textbox(label="Name", placeholder="due_date", scale=2)
                        new_type = gr.Dropdown(FIELD_TYPES, value="text", label="Type", scale=2)
                    new_instr = gr.Textbox(label="Instructions (optional)", placeholder="What should this field contain?")
                    new_opts = gr.Textbox(label="Enum options (comma-separated, enum only)",
                                          placeholder="low, medium, high")
                    with gr.Row():
                        add_btn = gr.Button("Add field to schema", size="sm")
                        clear_btn = gr.Button("Clear schema", size="sm", variant="stop")
            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('<div class="cf-section">③ ANSWERS</div>')
            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")

    add_btn.click(add_field, inputs=[fields, new_name, new_type, new_instr, new_opts], outputs=fields,
                  api_visibility="private")
    clear_btn.click(lambda: "{}", outputs=fields, 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('<div class="cf-footer">Built on <a href="https://huggingface.co/Cloudflare/clairic-flash" target="_blank">'
            'Cloudflare/clairic-flash</a> · runs on ZeroGPU · example receipts from '
            '<a href="https://huggingface.co/datasets/naver-clova-ix/cord-v2" target="_blank">CORD-v2</a> (CC-BY-4.0)</div>')

if __name__ == "__main__":
    demo.launch(theme=gr.themes.Citrus(), css=CSS, mcp_server=True)