File size: 1,722 Bytes
119c847
04d56e6
119c847
04d56e6
119c847
 
 
5fc4228
119c847
 
5fc4228
bc9c615
5fc4228
 
 
bc9c615
119c847
 
 
 
5fc4228
04d56e6
3982649
119c847
 
 
 
 
04d56e6
29ea819
04d56e6
22edca0
04d56e6
 
 
 
 
 
22edca0
04d56e6
119c847
04d56e6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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).")