Surajnitp commited on
Commit
d925845
Β·
verified Β·
1 Parent(s): b69118f

Update train.py

Browse files
Files changed (1) hide show
  1. train.py +119 -43
train.py CHANGED
@@ -1,3 +1,8 @@
 
 
 
 
 
1
  import os
2
  import sys
3
  from transformers import (
@@ -6,55 +11,104 @@ from transformers import (
6
  Trainer,
7
  TrainingArguments,
8
  )
9
- from datasets import load_dataset, Dataset # Added Dataset import for fallback
10
  from huggingface_hub import login
11
  import torch
12
 
13
- # --- Helper function for local dataset (fallback) ---
 
 
 
 
14
  def create_local_dataset():
15
- """Create a small local dataset for testing if IMDb fails to load."""
16
  print("\nπŸ“Š Creating local dataset for testing...")
17
 
18
- # Sample data (repeated to reach ~500 samples)
19
- train_texts = [
 
 
 
 
20
  "I loved this movie, it was amazing!",
21
- "The acting was terrible and boring.",
22
  "Great film, highly recommend it.",
23
- "Waste of time, don't watch it.",
24
  "Fantastic story and characters!",
25
- "Poorly made and disappointing.",
26
  "Excellent cinematography!",
27
- "The plot made no sense at all.",
28
  "Brilliant performance by the lead actor.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  "I fell asleep halfway through.",
30
- ] * 50 # 10 * 50 = 500 samples
 
 
 
 
 
 
 
 
 
 
31
 
32
- train_labels = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0] * 50 # 1=positive, 0=negative
 
 
33
 
34
- test_texts = [
35
  "Pretty good movie overall.",
36
- "Not worth the ticket price.",
37
  "Enjoyable with some flaws.",
38
- "Complete disaster.",
39
  "Decent watch for a rainy day.",
40
- ] * 20 # 5 * 20 = 100 samples
 
 
 
 
 
 
 
41
 
42
- test_labels = [1, 0, 1, 0, 1] * 20
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  train_dataset = Dataset.from_dict({
45
- 'text': train_texts,
46
- 'label': train_labels
47
  })
48
  test_dataset = Dataset.from_dict({
49
- 'text': test_texts,
50
- 'label': test_labels
51
  })
52
 
53
  print(f"βœ… Created local dataset: {len(train_dataset)} train, {len(test_dataset)} test")
54
  return train_dataset, test_dataset
55
 
56
 
57
- # --- Main Training Function (FIXED) ---
58
  def setup_training():
59
  """Setup and run the fine-tuning process"""
60
 
@@ -62,26 +116,55 @@ def setup_training():
62
  print("πŸš€ TRACKMODEL - FINE-TUNING STARTED")
63
  print("=" * 60)
64
 
65
- print(f"βœ… PyTorch version: {torch.__version__}")
66
- print(f"βœ… Transformers version: {__import__('transformers').__version__}")
67
- print(f"βœ… Datasets version: {__import__('datasets').__version__}")
68
-
69
- # 1. Load Dataset - FIXED with trust_remote_code=True
70
  print("\nπŸ“Š Step 1: Loading dataset...")
 
 
 
 
 
71
  try:
72
- # The key fix is adding trust_remote_code=True
73
- dataset = load_dataset("imdb", trust_remote_code=True)
74
- print(f"βœ… Loaded {len(dataset['train'])} training samples")
75
- print(f"βœ… Loaded {len(dataset['test'])} test samples")
76
 
77
- # Use a subset for faster training
78
- print("\n⚑ Using subset for faster training (500 train, 100 test)")
79
  train_subset = dataset["train"].select(range(500))
80
  test_subset = dataset["test"].select(range(100))
 
81
 
