Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
| """Download and install the Eve SDK .deb package from HuggingFace Hub. | |
| Called during Docker build. Tries authentication in order: | |
| 1. Docker BuildKit secret ``MODEL_ACCESS_TOKEN`` (HF Spaces — automatic) | |
| 2. ``HF_TOKEN`` build arg (local builds) | |
| 3. No token (public repos only) | |
| Local usage:: | |
| docker build --build-arg HF_TOKEN=$(cat ~/.cache/huggingface/token) \ | |
| -t eve ./src/demos/eve_hmi | |
| """ | |
| import os | |
| import re | |
| import shutil | |
| import subprocess | |
| import sys | |
| from huggingface_hub import hf_hub_download | |
| EVE_REPO = "LatticeSemi/EdgeVisionEngine-Files" | |
| EVE_DEB = "LINUX_X86-4-7.3-eve-sensai_7.3.4~git20260623.fd5012d_amd64.deb" | |
| EVE_LICENSE = "libEveDevLicense.so" | |
| SECRET_PATH = "/run/secrets/MODEL_ACCESS_TOKEN" | |
| DOWNLOAD_DIR = "/tmp/eve" | |
| # Finetuned MOD weights live in the same gated repo as the SDK so they never | |
| # ship inside the (public) Space. mod_models.py reads them at runtime from | |
| # MOD_MODELS_DIR. Kept outside /app: this script runs as root before the | |
| # user-owned WORKDIR /app is created, and writing into /app here would leave it | |
| # root-owned and break the later user-stage download_examples.py write. | |
| MOD_MODELS = ("automotive-640x640.tflite", "office-640x640.tflite") | |
| MOD_MODELS_DIR = "/opt/eve-models" | |
| def get_token(): | |
| """Return an HF token from the first available source, or None.""" | |
| # 1. Docker BuildKit secret (HF Spaces injects MODEL_ACCESS_TOKEN automatically) | |
| if os.path.isfile(SECRET_PATH): | |
| with open(SECRET_PATH) as f: | |
| token = f.read().strip() | |
| if token: | |
| return token, "build secret (MODEL_ACCESS_TOKEN)" | |
| # 2. HF_TOKEN build arg forwarded as env var | |
| token = os.environ.get("HF_TOKEN", "").strip() | |
| if token: | |
| return token, "build arg (HF_TOKEN)" | |
| # 3. No token — will only work for public repos | |
| return None, "no auth (will fail for private repos)" | |
| def get_license_destination_path() -> str: | |
| """Parse the .deb package of EVE to extract the version.""" | |
| # Alright so regexes are fun, what we want here is to extract the version | |
| # of EVE's package since we need to copy the license into EVE's install | |
| # folder which is /opt/EVE-version-Source/lib. | |
| # For example, in LINUX_X86-531-dev-eve-development_7.0.531~git20260309.c5f1ee6_amd64.deb, | |
| # we want the version extracted to be 7.0.531. | |
| match = re.search(r"(?<=_)\d+(?:\.\d+)+(?=~)", EVE_DEB) | |
| if not match: | |
| raise RuntimeError("Could not parse EVE's package version.") | |
| version = match.group() | |
| return f"/opt/EVE-{version}-Source/lib" | |
| def download_mod_models(token: str | None) -> None: | |
| """Download the finetuned MOD ``.tflite`` weights into ``MOD_MODELS_DIR``.""" | |
| os.makedirs(MOD_MODELS_DIR, exist_ok=True) | |
| for model in MOD_MODELS: | |
| print(f"Downloading {model} from {EVE_REPO}...") | |
| hf_hub_download( | |
| repo_id=EVE_REPO, | |
| filename=model, | |
| local_dir=MOD_MODELS_DIR, | |
| token=token, | |
| ) | |
| def main(): | |
| token, auth_source = get_token() | |
| if token is None and "STAGING" in EVE_REPO: | |
| print( | |
| f"ERROR: No authentication token found.\n" | |
| f" Repo '{EVE_REPO}' is private and requires a token.\n" | |
| f" On HF Spaces: set MODEL_ACCESS_TOKEN as a Space secret.\n" | |
| f" Locally: docker build --build-arg HF_TOKEN=$(cat ~/.cache/huggingface/token) ...", | |
| file=sys.stderr, | |
| ) | |
| sys.exit(1) | |
| print(f"Downloading Eve SDK from {EVE_REPO} using {auth_source}...") | |
| deb_path = hf_hub_download( | |
| repo_id=EVE_REPO, | |
| filename=EVE_DEB, | |
| local_dir=DOWNLOAD_DIR, | |
| token=token, | |
| ) | |
| print(f"Installing {deb_path}...") | |
| subprocess.run(["apt-get", "install", "-y", deb_path], check=True) | |
| license_path = hf_hub_download( | |
| repo_id=EVE_REPO, filename=EVE_LICENSE, local_dir=DOWNLOAD_DIR, token=token | |
| ) | |
| destination_path = get_license_destination_path() | |
| print(f"Installing {license_path}...") | |
| subprocess.run(["mv", license_path, destination_path], check=True) | |
| download_mod_models(token) | |
| shutil.rmtree(DOWNLOAD_DIR, ignore_errors=True) | |
| print("Eve SDK and MOD models installed successfully.") | |
| if __name__ == "__main__": | |
| main() | |