akash commited on
Commit
fa81b74
Β·
1 Parent(s): 01e7f9c

Fix dataset loading (stanfordnlp/imdb) and update requirements

Browse files
Files changed (2) hide show
  1. requirements.txt +3 -2
  2. train.py +38 -23
requirements.txt CHANGED
@@ -1,9 +1,10 @@
1
  gradio
2
  transformers
3
- datasets==2.21.0
4
  accelerate
5
  torch
6
  huggingface_hub
7
  safetensors
8
  tokenizers
9
- scikit-learn
 
 
1
  gradio
2
  transformers
3
+ datasets>=3.0.0
4
  accelerate
5
  torch
6
  huggingface_hub
7
  safetensors
8
  tokenizers
9
+ scikit-learn
10
+ numpy
train.py CHANGED
@@ -5,21 +5,30 @@ This script downloads a pre-trained model and fine-tunes it on IMDb dataset
5
 
6
  import os
7
  import sys
 
 
 
 
 
 
8
  from transformers import (
9
- AutoTokenizer,
10
  AutoModelForSequenceClassification,
 
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
 
@@ -30,16 +39,16 @@ def setup_training():
30
  # 1. Load Dataset
31
  print("\nπŸ“Š Step 1: Loading dataset...")
32
  try:
33
- # Simple load - works with datasets 2.21.0
34
- dataset = load_dataset("imdb")
35
  print(f"βœ… Loaded {len(dataset['train'])} training samples")
36
  print(f"βœ… Loaded {len(dataset['test'])} test samples")
37
-
38
  # Use a subset for faster training
39
  print("\n⚑ Using subset for faster training (500 train, 100 test)")
40
  train_subset = dataset["train"].select(range(500))
41
  test_subset = dataset["test"].select(range(100))
42
-
43
  except Exception as e:
44
  print(f"❌ Failed to load dataset: {str(e)}")
45
  return None
@@ -53,7 +62,9 @@ def setup_training():
53
  tokenizer = AutoTokenizer.from_pretrained(model_name)
54
  model = AutoModelForSequenceClassification.from_pretrained(
55
  model_name,
56
- num_labels=2
 
 
57
  )
58
  print("βœ… Model and tokenizer loaded successfully")
59
  except Exception as e:
@@ -94,13 +105,14 @@ def setup_training():
94
  num_train_epochs=2,
95
  per_device_train_batch_size=8,
96
  per_device_eval_batch_size=8,
97
- evaluation_strategy="epoch",
98
  save_strategy="epoch",
99
- logging_dir="./logs",
100
  logging_steps=10,
101
  load_best_model_at_end=True,
 
 
102
  save_total_limit=2,
103
- report_to=None,
104
  push_to_hub=False,
105
  )
106
  print("βœ… Training arguments configured")
@@ -115,7 +127,8 @@ def setup_training():
115
  args=training_args,
116
  train_dataset=tokenized_train,
117
  eval_dataset=tokenized_test,
118
- tokenizer=tokenizer,
 
119
  )
120
 
121
  trainer.train()
@@ -171,24 +184,26 @@ def push_to_hub():
171
  model.push_to_hub(repo_name)
172
  tokenizer.push_to_hub(repo_name)
173
 
174
- print(f"βœ… Model pushed successfully!")
175
  print(f"πŸ”— https://huggingface.co/{repo_name}")
176
 
177
 
178
  if __name__ == "__main__":
179
- # Train the model
180
  model_path = setup_training()
181
 
182
- if model_path:
183
- print("\n" + "=" * 60)
184
- choice = input("πŸ”„ Push model to Hugging Face Hub? (y/n): ")
185
 
186
- if choice.lower() == 'y':
 
 
 
187
  push_to_hub()
188
  else:
189
  print("⏭️ Skipping upload. You can push later.")
190
-
191
- print("\nβœ… All done! Your model is ready.")
192
- print("πŸ“ Model location: ./my_finetuned_model")
193
  else:
194
- print("\n❌ Training failed. Please check the errors above.")
 
 
 
 
5
 
6
  import os
7
  import sys
