Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +25 -18
- docker-compose.yml +1 -0
app.py
CHANGED
|
@@ -39,6 +39,7 @@ N_CTX = int(os.environ.get("N_CTX", "4096"))
|
|
| 39 |
N_THREADS = int(os.environ.get("N_THREADS", str(os.cpu_count() or 4)))
|
| 40 |
N_BATCH = int(os.environ.get("N_BATCH", "512"))
|
| 41 |
VERBOSE = os.environ.get("VERBOSE", "false").lower() == "true"
|
|
|
|
| 42 |
|
| 43 |
# ---------------------------------------------------------------------------
|
| 44 |
# Lazy model holder
|
|
@@ -57,27 +58,33 @@ def _download_model() -> None:
|
|
| 57 |
return
|
| 58 |
|
| 59 |
path.parent.mkdir(parents=True, exist_ok=True)
|
| 60 |
-
logger.info(f"Model not found — downloading from {MODEL_URL}
|
| 61 |
logger.info("This will take a while on first boot (file is ~9 GB).")
|
| 62 |
|
| 63 |
import urllib.request
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
tmp = Path(str(MODEL_PATH) + ".part")
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
tmp.rename(path)
|
| 80 |
-
logger.info(f"Download complete
|
| 81 |
|
| 82 |
|
| 83 |
def _load_model_background() -> None:
|
|
@@ -85,7 +92,7 @@ def _load_model_background() -> None:
|
|
| 85 |
global _llm, _llm_error
|
| 86 |
try:
|
| 87 |
_download_model()
|
| 88 |
-
logger.info(f"Loading model into memory from {MODEL_PATH}
|
| 89 |
llm = Llama(
|
| 90 |
model_path=MODEL_PATH,
|
| 91 |
n_ctx=N_CTX,
|
|
@@ -97,7 +104,7 @@ def _load_model_background() -> None:
|
|
| 97 |
)
|
| 98 |
with _llm_lock:
|
| 99 |
_llm = llm
|
| 100 |
-
logger.info("Model loaded and ready
|
| 101 |
except Exception as exc:
|
| 102 |
_llm_error = str(exc)
|
| 103 |
logger.error(f"Failed to load model: {exc}")
|
|
@@ -140,7 +147,7 @@ async def startup_event():
|
|
| 140 |
starts immediately and stays responsive during the (long) load phase."""
|
| 141 |
t = threading.Thread(target=_load_model_background, daemon=True)
|
| 142 |
t.start()
|
| 143 |
-
logger.info("Server is up. Model loading in background
|
| 144 |
|
| 145 |
|
| 146 |
# ---------------------------------------------------------------------------
|
|
|
|
| 39 |
N_THREADS = int(os.environ.get("N_THREADS", str(os.cpu_count() or 4)))
|
| 40 |
N_BATCH = int(os.environ.get("N_BATCH", "512"))
|
| 41 |
VERBOSE = os.environ.get("VERBOSE", "false").lower() == "true"
|
| 42 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 43 |
|
| 44 |
# ---------------------------------------------------------------------------
|
| 45 |
# Lazy model holder
|
|
|
|
| 58 |
return
|
| 59 |
|
| 60 |
path.parent.mkdir(parents=True, exist_ok=True)
|
| 61 |
+
logger.info(f"Model not found — downloading from {MODEL_URL} ...")
|
| 62 |
logger.info("This will take a while on first boot (file is ~9 GB).")
|
| 63 |
|
| 64 |
import urllib.request
|
| 65 |
|
| 66 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
|
| 67 |
+
if not HF_TOKEN:
|
| 68 |
+
logger.warning("HF_TOKEN not set — download may fail for gated models.")
|
| 69 |
+
|
| 70 |
tmp = Path(str(MODEL_PATH) + ".part")
|
| 71 |
+
req = urllib.request.Request(MODEL_URL, headers=headers)
|
| 72 |
+
|
| 73 |
+
with urllib.request.urlopen(req) as response, open(tmp, "wb") as out:
|
| 74 |
+
total = int(response.headers.get("Content-Length", 0))
|
| 75 |
+
downloaded = 0
|
| 76 |
+
last_pct = -1
|
| 77 |
+
while chunk := response.read(1 << 20): # 1 MB chunks
|
| 78 |
+
out.write(chunk)
|
| 79 |
+
downloaded += len(chunk)
|
| 80 |
+
if total:
|
| 81 |
+
pct = min(int(downloaded * 100 / total), 100)
|
| 82 |
+
if pct != last_pct and pct % 5 == 0:
|
| 83 |
+
logger.info(f"Download progress: {pct}%")
|
| 84 |
+
last_pct = pct
|
| 85 |
+
|
| 86 |
tmp.rename(path)
|
| 87 |
+
logger.info(f"Download complete -> {MODEL_PATH}")
|
| 88 |
|
| 89 |
|
| 90 |
def _load_model_background() -> None:
|
|
|
|
| 92 |
global _llm, _llm_error
|
| 93 |
try:
|
| 94 |
_download_model()
|
| 95 |
+
logger.info(f"Loading model into memory from {MODEL_PATH} ...")
|
| 96 |
llm = Llama(
|
| 97 |
model_path=MODEL_PATH,
|
| 98 |
n_ctx=N_CTX,
|
|
|
|
| 104 |
)
|
| 105 |
with _llm_lock:
|
| 106 |
_llm = llm
|
| 107 |
+
logger.info("Model loaded and ready")
|
| 108 |
except Exception as exc:
|
| 109 |
_llm_error = str(exc)
|
| 110 |
logger.error(f"Failed to load model: {exc}")
|
|
|
|
| 147 |
starts immediately and stays responsive during the (long) load phase."""
|
| 148 |
t = threading.Thread(target=_load_model_background, daemon=True)
|
| 149 |
t.start()
|
| 150 |
+
logger.info("Server is up. Model loading in background -- see /health for status.")
|
| 151 |
|
| 152 |
|
| 153 |
# ---------------------------------------------------------------------------
|
docker-compose.yml
CHANGED
|
@@ -21,6 +21,7 @@ services:
|
|
| 21 |
N_THREADS: "8" # set to your physical CPU core count
|
| 22 |
N_BATCH: "512"
|
| 23 |
VERBOSE: "false"
|
|
|
|
| 24 |
restart: unless-stopped
|
| 25 |
|
| 26 |
volumes:
|
|
|
|
| 21 |
N_THREADS: "8" # set to your physical CPU core count
|
| 22 |
N_BATCH: "512"
|
| 23 |
VERBOSE: "false"
|
| 24 |
+
HF_TOKEN: "hf_xxxxxxxxxxxxxxxxxxxx" # replace with your HuggingFace read token
|
| 25 |
restart: unless-stopped
|
| 26 |
|
| 27 |
volumes:
|