Spaces:
Paused
Paused
akash Cursor commited on
Commit Β·
5889719
1
Parent(s): cf81d51
Persist the fine-tuned model by pushing it to the Hub.
Browse filesLoad Surajnitp/sentiment-trackmodel first, and auto-upload after training when HF_TOKEN is set so restarts keep the custom weights.
Co-authored-by: Cursor <cursoragent@cursor.com>
app.py
CHANGED
|
@@ -13,6 +13,7 @@ from transformers import pipeline
|
|
| 13 |
|
| 14 |
classifier = None
|
| 15 |
|
|
|
|
| 16 |
LOCAL_MODEL_DIR = os.path.abspath("./my_finetuned_model")
|
| 17 |
DATA_MODEL_DIR = "/data/my_finetuned_model"
|
| 18 |
|
|
@@ -50,30 +51,29 @@ def format_status(text):
|
|
| 50 |
|
| 51 |
|
| 52 |
def load_model():
|
| 53 |
-
"""Load
|
| 54 |
global classifier
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
try:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
f"[load_model]
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
f"[load_model] data_dir={DATA_MODEL_DIR} exists={os.path.isdir(DATA_MODEL_DIR)} valid={model_dir_is_valid(DATA_MODEL_DIR)}",
|
| 71 |
-
flush=True,
|
| 72 |
-
)
|
| 73 |
|
| 74 |
model_path = find_local_model_dir()
|
| 75 |
if model_path:
|
| 76 |
try:
|
|
|
|
| 77 |
classifier = pipeline(
|
| 78 |
"sentiment-analysis",
|
| 79 |
model=model_path,
|
|
@@ -82,11 +82,8 @@ def load_model():
|
|
| 82 |
status = f"β
Loaded fine-tuned model from local ({model_path})"
|
| 83 |
print(f"[load_model] {status}", flush=True)
|
| 84 |
return status
|
| 85 |
-
except Exception as
|
| 86 |
-
print(f"[load_model] local load failed: {
|
| 87 |
-
local_error = str(e)
|
| 88 |
-
else:
|
| 89 |
-
local_error = "my_finetuned_model is missing (Space disk is ephemeral unless /data exists)"
|
| 90 |
|
| 91 |
try:
|
| 92 |
classifier = pipeline(
|
|
@@ -94,8 +91,8 @@ def load_model():
|
|
| 94 |
model="distilbert-base-uncased-finetuned-sst-2-english",
|
| 95 |
)
|
| 96 |
status = (
|
| 97 |
-
"β οΈ Using default model.
|
| 98 |
-
f"
|
| 99 |
)
|
| 100 |
print(f"[load_model] {status}", flush=True)
|
| 101 |
return status
|
|
@@ -180,8 +177,8 @@ Full Output:
|
|
| 180 |
Model saved to: {model_path}
|
| 181 |
Status: {load_status}
|
| 182 |
|
| 183 |
-
|
| 184 |
-
|
| 185 |
return message, format_status(load_status)
|
| 186 |
|
| 187 |
fail_status = load_model()
|
|
@@ -244,7 +241,7 @@ Click the button below to fine-tune the model on the IMDb dataset.
|
|
| 244 |
|
| 245 |
β οΈ **Note:** This will take ~3-5 minutes on a CPU instance.
|
| 246 |
|
| 247 |
-
|
| 248 |
""")
|
| 249 |
|
| 250 |
with gr.Row():
|
|
|
|
| 13 |
|
| 14 |
classifier = None
|
| 15 |
|
| 16 |
+
HUB_MODEL = "Surajnitp/sentiment-trackmodel"
|
| 17 |
LOCAL_MODEL_DIR = os.path.abspath("./my_finetuned_model")
|
| 18 |
DATA_MODEL_DIR = "/data/my_finetuned_model"
|
| 19 |
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
def load_model():
|
| 54 |
+
"""Load Hub model first, then local weights, then the default SST-2 model."""
|
| 55 |
global classifier
|
| 56 |
|
| 57 |
+
print(f"[load_model] cwd={os.getcwd()}", flush=True)
|
| 58 |
+
print(f"[load_model] HF_TOKEN set={bool(os.getenv('HF_TOKEN'))}", flush=True)
|
| 59 |
+
|
| 60 |
try:
|
| 61 |
+
print(f"[load_model] trying Hub: {HUB_MODEL}", flush=True)
|
| 62 |
+
classifier = pipeline(
|
| 63 |
+
"sentiment-analysis",
|
| 64 |
+
model=HUB_MODEL,
|
| 65 |
+
tokenizer=HUB_MODEL,
|
| 66 |
+
)
|
| 67 |
+
status = f"β
Loaded model from Hub: {HUB_MODEL}"
|
| 68 |
+
print(f"[load_model] {status}", flush=True)
|
| 69 |
+
return status
|
| 70 |
+
except Exception as hub_err:
|
| 71 |
+
print(f"[load_model] Hub load failed: {hub_err}", flush=True)
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
model_path = find_local_model_dir()
|
| 74 |
if model_path:
|
| 75 |
try:
|
| 76 |
+
print(f"[load_model] trying local: {model_path}", flush=True)
|
| 77 |
classifier = pipeline(
|
| 78 |
"sentiment-analysis",
|
| 79 |
model=model_path,
|
|
|
|
| 82 |
status = f"β
Loaded fine-tuned model from local ({model_path})"
|
| 83 |
print(f"[load_model] {status}", flush=True)
|
| 84 |
return status
|
| 85 |
+
except Exception as local_err:
|
| 86 |
+
print(f"[load_model] local load failed: {local_err}", flush=True)
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
try:
|
| 89 |
classifier = pipeline(
|
|
|
|
| 91 |
model="distilbert-base-uncased-finetuned-sst-2-english",
|
| 92 |
)
|
| 93 |
status = (
|
| 94 |
+
"β οΈ Using default model. Hub and local fine-tuned weights were not found. "
|
| 95 |
+
f"Set HF_TOKEN, train once to push {HUB_MODEL}, then restart."
|
| 96 |
)
|
| 97 |
print(f"[load_model] {status}", flush=True)
|
| 98 |
return status
|
|
|
|
| 177 |
Model saved to: {model_path}
|
| 178 |
Status: {load_status}
|
| 179 |
|
| 180 |
+
If HF_TOKEN is set, the model was also pushed to {HUB_MODEL}.
|
| 181 |
+
Restart the Space to load permanently from the Hub."""
|
| 182 |
return message, format_status(load_status)
|
| 183 |
|
| 184 |
fail_status = load_model()
|
|
|
|
| 241 |
|
| 242 |
β οΈ **Note:** This will take ~3-5 minutes on a CPU instance.
|
| 243 |
|
| 244 |
+
Add Space secret `HF_TOKEN` first. Training then uploads to `Surajnitp/sentiment-trackmodel` so the model survives restarts.
|
| 245 |
""")
|
| 246 |
|
| 247 |
with gr.Row():
|
train.py
CHANGED
|
@@ -9,7 +9,7 @@ import sys
|
|
| 9 |
import numpy as np
|
| 10 |
import torch
|
| 11 |
from datasets import load_dataset
|
| 12 |
-
from huggingface_hub import hf_hub_download, login
|
| 13 |
from sklearn.metrics import accuracy_score
|
| 14 |
from transformers import (
|
| 15 |
AutoModelForSequenceClassification,
|
|
@@ -18,7 +18,7 @@ from transformers import (
|
|
| 18 |
TrainingArguments,
|
| 19 |
)
|
| 20 |
|
| 21 |
-
print("π οΈ Train script version: 2026-09-
|
| 22 |
print(f"β
PyTorch version: {torch.__version__}")
|
| 23 |
print(f"β
Transformers version: {__import__('transformers').__version__}")
|
| 24 |
print(f"β
Datasets version: {__import__('datasets').__version__}")
|
|
@@ -189,31 +189,38 @@ def setup_training():
|
|
| 189 |
print("β
FINE-TUNING COMPLETE!")
|
| 190 |
print("=" * 60)
|
| 191 |
|
| 192 |
-
return "./my_finetuned_model"
|
| 193 |
|
| 194 |
|
| 195 |
-
|
| 196 |
-
"""Push the fine-tuned model to your Hugging Face profile"""
|
| 197 |
-
print("\nπ€ Pushing model to Hugging Face Hub...")
|
| 198 |
|
| 199 |
-
token = os.getenv("HF_TOKEN")
|
| 200 |
-
if token:
|
| 201 |
-
login(token=token)
|
| 202 |
-
else:
|
| 203 |
-
print("β οΈ No HF_TOKEN found in environment variables.")
|
| 204 |
-
print("π Please login manually:")
|
| 205 |
-
login()
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
| 214 |
|
| 215 |
-
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
|
| 219 |
if __name__ == "__main__":
|
|
@@ -223,15 +230,11 @@ if __name__ == "__main__":
|
|
| 223 |
print("\nβ Training failed. Please check the errors above.")
|
| 224 |
sys.exit(1)
|
| 225 |
|
| 226 |
-
|
| 227 |
-
if sys.stdin.isatty():
|
| 228 |
-
choice = input("π Push model to Hugging Face Hub? (y/n): ")
|
| 229 |
-
if choice.lower() == "y":
|
| 230 |
-
push_to_hub()
|
| 231 |
-
else:
|
| 232 |
-
print("βοΈ Skipping upload. You can push later.")
|
| 233 |
-
else:
|
| 234 |
-
print("βοΈ Non-interactive run: skipping Hub upload prompt.")
|
| 235 |
-
|
| 236 |
print("\nβ
All done! Your model is ready.")
|
| 237 |
-
print("π Model location:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
import torch
|
| 11 |
from datasets import load_dataset
|
| 12 |
+
from huggingface_hub import HfApi, hf_hub_download, login
|
| 13 |
from sklearn.metrics import accuracy_score
|
| 14 |
from transformers import (
|
| 15 |
AutoModelForSequenceClassification,
|
|
|
|
| 18 |
TrainingArguments,
|
| 19 |
)
|
| 20 |
|
| 21 |
+
print("π οΈ Train script version: 2026-09-08-hub-push")
|
| 22 |
print(f"β
PyTorch version: {torch.__version__}")
|
| 23 |
print(f"β
Transformers version: {__import__('transformers').__version__}")
|
| 24 |
print(f"β
Datasets version: {__import__('datasets').__version__}")
|
|
|
|
| 189 |
print("β
FINE-TUNING COMPLETE!")
|
| 190 |
print("=" * 60)
|
| 191 |
|
| 192 |
+
return os.path.abspath("./my_finetuned_model")
|
| 193 |
|
| 194 |
|
| 195 |
+
HUB_REPO = "Surajnitp/sentiment-trackmodel"
|
|
|
|
|
|
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
+
def push_to_hub(model_path):
|
| 199 |
+
"""Push the fine-tuned model to Hugging Face Hub when HF_TOKEN is set."""
|
| 200 |
+
print("\nπ€ Step 8: Pushing model to Hugging Face Hub...")
|
| 201 |
+
token = os.getenv("HF_TOKEN")
|
| 202 |
+
if not token:
|
| 203 |
+
print("β οΈ HF_TOKEN is not set. Skipping Hub upload.")
|
| 204 |
+
print("Add a Space secret named HF_TOKEN, then run training again.")
|
| 205 |
+
return False
|
| 206 |
|
| 207 |
+
try:
|
| 208 |
+
print(f"[push] logging in with HF_TOKEN (length={len(token)})", flush=True)
|
| 209 |
+
login(token=token)
|
| 210 |
+
api = HfApi(token=token)
|
| 211 |
+
api.create_repo(repo_id=HUB_REPO, repo_type="model", exist_ok=True, private=False)
|
| 212 |
+
print(f"[push] loading weights from {model_path}", flush=True)
|
| 213 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 214 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 215 |
+
print(f"[push] uploading to https://huggingface.co/{HUB_REPO}", flush=True)
|
| 216 |
+
model.push_to_hub(HUB_REPO, token=token)
|
| 217 |
+
tokenizer.push_to_hub(HUB_REPO, token=token)
|
| 218 |
+
print("β
Model pushed successfully!")
|
| 219 |
+
print(f"π https://huggingface.co/{HUB_REPO}")
|
| 220 |
+
return True
|
| 221 |
+
except Exception as e:
|
| 222 |
+
print(f"β Hub upload failed: {str(e)}")
|
| 223 |
+
return False
|
| 224 |
|
| 225 |
|
| 226 |
if __name__ == "__main__":
|
|
|
|
| 230 |
print("\nβ Training failed. Please check the errors above.")
|
| 231 |
sys.exit(1)
|
| 232 |
|
| 233 |
+
pushed = push_to_hub(model_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
print("\nβ
All done! Your model is ready.")
|
| 235 |
+
print(f"π Model location: {model_path}")
|
| 236 |
+
if pushed:
|
| 237 |
+
print(f"βοΈ Hub model: https://huggingface.co/{HUB_REPO}")
|
| 238 |
+
else:
|
| 239 |
+
print("βοΈ Hub upload skipped or failed. Local weights are still saved.")
|
| 240 |
+
sys.exit(0)
|