Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -51,21 +51,28 @@ EXAMPLES = [
|
|
| 51 |
]
|
| 52 |
|
| 53 |
# --- On ZeroGPU, torch is patched at import time and there is NO GPU at
|
| 54 |
-
# module scope, so we cannot load model weights
|
| 55 |
-
#
|
| 56 |
-
#
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
|
| 59 |
tokenizer.pad_token = tokenizer.eos_token
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
_model = None # lazily populated inside the @spaces.GPU function
|
| 63 |
|
| 64 |
|
| 65 |
def _load_model():
|
| 66 |
"""
|
| 67 |
-
Load base + LoRA adapter and place on CUDA. Called
|
| 68 |
-
inside the @spaces.GPU function where a GPU is
|
|
|
|
| 69 |
"""
|
| 70 |
global _model
|
| 71 |
if _model is not None:
|
|
@@ -100,7 +107,7 @@ def _decode(output_ids, input_len: int) -> str:
|
|
| 100 |
return tokenizer.decode(gen, skip_special_tokens=True).strip()
|
| 101 |
|
| 102 |
|
| 103 |
-
@spaces.GPU(duration=
|
| 104 |
def rewrite(text: str):
|
| 105 |
"""
|
| 106 |
Produce base ('before') and fine-tuned ('after') rewrites.
|
|
|
|
| 51 |
]
|
| 52 |
|
| 53 |
# --- On ZeroGPU, torch is patched at import time and there is NO GPU at
|
| 54 |
+
# module scope, so we cannot load model weights onto CUDA here. But we CAN
|
| 55 |
+
# pre-download the files to the local cache at startup, so the GPU call
|
| 56 |
+
# only has to load from disk (fast) and stays within the 60s GPU budget. ---
|
| 57 |
+
from huggingface_hub import snapshot_download
|
| 58 |
+
|
| 59 |
+
print("Loading tokenizer and pre-downloading model files...")
|
| 60 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
|
| 61 |
tokenizer.pad_token = tokenizer.eos_token
|
| 62 |
+
|
| 63 |
+
# Warm the HF cache so weights are on local disk before any GPU call.
|
| 64 |
+
snapshot_download(BASE_MODEL)
|
| 65 |
+
snapshot_download(ADAPTER_ID, token=HF_TOKEN)
|
| 66 |
+
print("Tokenizer ready and weights cached; model loads on first request.")
|
| 67 |
|
| 68 |
_model = None # lazily populated inside the @spaces.GPU function
|
| 69 |
|
| 70 |
|
| 71 |
def _load_model():
|
| 72 |
"""
|
| 73 |
+
Load base + LoRA adapter from the local cache and place on CUDA. Called
|
| 74 |
+
once, lazily, from inside the @spaces.GPU function where a GPU is
|
| 75 |
+
attached. Because files are already cached, this is just a disk load.
|
| 76 |
"""
|
| 77 |
global _model
|
| 78 |
if _model is not None:
|
|
|
|
| 107 |
return tokenizer.decode(gen, skip_special_tokens=True).strip()
|
| 108 |
|
| 109 |
|
| 110 |
+
@spaces.GPU(duration=60)
|
| 111 |
def rewrite(text: str):
|
| 112 |
"""
|
| 113 |
Produce base ('before') and fine-tuned ('after') rewrites.
|