#!/usr/bin/env python3 """Hugging Face Space — MiniMax Video Prompt Enhancer (CPU / GGUF). Serves the fine-tuned 350M model via llama.cpp on CPU. No ZeroGPU quota, no CUDA, no login required for inference. llama-cpp-python is installed from a prebuilt manylinux wheel (see requirements.txt) — never compile from source on the Space. Model weights (Q4_K_M GGUF) come from preload_from_hub / Hub download unless a local models/*.gguf or GGUF_PATH is set. """ from __future__ import annotations import os import sys from pathlib import Path ROOT = Path(__file__).resolve().parent if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) # Fail fast with a clear message if the wheel did not install. try: import llama_cpp # noqa: F401 except Exception as exc: # pragma: no cover raise SystemExit( "llama-cpp-python failed to import. The Space must install the prebuilt " "manylinux wheel from requirements.txt (not a source build).\n" f"Import error: {exc}" ) from exc from huggingface_hub import hf_hub_download from minimax.apps.gradio import build_app from minimax.modeling.gguf_runtime import effective_cpus, resolve_gguf_file from minimax.paths import DEFAULT_GGUF_FILE, DEFAULT_GGUF_REPO GGUF_REPO = os.environ.get("GGUF_REPO", DEFAULT_GGUF_REPO) GGUF_FILE = os.environ.get("GGUF_FILE", DEFAULT_GGUF_FILE) LOCAL_MODELS = ROOT / "models" N_THREADS = int(os.environ.get("N_THREADS", "0")) or None N_CTX = int(os.environ.get("N_CTX", "4096")) def resolve_model_path() -> str: env = os.environ.get("GGUF_PATH") or os.environ.get("MODEL_ID") if env and (Path(env).exists() or str(env).endswith(".gguf")): p = Path(env) if p.exists(): return str(resolve_gguf_file(p) if p.is_dir() else p) if str(env).endswith(".gguf") and (LOCAL_MODELS / env).is_file(): return str(LOCAL_MODELS / env) if LOCAL_MODELS.is_dir() and list(LOCAL_MODELS.glob("*.gguf")): path = str(resolve_gguf_file(LOCAL_MODELS)) print(f"[boot] using local GGUF {path}", flush=True) return path # Prefer HF cache from preload_from_hub; falls back to download. print(f"[boot] resolving {GGUF_REPO}/{GGUF_FILE} …", flush=True) return hf_hub_download(repo_id=GGUF_REPO, filename=GGUF_FILE) print(f"[boot] llama_cpp ok cpus={effective_cpus()}", flush=True) MODEL_PATH = resolve_model_path() print( f"[boot] GGUF={MODEL_PATH} n_threads={N_THREADS or 'auto'} n_ctx={N_CTX}", flush=True, ) demo = build_app( model_path=MODEL_PATH, backend="gguf", n_ctx=N_CTX, n_threads=N_THREADS, n_gpu_layers=0, prefer_cpu=True, ) # Eager-load so the first visitor is not hit with cold-start load only. _session = demo._minimax_session # type: ignore[attr-defined] _loaded = _session.ensure_loaded() print( f"[boot] ready path={getattr(_loaded, 'path', MODEL_PATH)} " f"n_threads={getattr(_loaded, 'n_threads', '?')}", flush=True, ) if __name__ == "__main__": demo.queue(default_concurrency_limit=1) demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))