Upload analyst.py with huggingface_hub
Browse files- analyst.py +27 -1
analyst.py
CHANGED
|
@@ -567,10 +567,30 @@ def _request_counts() -> dict:
|
|
| 567 |
return {}
|
| 568 |
|
| 569 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 570 |
def cached_report(ticker: str) -> dict | None:
|
| 571 |
"""Fresh (<7d) cached result for a ticker, with a price-drift staleness flag."""
|
| 572 |
f = _REPORTS_DIR / f"{ticker.upper()}.json"
|
| 573 |
-
if not f.exists():
|
| 574 |
return None
|
| 575 |
try:
|
| 576 |
d = json.loads(f.read_text())
|
|
@@ -619,6 +639,12 @@ def start(ticker: str) -> str:
|
|
| 619 |
ticker = ticker.upper().strip()
|
| 620 |
if not re.fullmatch(r"[A-Z.\-]{1,6}", ticker):
|
| 621 |
raise ValueError(f"invalid ticker {ticker!r}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
job_id = uuid.uuid4().hex[:12]
|
| 623 |
job = {
|
| 624 |
"ticker": ticker,
|
|
|
|
| 567 |
return {}
|
| 568 |
|
| 569 |
|
| 570 |
+
def _hydrate_one(ticker: str) -> bool:
|
| 571 |
+
"""Pull ONE ticker's report from the dataset repo on demand. After a rebuild the container
|
| 572 |
+
starts with an empty cache and the background hydration takes minutes; without this, a
|
| 573 |
+
Recent-chip click in that window found nothing and the client fell back to a full live run
|
| 574 |
+
(observed: an unrequested AAPL run after every deploy today)."""
|
| 575 |
+
if not _HF_TOKEN:
|
| 576 |
+
return False
|
| 577 |
+
try:
|
| 578 |
+
from huggingface_hub import hf_hub_download
|
| 579 |
+
got = hf_hub_download(_REPORTS_REPO, f"reports/{ticker.upper()}.json", repo_type="dataset", token=_HF_TOKEN)
|
| 580 |
+
d = json.loads(open(got).read())
|
| 581 |
+
if time.time() - (d.get("cached_at") or 0) <= _CACHE_MAX_AGE_S:
|
| 582 |
+
_REPORTS_DIR.mkdir(parents=True, exist_ok=True)
|
| 583 |
+
(_REPORTS_DIR / f"{ticker.upper()}.json").write_text(json.dumps(d))
|
| 584 |
+
return True
|
| 585 |
+
except Exception:
|
| 586 |
+
pass
|
| 587 |
+
return False
|
| 588 |
+
|
| 589 |
+
|
| 590 |
def cached_report(ticker: str) -> dict | None:
|
| 591 |
"""Fresh (<7d) cached result for a ticker, with a price-drift staleness flag."""
|
| 592 |
f = _REPORTS_DIR / f"{ticker.upper()}.json"
|
| 593 |
+
if not f.exists() and not _hydrate_one(ticker):
|
| 594 |
return None
|
| 595 |
try:
|
| 596 |
d = json.loads(f.read_text())
|
|
|
|
| 639 |
ticker = ticker.upper().strip()
|
| 640 |
if not re.fullmatch(r"[A-Z.\-]{1,6}", ticker):
|
| 641 |
raise ValueError(f"invalid ticker {ticker!r}")
|
| 642 |
+
# Same ticker already in flight (running or queued): attach to that job instead of
|
| 643 |
+
# spending a second engine run on an identical request (double-click, page reload).
|
| 644 |
+
for _j in list(_jobs.values()):
|
| 645 |
+
if _j.get("ticker") == ticker and _j.get("status") in ("running", "queued") \
|
| 646 |
+
and time.time() - (_j.get("created") or 0) < 1800:
|
| 647 |
+
return _j["id"]
|
| 648 |
job_id = uuid.uuid4().hex[:12]
|
| 649 |
job = {
|
| 650 |
"ticker": ticker,
|