File size: 27,845 Bytes
db1ae47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70381f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db1ae47
 
 
 
 
 
 
 
 
 
 
 
 
70381f4
db1ae47
 
70381f4
 
 
db1ae47
70381f4
db1ae47
70381f4
 
 
 
 
 
 
db1ae47
 
70381f4
 
 
db1ae47
70381f4
 
 
 
 
 
 
 
 
 
 
db1ae47
 
 
 
 
 
70381f4
db1ae47
 
 
70381f4
 
 
 
 
 
 
 
db1ae47
 
 
 
 
 
 
 
 
70381f4
db1ae47
 
 
 
 
70381f4
db1ae47
 
 
 
 
 
 
 
345bc73
db1ae47
 
345bc73
db1ae47
 
 
 
 
 
 
 
 
 
 
 
 
 
70381f4
 
 
 
 
 
db1ae47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345bc73
db1ae47
 
 
 
 
345bc73
 
 
 
 
 
 
 
db1ae47
 
 
 
 
e3d4a23
345bc73
 
 
 
 
 
 
 
 
 
 
 
 
e3d4a23
 
 
 
 
 
 
 
345bc73
 
 
 
 
e3d4a23
345bc73
 
 
e3d4a23
 
db1ae47
345bc73
db1ae47
345bc73
 
 
 
db1ae47
e3d4a23
345bc73
 
 
 
 
 
e3d4a23
345bc73
 
 
 
 
 
 
e3d4a23
345bc73
 
 
e3d4a23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db1ae47
 
 
 
 
 
 
345bc73
 
 
db1ae47
345bc73
db1ae47
345bc73
 
 
 
 
 
e3d4a23
 
 
 
 
 
 
345bc73
 
db1ae47
 
 
345bc73
db1ae47
 
345bc73
 
 
 
 
 
 
 
 
 
 
db1ae47
345bc73
 
 
 
db1ae47
 
 
 
e3d4a23
db1ae47
 
345bc73
db1ae47
 
 
 
 
345bc73
db1ae47
 
 
 
 
 
345bc73
db1ae47
345bc73
db1ae47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345bc73
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
580
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 '<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>'
    )


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: <=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 = """
<svg class="cf-logo" viewBox="0 0 120 56" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
  <path fill="#F38020" 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"/>
</svg>
"""

HEADER = f"""
<div class="cf-hero">
  <div class="cf-brand">{CF_LOGO}<span>Cloudflare</span></div>
  <h1>Clairic <span>27B</span></h1>
  <p>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.</p>
  <div class="cf-links">
    <a href="https://huggingface.co/Cloudflare/clairic" target="_blank">Model card</a>
    <a href="https://huggingface.co/Cloudflare/clairic-flash" target="_blank">Clairic Flash</a>
    <span class="cf-meta">Apache-2.0 · Multimodal · Structured output</span>
  </div>
</div>
"""

CSS = """
:root { --cf-orange:#F38020; --cf-orange-dark:#E06A10; --cf-text:#222; --cf-muted:#595959; --cf-line:#EEEEEE; }
.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; }

/* Borders: 1px solid hairlines in a very light neutral gray, never dashed, never thick. */
.gradio-container .block, .gradio-container .placeholder { border-style:solid !important; }
.gradio-container .block { border-color:var(--cf-line) !important; }
.gradio-container .cf-panel { border:1px solid var(--cf-line) !important; }
.gradio-container .cf-panel .block { border:none !important; }
.gradio-container .gallery, .gradio-container .gallery-item, .gradio-container .table-wrap,
.gradio-container th, .gradio-container td { border-width:1px !important; border-color:var(--cf-line) !important; }
.gradio-container label[data-testid="block-label"], .gradio-container .block-label { border:none !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:none; border-radius:6px; background:#FAFAFA; 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(min(250px, 100%), 1fr)); gap:12px; }
.cf-card { border-radius:6px; padding:14px; background:var(--block-background-fill); border:1px solid var(--cf-line); min-width:0; overflow-wrap:anywhere; }
.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:none; background:#F5F5F5; }
.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:#EEEEEE; 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(--cf-line); }
.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; }

/* Small screens: stack the two columns, no horizontal overflow, touch-friendly targets. */
html, body { overflow-x:hidden; }
@media (max-width: 768px) {
  .gradio-container { padding-left:12px !important; padding-right:12px !important; }
  .cf-main { flex-direction:column !important; flex-wrap:nowrap !important; gap:16px !important; }
  .cf-main > div { width:100% !important; min-width:0 !important; max-width:100% !important; flex:1 1 auto !important; }
  .cf-hero { padding:18px 0 16px; margin-bottom:12px; }
  .cf-brand { margin-bottom:12px; }
  .cf-hero h1 { font-size:26px; }
  .cf-hero p { font-size:14px; }
  .cf-links { gap:12px 16px; }
  .cf-presets { flex-wrap:wrap !important; gap:8px !important; }
  .cf-presets > button { flex:1 1 calc(50% - 8px) !important; min-width:calc(50% - 8px) !important; min-height:44px; }
  #cf-run { width:100%; min-height:48px; }
  .gradio-container textarea, .gradio-container input { font-size:16px !important; }
  .cf-grid { grid-template-columns:1fr; }
  .cf-results-head { flex-wrap:wrap; gap:6px; }
  .cf-stats { gap:16px; }
  .cf-empty { padding:32px 16px; }
  .gradio-container .table-wrap, .gradio-container .cm-editor { max-width:100%; overflow-x:auto; }
}
"""

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",
    border_color_primary="#EEEEEE",
    block_border_width="1px",
    block_border_color="#EEEEEE",
    block_label_border_width="0px",
    button_secondary_border_color="#EEEEEE",
    button_secondary_border_color_hover="#E0E0E0",
    table_border_color="#EEEEEE",
    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, elem_classes="cf-main"):
        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.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")

    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" target="_blank">'
            'Cloudflare/clairic</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=theme, css=CSS, js=FORCE_LIGHT, mcp_server=True)