agrovia-detection-api / app /core /model_loader.py
Kaaado's picture
feat: pre-download all 8 AI models on boot via huggingface_hub SDK
5fc4228
Raw History Blame Contribute Delete
1.72 kB
import os
import shutil
from pathlib import Path
from huggingface_hub import hf_hub_download
REPO_ID = "Kaaado/agrovia-models"
# All 8 production AI models from Hugging Face Repository
MODEL_MANIFEST = [
("data/cnn", "cnn_server_int8.onnx"),
("data/cnn", "cnn_server_fp32.onnx"),
("data/cnn", "cnn_limited.onnx"),
("data/yolo", "yolov8_optimized_mobile_int8.tflite"),
("data/yolo", "yolov8_standard_mobile_fp16.tflite"),
("data/yolo", "yolov8_high_res_web_fp32.tflite"),
("data/yolo", "yolov8s_limited.onnx"),
("data/yolo", "yolov8_mobile_web.onnx"),
]
def ensure_models_downloaded(base_dir: str = "."):
"""Downloads all 8 production AI models using huggingface_hub SDK on startup."""
print("Verifying and downloading Agrovia AI models via HF Hub SDK...")
for rel_dir, filename in MODEL_MANIFEST:
dest_dir = Path(base_dir) / rel_dir
dest_dir.mkdir(parents=True, exist_ok=True)
dest_path = dest_dir / filename
# Require file to exist and be larger than 1MB (to avoid git-lfs text pointers)
if not dest_path.exists() or dest_path.stat().st_size < 1_000_000:
print(f"Downloading {filename} from HF Hub ({REPO_ID})...")
try:
downloaded_file = hf_hub_download(
repo_id=REPO_ID,
filename=filename,
local_dir=str(dest_dir),
)
print(f"Model {filename} successfully saved to {downloaded_file}")
except Exception as exc:
print(f"Failed to download {filename} via HF Hub: {exc}")
else:
print(f"Model {filename} verified ({dest_path.stat().st_size:,} bytes).")