llm.py corrected
Browse files- app/models/llm.py +65 -18
app/models/llm.py
CHANGED
|
@@ -6,9 +6,6 @@ from typing import Any
|
|
| 6 |
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
|
| 9 |
-
# HF_REPO = os.getenv("LLAMA_HF_REPO", "openbmb/MiniCPM5-1B-GGUF")
|
| 10 |
-
# HF_FILENAME = os.getenv("LLAMA_HF_FILENAME", "MiniCPM5-1B-Q4_K_M.gguf")
|
| 11 |
-
|
| 12 |
HF_REPO = os.getenv("LLAMA_HF_REPO", "ps1811/advisor-minicpm-finetuned-gguf")
|
| 13 |
HF_FILENAME = os.getenv("LLAMA_HF_FILENAME", "advisor-minicpm-q4_k_m.gguf")
|
| 14 |
|
|
@@ -16,6 +13,36 @@ _model: Any = None
|
|
| 16 |
_init_lock = threading.Lock()
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def _preload_cuda_libs() -> None:
|
| 20 |
"""Expose pip-installed CUDA runtime to llama.cpp on ZeroGPU (no system libcudart)."""
|
| 21 |
try:
|
|
@@ -45,19 +72,29 @@ def _preload_cuda_libs() -> None:
|
|
| 45 |
|
| 46 |
def load_model() -> Any:
|
| 47 |
global _model
|
| 48 |
-
print("
|
| 49 |
|
| 50 |
if _model is not None:
|
| 51 |
-
print("
|
| 52 |
return _model
|
| 53 |
|
| 54 |
with _init_lock:
|
| 55 |
if _model is not None:
|
| 56 |
return _model
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
_preload_cuda_libs()
|
| 63 |
from llama_cpp import Llama
|
|
@@ -65,19 +102,29 @@ def load_model() -> Any:
|
|
| 65 |
gpu_layers = int(os.getenv("LLAMA_GPU_LAYERS", "-1"))
|
| 66 |
n_ctx = int(os.getenv("LLAMA_N_CTX", "2048"))
|
| 67 |
n_threads = int(os.getenv("LLAMA_N_THREADS", "4"))
|
|
|
|
| 68 |
print(
|
| 69 |
-
f"
|
| 70 |
-
f"(n_gpu_layers={gpu_layers}, n_ctx={n_ctx}, n_threads={n_threads})",
|
| 71 |
flush=True,
|
| 72 |
)
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
return _model
|
|
|
|
| 6 |
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
HF_REPO = os.getenv("LLAMA_HF_REPO", "ps1811/advisor-minicpm-finetuned-gguf")
|
| 10 |
HF_FILENAME = os.getenv("LLAMA_HF_FILENAME", "advisor-minicpm-q4_k_m.gguf")
|
| 11 |
|
|
|
|
| 13 |
_init_lock = threading.Lock()
|
| 14 |
|
| 15 |
|
| 16 |
+
def _validate_gguf_file(model_path: str) -> None:
|
| 17 |
+
if not os.path.isfile(model_path):
|
| 18 |
+
raise FileNotFoundError(f"Downloaded model file does not exist: {model_path}")
|
| 19 |
+
|
| 20 |
+
size_bytes = os.path.getsize(model_path)
|
| 21 |
+
with open(model_path, "rb") as fh:
|
| 22 |
+
magic = fh.read(4)
|
| 23 |
+
fh.seek(0)
|
| 24 |
+
first_128 = fh.read(128)
|
| 25 |
+
|
| 26 |
+
print(
|
| 27 |
+
f"[load_model] GGUF file check: size={size_bytes / (1024 ** 2):.1f} MB, "
|
| 28 |
+
f"magic={magic!r}",
|
| 29 |
+
flush=True,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
if magic != b"GGUF":
|
| 33 |
+
preview = first_128.decode("utf-8", errors="replace")
|
| 34 |
+
raise RuntimeError(
|
| 35 |
+
"Downloaded file is not a valid GGUF file. "
|
| 36 |
+
f"Expected magic b'GGUF', got {magic!r}. First bytes: {preview!r}"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
if size_bytes < 100 * 1024 * 1024:
|
| 40 |
+
raise RuntimeError(
|
| 41 |
+
"Downloaded GGUF file is unexpectedly small. "
|
| 42 |
+
f"Size was {size_bytes} bytes; this often means a bad upload or LFS pointer."
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
def _preload_cuda_libs() -> None:
|
| 47 |
"""Expose pip-installed CUDA runtime to llama.cpp on ZeroGPU (no system libcudart)."""
|
| 48 |
try:
|
|
|
|
| 72 |
|
| 73 |
def load_model() -> Any:
|
| 74 |
global _model
|
| 75 |
+
print("[load_model] called", flush=True)
|
| 76 |
|
| 77 |
if _model is not None:
|
| 78 |
+
print("[load_model] returning cached model", flush=True)
|
| 79 |
return _model
|
| 80 |
|
| 81 |
with _init_lock:
|
| 82 |
if _model is not None:
|
| 83 |
return _model
|
| 84 |
|
| 85 |
+
force_download = os.getenv("LLAMA_FORCE_DOWNLOAD", "0") == "1"
|
| 86 |
+
print(
|
| 87 |
+
f"[load_model] downloading/resolving model {HF_REPO}/{HF_FILENAME} "
|
| 88 |
+
f"(force_download={force_download})...",
|
| 89 |
+
flush=True,
|
| 90 |
+
)
|
| 91 |
+
model_path = hf_hub_download(
|
| 92 |
+
repo_id=HF_REPO,
|
| 93 |
+
filename=HF_FILENAME,
|
| 94 |
+
force_download=force_download,
|
| 95 |
+
)
|
| 96 |
+
print(f"[load_model] model resolved at {model_path}", flush=True)
|
| 97 |
+
_validate_gguf_file(model_path)
|
| 98 |
|
| 99 |
_preload_cuda_libs()
|
| 100 |
from llama_cpp import Llama
|
|
|
|
| 102 |
gpu_layers = int(os.getenv("LLAMA_GPU_LAYERS", "-1"))
|
| 103 |
n_ctx = int(os.getenv("LLAMA_N_CTX", "2048"))
|
| 104 |
n_threads = int(os.getenv("LLAMA_N_THREADS", "4"))
|
| 105 |
+
verbose = os.getenv("LLAMA_VERBOSE", "0") == "1"
|
| 106 |
print(
|
| 107 |
+
f"[load_model] initializing Llama "
|
| 108 |
+
f"(n_gpu_layers={gpu_layers}, n_ctx={n_ctx}, n_threads={n_threads}, verbose={verbose})",
|
| 109 |
flush=True,
|
| 110 |
)
|
| 111 |
|
| 112 |
+
try:
|
| 113 |
+
_model = Llama(
|
| 114 |
+
model_path=model_path,
|
| 115 |
+
n_ctx=n_ctx,
|
| 116 |
+
n_gpu_layers=gpu_layers,
|
| 117 |
+
n_threads=n_threads,
|
| 118 |
+
verbose=verbose,
|
| 119 |
+
)
|
| 120 |
+
except Exception as exc:
|
| 121 |
+
raise RuntimeError(
|
| 122 |
+
f"llama.cpp failed to load a valid GGUF file from {model_path}. "
|
| 123 |
+
"If the file check above says magic=b'GGUF' and the size is large, "
|
| 124 |
+
"this is usually a llama-cpp-python/GGUF compatibility issue or a bad quantized export. "
|
| 125 |
+
"Try setting LLAMA_VERBOSE=1 for the next run."
|
| 126 |
+
) from exc
|
| 127 |
+
|
| 128 |
+
print("[load_model] model initialized", flush=True)
|
| 129 |
|
| 130 |
return _model
|