Spaces:
Sleeping
Sleeping
Fix: download GGUF eagerly at container boot, outside the @spaces.GPU lease (was burning GPU-seconds on a 6-7min network download, causing the lease to be reclaimed mid-download with no error surfaced)
Browse files
app.py
CHANGED
|
@@ -22,8 +22,15 @@ Real fix history (found + fixed 2026-07-23, in order):
|
|
| 22 |
This stays entirely within Gradio's own request-handling pipeline, which is what ZeroGPU's
|
| 23 |
detection actually requires.
|
| 24 |
|
| 25 |
-
Cold-start note:
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
"""
|
| 28 |
from __future__ import annotations
|
| 29 |
|
|
@@ -38,14 +45,9 @@ FILENAME = "chatpdb_32b_v1_q4km.gguf"
|
|
| 38 |
N_CTX = 1536 # matches chatPDB's real training max_seq_length (config/train_config.yaml)
|
| 39 |
N_GPU_LAYERS = -1 # offload all layers to GPU
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
def _get_model_path() -> str:
|
| 45 |
-
global _model_path
|
| 46 |
-
if _model_path is None:
|
| 47 |
-
_model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 48 |
-
return _model_path
|
| 49 |
|
| 50 |
|
| 51 |
@spaces.GPU(duration=180)
|
|
@@ -97,7 +99,7 @@ def _generate_tokens(
|
|
| 97 |
from llama_cpp import Llama
|
| 98 |
|
| 99 |
llm = Llama(
|
| 100 |
-
model_path=
|
| 101 |
n_ctx=N_CTX,
|
| 102 |
n_gpu_layers=N_GPU_LAYERS,
|
| 103 |
verbose=False,
|
|
@@ -127,9 +129,10 @@ with gr.Blocks(title="chatPDB API") as demo:
|
|
| 127 |
gr.Markdown(
|
| 128 |
"## 🧬 chatPDB Inference API\n\n"
|
| 129 |
"Internal endpoint for [chatpdb.mdeller.com](https://chatpdb.mdeller.com). "
|
| 130 |
-
"Consumed via Gradio's own API: `POST /call/generate` then
|
| 131 |
-
"(SSE).\n\n"
|
| 132 |
-
"**Cold start:**
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
# Hidden inputs/outputs purely to register a real Gradio event with api_name="generate" --
|
|
|
|
| 22 |
This stays entirely within Gradio's own request-handling pipeline, which is what ZeroGPU's
|
| 23 |
detection actually requires.
|
| 24 |
|
| 25 |
+
Cold-start note: the 19.8GB GGUF is downloaded once at container boot (module import,
|
| 26 |
+
below), NOT inside the @spaces.GPU-decorated function. Real bug found + fixed 2026-07-23:
|
| 27 |
+
the download was originally triggered lazily from inside _generate_tokens(), which meant it
|
| 28 |
+
ran *inside* the ZeroGPU lease window and burned GPU-seconds on a plain network transfer --
|
| 29 |
+
confirmed via real Space logs showing a live download stuck at 81%/16.0GB after 5m24s
|
| 30 |
+
wall-clock, well past the `@spaces.GPU(duration=180)` cap, with no further log lines ever
|
| 31 |
+
written (the lease was silently reclaimed mid-download). Downloading eagerly at import time
|
| 32 |
+
means the file is already local on the Space's persistent container disk by the time any
|
| 33 |
+
real request arrives, so the GPU lease only has to cover model load + generation.
|
| 34 |
"""
|
| 35 |
from __future__ import annotations
|
| 36 |
|
|
|
|
| 45 |
N_CTX = 1536 # matches chatPDB's real training max_seq_length (config/train_config.yaml)
|
| 46 |
N_GPU_LAYERS = -1 # offload all layers to GPU
|
| 47 |
|
| 48 |
+
print(f"[chatPDB] downloading {FILENAME} from {REPO_ID} (one-time, outside GPU lease)...")
|
| 49 |
+
_MODEL_PATH = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 50 |
+
print(f"[chatPDB] model ready at {_MODEL_PATH}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
@spaces.GPU(duration=180)
|
|
|
|
| 99 |
from llama_cpp import Llama
|
| 100 |
|
| 101 |
llm = Llama(
|
| 102 |
+
model_path=_MODEL_PATH,
|
| 103 |
n_ctx=N_CTX,
|
| 104 |
n_gpu_layers=N_GPU_LAYERS,
|
| 105 |
verbose=False,
|
|
|
|
| 129 |
gr.Markdown(
|
| 130 |
"## 🧬 chatPDB Inference API\n\n"
|
| 131 |
"Internal endpoint for [chatpdb.mdeller.com](https://chatpdb.mdeller.com). "
|
| 132 |
+
"Consumed via Gradio's own API: `POST /gradio_api/call/generate` then "
|
| 133 |
+
"`GET /gradio_api/call/generate/<event_id>` (SSE).\n\n"
|
| 134 |
+
"**Cold start:** model download happens once at container boot, outside the GPU "
|
| 135 |
+
"lease. First real request per GPU lease takes ~10-30 s to load the GGUF onto the GPU."
|
| 136 |
)
|
| 137 |
|
| 138 |
# Hidden inputs/outputs purely to register a real Gradio event with api_name="generate" --
|