Spaces:
Running on Zero
Running on Zero
Upload folder using huggingface_hub
Browse files- .gitattributes +1 -0
- README.md +19 -6
- app.py +577 -0
- examples/receipt_clear.jpg +0 -0
- examples/receipt_crumpled.jpg +3 -0
- requirements.txt +15 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
examples/receipt_crumpled.jpg filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,13 +1,26 @@
|
|
| 1 |
---
|
| 2 |
title: Clairic Flash
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.32.0
|
| 8 |
-
python_version: '3.12'
|
| 9 |
app_file: app.py
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Clairic Flash
|
| 3 |
+
emoji: ⚡
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.32.0
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
+
short_description: Cloudflare Clairic Flash 9B typed structured extraction
|
| 10 |
+
python_version: "3.12"
|
| 11 |
+
startup_duration_timeout: 1h
|
| 12 |
+
models:
|
| 13 |
+
- Cloudflare/clairic-flash
|
| 14 |
---
|
| 15 |
|
| 16 |
+
# Clairic Flash · System One: Extract
|
| 17 |
+
|
| 18 |
+
Gradio demo for [Cloudflare/clairic-flash](https://huggingface.co/Cloudflare/clairic-flash), a 9B multimodal
|
| 19 |
+
model (Qwen3.5-9B base + typed output head) that turns a text/JSON state (and optional images) plus a schema of
|
| 20 |
+
typed fields into structured, scored answers (confidence, p_null, optional quantiles).
|
| 21 |
+
|
| 22 |
+
Runs on ZeroGPU. The release runtime's Hopper-only kernels (FA3, DeepGEMM, FlashInfer RMSNorm, CUDA-graph
|
| 23 |
+
decode) are replaced by SDPA and numerically equivalent PyTorch ops so it runs on Blackwell sm_120.
|
| 24 |
+
|
| 25 |
+
Example receipt images come from the [CORD-v2 dataset](https://huggingface.co/datasets/naver-clova-ix/cord-v2)
|
| 26 |
+
by NAVER Clova AI, licensed CC-BY-4.0.
|
app.py
ADDED
|
@@ -0,0 +1,577 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
+
import html # noqa: E402
|
| 9 |
+
import json # noqa: E402
|
| 10 |
+
import sys # noqa: E402
|
| 11 |
+
import time # noqa: E402
|
| 12 |
+
import types # noqa: E402
|
| 13 |
+
|
| 14 |
+
import gradio as gr # noqa: E402
|
| 15 |
+
import torch # noqa: E402
|
| 16 |
+
from huggingface_hub import snapshot_download # noqa: E402
|
| 17 |
+
|
| 18 |
+
MODEL_ID = "Cloudflare/clairic-flash"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# ---------------------------------------------------------------------------
|
| 22 |
+
# ZeroGPU compatibility shims
|
| 23 |
+
# ---------------------------------------------------------------------------
|
| 24 |
+
# The release runtime targets Hopper (sm_90) and calls DeepGEMM / FlashInfer
|
| 25 |
+
# for a couple of BF16 GEMMs and Gemma-style RMSNorms. ZeroGPU runs on
|
| 26 |
+
# Blackwell sm_120, where DeepGEMM has no kernels, so we provide numerically
|
| 27 |
+
# equivalent pure-PyTorch implementations of exactly the functions it uses.
|
| 28 |
+
def _install_shims() -> None:
|
| 29 |
+
dg = types.ModuleType("deep_gemm")
|
| 30 |
+
|
| 31 |
+
def bf16_gemm_nn(a, b, out):
|
| 32 |
+
torch.matmul(a, b, out=out)
|
| 33 |
+
|
| 34 |
+
dg.bf16_gemm_nn = bf16_gemm_nn
|
| 35 |
+
dg.get_num_sms = lambda: torch.cuda.get_device_properties(0).multi_processor_count
|
| 36 |
+
dg.get_tc_util = lambda: 100
|
| 37 |
+
dg.get_pdl = lambda: False
|
| 38 |
+
sys.modules["deep_gemm"] = dg
|
| 39 |
+
|
| 40 |
+
fi = types.ModuleType("flashinfer")
|
| 41 |
+
norm = types.ModuleType("flashinfer.norm")
|
| 42 |
+
|
| 43 |
+
def _gemma(x32, weight, eps):
|
| 44 |
+
var = x32.pow(2).mean(-1, keepdim=True)
|
| 45 |
+
return x32 * torch.rsqrt(var + eps) * (1.0 + weight.float())
|
| 46 |
+
|
| 47 |
+
def gemma_rmsnorm(x, weight, eps=1e-6):
|
| 48 |
+
return _gemma(x.float(), weight, eps).to(x.dtype)
|
| 49 |
+
|
| 50 |
+
def gemma_fused_add_rmsnorm(x, residual, weight, eps=1e-6):
|
| 51 |
+
total = x.float() + residual.float()
|
| 52 |
+
residual.copy_(total)
|
| 53 |
+
x.copy_(_gemma(total, weight, eps))
|
| 54 |
+
|
| 55 |
+
norm.gemma_rmsnorm = gemma_rmsnorm
|
| 56 |
+
norm.gemma_fused_add_rmsnorm = gemma_fused_add_rmsnorm
|
| 57 |
+
fi.norm = norm
|
| 58 |
+
sys.modules["flashinfer"] = fi
|
| 59 |
+
sys.modules["flashinfer.norm"] = norm
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
_install_shims()
|
| 63 |
+
|
| 64 |
+
# ---------------------------------------------------------------------------
|
| 65 |
+
# Model loading (module scope, eager .to("cuda"))
|
| 66 |
+
# ---------------------------------------------------------------------------
|
| 67 |
+
MODEL_PATH = snapshot_download(MODEL_ID)
|
| 68 |
+
sys.path.insert(0, MODEL_PATH)
|
| 69 |
+
|
| 70 |
+
import xgrammar as xgr # noqa: E402
|
| 71 |
+
from transformers import Qwen3_5ForConditionalGeneration # noqa: E402
|
| 72 |
+
|
| 73 |
+
from clairic_model import ClairicModel # noqa: E402
|
| 74 |
+
from clairic_runtime import native as _native # noqa: E402
|
| 75 |
+
from clairic_runtime.backend import ClairicFlashModel # noqa: E402
|
| 76 |
+
from clairic_runtime.math import install as _install_math # noqa: E402
|
| 77 |
+
from clairic_runtime.readout import load_head # noqa: E402
|
| 78 |
+
from clairic_runtime.settings import RuntimeSettings # noqa: E402
|
| 79 |
+
|
| 80 |
+
_TOKENIZER_INFO = {}
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def _zerogpu_engine_load(cls, path, tokenizer, device="cuda", dtype=None, batch_size=None,
|
| 84 |
+
text_batch_size=None, batch_token_budget=None, **_):
|
| 85 |
+
"""Same as the release loader, minus Hopper-only FA3 and CUDA-graph capture."""
|
| 86 |
+
|
| 87 |
+
class _NativeQwen3_5ForConditionalGeneration(Qwen3_5ForConditionalGeneration):
|
| 88 |
+
_keep_in_fp32_modules_strict = ["A_log"]
|
| 89 |
+
|
| 90 |
+
backbone = _NativeQwen3_5ForConditionalGeneration.from_pretrained(
|
| 91 |
+
path,
|
| 92 |
+
dtype=torch.bfloat16,
|
| 93 |
+
local_files_only=True,
|
| 94 |
+
attn_implementation={"text_config": "sdpa", "vision_config": "sdpa"},
|
| 95 |
+
)
|
| 96 |
+
backbone.to("cuda")
|
| 97 |
+
head = load_head(path, match_bfloat16=True).float().eval().to("cuda")
|
| 98 |
+
engine = cls(backbone, head, tokenizer, batch_size=batch_size,
|
| 99 |
+
text_batch_size=text_batch_size, batch_token_budget=batch_token_budget)
|
| 100 |
+
_TOKENIZER_INFO["info"] = xgr.TokenizerInfo.from_huggingface(
|
| 101 |
+
tokenizer,
|
| 102 |
+
vocab_size=backbone.get_output_embeddings().weight.shape[0],
|
| 103 |
+
stop_token_ids=[engine.eos],
|
| 104 |
+
)
|
| 105 |
+
return engine
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
_native.TransformersEngine.load = classmethod(_zerogpu_engine_load)
|
| 109 |
+
|
| 110 |
+
_settings = RuntimeSettings(model_path=MODEL_PATH, version="1", model_id="clairic-flash",
|
| 111 |
+
max_length=65_273, max_concurrency=1)
|
| 112 |
+
RUNTIME = ClairicFlashModel.load(_settings, device="cuda")
|
| 113 |
+
MODEL = ClairicModel(RUNTIME, "clairic-flash")
|
| 114 |
+
print("Clairic Flash loaded.", flush=True)
|
| 115 |
+
|
| 116 |
+
_WORKER_PID = None
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def _prepare_worker() -> None:
|
| 120 |
+
"""Re-create thread pools / grammar compiler / fused math inside the GPU fork."""
|
| 121 |
+
global _WORKER_PID
|
| 122 |
+
if _WORKER_PID == os.getpid():
|
| 123 |
+
return
|
| 124 |
+
engine = RUNTIME.engine
|
| 125 |
+
engine._worker = concurrent.futures.ThreadPoolExecutor(max_workers=1, thread_name_prefix="clairic-model")
|
| 126 |
+
RUNTIME._encoder = concurrent.futures.ThreadPoolExecutor(max_workers=1, thread_name_prefix="clairic-encode")
|
| 127 |
+
engine.compiler = xgr.GrammarCompiler(_TOKENIZER_INFO["info"], max_threads=2)
|
| 128 |
+
if engine._restore_math is None:
|
| 129 |
+
engine._restore_math = _install_math(engine.backbone, engine.head)
|
| 130 |
+
_WORKER_PID = os.getpid()
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
# ---------------------------------------------------------------------------
|
| 134 |
+
# Presets (from the model card) and rendering helpers
|
| 135 |
+
# ---------------------------------------------------------------------------
|
| 136 |
+
INVOICE_STATE = (
|
| 137 |
+
"Invoice from Northwind Supply. Issued 2026-09-02. "
|
| 138 |
+
"Items: Pens; Paper. Total: $1080.00."
|
| 139 |
+
)
|
| 140 |
+
INVOICE_FIELDS = {
|
| 141 |
+
"vendor": {"type": "text", "instructions": "The invoice vendor."},
|
| 142 |
+
"total": {"type": "number", "decimals": 2},
|
| 143 |
+
"issued": {"type": "date"},
|
| 144 |
+
"items": {"type": "list", "items": "text", "instructions": "The purchased item names, in order."},
|
| 145 |
+
}
|
| 146 |
+
DEMAND_STATE = "Daily demand for the last seven days: 41, 45, 43, 47, 49, 46, 48."
|
| 147 |
+
DEMAND_FIELDS = {
|
| 148 |
+
"next_day": {"type": "estimate", "min": 0, "max": 100},
|
| 149 |
+
"next_three_days": {"type": "forecast", "min": 0, "max": 100, "horizon": 3, "interval": "day"},
|
| 150 |
+
}
|
| 151 |
+
RECEIPT_STATE = "Read the attached receipt."
|
| 152 |
+
RECEIPT_FIELDS = {
|
| 153 |
+
"items": {"type": "list", "items": "text", "instructions": "The purchased item names, in order."},
|
| 154 |
+
"total": {"type": "number", "source_only": True, "instructions": "The receipt total."},
|
| 155 |
+
"paid_cash": {"type": "boolean", "instructions": "Whether the customer paid in cash."},
|
| 156 |
+
}
|
| 157 |
+
TICKET_STATE = (
|
| 158 |
+
"Hi team, our checkout page has been returning 502 errors since about 14:20 UTC today. "
|
| 159 |
+
"Roughly 30% of EU customers are affected and we're losing orders. Account: ACME-4471. "
|
| 160 |
+
"Please escalate - Priya, Head of Ops"
|
| 161 |
+
)
|
| 162 |
+
TICKET_FIELDS = {
|
| 163 |
+
"category": {"type": "enum", "enum": ["billing", "outage", "feature_request", "security", "other"]},
|
| 164 |
+
"urgent": {"type": "boolean", "instructions": "Whether the issue needs immediate attention."},
|
| 165 |
+
"account_id": {"type": "text"},
|
| 166 |
+
"share_affected": {"type": "number", "unit": "percent", "instructions": "Share of customers affected."},
|
| 167 |
+
"reporter": {"type": "text", "instructions": "Name of the person reporting."},
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def _fmt(fields: dict) -> str:
|
| 172 |
+
return json.dumps(fields, indent=2)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def _esc(value) -> str:
|
| 176 |
+
return html.escape(str(value))
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def _pct(p) -> float:
|
| 180 |
+
try:
|
| 181 |
+
return max(0.0, min(1.0, float(p))) * 100.0
|
| 182 |
+
except (TypeError, ValueError):
|
| 183 |
+
return 0.0
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def _num(v) -> str:
|
| 187 |
+
if isinstance(v, bool):
|
| 188 |
+
return str(v).lower()
|
| 189 |
+
if isinstance(v, float):
|
| 190 |
+
return f"{v:,.2f}".rstrip("0").rstrip(".") if abs(v) < 1e6 else f"{v:,.0f}"
|
| 191 |
+
if isinstance(v, int):
|
| 192 |
+
return f"{v:,}"
|
| 193 |
+
return str(v)
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def _value_inline(value) -> str:
|
| 197 |
+
if isinstance(value, dict):
|
| 198 |
+
if "value" in value:
|
| 199 |
+
return _esc(_num(value["value"]))
|
| 200 |
+
return _esc(json.dumps(value, ensure_ascii=False))
|
| 201 |
+
return _esc(_num(value))
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def _value_html(value) -> str:
|
| 205 |
+
if value is None:
|
| 206 |
+
return '<span class="cf-null">null</span>'
|
| 207 |
+
if isinstance(value, bool):
|
| 208 |
+
return f'<span class="cf-bool cf-{str(value).lower()}">{str(value).lower()}</span>'
|
| 209 |
+
if isinstance(value, list):
|
| 210 |
+
if not value:
|
| 211 |
+
return '<span class="cf-null">[ ]</span>'
|
| 212 |
+
chips = "".join(f'<span class="cf-chip">{_value_inline(v)}</span>' for v in value)
|
| 213 |
+
return f'<div class="cf-chips">{chips}</div>'
|
| 214 |
+
if isinstance(value, dict):
|
| 215 |
+
rows = "".join(f"<div><b>{_esc(k)}</b> {_value_inline(v)}</div>" for k, v in value.items())
|
| 216 |
+
return f'<div class="cf-obj">{rows}</div>'
|
| 217 |
+
return f'<span class="cf-val">{_esc(_num(value))}</span>'
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def _floats(seq):
|
| 221 |
+
out = []
|
| 222 |
+
for v in seq or []:
|
| 223 |
+
try:
|
| 224 |
+
out.append(float(v))
|
| 225 |
+
except (TypeError, ValueError):
|
| 226 |
+
return []
|
| 227 |
+
return out
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
def _spark(vals, lows=None, highs=None) -> str:
|
| 231 |
+
vals = _floats(vals)
|
| 232 |
+
lows, highs = _floats(lows), _floats(highs)
|
| 233 |
+
if len(vals) < 2:
|
| 234 |
+
return ""
|
| 235 |
+
band = len(lows) == len(vals) == len(highs)
|
| 236 |
+
every = vals + (lows + highs if band else [])
|
| 237 |
+
lo, hi = min(every), max(every)
|
| 238 |
+
span = (hi - lo) or 1.0
|
| 239 |
+
w, h = 240, 70
|
| 240 |
+
X = lambda i: 12 + i * (w - 24) / (len(vals) - 1)
|
| 241 |
+
Y = lambda v: h - 10 - (v - lo) / span * (h - 20)
|
| 242 |
+
poly = ""
|
| 243 |
+
if band:
|
| 244 |
+
top = " ".join(f"{X(i):.1f},{Y(v):.1f}" for i, v in enumerate(highs))
|
| 245 |
+
bot = " ".join(f"{X(i):.1f},{Y(v):.1f}" for i, v in reversed(list(enumerate(lows))))
|
| 246 |
+
poly = f'<polygon class="cf-band" points="{top} {bot}"/>'
|
| 247 |
+
line = " ".join(f"{X(i):.1f},{Y(v):.1f}" for i, v in enumerate(vals))
|
| 248 |
+
dots = "".join(f'<circle cx="{X(i):.1f}" cy="{Y(v):.1f}" r="3.5"/>' for i, v in enumerate(vals))
|
| 249 |
+
return f'<svg class="cf-spark" viewBox="0 0 {w} {h}">{poly}<polyline points="{line}"/>{dots}</svg>'
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
def render_answers(response: dict, elapsed: float) -> str:
|
| 253 |
+
cards = []
|
| 254 |
+
for name, ans in response.get("answers", {}).items():
|
| 255 |
+
ans = ans if isinstance(ans, dict) else {"value": ans}
|
| 256 |
+
kind = ans.get("type", "")
|
| 257 |
+
value = ans.get("value")
|
| 258 |
+
body = _value_html(value)
|
| 259 |
+
extra = ""
|
| 260 |
+
if kind == "forecast" and isinstance(value, list):
|
| 261 |
+
extra += _spark(value, ans.get("p10"), ans.get("p90"))
|
| 262 |
+
if isinstance(ans.get("p10"), list):
|
| 263 |
+
extra += '<div class="cf-sub">shaded band: 80% interval (p10–p90)</div>'
|
| 264 |
+
elif "p10" in ans and "p90" in ans and not isinstance(ans["p10"], list):
|
| 265 |
+
extra += (f'<div class="cf-range"><span>p10 {_esc(_num(ans["p10"]))}</span>'
|
| 266 |
+
f'<span>median {_esc(_num(ans.get("median", value)))}</span>'
|
| 267 |
+
f'<span>p90 {_esc(_num(ans["p90"]))}</span></div>')
|
| 268 |
+
meters = ""
|
| 269 |
+
if "confidence" in ans:
|
| 270 |
+
c = _pct(ans["confidence"])
|
| 271 |
+
meters += (f'<div class="cf-meter"><span>confidence</span><div class="cf-bar">'
|
| 272 |
+
f'<i style="width:{c:.1f}%"></i></div><em>{c:.1f}%</em></div>')
|
| 273 |
+
if "p_null" in ans:
|
| 274 |
+
p = _pct(ans["p_null"])
|
| 275 |
+
meters += (f'<div class="cf-meter cf-meter-null"><span>p(null)</span><div class="cf-bar">'
|
| 276 |
+
f'<i style="width:{p:.1f}%"></i></div><em>{p:.1f}%</em></div>')
|
| 277 |
+
flags = f'<span class="cf-type">{_esc(kind)}</span>' if kind else ""
|
| 278 |
+
if ans.get("valid") is False:
|
| 279 |
+
flags += '<span class="cf-flag">invalid</span>'
|
| 280 |
+
if ans.get("complete") is False:
|
| 281 |
+
flags += '<span class="cf-flag">incomplete</span>'
|
| 282 |
+
cards.append(
|
| 283 |
+
f'<div class="cf-card"><div class="cf-card-head"><code>{_esc(name)}</code><span>{flags}</span></div>'
|
| 284 |
+
f'<div class="cf-card-body">{body}{extra}</div>{meters}</div>'
|
| 285 |
+
)
|
| 286 |
+
usage = response.get("usage", {})
|
| 287 |
+
stats = "".join(
|
| 288 |
+
f'<div class="cf-stat"><b>{_esc(v)}</b><span>{_esc(k.replace("_", " "))}</span></div>'
|
| 289 |
+
for k, v in usage.items()
|
| 290 |
+
)
|
| 291 |
+
trunc = ('<span class="cf-flag">input truncated</span>' if response.get("truncated") else
|
| 292 |
+
'<span class="cf-ok">● complete input</span>')
|
| 293 |
+
return (
|
| 294 |
+
f'<div class="cf-results"><div class="cf-results-head"><span>Structured answers</span>'
|
| 295 |
+
f'<span class="cf-time">{trunc}<span>⚡ {elapsed:.1f}s</span></span></div>'
|
| 296 |
+
f'<div class="cf-grid">{"".join(cards)}</div><div class="cf-stats">{stats}</div></div>'
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
FIELD_TYPES = ["text", "number", "date", "time", "datetime", "duration", "boolean", "enum",
|
| 301 |
+
"list", "estimate", "forecast"]
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
def add_field(fields_json: str, name: str, kind: str, instructions: str, options: str) -> str:
|
| 305 |
+
"""Append a typed field spec to the schema JSON (UI helper)."""
|
| 306 |
+
name = (name or "").strip()
|
| 307 |
+
if not name:
|
| 308 |
+
raise gr.Error("Give the field a name.")
|
| 309 |
+
try:
|
| 310 |
+
fields = json.loads(fields_json or "{}")
|
| 311 |
+
if not isinstance(fields, dict):
|
| 312 |
+
raise ValueError
|
| 313 |
+
except ValueError:
|
| 314 |
+
raise gr.Error("The current schema is not a valid JSON object.")
|
| 315 |
+
spec = {"type": kind}
|
| 316 |
+
if kind == "list":
|
| 317 |
+
spec["items"] = "text"
|
| 318 |
+
elif kind == "enum":
|
| 319 |
+
values = [v.strip() for v in (options or "").split(",") if v.strip()]
|
| 320 |
+
if not values:
|
| 321 |
+
raise gr.Error("Enum fields need at least one option.")
|
| 322 |
+
spec["enum"] = values
|
| 323 |
+
elif kind in ("estimate", "forecast"):
|
| 324 |
+
spec.update({"min": 0, "max": 100})
|
| 325 |
+
if kind == "forecast":
|
| 326 |
+
spec.update({"horizon": 3, "interval": "day"})
|
| 327 |
+
if instructions and instructions.strip():
|
| 328 |
+
spec["instructions"] = instructions.strip()
|
| 329 |
+
fields[name] = spec
|
| 330 |
+
return _fmt(fields)
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
EMPTY_HTML = (
|
| 334 |
+
'<div class="cf-empty"><div class="cf-empty-icon">{ }</div>'
|
| 335 |
+
"<p>Pick a preset or describe your own fields, then hit <b>Extract</b>.</p>"
|
| 336 |
+
"<p class='cf-sub'>Each field comes back typed, with a confidence score and a null probability.</p></div>"
|
| 337 |
+
)
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
# ---------------------------------------------------------------------------
|
| 341 |
+
# Inference
|
| 342 |
+
# ---------------------------------------------------------------------------
|
| 343 |
+
def _gpu_duration(state, fields_json, image=None, *args, **kwargs):
|
| 344 |
+
# measured: <=22.4s text (cold worker incl. Triton JIT), <=13.9s single image
|
| 345 |
+
return 40 if image is not None else 26
|
| 346 |
+
|
| 347 |
+
|
| 348 |
+
@spaces.GPU(duration=_gpu_duration)
|
| 349 |
+
def extract(state: str, fields_json: str, image=None, reference_date: str = "",
|
| 350 |
+
include_quantiles: bool = False):
|
| 351 |
+
"""Extract typed, scored structured answers from text and/or an image with Clairic Flash.
|
| 352 |
+
|
| 353 |
+
Args:
|
| 354 |
+
state: Source text (or JSON) to read from. May be empty when an image is provided.
|
| 355 |
+
fields_json: JSON object mapping field names to typed field specs, e.g.
|
| 356 |
+
{"total": {"type": "number", "decimals": 2}}. Supported types: text, number, date,
|
| 357 |
+
time, datetime, duration, boolean, enum, object, list, compute, estimate, forecast.
|
| 358 |
+
image: Optional image (e.g. a receipt or document photo).
|
| 359 |
+
reference_date: Optional ISO date (YYYY-MM-DD) used for relative date instructions.
|
| 360 |
+
include_quantiles: Return quantiles for estimate / forecast fields.
|
| 361 |
+
|
| 362 |
+
Returns:
|
| 363 |
+
A rendered HTML summary and the raw JSON response (answers, truncated, usage).
|
| 364 |
+
"""
|
| 365 |
+
try:
|
| 366 |
+
fields = json.loads(fields_json)
|
| 367 |
+
except json.JSONDecodeError as exc:
|
| 368 |
+
raise gr.Error(f"Fields must be valid JSON: {exc}")
|
| 369 |
+
if not isinstance(fields, dict) or not fields:
|
| 370 |
+
raise gr.Error("Fields must be a non-empty JSON object of field specs.")
|
| 371 |
+
request = {"state": state or "", "fields": fields,
|
| 372 |
+
"options": {"include_quantiles": bool(include_quantiles)}}
|
| 373 |
+
if image is not None:
|
| 374 |
+
request["images"] = [image]
|
| 375 |
+
if reference_date and reference_date.strip():
|
| 376 |
+
request["reference_date"] = reference_date.strip()
|
| 377 |
+
if not request["state"].strip() and image is None:
|
| 378 |
+
raise gr.Error("Provide some source text or an image.")
|
| 379 |
+
|
| 380 |
+
_prepare_worker()
|
| 381 |
+
t0 = time.perf_counter()
|
| 382 |
+
try:
|
| 383 |
+
response = MODEL.predict(request)
|
| 384 |
+
except Exception as exc: # validation / request errors → readable message
|
| 385 |
+
raise gr.Error(f"{type(exc).__name__}: {exc}")
|
| 386 |
+
elapsed = time.perf_counter() - t0
|
| 387 |
+
print(f"[clairic-flash] inference {elapsed:.2f}s fields={list(fields)} image={image is not None}", flush=True)
|
| 388 |
+
response = json.loads(json.dumps(response, default=str))
|
| 389 |
+
return render_answers(response, elapsed), response
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
# ---------------------------------------------------------------------------
|
| 393 |
+
# UI
|
| 394 |
+
# ---------------------------------------------------------------------------
|
| 395 |
+
CF_LOGO = """
|
| 396 |
+
<svg class="cf-logo" viewBox="0 0 120 56" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
|
| 397 |
+
<defs><linearGradient id="cfg" x1="0" x2="1"><stop offset="0" stop-color="#FBAD41"/><stop offset="1" stop-color="#F38020"/></linearGradient></defs>
|
| 398 |
+
<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"/>
|
| 399 |
+
<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"/>
|
| 400 |
+
</svg>
|
| 401 |
+
"""
|
| 402 |
+
|
| 403 |
+
HEADER = f"""
|
| 404 |
+
<div class="cf-hero">
|
| 405 |
+
<div class="cf-hero-glow"></div>
|
| 406 |
+
<div class="cf-hero-row">
|
| 407 |
+
{CF_LOGO}
|
| 408 |
+
<div>
|
| 409 |
+
<div class="cf-eyebrow">CLOUDFLARE · SYSTEM ONE: EXTRACT</div>
|
| 410 |
+
<h1>Clairic Flash <span>9B</span></h1>
|
| 411 |
+
<p>Turn messy text and images into <b>typed, scored</b> structured answers. Declare a schema of fields —
|
| 412 |
+
text, numbers, dates, enums, lists, estimates, forecasts — and get back values with confidence and null probabilities.</p>
|
| 413 |
+
<div class="cf-links">
|
| 414 |
+
<a href="https://huggingface.co/Cloudflare/clairic-flash" target="_blank">Model card ↗</a>
|
| 415 |
+
<a href="https://huggingface.co/Cloudflare/clairic" target="_blank">Clairic 27B ↗</a>
|
| 416 |
+
<span class="cf-pill">Apache-2.0</span><span class="cf-pill">Multimodal</span><span class="cf-pill">Structured output</span>
|
| 417 |
+
</div>
|
| 418 |
+
</div>
|
| 419 |
+
</div>
|
| 420 |
+
</div>
|
| 421 |
+
"""
|
| 422 |
+
|
| 423 |
+
CSS = """
|
| 424 |
+
:root { --cf-orange:#F38020; --cf-amber:#FBAD41; --cf-ink:#0f1115; --cf-ink2:#1b1e24; --cf-line:rgba(243,128,32,.28); }
|
| 425 |
+
.gradio-container { max-width: 1240px !important; margin: 0 auto !important; font-family: 'Inter', system-ui, sans-serif; }
|
| 426 |
+
.dark .gradio-container { color: var(--body-text-color); }
|
| 427 |
+
.cf-hero { position:relative; overflow:hidden; border-radius:20px; padding:30px 34px; margin-bottom:10px;
|
| 428 |
+
background: radial-gradient(1200px 300px at 85% -20%, rgba(243,128,32,.55), transparent 60%),
|
| 429 |
+
linear-gradient(135deg, #0f1115 0%, #1b1e24 60%, #2a1b10 100%);
|
| 430 |
+
border:1px solid var(--cf-line); color:#f5f5f5; }
|
| 431 |
+
.cf-hero-glow { position:absolute; right:-80px; bottom:-120px; width:340px; height:340px; border-radius:50%;
|
| 432 |
+
background: radial-gradient(circle, rgba(251,173,65,.35), transparent 70%); filter: blur(10px); }
|
| 433 |
+
.cf-hero-row { position:relative; display:flex; gap:26px; align-items:center; }
|
| 434 |
+
.cf-logo { width:120px; flex:none; filter: drop-shadow(0 6px 24px rgba(243,128,32,.55)); }
|
| 435 |
+
.cf-eyebrow { font-size:12px; letter-spacing:.18em; color:var(--cf-amber); font-weight:700; }
|
| 436 |
+
.cf-hero h1 { font-size:46px; line-height:1.05; margin:6px 0 8px; color:#fff; font-weight:800; letter-spacing:-.02em; }
|
| 437 |
+
.cf-hero h1 span { font-size:20px; vertical-align:middle; padding:3px 10px; border-radius:999px; margin-left:6px;
|
| 438 |
+
background: linear-gradient(90deg, var(--cf-amber), var(--cf-orange)); color:#1a0e03; font-weight:800; }
|
| 439 |
+
.cf-hero p { color:#d6d8de; max-width:760px; margin:0 0 12px; font-size:15px; }
|
| 440 |
+
.cf-hero p b { color:#fff; }
|
| 441 |
+
.cf-links { display:flex; gap:10px; flex-wrap:wrap; align-items:center; }
|
| 442 |
+
.cf-links a { color:#fff !important; text-decoration:none; font-weight:600; font-size:13px; padding:6px 12px; border-radius:10px;
|
| 443 |
+
background: rgba(243,128,32,.18); border:1px solid rgba(243,128,32,.5); }
|
| 444 |
+
.cf-links a:hover { background: rgba(243,128,32,.35); }
|
| 445 |
+
.cf-pill { font-size:12px; color:#c9ccd3; padding:5px 10px; border-radius:999px; border:1px solid rgba(255,255,255,.15); }
|
| 446 |
+
@media (max-width: 720px) { .cf-hero-row { flex-direction:column; align-items:flex-start; } .cf-hero h1 { font-size:36px; } }
|
| 447 |
+
|
| 448 |
+
.cf-panel { border-radius:16px !important; border:1px solid var(--cf-line) !important; }
|
| 449 |
+
.cf-section { font-size:12px; letter-spacing:.14em; font-weight:800; color:var(--cf-orange); margin:2px 0 4px; }
|
| 450 |
+
#cf-run { background: linear-gradient(90deg, var(--cf-amber), var(--cf-orange)) !important; color:#1a0e03 !important;
|
| 451 |
+
font-weight:800 !important; font-size:16px !important; border:none !important; box-shadow: 0 8px 24px rgba(243,128,32,.35); }
|
| 452 |
+
#cf-run:hover { filter: brightness(1.06); transform: translateY(-1px); }
|
| 453 |
+
.cf-presets button { border-radius:999px !important; }
|
| 454 |
+
|
| 455 |
+
.cf-empty { text-align:center; padding:56px 20px; border:1.5px dashed var(--cf-line); border-radius:16px; color: var(--body-text-color-subdued); }
|
| 456 |
+
.cf-empty-icon { font-family: ui-monospace, monospace; font-size:42px; font-weight:800; color: var(--cf-orange); }
|
| 457 |
+
.cf-results-head { display:flex; justify-content:space-between; align-items:center; font-weight:800; margin-bottom:10px; font-size:15px; }
|
| 458 |
+
.cf-time { display:flex; gap:10px; align-items:center; font-weight:600; font-size:13px; color: var(--body-text-color-subdued); }
|
| 459 |
+
.cf-grid { display:grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap:12px; }
|
| 460 |
+
.cf-card { border-radius:14px; padding:14px 14px 12px; background: var(--block-background-fill);
|
| 461 |
+
border:1px solid var(--border-color-primary); border-top:3px solid var(--cf-orange);
|
| 462 |
+
box-shadow: 0 1px 2px rgba(0,0,0,.05); animation: cfIn .35s ease both; }
|
| 463 |
+
@keyframes cfIn { from { opacity:0; transform: translateY(6px);} to { opacity:1; transform:none; } }
|
| 464 |
+
.cf-card-head { display:flex; justify-content:space-between; align-items:center; gap:6px; margin-bottom:8px; }
|
| 465 |
+
.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; }
|
| 466 |
+
.cf-card-body { min-height:30px; margin-bottom:10px; }
|
| 467 |
+
.cf-val { font-size:20px; font-weight:700; word-break:break-word; }
|
| 468 |
+
.cf-null { font-family: ui-monospace, monospace; color: var(--body-text-color-subdued); font-style: italic; }
|
| 469 |
+
.cf-bool { font-family: ui-monospace, monospace; font-weight:800; padding:2px 10px; border-radius:8px; }
|
| 470 |
+
.cf-true { background: rgba(34,197,94,.15); color:#16a34a; } .cf-false { background: rgba(239,68,68,.12); color:#dc2626; }
|
| 471 |
+
.cf-chips { display:flex; flex-wrap:wrap; gap:6px; }
|
| 472 |
+
.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); }
|
| 473 |
+
.cf-obj div { font-size:13px; } .cf-sub { font-size:12px; color: var(--body-text-color-subdued); margin-top:6px; }
|
| 474 |
+
.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; }
|
| 475 |
+
.cf-spark .cf-band { fill: rgba(243,128,32,.16); stroke:none; }
|
| 476 |
+
.cf-spark circle { fill: var(--cf-amber); stroke: var(--cf-orange); stroke-width:1.5; }
|
| 477 |
+
.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; }
|
| 478 |
+
.cf-meter em { font-style:normal; text-align:right; font-weight:700; color: var(--body-text-color); }
|
| 479 |
+
.cf-bar { height:7px; border-radius:99px; background: var(--border-color-primary); overflow:hidden; }
|
| 480 |
+
.cf-bar i { display:block; height:100%; border-radius:99px; background: linear-gradient(90deg, var(--cf-amber), var(--cf-orange)); }
|
| 481 |
+
.cf-meter-null .cf-bar i { background: linear-gradient(90deg, #9aa0aa, #5f6670); }
|
| 482 |
+
.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; }
|
| 483 |
+
.cf-range { display:flex; justify-content:space-between; font-size:12px; margin-top:8px; color: var(--body-text-color-subdued); }
|
| 484 |
+
.cf-range span:nth-child(2) { color: var(--cf-orange); font-weight:700; }
|
| 485 |
+
.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; }
|
| 486 |
+
.cf-ok { color:#16a34a; }
|
| 487 |
+
.cf-stats { display:flex; flex-wrap:wrap; gap:8px; margin-top:14px; }
|
| 488 |
+
.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); }
|
| 489 |
+
.cf-stat b { display:block; font-size:16px; } .cf-stat span { font-size:11px; color: var(--body-text-color-subdued); text-transform: capitalize; }
|
| 490 |
+
.cf-footer { text-align:center; font-size:12px; color: var(--body-text-color-subdued); margin-top:10px; }
|
| 491 |
+
.cf-footer a { color: var(--cf-orange) !important; }
|
| 492 |
+
"""
|
| 493 |
+
|
| 494 |
+
PRESETS = {
|
| 495 |
+
"🧾 Invoice": (INVOICE_STATE, _fmt(INVOICE_FIELDS)),
|
| 496 |
+
"📈 Forecast": (DEMAND_STATE, _fmt(DEMAND_FIELDS)),
|
| 497 |
+
"🎫 Support ticket": (TICKET_STATE, _fmt(TICKET_FIELDS)),
|
| 498 |
+
"🧾 Receipt image": (RECEIPT_STATE, _fmt(RECEIPT_FIELDS)),
|
| 499 |
+
}
|
| 500 |
+
|
| 501 |
+
with gr.Blocks(title="Clairic Flash · Cloudflare") as demo:
|
| 502 |
+
gr.HTML(HEADER)
|
| 503 |
+
with gr.Row(equal_height=False):
|
| 504 |
+
with gr.Column(scale=5):
|
| 505 |
+
with gr.Group(elem_classes="cf-panel"):
|
| 506 |
+
gr.HTML('<div class="cf-section" style="padding:10px 12px 0">① SOURCE</div>')
|
| 507 |
+
state = gr.Textbox(label="State (text or JSON)", lines=5, value=INVOICE_STATE,
|
| 508 |
+
placeholder="Paste an email, invoice, log line, table, JSON…")
|
| 509 |
+
image = gr.Image(label="Image (optional)", type="pil", height=240,
|
| 510 |
+
sources=["upload", "clipboard"])
|
| 511 |
+
with gr.Group(elem_classes="cf-panel"):
|
| 512 |
+
gr.HTML('<div class="cf-section" style="padding:10px 12px 0">② SCHEMA</div>')
|
| 513 |
+
fields = gr.Code(label="Fields (JSON)", language="json", value=_fmt(INVOICE_FIELDS), lines=12)
|
| 514 |
+
with gr.Accordion("➕ Add a field", open=False):
|
| 515 |
+
with gr.Row():
|
| 516 |
+
new_name = gr.Textbox(label="Name", placeholder="due_date", scale=2)
|
| 517 |
+
new_type = gr.Dropdown(FIELD_TYPES, value="text", label="Type", scale=2)
|
| 518 |
+
new_instr = gr.Textbox(label="Instructions (optional)", placeholder="What should this field contain?")
|
| 519 |
+
new_opts = gr.Textbox(label="Enum options (comma-separated, enum only)",
|
| 520 |
+
placeholder="low, medium, high")
|
| 521 |
+
with gr.Row():
|
| 522 |
+
add_btn = gr.Button("Add field to schema", size="sm")
|
| 523 |
+
clear_btn = gr.Button("Clear schema", size="sm", variant="stop")
|
| 524 |
+
with gr.Row(elem_classes="cf-presets"):
|
| 525 |
+
preset_buttons = [gr.Button(name, size="sm", variant="secondary") for name in PRESETS]
|
| 526 |
+
with gr.Accordion("Advanced options", open=False):
|
| 527 |
+
reference_date = gr.Textbox(label="Reference date (ISO, optional)", placeholder="2026-11-18")
|
| 528 |
+
include_quantiles = gr.Checkbox(label="Include quantiles for estimates / forecasts", value=False)
|
| 529 |
+
run = gr.Button("Extract ⚡", variant="primary", elem_id="cf-run", size="lg")
|
| 530 |
+
with gr.Column(scale=6):
|
| 531 |
+
gr.HTML('<div class="cf-section">③ ANSWERS</div>')
|
| 532 |
+
out_html = gr.HTML(EMPTY_HTML)
|
| 533 |
+
with gr.Accordion("Raw JSON response", open=False):
|
| 534 |
+
out_json = gr.JSON(label="Response")
|
| 535 |
+
|
| 536 |
+
for btn, (name, (st, fl)) in zip(preset_buttons, PRESETS.items()):
|
| 537 |
+
btn.click(lambda s=st, f=fl: (s, f, None), outputs=[state, fields, image], api_visibility="private")
|
| 538 |
+
|
| 539 |
+
add_btn.click(add_field, inputs=[fields, new_name, new_type, new_instr, new_opts], outputs=fields,
|
| 540 |
+
api_visibility="private")
|
| 541 |
+
clear_btn.click(lambda: "{}", outputs=fields, api_visibility="private")
|
| 542 |
+
|
| 543 |
+
run.click(extract, inputs=[state, fields, image, reference_date, include_quantiles],
|
| 544 |
+
outputs=[out_html, out_json], api_name="extract", concurrency_limit=1)
|
| 545 |
+
|
| 546 |
+
gr.Markdown("### Examples")
|
| 547 |
+
gr.Examples(
|
| 548 |
+
label="Text",
|
| 549 |
+
examples=[
|
| 550 |
+
[INVOICE_STATE, _fmt(INVOICE_FIELDS)],
|
| 551 |
+
[DEMAND_STATE, _fmt(DEMAND_FIELDS)],
|
| 552 |
+
[TICKET_STATE, _fmt(TICKET_FIELDS)],
|
| 553 |
+
],
|
| 554 |
+
inputs=[state, fields],
|
| 555 |
+
outputs=[out_html, out_json],
|
| 556 |
+
fn=extract,
|
| 557 |
+
cache_examples=True,
|
| 558 |
+
cache_mode="lazy",
|
| 559 |
+
)
|
| 560 |
+
gr.Examples(
|
| 561 |
+
label="Images (receipts from CORD-v2, CC-BY-4.0)",
|
| 562 |
+
examples=[
|
| 563 |
+
[RECEIPT_STATE, _fmt(RECEIPT_FIELDS), "examples/receipt_clear.jpg"],
|
| 564 |
+
[RECEIPT_STATE, _fmt(RECEIPT_FIELDS), "examples/receipt_crumpled.jpg"],
|
| 565 |
+
],
|
| 566 |
+
inputs=[state, fields, image],
|
| 567 |
+
outputs=[out_html, out_json],
|
| 568 |
+
fn=extract,
|
| 569 |
+
cache_examples=True,
|
| 570 |
+
cache_mode="lazy",
|
| 571 |
+
)
|
| 572 |
+
gr.HTML('<div class="cf-footer">Built on <a href="https://huggingface.co/Cloudflare/clairic-flash" target="_blank">'
|
| 573 |
+
'Cloudflare/clairic-flash</a> · runs on ZeroGPU · example receipts from '
|
| 574 |
+
'<a href="https://huggingface.co/datasets/naver-clova-ix/cord-v2" target="_blank">CORD-v2</a> (CC-BY-4.0)</div>')
|
| 575 |
+
|
| 576 |
+
if __name__ == "__main__":
|
| 577 |
+
demo.launch(theme=gr.themes.Citrus(), css=CSS, mcp_server=True)
|
examples/receipt_clear.jpg
ADDED
|
examples/receipt_crumpled.jpg
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers==5.10.2
|
| 2 |
+
flash-linear-attention==0.6.0
|
| 3 |
+
fla-core==0.6.0
|
| 4 |
+
einops==0.8.1
|
| 5 |
+
xgrammar==0.2.1
|
| 6 |
+
accelerate>=1.13.0
|
| 7 |
+
safetensors>=0.8.0
|
| 8 |
+
Pillow>=12.2.0
|
| 9 |
+
pydantic>=2.11,<3
|
| 10 |
+
jsonschema>=4.26.0,<5
|
| 11 |
+
numpy>=2.2.6
|
| 12 |
+
opencv-python-headless>=4.13.0.92
|
| 13 |
+
pypdfium2>=5.14.0
|
| 14 |
+
tomlkit==0.13.3
|
| 15 |
+
torchvision
|