File size: 1,275 Bytes
16dd7c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Download the Keras model from Google Drive when it is not already on disk.

Public sharing link:
https://drive.google.com/file/d/1VHHRMPoqpTOy53s2mkYwrBp2LOYDbUWL/view

Override file id with env GDRIVE_MODEL_FILE_ID if you mirror the file elsewhere.
"""
from __future__ import annotations

import os
import sys

DEFAULT_DRIVE_FILE_ID = "1VHHRMPoqpTOy53s2mkYwrBp2LOYDbUWL"


def ensure_model_file(model_path: str, *, drive_file_id: str | None = None) -> bool:
    """Return True if ``model_path`` exists after a possible Drive download."""
    if os.path.isfile(model_path):
        return True
    directory = os.path.dirname(model_path)
    if directory:
        os.makedirs(directory, exist_ok=True)
    fid = (drive_file_id or os.environ.get("GDRIVE_MODEL_FILE_ID") or DEFAULT_DRIVE_FILE_ID).strip()
    if not fid:
        return False
    try:
        import gdown
    except ImportError:
        print("Install gdown to fetch the model from Google Drive.", file=sys.stderr)
        return False
    url = f"https://drive.google.com/uc?id={fid}"
    try:
        gdown.download(url, model_path, quiet=False)
    except Exception as exc:
        print(f"Google Drive download failed: {exc}", file=sys.stderr)
        return False
    return os.path.isfile(model_path)