akash Cursor commited on
Commit
5889719
Β·
1 Parent(s): cf81d51

Persist the fine-tuned model by pushing it to the Hub.

Browse files

Load 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>

Files changed (2) hide show
  1. app.py +24 -27
  2. train.py +36 -33
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 the fine-tuned model from local disk only."""
54
  global classifier
55
 
56
- cwd = os.getcwd()
57
- listing = []
 
58
  try:
59
- listing = os.listdir(cwd)
60
- except Exception as list_err:
61
- listing = [f"<listdir failed: {list_err}>"]
62
-
63
- print(f"[load_model] cwd={cwd}", flush=True)
64
- print(f"[load_model] files={listing}", flush=True)
65
- print(
66
- f"[load_model] local_dir={LOCAL_MODEL_DIR} exists={os.path.isdir(LOCAL_MODEL_DIR)} valid={model_dir_is_valid(LOCAL_MODEL_DIR)}",
67
- flush=True,
68
- )
69
- print(
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 e:
86
- print(f"[load_model] local load failed: {e}", flush=True)
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. Fine-tuned weights were not found after restart. "
98
- f"Train again (saved under {persistent_model_dir()}). Detail: {local_error}"
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
- The header status should now say the fine-tuned model is loaded.
184
- Use Predict without restarting β€” a Space restart wipes ephemeral disk unless /data is enabled."""
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
- Do **not** restart the Space after training unless persistent storage (`/data`) is enabled β€” the weights live on ephemeral disk.
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-07-parquet-imdb")
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
- def push_to_hub():
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
- model = AutoModelForSequenceClassification.from_pretrained("./my_finetuned_model")
208
- tokenizer = AutoTokenizer.from_pretrained("./my_finetuned_model")
209
-
210
- repo_name = "Surajnitp/sentiment-trackmodel"
211
- print(f"πŸ“€ Uploading to: https://huggingface.co/{repo_name}")
212
- model.push_to_hub(repo_name)
213
- tokenizer.push_to_hub(repo_name)
 
214
 
215
- print("βœ… Model pushed successfully!")
216
- print(f"πŸ”— https://huggingface.co/{repo_name}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- print("\n" + "=" * 60)
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: ./my_finetuned_model")
 
 
 
 
 
 
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)