Upload analyst.py with huggingface_hub
Browse files566 unmodified lines56756856957057157257357457557642 unmodified lines619620621622623624566 unmodified linesreturn {}def cached_report(ticker: str) -> dict | None:"""Fresh (<7d) cached result for a ticker, with a price-drift staleness flag."""f = _REPORTS_DIR / f"{ticker.upper()}.json"if not f.exists():return Nonetry:d = json.loads(f.read_text())42 unmodified linesticker = ticker.upper().strip()if not re.fullmatch(r"[A-Z.\-]{1,6}", ticker):raise ValueError(f"invalid ticker {ticker!r}")job_id = uuid.uuid4().hex[:12]job = {"ticker": ticker,566 unmodified lines56756856957057157257357457557657757857958058158258358458558658758858959059159259359459559642 unmodified lines639640641642643644645646647648649650566 unmodified linesreturn {}def _hydrate_one(ticker: str) -> bool:"""Pull ONE ticker's report from the dataset repo on demand. After a rebuild the containerstarts with an empty cache and the background hydration takes minutes; without this, aRecent-chip click in that window found nothing and the client fell back to a full live run(observed: an unrequested AAPL run after every deploy today)."""if not _HF_TOKEN:return Falsetry:from huggingface_hub import hf_hub_downloadgot = hf_hub_download(_REPORTS_REPO, f"reports/{ticker.upper()}.json", repo_type="dataset", token=_HF_TOKEN)d = json.loads(open(got).read())if time.time() - (d.get("cached_at") or 0) <= _CACHE_MAX_AGE_S:_REPORTS_DIR.mkdir(parents=True, exist_ok=True)(_REPORTS_DIR / f"{ticker.upper()}.json").write_text(json.dumps(d))return Trueexcept Exception:passreturn Falsedef cached_report(ticker: str) -> dict | None:"""Fresh (<7d) cached result for a ticker, with a price-drift staleness flag."""f = _REPORTS_DIR / f"{ticker.upper()}.json"if not f.exists() and not _hydrate_one(ticker):return Nonetry:d = json.loads(f.read_text())42 unmodified linesticker = ticker.upper().strip()if not re.fullmatch(r"[A-Z.\-]{1,6}", ticker):raise ValueError(f"invalid ticker {ticker!r}")# Same ticker already in flight (running or queued): attach to that job instead of# spending a second engine run on an identical request (double-click, page reload).for _j in list(_jobs.values()):if _j.get("ticker") == ticker and _j.get("status") in ("running", "queued") \and time.time() - (_j.get("created") or 0) < 1800:return _j["id"]job_id = uuid.uuid4().hex[:12]job = {"ticker": ticker,
analyst.py+27-1