Spaces:
Sleeping
Sleeping
Update app/routers/predictions.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253# =====================================================# app/routers/predictions.py — Model tahminleri endpointi# =====================================================from fastapi import APIRouter, HTTPException, Queryimport pandas as pdfrom app.core.config import Configfrom app.core.cache import load_dataset, last_update_isoimport osrouter = APIRouter()@router.get("/predictions")def get_predictions(n: int = Query(100, ge=1, le=2000)):"""Pegos model tahminlerini JSON formatında döndürür."""try:df = load_dataset(Config.HF_DATASET_REPO, Config.PRED_LATEST, os.getenv("HF_TOKEN"))except Exception as e:raise HTTPException(status_code=503, detail=f"Dataset erişilemedi: {e}")if df.empty:raise HTTPException(status_code=404, detail="Dataset boş.")# Sütunları temizlekeep_cols = ["time","pred_label","pred_proba","pred_diff","predicted_price",]df = df[[c for c in keep_cols if c in df.columns]].copy()# Tarih formatıdf["time"] = pd.to_datetime(df["time"], errors="coerce")df = df.dropna(subset=["time"])df = df.sort_values("time").tail(n)# Ek etiketlemedf["Tahmin"] = df["pred_label"].map({1: "Yükseliş", 0: "Düşüş"})if "pred_proba" in df.columns:df["Güven (%)"] = (df["pred_proba"] * 100).round(2)return {"count": len(df),"updated_at": last_update_iso(),"items": df.to_dict(orient="records"),}1234567891011121314151617181920212223242526272829# =====================================================# app/routers/predictions.py# Günlük veya belirtilen tarihe ait tüm tahminleri döndürür# =====================================================from fastapi import APIRouter, Query, HTTPExceptionfrom app.core.hf_client import load_day_csv, download_latest_csvrouter = APIRouter()@router.get("/predictions")def get_predictions(day: str = Query(None)):"""Belirli bir günün veya bugünün tahmin detaylarını JSON formatında döndürür."""try:df = load_day_csv(day) if day else download_latest_csv()df = df.sort_values("time", ascending=False).head(200)# Türkçe etiketler ekledf["Tahmin"] = df["pred_label"].map({1: "Yükseliş", 0: "Düşüş"})if "pred_proba" in df:df["Güven (%)"] = (df["pred_proba"] * 100).round(2)return {"count": len(df),"day": df["source_day"].iloc[0],"items": df.to_dict(orient="records")}except Exception as e:raise HTTPException(503, f"Tahmin verileri alınamadı: {e}")
app/routers/predictions.py+20-44