Spaces:
Sleeping
Sleeping
some shit
Browse files
app.py
CHANGED
|
@@ -21,22 +21,18 @@ _dist_cache: dict = {}
|
|
| 21 |
|
| 22 |
def _ensure_data():
|
| 23 |
if not os.path.exists(DATA_PATH):
|
| 24 |
-
from huggingface_hub import
|
| 25 |
-
from huggingface_hub.errors import EntryNotFoundError, RepositoryNotFoundError
|
| 26 |
os.makedirs(_DATA_DIR, exist_ok=True)
|
| 27 |
filename = os.path.basename(DATA_PATH)
|
| 28 |
-
print(f"Downloading {filename} from HF
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
repo_type="dataset",
|
| 34 |
-
local_dir=_DATA_DIR,
|
| 35 |
-
local_dir_use_symlinks=False,
|
| 36 |
token=os.environ.get("HF_TOKEN"),
|
| 37 |
)
|
| 38 |
-
except
|
| 39 |
-
print(f"{filename} not found
|
| 40 |
result = update_season_data()
|
| 41 |
if result["status"] != "ok":
|
| 42 |
raise RuntimeError(f"Auto-fetch failed: {result['message']}")
|
|
@@ -188,16 +184,17 @@ def _load_cache(today: str, n_simulations: int) -> dict | None:
|
|
| 188 |
return entry["result"]
|
| 189 |
|
| 190 |
try:
|
| 191 |
-
from huggingface_hub import
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
| 201 |
cached_at = datetime.fromisoformat(stored["cached_at"])
|
| 202 |
if cached_at.tzinfo is None:
|
| 203 |
cached_at = cached_at.replace(tzinfo=timezone.utc)
|
|
@@ -218,15 +215,13 @@ def _save_cache(today: str, n_simulations: int, result: dict) -> None:
|
|
| 218 |
payload = {"cached_at": now.isoformat(), "result": result}
|
| 219 |
tmp_path = None
|
| 220 |
try:
|
| 221 |
-
from huggingface_hub import
|
| 222 |
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
|
| 223 |
json.dump(payload, f)
|
| 224 |
tmp_path = f.name
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
repo_id=_HF_REPO_ID,
|
| 229 |
-
repo_type="dataset",
|
| 230 |
token=os.environ.get("HF_TOKEN"),
|
| 231 |
)
|
| 232 |
except Exception as e:
|
|
@@ -347,18 +342,14 @@ def update_season_data(season_year: int = 2026) -> dict:
|
|
| 347 |
print(f"Saved {len(df):,} rows to {local_path}")
|
| 348 |
|
| 349 |
try:
|
| 350 |
-
from huggingface_hub import
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
path_in_repo=filename,
|
| 356 |
-
repo_id=_HF_REPO_ID,
|
| 357 |
-
repo_type="dataset",
|
| 358 |
-
token=token,
|
| 359 |
)
|
| 360 |
except Exception as e:
|
| 361 |
-
return {"status": "error", "message": f"HF
|
| 362 |
|
| 363 |
global DATA_PATH
|
| 364 |
DATA_PATH = local_path
|
|
|
|
| 21 |
|
| 22 |
def _ensure_data():
|
| 23 |
if not os.path.exists(DATA_PATH):
|
| 24 |
+
from huggingface_hub import download_bucket_files
|
|
|
|
| 25 |
os.makedirs(_DATA_DIR, exist_ok=True)
|
| 26 |
filename = os.path.basename(DATA_PATH)
|
| 27 |
+
print(f"Downloading {filename} from HF bucket...")
|
| 28 |
try:
|
| 29 |
+
download_bucket_files(
|
| 30 |
+
_HF_REPO_ID,
|
| 31 |
+
files=[(filename, DATA_PATH)],
|
|
|
|
|
|
|
|
|
|
| 32 |
token=os.environ.get("HF_TOKEN"),
|
| 33 |
)
|
| 34 |
+
except Exception:
|
| 35 |
+
print(f"{filename} not found in bucket. Fetching from Baseball Savant...")
|
| 36 |
result = update_season_data()
|
| 37 |
if result["status"] != "ok":
|
| 38 |
raise RuntimeError(f"Auto-fetch failed: {result['message']}")
|
|
|
|
| 184 |
return entry["result"]
|
| 185 |
|
| 186 |
try:
|
| 187 |
+
from huggingface_hub import download_bucket_files
|
| 188 |
+
remote_path = _cache_hf_path(today, n_simulations)
|
| 189 |
+
with tempfile.TemporaryDirectory() as tmp_dir:
|
| 190 |
+
local_path = os.path.join(tmp_dir, "cache.json")
|
| 191 |
+
download_bucket_files(
|
| 192 |
+
_HF_REPO_ID,
|
| 193 |
+
files=[(remote_path, local_path)],
|
| 194 |
+
token=os.environ.get("HF_TOKEN"),
|
| 195 |
+
)
|
| 196 |
+
with open(local_path) as f:
|
| 197 |
+
stored = json.load(f)
|
| 198 |
cached_at = datetime.fromisoformat(stored["cached_at"])
|
| 199 |
if cached_at.tzinfo is None:
|
| 200 |
cached_at = cached_at.replace(tzinfo=timezone.utc)
|
|
|
|
| 215 |
payload = {"cached_at": now.isoformat(), "result": result}
|
| 216 |
tmp_path = None
|
| 217 |
try:
|
| 218 |
+
from huggingface_hub import batch_bucket_files
|
| 219 |
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
|
| 220 |
json.dump(payload, f)
|
| 221 |
tmp_path = f.name
|
| 222 |
+
batch_bucket_files(
|
| 223 |
+
_HF_REPO_ID,
|
| 224 |
+
add=[(tmp_path, _cache_hf_path(today, n_simulations))],
|
|
|
|
|
|
|
| 225 |
token=os.environ.get("HF_TOKEN"),
|
| 226 |
)
|
| 227 |
except Exception as e:
|
|
|
|
| 342 |
print(f"Saved {len(df):,} rows to {local_path}")
|
| 343 |
|
| 344 |
try:
|
| 345 |
+
from huggingface_hub import batch_bucket_files
|
| 346 |
+
batch_bucket_files(
|
| 347 |
+
_HF_REPO_ID,
|
| 348 |
+
add=[(local_path, filename)],
|
| 349 |
+
token=os.environ.get("HF_TOKEN"),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
)
|
| 351 |
except Exception as e:
|
| 352 |
+
return {"status": "error", "message": f"HF bucket upload failed: {e}", "rows": len(df), "filename": filename}
|
| 353 |
|
| 354 |
global DATA_PATH
|
| 355 |
DATA_PATH = local_path
|