8
+
9
+ import numpy as np
10
+ import torch
11
+ from datasets import load_dataset
12
+ from huggingface_hub import login
13
+ from sklearn.metrics import accuracy_score
14
  from transformers import (
 
15
  AutoModelForSequenceClassification,
16
+ AutoTokenizer,
17
  Trainer,
18
  TrainingArguments,
19
  )
 
 
 
20
 
21
  print(f"βœ… PyTorch version: {torch.__version__}")
22
  print(f"βœ… Transformers version: {__import__('transformers').__version__}")
23
  print(f"βœ… Datasets version: {__import__('datasets').__version__}")
24
 
25
 
26
+ def compute_metrics(eval_pred):
27
+ logits, labels = eval_pred
28
+ predictions = np.argmax(logits, axis=-1)
29
+ return {"accuracy": float(accuracy_score(labels, predictions))}
30
+
31
+
32
  def setup_training():
33
  """Setup and run the fine-tuning process"""
34
 
 
39
  # 1. Load Dataset
40
  print("\nπŸ“Š Step 1: Loading dataset...")
41
  try:
42
+ # Hub now requires namespace/name. "imdb" is stanfordnlp/imdb.
43
+ dataset = load_dataset("stanfordnlp/imdb")
44
  print(f"βœ… Loaded {len(dataset['train'])} training samples")
45
  print(f"βœ… Loaded {len(dataset['test'])} test samples")
46
+
47
  # Use a subset for faster training
48
  print("\n⚑ Using subset for faster training (500 train, 100 test)")
49
  train_subset = dataset["train"].select(range(500))
50
  test_subset = dataset["test"].select(range(100))
51
+
52
  except Exception as e:
53
  print(f"❌ Failed to load dataset: {str(e)}")
54
  return None
 
62
  tokenizer = AutoTokenizer.from_pretrained(model_name)
63
  model = AutoModelForSequenceClassification.from_pretrained(
64
  model_name,
65
+ num_labels=2,
66
+ id2label={0: "NEGATIVE", 1: "POSITIVE"},
67
+ label2id={"NEGATIVE": 0, "POSITIVE": 1},
68
  )
69
  print("βœ… Model and tokenizer loaded successfully")
70
  except Exception as e:
 
105
  num_train_epochs=2,
106
  per_device_train_batch_size=8,
107
  per_device_eval_batch_size=8,
108
+ eval_strategy="epoch",
109
  save_strategy="epoch",
 
110
  logging_steps=10,
111
  load_best_model_at_end=True,
112
+ metric_for_best_model="eval_accuracy",
113
+ greater_is_better=True,
114
  save_total_limit=2,
115
+ report_to="none",
116
  push_to_hub=False,
117
  )
118
  print("βœ… Training arguments configured")
 
127
  args=training_args,
128
  train_dataset=tokenized_train,
129
  eval_dataset=tokenized_test,
130
+ processing_class=tokenizer,
131
+ compute_metrics=compute_metrics,
132
  )
133
 
134
  trainer.train()
 
184
  model.push_to_hub(repo_name)
185
  tokenizer.push_to_hub(repo_name)
186
 
187
+ print("βœ… Model pushed successfully!")
188
  print(f"πŸ”— https://huggingface.co/{repo_name}")
189
 
190
 
191
  if __name__ == "__main__":
 
192
  model_path = setup_training()
193
 
194
+ if not model_path:
195
+ print("\n❌ Training failed. Please check the errors above.")
196
+ sys.exit(1)
197
 
198
+ print("\n" + "=" * 60)
199
+ if sys.stdin.isatty():
200
+ choice = input("πŸ”„ Push model to Hugging Face Hub? (y/n): ")
201
+ if choice.lower() == "y":
202
  push_to_hub()
203
  else:
204
  print("⏭️ Skipping upload. You can push later.")
 
 
 
205
  else:
206
+ print("⏭️ Non-interactive run: skipping Hub upload prompt.")
207
+
208
+ print("\nβœ… All done! Your model is ready.")
209
+ print("πŸ“ Model location: ./my_finetuned_model")