Spaces:
Sleeping
Sleeping
Update app/core/hf_client.py
Browse files1234562 unmodified lines91011121314155 unmodified lines212223242526272829303132333435363738394041424344454647484950511 unmodified line5354555657588 unmodified lines676869707172737475# =====================================================# app/core/hf_client.py# Hugging Face dataset erişimi (cache ve fallback destekli)# =====================================================import os2 unmodified linesfrom huggingface_hub import hf_hub_downloadfrom app.core.config import settingsCACHE_DIR = "/tmp/pegos_cache"def _try_download(day: str):"""Belirtilen günün predictions_latest.csv dosyasını indirir."""5 unmodified linesfilename=path,repo_type="dataset",token=settings.HF_TOKEN,cache_dir=CACHE_DIR,force_download=False,)df = pd.read_csv(local, encoding="utf-8-sig")# Hatalı değerleri temizledf = df.replace([float("inf"), float("-inf")], None)df = df.fillna(0)df["source_day"] = dayprint(f"✅ {path} yüklendi ({len(df)} satır)")return dfexcept Exception as e:print(f"⚠️ {path} indirilemedi: {e}")raisedef download_latest_csv():"""Bugün veya gerekirse dünkü predictions_latest.csv'yi indirir."""today = datetime.utcnow().strftime("%Y-%m-%d")try:return _try_download(today)except Exception:yesterday = (datetime.utcnow() - timedelta(days=1)).strftime("%Y-%m-%d")print(f"⚠️ {today} bulunamadı, {yesterday} dosyası deneniyor...")return _try_download(yesterday)def load_day_csv(day: str):"""Belirtilen günün predictions_latest.csv dosyasını indirir."""1 unmodified lineraise ValueError("Gün parametresi boş olamaz.")return _try_download(day)def load_all_history(days_back: int = 7):"""Son N günün predictions_latest.csv dosyalarını birleştirir."""frames = []8 unmodified linesraise RuntimeError("❌ Geçmişte hiçbir predictions_latest.csv bulunamadı.")merged = pd.concat(frames, ignore_index=True)merged.sort_values("source_day", inplace=True)print(f"📊 Birleştirilen toplam: {len(merged)} satır ({len(frames)} gün)")return mergeddef get_latest_timestamp():"""Son veri güncelleme zamanını ISO formatında döndürür."""return datetime.utcnow().replace(microsecond=0).isoformat() + "Z"1234562 unmodified lines910111213145 unmodified lines2021222324252627282930313233343536373839404142431 unmodified line454647484950518 unmodified lines606162636465666768# =====================================================# app/core/hf_client.py# Hugging Face dataset erişimi (günlük predictions_latest.csv)# =====================================================import os2 unmodified linesfrom huggingface_hub import hf_hub_downloadfrom app.core.config import settingsdef _try_download(day: str):"""Belirtilen günün predictions_latest.csv dosyasını indirir."""5 unmodified linesfilename=path,repo_type="dataset",token=settings.HF_TOKEN,force_download=True,)df = pd.read_csv(local, encoding="utf-8-sig")df = df.replace([float("inf"), float("-inf")], None)df = df.fillna(0)df["source_day"] = dayprint(f"✅ {path} yüklendi ({len(df)} satır)")return dfexcept Exception as e:print(f"⚠️ {path} indirilemedi: {e}")raisedef download_latest_csv():"""Sadece bugünün klasöründen predictions_latest.csv dosyasını indirir."""today = datetime.utcnow().strftime("%Y-%m-%d")return _try_download(today)def load_day_csv(day: str):"""Belirtilen günün predictions_latest.csv dosyasını indirir."""1 unmodified lineraise ValueError("Gün parametresi boş olamaz.")return _try_download(day)def load_all_history(days_back: int = 7):"""Son N günün predictions_latest.csv dosyalarını birleştirir."""frames = []8 unmodified linesraise RuntimeError("❌ Geçmişte hiçbir predictions_latest.csv bulunamadı.")merged = pd.concat(frames, ignore_index=True)merged.sort_values("source_day", inplace=True)return mergeddef get_latest_timestamp():"""Son veri güncelleme zamanını ISO formatında döndürür."""return datetime.utcnow().replace(microsecond=0).isoformat() + "Z"
app/core/hf_client.py+8-15