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

Update train.py

Browse files
Files changed (1) hide show
  1. train.py +64 -23
train.py CHANGED
@@ -1,8 +1,3 @@
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,15 +6,55 @@ from transformers import (
11
  Trainer,
12
  TrainingArguments,
13
  )
14
- from datasets import load_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 setup_training():
24
  """Setup and run the fine-tuning process"""
25
 
@@ -27,24 +62,27 @@ def setup_training():
27
  print("πŸš€ TRACKMODEL - FINE-TUNING STARTED")
28
  print("=" * 60)
29
 
30
- # 1. Load Dataset
 
 
 
 
31
  print("\nπŸ“Š Step 1: Loading dataset...")
32
  try:
33
- dataset = load_dataset("imdb")
 
34
  print(f"βœ… Loaded {len(dataset['train'])} training samples")
35
  print(f"βœ… Loaded {len(dataset['test'])} test samples")
36
- except Exception as e:
37
- print(f"❌ Failed to load dataset: {str(e)}")
38
- return None
39
-
40
- # Use a subset for faster training
41
- print("\n⚑ Using subset for faster training (500 train, 100 test)")
42
- try:
43
  train_subset = dataset["train"].select(range(500))
44
  test_subset = dataset["test"].select(range(100))
 
45
  except Exception as e:
46
- print(f"❌ Failed to select subset: {str(e)}")
47
- return None
 
48
 
49
  # 2. Load Model and Tokenizer
50
  print("\nπŸ€– Step 2: Loading pre-trained model...")
@@ -155,6 +193,7 @@ def setup_training():
155
  return "./my_finetuned_model"
156
 
157
 
 
158
  def push_to_hub():
159
  """Push the fine-tuned model to your Hugging Face profile"""
160
  print("\nπŸ“€ Pushing model to Hugging Face Hub...")
@@ -172,7 +211,8 @@ def push_to_hub():
172
  model = AutoModelForSequenceClassification.from_pretrained("./my_finetuned_model")
173
  tokenizer = AutoTokenizer.from_pretrained("./my_finetuned_model")
174
 
175
- repo_name = "Surajnitp/sentiment-trackmodel"
 
176
  print(f"πŸ“€ Uploading to: https://huggingface.co/{repo_name}")
177
  model.push_to_hub(repo_name)
178
  tokenizer.push_to_hub(repo_name)
@@ -181,6 +221,7 @@ def push_to_hub():
181
  print(f"πŸ”— https://huggingface.co/{repo_name}")
182
 
183
 
 
184
  if __name__ == "__main__":
185
  # Train the model
186
  model_path = setup_training()
 
 
 
 
 
 
1
  import os
2
  import sys
3
  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
  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
88
  print("\nπŸ€– Step 2: Loading pre-trained model...")
 
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...")
 
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
  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()