Upload model.py with huggingface_hub
Browse files
model.py
CHANGED
|
@@ -9,6 +9,7 @@ create_completion. Result: immediate response, no thinking.
|
|
| 9 |
|
| 10 |
from __future__ import annotations
|
| 11 |
|
|
|
|
| 12 |
import threading
|
| 13 |
import time
|
| 14 |
from pathlib import Path
|
|
@@ -80,7 +81,27 @@ def _load_model():
|
|
| 80 |
try:
|
| 81 |
from llama_cpp import Llama
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
_using_mock = False
|
| 85 |
except Exception as exc:
|
| 86 |
print(f"[model] Could not load GGUF ({gguf}): {exc}")
|
|
@@ -151,7 +172,7 @@ def stream_broadcast(
|
|
| 151 |
system_prompt: str,
|
| 152 |
user_prompt: str = DEFAULT_USER_PROMPT,
|
| 153 |
seed: int | None = None,
|
| 154 |
-
max_tokens: int =
|
| 155 |
temperature: float = 0.7,
|
| 156 |
):
|
| 157 |
"""Generator that yields the broadcast text token by token."""
|
|
|
|
| 9 |
|
| 10 |
from __future__ import annotations
|
| 11 |
|
| 12 |
+
import os
|
| 13 |
import threading
|
| 14 |
import time
|
| 15 |
from pathlib import Path
|
|
|
|
| 81 |
try:
|
| 82 |
from llama_cpp import Llama
|
| 83 |
|
| 84 |
+
# Thread count is the single biggest speed lever on CPU. Left to auto-
|
| 85 |
+
# detect, llama.cpp counts the *host's* cores inside the container and
|
| 86 |
+
# spawns far more threads than the Space actually has (HF cpu-basic =
|
| 87 |
+
# 2 vCPU), so they thrash and generation crawls. Pin it to the real
|
| 88 |
+
# quota on Spaces; use all cores locally. Override with LLAMA_THREADS.
|
| 89 |
+
env_threads = os.environ.get("LLAMA_THREADS")
|
| 90 |
+
if env_threads:
|
| 91 |
+
n_threads = int(env_threads)
|
| 92 |
+
elif os.environ.get("SPACE_ID"):
|
| 93 |
+
n_threads = 2 # HF cpu-basic
|
| 94 |
+
else:
|
| 95 |
+
n_threads = os.cpu_count() or 4
|
| 96 |
+
|
| 97 |
+
_llm = Llama(
|
| 98 |
+
model_path=str(gguf),
|
| 99 |
+
n_ctx=1536,
|
| 100 |
+
n_threads=n_threads,
|
| 101 |
+
n_batch=256,
|
| 102 |
+
verbose=False,
|
| 103 |
+
)
|
| 104 |
+
print(f"[model] llama.cpp loaded with n_threads={n_threads}")
|
| 105 |
_using_mock = False
|
| 106 |
except Exception as exc:
|
| 107 |
print(f"[model] Could not load GGUF ({gguf}): {exc}")
|
|
|
|
| 172 |
system_prompt: str,
|
| 173 |
user_prompt: str = DEFAULT_USER_PROMPT,
|
| 174 |
seed: int | None = None,
|
| 175 |
+
max_tokens: int = 160,
|
| 176 |
temperature: float = 0.7,
|
| 177 |
):
|
| 178 |
"""Generator that yields the broadcast text token by token."""
|