| """ |
| llama-cpp-python bootstrap β MUST import before gradio or anything else. |
| |
| 2026-07-27: simplified after the great wheel hunt. |
| - PyPI = source-only (compiles 20+ min β the old "runs and runs" bug). |
| - abetlen CUDA indexes = abandoned (newest cu121 wheel is v0.2.59). Do NOT attempt. |
| - abetlen CPU index = current prebuilt wheels. That is the only fast path. |
| GPU speed returns via the Level-2 llama-server route (official llama.cpp CUDA |
| binaries + MTP flags) β separate spec, not this file. |
| """ |
|
|
| from __future__ import annotations |
|
|
| import os |
| import subprocess |
| import sys |
|
|
| CPU_WHEEL_INDEX = "https://abetlen.github.io/llama-cpp-python/whl/cpu" |
|
|
|
|
| def _pip(args: list) -> None: |
| cmd = [sys.executable, "-m", "pip", *args] |
| print(f"[LLAMA] $ {' '.join(cmd)}", flush=True) |
| env = os.environ.copy() |
| env["PIP_DISABLE_PIP_VERSION_CHECK"] = "1" |
| env["PIP_ROOT_USER_ACTION"] = "ignore" |
| subprocess.check_call(cmd, env=env) |
|
|
|
|
| def try_import_llama(): |
| import llama_cpp |
| return llama_cpp |
|
|
|
|
| def bootstrap_llama_cpp(): |
| try: |
| mod = try_import_llama() |
| print("[LLAMA] Ready (prebuilt wheel already installed)", flush=True) |
| except ImportError: |
| print("[LLAMA] Not installed β installing CPU wheel (fast path)...", flush=True) |
| _pip([ |
| "install", "--no-cache-dir", "llama-cpp-python==0.3.34", |
| "--extra-index-url", CPU_WHEEL_INDEX, |
| ]) |
| mod = try_import_llama() |
|
|
| |
| os.environ.setdefault("DAVIDAU_N_GPU_LAYERS", "0") |
| print("[LLAMA] CPU mode (n_gpu_layers=0) β GPU speed is the Level-2 llama-server job", flush=True) |
| return mod |
|
|
|
|
| bootstrap_llama_cpp() |