Spaces:
Running
Running
“Namhyun-Kim”
commited on
Commit
·
dc6f94e
1
Parent(s):
79fa077
Fix hub download repo_type and rely on model repo data
Browse files
app.py
CHANGED
|
@@ -62,12 +62,52 @@ def _safe_load_tensor(path: Path):
|
|
| 62 |
return torch.load(path, weights_only=False)
|
| 63 |
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
def _ensure_local_file(local_path: Path, hub_filename: str) -> Optional[Path]:
|
| 66 |
"""Ensure a file exists locally; try Hub download if missing."""
|
| 67 |
if local_path.exists():
|
| 68 |
return local_path
|
| 69 |
try:
|
| 70 |
-
cached = hf_hub_download(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
cached_path = Path(cached)
|
| 72 |
shutil.copyfile(cached_path, local_path)
|
| 73 |
print(f"[INFO] Downloaded {hub_filename} from Hub to {local_path}")
|
|
@@ -84,7 +124,7 @@ def load_augmented_samples() -> Tuple[List[Dict[str, object]], bool]:
|
|
| 84 |
print(f"[INFO] Loading MoE-augmented dataset from {MOE_DATA_PATH}")
|
| 85 |
return _safe_load_tensor(MOE_DATA_PATH), True
|
| 86 |
if not DEMO_DATA_PATH.exists():
|
| 87 |
-
|
| 88 |
print(f"[WARN] {MOE_DATA_PATH} missing; falling back to base data only")
|
| 89 |
return _safe_load_tensor(DEMO_DATA_PATH), False
|
| 90 |
|
|
|
|
| 62 |
return torch.load(path, weights_only=False)
|
| 63 |
|
| 64 |
|
| 65 |
+
def _create_dummy_dataset(base_path: Path, moe_path: Path) -> None:
|
| 66 |
+
"""Create a tiny synthetic dataset so the Space can start even if hub download fails."""
|
| 67 |
+
print(f"[WARN] Creating synthetic demo dataset at {base_path}")
|
| 68 |
+
rng = np.random.default_rng(42)
|
| 69 |
+
samples: List[Dict[str, object]] = []
|
| 70 |
+
techs = ["LTE", "WiFi", "5G"]
|
| 71 |
+
snrs = ["SNR0dB", "SNR10dB", "SNR20dB"]
|
| 72 |
+
mods = ["QPSK", "16QAM", "64QAM"]
|
| 73 |
+
mobs = ["pedestrian", "vehicular"]
|
| 74 |
+
|
| 75 |
+
for i in range(30):
|
| 76 |
+
tech = techs[i % len(techs)]
|
| 77 |
+
snr = snrs[i % len(snrs)]
|
| 78 |
+
mob = mobs[i % len(mobs)]
|
| 79 |
+
mod = mods[i % len(mods)]
|
| 80 |
+
spectrogram = rng.normal(size=(128, 128)).astype(np.float32)
|
| 81 |
+
embedding = rng.normal(size=(128,)).astype(np.float32)
|
| 82 |
+
moe_embedding = rng.normal(size=(128,)).astype(np.float32)
|
| 83 |
+
samples.append(
|
| 84 |
+
{
|
| 85 |
+
"tech": tech,
|
| 86 |
+
"snr": snr,
|
| 87 |
+
"mod": mod,
|
| 88 |
+
"mob": mob,
|
| 89 |
+
"data": spectrogram,
|
| 90 |
+
"embedding": embedding,
|
| 91 |
+
"moe_embedding": moe_embedding,
|
| 92 |
+
}
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
torch.save(samples, base_path)
|
| 96 |
+
torch.save(samples, moe_path)
|
| 97 |
+
print(f"[INFO] Synthetic dataset written to {base_path} and {moe_path}")
|
| 98 |
+
|
| 99 |
+
|
| 100 |
def _ensure_local_file(local_path: Path, hub_filename: str) -> Optional[Path]:
|
| 101 |
"""Ensure a file exists locally; try Hub download if missing."""
|
| 102 |
if local_path.exists():
|
| 103 |
return local_path
|
| 104 |
try:
|
| 105 |
+
cached = hf_hub_download(
|
| 106 |
+
repo_id=HUB_REPO_ID,
|
| 107 |
+
filename=hub_filename,
|
| 108 |
+
token=HF_TOKEN,
|
| 109 |
+
repo_type="model",
|
| 110 |
+
)
|
| 111 |
cached_path = Path(cached)
|
| 112 |
shutil.copyfile(cached_path, local_path)
|
| 113 |
print(f"[INFO] Downloaded {hub_filename} from Hub to {local_path}")
|
|
|
|
| 124 |
print(f"[INFO] Loading MoE-augmented dataset from {MOE_DATA_PATH}")
|
| 125 |
return _safe_load_tensor(MOE_DATA_PATH), True
|
| 126 |
if not DEMO_DATA_PATH.exists():
|
| 127 |
+
_create_dummy_dataset(DEMO_DATA_PATH, MOE_DATA_PATH)
|
| 128 |
print(f"[WARN] {MOE_DATA_PATH} missing; falling back to base data only")
|
| 129 |
return _safe_load_tensor(DEMO_DATA_PATH), False
|
| 130 |
|