Spaces:
Sleeping
Sleeping
Upload runtime.py
Browse files1260 unmodified lines126112621263126412651266126739 unmodified lines13071308130913101311131217 unmodified lines133013311332133313341335133613371338133913401341134213431344134513461347134813493 unmodified lines135313541355135613571358126 unmodified lines148514861487148814891490149114 unmodified lines15061507150815091510151115121260 unmodified lines_file_cache_key(TOMORROW_PREDICTION_HISTORY_PATH),)with _dashboard_payload_lock:return copy.deepcopy(_dashboard_payload_cached(key))def warm_dashboard_payload_cache() -> None:39 unmodified linesreturn indexeddef _rolling_tomorrow_prediction(input_day: date,daily_rows: pd.DataFrame,17 unmodified linesfallback_prob = float(summary.get("latest_forecast_prob_up", 0.49900560447008563))forecaster_by_target = _load_forecaster_predictions_by_target()end_session = _track_record_end_session()latest_daily = latest_parquet_date(NIFTY_1D_PATH)if latest_daily is None or latest_daily < end_session:try:refresh_daily_data()except Exception:passdaily_rows = pd.read_parquet(NIFTY_1D_PATH)daily_rows["date"] = pd.to_datetime(daily_rows["date"], errors="coerce").dt.normalize()daily_rows = daily_rows.dropna(subset=["date"]).sort_values("date")daily_rows = daily_rows[daily_rows["close"].map(lambda value: np.isfinite(float(value)) if pd.notna(value) else False)].copy()if daily_rows.empty:return []3 unmodified linesif pd.notna(row["close"]) and np.isfinite(float(row["close"]))}session_dates = last_n_trading_sessions(end_session, sessions)records: list[dict[str, Any]] = []126 unmodified lines"total_test_days": int(tomorrow_summary.get("n_test") or len(tomorrow_test) or 0),"models": model_metrics,}track_record = build_prediction_track_record(sessions=10)return {"latest": t5_latest,"tomorrow_latest": tomorrow_latest,14 unmodified lines"tomorrow_recent_predictions": _json_ready_frame(tomorrow_recent),"tomorrow_history_predictions": _json_ready_frame(tomorrow_history.tail(80)),"tplus1_recent_predictions": _json_ready_frame(tplus1_test.tail(40)),"track_record": track_record,},"data_status": {"nifty_1m_rows": int(len(pd.read_parquet(NIFTY_1M_PATH, columns=["date"]))),1260 unmodified lines126112621263126412651266126712681269127039 unmodified lines1310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134717 unmodified lines13651366136713681369137013713 unmodified lines1375137613771378137913801381138213831384138513861387138813891390126 unmodified lines15171518151915201521152214 unmodified lines15371538153915401541154215431260 unmodified lines_file_cache_key(TOMORROW_PREDICTION_HISTORY_PATH),)with _dashboard_payload_lock:payload = copy.deepcopy(_dashboard_payload_cached(key))# Always rebuild track record (fresh Yahoo daily + calendar window), never serve from LRU cache.payload["charts"]["track_record"] = build_prediction_track_record(sessions=10)return payloaddef warm_dashboard_payload_cache() -> None:39 unmodified linesreturn indexeddef _load_track_record_daily_rows() -> pd.DataFrame:"""Daily OHLC for track record: live Yahoo pull merged over parquet history."""frames: list[pd.DataFrame] = []if NIFTY_1D_PATH.exists():try:frames.append(pd.read_parquet(NIFTY_1D_PATH))except Exception:passtry:yahoo_daily = fetch_yahoo_daily(period="3mo")if not yahoo_daily.empty:frames.append(yahoo_daily)try:append_parquet_rows(NIFTY_1D_PATH, yahoo_daily, ["date"])except Exception:passexcept Exception:passif not frames:return pd.DataFrame()combined = pd.concat(frames, ignore_index=True)combined["date"] = pd.to_datetime(combined["date"], errors="coerce").dt.normalize()combined = combined.dropna(subset=["date"]).sort_values("date")combined = combined.drop_duplicates(subset=["date"], keep="last")combined = combined[combined["close"].map(lambda value: np.isfinite(float(value)) if pd.notna(value) else False)].copy()return combined.reset_index(drop=True)def _rolling_tomorrow_prediction(input_day: date,daily_rows: pd.DataFrame,17 unmodified linesfallback_prob = float(summary.get("latest_forecast_prob_up", 0.49900560447008563))forecaster_by_target = _load_forecaster_predictions_by_target()daily_rows = _load_track_record_daily_rows()if daily_rows.empty:return []3 unmodified linesif pd.notna(row["close"]) and np.isfinite(float(row["close"]))}end_session = _track_record_end_session()available_through = max((day for day in closes_by_date if day <= end_session),default=None,)if available_through is None:return []if available_through < end_session:end_session = available_throughsession_dates = last_n_trading_sessions(end_session, sessions)records: list[dict[str, Any]] = []126 unmodified lines"total_test_days": int(tomorrow_summary.get("n_test") or len(tomorrow_test) or 0),"models": model_metrics,}return {"latest": t5_latest,"tomorrow_latest": tomorrow_latest,14 unmodified lines"tomorrow_recent_predictions": _json_ready_frame(tomorrow_recent),"tomorrow_history_predictions": _json_ready_frame(tomorrow_history.tail(80)),"tplus1_recent_predictions": _json_ready_frame(tplus1_test.tail(40)),"track_record": [],},"data_status": {"nifty_1m_rows": int(len(pd.read_parquet(NIFTY_1M_PATH, columns=["date"]))),
runtime.py+48-17