mc0117 commited on
Commit
075bee9
·
1 Parent(s): af7e6ad

some shit

Browse files
Files changed (1) hide show
  1. app.py +28 -37
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 hf_hub_download
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 Hub...")
29
  try:
30
- hf_hub_download(
31
- repo_id=_HF_REPO_ID,
32
- filename=filename,
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 (EntryNotFoundError, RepositoryNotFoundError):
39
- print(f"{filename} not found on HF Hub. Fetching from Baseball Savant...")
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 hf_hub_download
192
- local_path = hf_hub_download(
193
- repo_id=_HF_REPO_ID,
194
- filename=_cache_hf_path(today, n_simulations),
195
- repo_type="dataset",
196
- token=os.environ.get("HF_TOKEN"),
197
- force_download=True,
198
- )
199
- with open(local_path) as f:
200
- stored = json.load(f)
 
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 upload_file
222
  with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
223
  json.dump(payload, f)
224
  tmp_path = f.name
225
- upload_file(
226
- path_or_fileobj=tmp_path,
227
- path_in_repo=_cache_hf_path(today, n_simulations),
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 create_repo, upload_file
351
- token = os.environ.get("HF_TOKEN")
352
- create_repo(repo_id=_HF_REPO_ID, repo_type="dataset", token=token, exist_ok=True)
353
- upload_file(
354
- path_or_fileobj=local_path,
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 Hub upload failed: {e}", "rows": len(df), "filename": filename}
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