Spaces:
Running
Running
Upload webapp/app.py with huggingface_hub
Browse files- webapp/app.py +39 -3
webapp/app.py
CHANGED
|
@@ -368,14 +368,50 @@ def _md_to_html(md: str, terse: bool = False) -> str:
|
|
| 368 |
return "\n".join(out)
|
| 369 |
|
| 370 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
@app.route("/digest")
|
| 372 |
def digest():
|
| 373 |
-
|
| 374 |
-
if not
|
| 375 |
return page(f"{BRAND} — digest",
|
| 376 |
"<div class=hero><h1>No digest yet</h1>"
|
| 377 |
"<p class=mut>Run <code>python scripts/05_digest.py</code> to generate one.</p></div>")
|
| 378 |
-
md = files[-1].read_text(encoding="utf-8")
|
| 379 |
body = DIGEST_EXPLAINER \
|
| 380 |
+ f"<div class='card digest'>{_md_to_html(md, terse=True)}</div>" \
|
| 381 |
f"<div class=card><b>Want it daily by email?</b>" \
|
|
|
|
| 368 |
return "\n".join(out)
|
| 369 |
|
| 370 |
|
| 371 |
+
DIGESTS_DATASET = "sharpflowapp/digests"
|
| 372 |
+
_digest_cache: dict = {"md": None, "ts": 0.0}
|
| 373 |
+
_DIGEST_TTL = 300 # seconds — the daily cron only publishes once/day
|
| 374 |
+
|
| 375 |
+
|
| 376 |
+
def _local_digest_md() -> str | None:
|
| 377 |
+
files = sorted(cache_path(_cfg, ".").glob("digest_*.md"))
|
| 378 |
+
return files[-1].read_text(encoding="utf-8") if files else None
|
| 379 |
+
|
| 380 |
+
|
| 381 |
+
def _latest_digest_md() -> str | None:
|
| 382 |
+
"""Newest digest, fetched from the HF dataset (so publishing never rebuilds
|
| 383 |
+
the Space) with a short TTL cache and a fallback to any baked-in seed file."""
|
| 384 |
+
import time
|
| 385 |
+
now = time.time()
|
| 386 |
+
if _digest_cache["md"] is not None and now - _digest_cache["ts"] < _DIGEST_TTL:
|
| 387 |
+
return _digest_cache["md"]
|
| 388 |
+
md = None
|
| 389 |
+
token = os.environ.get("HF_TOKEN")
|
| 390 |
+
try:
|
| 391 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 392 |
+
names = [f for f in HfApi(token=token).list_repo_files(
|
| 393 |
+
DIGESTS_DATASET, repo_type="dataset")
|
| 394 |
+
if f.startswith("digest_") and f.endswith(".md")]
|
| 395 |
+
if names:
|
| 396 |
+
path = hf_hub_download(DIGESTS_DATASET, sorted(names)[-1],
|
| 397 |
+
repo_type="dataset", token=token)
|
| 398 |
+
md = Path(path).read_text(encoding="utf-8")
|
| 399 |
+
except Exception:
|
| 400 |
+
md = None
|
| 401 |
+
if md is None:
|
| 402 |
+
md = _local_digest_md() # fallback: baked-in seed digest
|
| 403 |
+
if md is not None:
|
| 404 |
+
_digest_cache.update(md=md, ts=now)
|
| 405 |
+
return md
|
| 406 |
+
|
| 407 |
+
|
| 408 |
@app.route("/digest")
|
| 409 |
def digest():
|
| 410 |
+
md = _latest_digest_md()
|
| 411 |
+
if not md:
|
| 412 |
return page(f"{BRAND} — digest",
|
| 413 |
"<div class=hero><h1>No digest yet</h1>"
|
| 414 |
"<p class=mut>Run <code>python scripts/05_digest.py</code> to generate one.</p></div>")
|
|
|
|
| 415 |
body = DIGEST_EXPLAINER \
|
| 416 |
+ f"<div class='card digest'>{_md_to_html(md, terse=True)}</div>" \
|
| 417 |
f"<div class=card><b>Want it daily by email?</b>" \
|