Fix Space build: defer llama-cpp install; slim requirements
Browse files- model_engine.py +48 -0
- packages.txt +0 -1
- requirements.txt +6 -9
model_engine.py
CHANGED
|
@@ -149,6 +149,53 @@ def _download_gguf() -> Path:
|
|
| 149 |
raise RuntimeError(f"Failed to download GGUF from {REPO_ID}: {last_err}")
|
| 150 |
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
def get_llm():
|
| 153 |
"""
|
| 154 |
Singleton Llama instance. Only loads on Spaces (or FORCE_LOCAL_MODEL).
|
|
@@ -164,6 +211,7 @@ def get_llm():
|
|
| 164 |
if _llm is not None:
|
| 165 |
return _llm
|
| 166 |
try:
|
|
|
|
| 167 |
from llama_cpp import Llama
|
| 168 |
|
| 169 |
path = _find_gguf_path()
|
|
|
|
| 149 |
raise RuntimeError(f"Failed to download GGUF from {REPO_ID}: {last_err}")
|
| 150 |
|
| 151 |
|
| 152 |
+
def _ensure_llama_cpp() -> None:
|
| 153 |
+
"""
|
| 154 |
+
Install llama-cpp-python at runtime on Spaces if missing.
|
| 155 |
+
Avoids failing the Space Docker build (CUDA/source wheels are fragile).
|
| 156 |
+
"""
|
| 157 |
+
try:
|
| 158 |
+
import llama_cpp # noqa: F401
|
| 159 |
+
return
|
| 160 |
+
except ImportError:
|
| 161 |
+
pass
|
| 162 |
+
|
| 163 |
+
import subprocess
|
| 164 |
+
import sys
|
| 165 |
+
|
| 166 |
+
env = os.environ.copy()
|
| 167 |
+
# Prefer CUDA build when toolchain allows; fall back handled by pip error paths.
|
| 168 |
+
env.setdefault("CMAKE_ARGS", "-DGGML_CUDA=on")
|
| 169 |
+
env.setdefault("FORCE_CMAKE", "1")
|
| 170 |
+
|
| 171 |
+
cmds = [
|
| 172 |
+
# Try CUDA wheel index first (may or may not match Space image)
|
| 173 |
+
[
|
| 174 |
+
sys.executable,
|
| 175 |
+
"-m",
|
| 176 |
+
"pip",
|
| 177 |
+
"install",
|
| 178 |
+
"--quiet",
|
| 179 |
+
"llama-cpp-python",
|
| 180 |
+
"--extra-index-url",
|
| 181 |
+
"https://abetlen.github.io/llama-cpp-python/whl/cu124",
|
| 182 |
+
],
|
| 183 |
+
# Generic install (source or manylinux wheel)
|
| 184 |
+
[sys.executable, "-m", "pip", "install", "--quiet", "llama-cpp-python"],
|
| 185 |
+
]
|
| 186 |
+
last: Optional[Exception] = None
|
| 187 |
+
for cmd in cmds:
|
| 188 |
+
try:
|
| 189 |
+
subprocess.check_call(cmd, env=env)
|
| 190 |
+
import llama_cpp # noqa: F401
|
| 191 |
+
|
| 192 |
+
return
|
| 193 |
+
except Exception as exc: # noqa: BLE001
|
| 194 |
+
last = exc
|
| 195 |
+
continue
|
| 196 |
+
raise RuntimeError(f"Failed to install llama-cpp-python: {last}")
|
| 197 |
+
|
| 198 |
+
|
| 199 |
def get_llm():
|
| 200 |
"""
|
| 201 |
Singleton Llama instance. Only loads on Spaces (or FORCE_LOCAL_MODEL).
|
|
|
|
| 211 |
if _llm is not None:
|
| 212 |
return _llm
|
| 213 |
try:
|
| 214 |
+
_ensure_llama_cpp()
|
| 215 |
from llama_cpp import Llama
|
| 216 |
|
| 217 |
path = _find_gguf_path()
|
packages.txt
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# System packages for Spaces builds that compile llama-cpp-python
|
| 2 |
build-essential
|
| 3 |
cmake
|
| 4 |
ninja-build
|
|
|
|
|
|
|
| 1 |
build-essential
|
| 2 |
cmake
|
| 3 |
ninja-build
|
requirements.txt
CHANGED
|
@@ -1,11 +1,8 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
huggingface_hub>=0.26.0
|
| 4 |
hf_transfer>=0.1.8
|
| 5 |
-
|
| 6 |
-
# Build from source if no wheel matches (may take several minutes).
|
| 7 |
-
llama-cpp-python>=0.3.0
|
| 8 |
-
|
| 9 |
-
# Notes:
|
| 10 |
-
# - App supports Gradio 5 (type=messages) and Gradio 6 (buttons=["copy"]).
|
| 11 |
-
# - On Spaces, theme/css/js/head are applied in app.py via launch kwargs when run as __main__.
|
|
|
|
| 1 |
+
# Keep Space build lightweight and reliable.
|
| 2 |
+
# llama-cpp-python is installed on Spaces at first model load (see model_engine.py)
|
| 3 |
+
# because CUDA wheels / source builds often break the Docker image build.
|
| 4 |
+
gradio==5.49.1
|
| 5 |
+
spaces>=0.30.0,<0.60.0
|
| 6 |
huggingface_hub>=0.26.0
|
| 7 |
hf_transfer>=0.1.8
|
| 8 |
+
numpy>=1.24.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|