"""Loads the fine-tuned GGUF via llama-cpp-python. Pattern follows the HF ZeroGPU + small-talk reference: - hf_hub_download files at module import (warm cache) - instantiate Llama inside @spaces.GPU function on each request """ from huggingface_hub import hf_hub_download from .config import MODEL_REPO, GGUF_FILE, MMPROJ_FILE print(f"[model_loader] downloading {MODEL_REPO}/{GGUF_FILE} …") MODEL_PATH = hf_hub_download(repo_id=MODEL_REPO, filename=GGUF_FILE) try: MMPROJ_PATH = hf_hub_download(repo_id=MODEL_REPO, filename=MMPROJ_FILE) print(f"[model_loader] mmproj ready: {MMPROJ_PATH}") except Exception as e: print(f"[model_loader] mmproj unavailable ({e}) — vision disabled") MMPROJ_PATH = None def make_llm(): """Create a fresh Llama inside a GPU context. The .gguf file is filesystem-cached, so this is fast after the first call.""" from llama_cpp import Llama chat_handler = None if MMPROJ_PATH: try: from llama_cpp.llama_chat_format import MiniCPMv26ChatHandler chat_handler = MiniCPMv26ChatHandler(clip_model_path=MMPROJ_PATH, verbose=False) except Exception as e: print(f"[model_loader] vision chat handler failed: {e}") return Llama( model_path=MODEL_PATH, chat_handler=chat_handler, n_gpu_layers=-1, n_ctx=8192, flash_attn=True, verbose=False, )