Spaces:
Sleeping
Sleeping
| """ | |
| Download exported model from Hugging Face Hub before API startup. | |
| Set Space secret / env: | |
| HF_MODEL_REPO=your-username/deberta-complexity-lcp | |
| MODEL_PATH=/app/model | |
| """ | |
| from __future__ import annotations | |
| import os | |
| import sys | |
| from pathlib import Path | |
| def main() -> None: | |
| repo = os.environ.get("HF_MODEL_REPO", "").strip() | |
| target = Path(os.environ.get("MODEL_PATH", "/home/user/model")) | |
| if not repo: | |
| print("HF_MODEL_REPO not set — expecting model_weights.pt already on disk.") | |
| weights = target / "model_weights.pt" | |
| if weights.exists(): | |
| print(f"Found {weights}") | |
| else: | |
| print(f"WARN: no weights at {weights}", file=sys.stderr) | |
| return | |
| weights = target / "model_weights.pt" | |
| if weights.exists(): | |
| print(f"Model already present at {weights}") | |
| return | |
| print(f"Downloading {repo} → {target}") | |
| from huggingface_hub import snapshot_download | |
| token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN") | |
| target.mkdir(parents=True, exist_ok=True) | |
| snapshot_download( | |
| repo_id=repo, | |
| local_dir=str(target), | |
| token=token, | |
| allow_patterns=[ | |
| "model_weights.pt", | |
| "config.json", | |
| "tokenizer/*", | |
| "*.json", | |
| ], | |
| ) | |
| if not weights.exists(): | |
| raise FileNotFoundError( | |
| f"Download finished but {weights} missing. " | |
| f"Upload model with: python deploy/huggingface/upload_model_to_hub.py" | |
| ) | |
| print(f"Ready: {weights} ({weights.stat().st_size / 1e6:.1f} MB)") | |
| if __name__ == "__main__": | |
| main() | |