Spaces:
Running on Zero
Running on Zero
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -183,6 +183,24 @@ def _pct(p) -> float:
|
|
| 183 |
return 0.0
|
| 184 |
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
def _value_html(value) -> str:
|
| 187 |
if value is None:
|
| 188 |
return '<span class="cf-null">null</span>'
|
|
@@ -196,54 +214,57 @@ def _value_html(value) -> str:
|
|
| 196 |
if isinstance(value, dict):
|
| 197 |
rows = "".join(f"<div><b>{_esc(k)}</b> {_value_inline(v)}</div>" for k, v in value.items())
|
| 198 |
return f'<div class="cf-obj">{rows}</div>'
|
| 199 |
-
return f'<span class="cf-val">{_esc(value)}</span>'
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
def _value_inline(value) -> str:
|
| 203 |
-
if isinstance(value, dict):
|
| 204 |
-
if "value" in value:
|
| 205 |
-
return _esc(value["value"])
|
| 206 |
-
return _esc(json.dumps(value, ensure_ascii=False))
|
| 207 |
-
return _esc(value)
|
| 208 |
|
| 209 |
|
| 210 |
-
def
|
| 211 |
-
|
| 212 |
-
for
|
| 213 |
-
v = s.get("value") if isinstance(s, dict) else s
|
| 214 |
try:
|
| 215 |
-
|
| 216 |
except (TypeError, ValueError):
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
if len(vals) < 2:
|
| 219 |
return ""
|
| 220 |
-
|
|
|
|
|
|
|
| 221 |
span = (hi - lo) or 1.0
|
| 222 |
-
w, h =
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
|
| 229 |
def render_answers(response: dict, elapsed: float) -> str:
|
| 230 |
cards = []
|
| 231 |
for name, ans in response.get("answers", {}).items():
|
| 232 |
ans = ans if isinstance(ans, dict) else {"value": ans}
|
|
|
|
| 233 |
value = ans.get("value")
|
| 234 |
body = _value_html(value)
|
| 235 |
extra = ""
|
| 236 |
-
if
|
| 237 |
-
extra += _spark(ans
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
if isinstance(q, dict) and q:
|
| 245 |
-
qs = " · ".join(f"q{_esc(k)}={_esc(v)}" for k, v in list(q.items())[:7])
|
| 246 |
-
extra += f'<div class="cf-sub">{qs}</div>'
|
| 247 |
meters = ""
|
| 248 |
if "confidence" in ans:
|
| 249 |
c = _pct(ans["confidence"])
|
|
@@ -253,13 +274,13 @@ def render_answers(response: dict, elapsed: float) -> str:
|
|
| 253 |
p = _pct(ans["p_null"])
|
| 254 |
meters += (f'<div class="cf-meter cf-meter-null"><span>p(null)</span><div class="cf-bar">'
|
| 255 |
f'<i style="width:{p:.1f}%"></i></div><em>{p:.1f}%</em></div>')
|
| 256 |
-
flags = ""
|
| 257 |
if ans.get("valid") is False:
|
| 258 |
flags += '<span class="cf-flag">invalid</span>'
|
| 259 |
if ans.get("complete") is False:
|
| 260 |
flags += '<span class="cf-flag">incomplete</span>'
|
| 261 |
cards.append(
|
| 262 |
-
f'<div class="cf-card"><div class="cf-card-head"><code>{_esc(name)}</code>{flags}</div>'
|
| 263 |
f'<div class="cf-card-body">{body}{extra}</div>{meters}</div>'
|
| 264 |
)
|
| 265 |
usage = response.get("usage", {})
|
|
@@ -286,7 +307,12 @@ EMPTY_HTML = (
|
|
| 286 |
# ---------------------------------------------------------------------------
|
| 287 |
# Inference
|
| 288 |
# ---------------------------------------------------------------------------
|
| 289 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
def extract(state: str, fields_json: str, image=None, reference_date: str = "",
|
| 291 |
include_quantiles: bool = False):
|
| 292 |
"""Extract typed, scored structured answers from text and/or an image with Clairic.
|
|
@@ -413,13 +439,17 @@ CSS = """
|
|
| 413 |
.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); }
|
| 414 |
.cf-obj div { font-size:13px; } .cf-sub { font-size:12px; color: var(--body-text-color-subdued); margin-top:6px; }
|
| 415 |
.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; }
|
|
|
|
| 416 |
.cf-spark circle { fill: var(--cf-amber); stroke: var(--cf-orange); stroke-width:1.5; }
|
| 417 |
.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; }
|
| 418 |
.cf-meter em { font-style:normal; text-align:right; font-weight:700; color: var(--body-text-color); }
|
| 419 |
.cf-bar { height:7px; border-radius:99px; background: var(--border-color-primary); overflow:hidden; }
|
| 420 |
.cf-bar i { display:block; height:100%; border-radius:99px; background: linear-gradient(90deg, var(--cf-amber), var(--cf-orange)); }
|
| 421 |
.cf-meter-null .cf-bar i { background: linear-gradient(90deg, #9aa0aa, #5f6670); }
|
| 422 |
-
.cf-
|
|
|
|
|
|
|
|
|
|
| 423 |
.cf-ok { color:#16a34a; }
|
| 424 |
.cf-stats { display:flex; flex-wrap:wrap; gap:8px; margin-top:14px; }
|
| 425 |
.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); }
|
|
|
|
| 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>'
|
|
|
|
| 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"])
|
|
|
|
| 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", {})
|
|
|
|
| 307 |
# ---------------------------------------------------------------------------
|
| 308 |
# Inference
|
| 309 |
# ---------------------------------------------------------------------------
|
| 310 |
+
def _gpu_duration(state, fields_json, image=None, *args, **kwargs):
|
| 311 |
+
# measured: <=22.4s text (cold worker incl. Triton JIT), <=13.9s single image
|
| 312 |
+
return 40 if image is not None else 26
|
| 313 |
+
|
| 314 |
+
|
| 315 |
+
@spaces.GPU(duration=_gpu_duration, size="xlarge")
|
| 316 |
def extract(state: str, fields_json: str, image=None, reference_date: str = "",
|
| 317 |
include_quantiles: bool = False):
|
| 318 |
"""Extract typed, scored structured answers from text and/or an image with Clairic.
|
|
|
|
| 439 |
.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); }
|
| 440 |
.cf-obj div { font-size:13px; } .cf-sub { font-size:12px; color: var(--body-text-color-subdued); margin-top:6px; }
|
| 441 |
.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; }
|
| 442 |
+
.cf-spark .cf-band { fill: rgba(243,128,32,.16); stroke:none; }
|
| 443 |
.cf-spark circle { fill: var(--cf-amber); stroke: var(--cf-orange); stroke-width:1.5; }
|
| 444 |
.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; }
|
| 445 |
.cf-meter em { font-style:normal; text-align:right; font-weight:700; color: var(--body-text-color); }
|
| 446 |
.cf-bar { height:7px; border-radius:99px; background: var(--border-color-primary); overflow:hidden; }
|
| 447 |
.cf-bar i { display:block; height:100%; border-radius:99px; background: linear-gradient(90deg, var(--cf-amber), var(--cf-orange)); }
|
| 448 |
.cf-meter-null .cf-bar i { background: linear-gradient(90deg, #9aa0aa, #5f6670); }
|
| 449 |
+
.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; }
|
| 450 |
+
.cf-range { display:flex; justify-content:space-between; font-size:12px; margin-top:8px; color: var(--body-text-color-subdued); }
|
| 451 |
+
.cf-range span:nth-child(2) { color: var(--cf-orange); font-weight:700; }
|
| 452 |
+
.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; }
|
| 453 |
.cf-ok { color:#16a34a; }
|
| 454 |
.cf-stats { display:flex; flex-wrap:wrap; gap:8px; margin-top:14px; }
|
| 455 |
.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); }
|