82
- except Exception as e:
83
- print(f"❌ Failed to load IMDb dataset: {str(e)}")
84
- print("πŸ’‘ Falling back to local dataset...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  train_subset, test_subset = create_local_dataset()
86
 
87
  # 2. Load Model and Tokenizer
@@ -98,7 +181,6 @@ def setup_training():
98
  print("βœ… Model and tokenizer loaded successfully")
99
  except Exception as e:
100
  print(f"❌ Failed to load model: {str(e)}")
101
- print("πŸ’‘ Try: pip install transformers==4.30.0")
102
  return None
103
 
104
  # 3. Tokenize the dataset
@@ -163,7 +245,6 @@ def setup_training():
163
  print("βœ… Training complete!")
164
  except Exception as e:
165
  print(f"❌ Training failed: {str(e)}")
166
- print("πŸ’‘ If out of memory, reduce batch size or dataset size")
167
  return None
168
 
169
  # 6. Save the model
@@ -193,7 +274,6 @@ def setup_training():
193
  return "./my_finetuned_model"
194
 
195
 
196
- # --- Push to Hugging Face Hub (Unchanged) ---
197
  def push_to_hub():
198
  """Push the fine-tuned model to your Hugging Face profile"""
199
  print("\nπŸ“€ Pushing model to Hugging Face Hub...")
@@ -206,13 +286,10 @@ def push_to_hub():
206
  print("πŸ“ Please login manually:")
207
  login()
208
 
209
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
210
-
211
  model = AutoModelForSequenceClassification.from_pretrained("./my_finetuned_model")
212
  tokenizer = AutoTokenizer.from_pretrained("./my_finetuned_model")
213
 
214
- # Change this to your own Hugging Face username if needed
215
- repo_name = "Surajnitp/sentiment-trackmodel"
216
  print(f"πŸ“€ Uploading to: https://huggingface.co/{repo_name}")
217
  model.push_to_hub(repo_name)
218
  tokenizer.push_to_hub(repo_name)
@@ -221,7 +298,6 @@ def push_to_hub():
221
  print(f"πŸ”— https://huggingface.co/{repo_name}")
222
 
223
 
224
- # --- Main Execution ---
225
  if __name__ == "__main__":
226
  # Train the model
227
  model_path = setup_training()
 
1
+ """
2
+ Trackmodel - Fine-Tuning Script
3
+ This script downloads a pre-trained model and fine-tunes it on IMDb dataset
4
+ """
5
+
6
  import os
7
  import sys
8
  from transformers import (
 
11
  Trainer,
12
  TrainingArguments,
13
  )
14
+ from datasets import load_dataset, Dataset
15
  from huggingface_hub import login
16
  import torch
17
 
18
+ print(f"βœ… PyTorch version: {torch.__version__}")
19
+ print(f"βœ… Transformers version: {__import__('transformers').__version__}")
20
+ print(f"βœ… Datasets version: {__import__('datasets').__version__}")
21
+
22
+
23
  def create_local_dataset():
24
+ """Create a small local dataset for testing when IMDb fails"""
25
  print("\nπŸ“Š Creating local dataset for testing...")
26
 
27
+ # Training data (500 samples)
28
+ train_texts = []
29
+ train_labels = []
30
+
31
+ # Positive examples (250)
32
+ positive_texts = [
33
  "I loved this movie, it was amazing!",
 
34
  "Great film, highly recommend it.",
 
35
  "Fantastic story and characters!",
 
36
  "Excellent cinematography!",
 
37
  "Brilliant performance by the lead actor.",
38
+ "This movie exceeded all my expectations.",
39
+ "Absolutely wonderful film!",
40
+ "The best movie I've seen this year.",
41
+ "Incredible acting and beautiful visuals.",
42
+ "A masterpiece of modern cinema.",
43
+ ]
44
+ for i in range(25):
45
+ for text in positive_texts:
46
+ if len(train_texts) < 250:
47
+ train_texts.append(text)
48
+ train_labels.append(1)
49
+
50
+ # Negative examples (250)
51
+ negative_texts = [
52
+ "The acting was terrible and boring.",
53
+ "Waste of time, don't watch it.",
54
+ "Poorly made and disappointing.",
55
+ "The plot made no sense at all.",
56
  "I fell asleep halfway through.",
57
+ "This movie was a complete disaster.",
58
+ "Absolutely terrible film.",
59
+ "Worst movie I've ever seen.",
60
+ "Boring and completely predictable.",
61
+ "A complete waste of money.",
62
+ ]
63
+ for i in range(25):
64
+ for text in negative_texts:
65
+ if len(train_texts) < 500:
66
+ train_texts.append(text)
67
+ train_labels.append(0)
68
 
69
+ # Test data (100 samples)
70
+ test_texts = []
71
+ test_labels = []
72
 
73
+ test_positive = [
74
  "Pretty good movie overall.",
 
75
  "Enjoyable with some flaws.",
 
76
  "Decent watch for a rainy day.",
77
+ "Interesting plot with good acting.",
78
+ "A solid film worth watching.",
79
+ ]
80
+ for i in range(10):
81
+ for text in test_positive:
82
+ if len(test_texts) < 50:
83
+ test_texts.append(text)
84
+ test_labels.append(1)
85
 
86
+ test_negative = [
87
+ "Not worth the ticket price.",
88
+ "Complete disaster of a movie.",
89
+ "Terrible acting and boring.",
90
+ "Predictable and unoriginal.",
91
+ "This movie was a mistake.",
92
+ ]
93
+ for i in range(10):
94
+ for text in test_negative:
95
+ if len(test_texts) < 100:
96
+ test_texts.append(text)
97
+ test_labels.append(0)
98
 
99
  train_dataset = Dataset.from_dict({
100
+ 'text': train_texts[:500],
101
+ 'label': train_labels[:500]
102
  })
103
  test_dataset = Dataset.from_dict({
104
+ 'text': test_texts[:100],
105
+ 'label': test_labels[:100]
106
  })
107
 
108
  print(f"βœ… Created local dataset: {len(train_dataset)} train, {len(test_dataset)} test")
109
  return train_dataset, test_dataset
110
 
111
 
 
112
  def setup_training():
113
  """Setup and run the fine-tuning process"""
114
 
 
116
  print("πŸš€ TRACKMODEL - FINE-TUNING STARTED")
117
  print("=" * 60)
118
 
119
+ # 1. Load Dataset
 
 
 
 
120
  print("\nπŸ“Š Step 1: Loading dataset...")
121
+
122
+ train_subset = None
123
+ test_subset = None
124
+
125
+ # Try loading from Hugging Face first
126
  try:
127
+ # Try different loading methods
128
+ print("πŸ”„ Attempting to load IMDb dataset...")
 
 
129
 
130
+ # Method 1: Standard load
131
+ dataset = load_dataset("imdb", trust_remote_code=True)
132
  train_subset = dataset["train"].select(range(500))
133
  test_subset = dataset["test"].select(range(100))
134
+ print(f"βœ… Loaded IMDb dataset: {len(train_subset)} train, {len(test_subset)} test")
135
 
136
+ except Exception as e1:
137
+ print(f"⚠️ Method 1 failed: {str(e1)[:100]}...")
138
+
139
+ try:
140
+ # Method 2: Try without trust_remote_code
141
+ print("πŸ”„ Attempting method 2...")
142
+ dataset = load_dataset("imdb")
143
+ train_subset = dataset["train"].select(range(500))
144
+ test_subset = dataset["test"].select(range(100))
145
+ print(f"βœ… Loaded IMDb dataset: {len(train_subset)} train, {len(test_subset)} test")
146
+
147
+ except Exception as e2:
148
+ print(f"⚠️ Method 2 failed: {str(e2)[:100]}...")
149
+
150
+ try:
151
+ # Method 3: Try with specific config
152
+ print("πŸ”„ Attempting method 3...")
153
+ dataset = load_dataset("imdb", split="train+test", trust_remote_code=True)
154
+ # Split back into train/test
155
+ train_size = int(len(dataset) * 0.8)
156
+ train_subset = dataset.select(range(500))
157
+ test_subset = dataset.select(range(500, 600))
158
+ print(f"βœ… Loaded IMDb dataset: {len(train_subset)} train, {len(test_subset)} test")
159
+
160
+ except Exception as e3:
161
+ print(f"❌ All IMDb loading methods failed!")
162
+ print("πŸ“ Using local dataset instead...")
163
+ train_subset, test_subset = create_local_dataset()
164
+
165
+ # If still None, use local dataset
166
+ if train_subset is None or test_subset is None:
167
+ print("πŸ“ Using local dataset...")
168
  train_subset, test_subset = create_local_dataset()
169
 
170
  # 2. Load Model and Tokenizer
 
181
  print("βœ… Model and tokenizer loaded successfully")
182
  except Exception as e:
183
  print(f"❌ Failed to load model: {str(e)}")
 
184
  return None
185
 
186
  # 3. Tokenize the dataset
 
245
  print("βœ… Training complete!")
246
  except Exception as e:
247
  print(f"❌ Training failed: {str(e)}")
 
248
  return None
249
 
250
  # 6. Save the model
 
274
  return "./my_finetuned_model"
275
 
276
 
 
277
  def push_to_hub():
278
  """Push the fine-tuned model to your Hugging Face profile"""
279
  print("\nπŸ“€ Pushing model to Hugging Face Hub...")
 
286
  print("πŸ“ Please login manually:")
287
  login()
288
 
 
 
289
  model = AutoModelForSequenceClassification.from_pretrained("./my_finetuned_model")
290
  tokenizer = AutoTokenizer.from_pretrained("./my_finetuned_model")
291
 
292
+ repo_name = "Surajnitp/sentiment-trackmodel"
 
293
  print(f"πŸ“€ Uploading to: https://huggingface.co/{repo_name}")
294
  model.push_to_hub(repo_name)
295
  tokenizer.push_to_hub(repo_name)
 
298
  print(f"πŸ”— https://huggingface.co/{repo_name}")
299
 
300
 
 
301
  if __name__ == "__main__":
302
  # Train the model
303
  model_path = setup_training()