Spaces:
Sleeping
Sleeping
Update app/routers/predictions.py
Browse files12234556789678910131115161718121314202122151617181920212223242526272824252627282930313233343536373839404142434445464748495051525329# =====================================================# app/routers/predictions.py — Model tahminleri endpointi# app/routers/predictions.py# Günlük veya belirtilen tarihe ait tüm tahminleri döndürür# =====================================================
from fastapi import APIRouter, HTTPException, Queryimport pandas as pdfrom app.core.config import Configfrom app.core.cache import load_dataset, last_update_isoimport osfrom fastapi import APIRouter, Query, HTTPExceptionfrom app.core.hf_client import load_day_csv, download_latest_csv
router = 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. """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_dataset( Config.HF_DATASET_REPO, Config.PRED_LATEST, os.getenv("HF_TOKEN") ) df = load_day_csv(day) if day else download_latest_csv() df = df.sort_values("time", ascending=False).head(200)
# Türkçe etiketler ekle df["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(status_code=503, detail=f"Dataset erişilemedi: {e}")
if df.empty: raise HTTPException(status_code=404, detail="Dataset boş.")
# Sütunları temizle keep_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 etiketleme df["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"), } raise HTTPException(503, f"Tahmin verileri alınamadı: {e}")app/routers/predictions.py+20-44