File size: 8,176 Bytes
d925845
 
 
 
 
26094c7
0c87111
fa81b74
 
 
 
5889719
fa81b74
26094c7
 
fa81b74
26094c7
 
 
 
5889719
d925845
 
 
 
 
1c8e925
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa81b74
 
 
 
 
 
26094c7
 
9f7633c
26094c7
 
 
9f7633c
d925845
26094c7
0c87111
1c8e925
af1ac63
 
fa81b74
af1ac63
 
0c87111
 
fa81b74
af1ac63
 
 
9f7633c
26094c7
 
 
 
9f7633c
0c87111
 
 
 
fa81b74
 
 
0c87111
 
 
 
 
9f7633c
26094c7
 
9f7633c
26094c7
 
 
 
 
0c87111
26094c7
9f7633c
0c87111
 
 
 
 
 
 
 
 
 
 
 
 
 
9f7633c
26094c7
 
9f7633c
26094c7
 
0c87111
26094c7
 
fa81b74
26094c7
 
 
fa81b74
 
26094c7
fa81b74
0c87111
26094c7
 
9f7633c
26094c7
 
 
9f7633c
0c87111
 
 
 
 
 
fa81b74
 
0c87111
9f7633c
0c87111
 
 
 
 
9f7633c
26094c7
 
0c87111
cf81d51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c87111
 
 
9f7633c
26094c7
 
0c87111
 
 
 
 
 
9f7633c
26094c7
 
 
9f7633c
5889719
26094c7
0c87111
5889719
9f7633c
 
5889719
 
 
 
 
 
 
 
9f7633c
5889719
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26094c7
0c87111
26094c7
 
9f7633c
fa81b74
 
 
9f7633c
5889719
fa81b74
5889719
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""
Trackmodel - Fine-Tuning Script
This script downloads a pre-trained model and fine-tunes it on IMDb dataset
"""

import os
import sys

import numpy as np
import torch
from datasets import load_dataset
from huggingface_hub import HfApi, hf_hub_download, login
from sklearn.metrics import accuracy_score
from transformers import (
    AutoModelForSequenceClassification,
    AutoTokenizer,
    Trainer,
    TrainingArguments,
)

print("πŸ› οΈ Train script version: 2026-09-08-hub-push")
print(f"βœ… PyTorch version: {torch.__version__}")
print(f"βœ… Transformers version: {__import__('transformers').__version__}")
print(f"βœ… Datasets version: {__import__('datasets').__version__}")


def load_imdb_dataset():
    """Load IMDb without the legacy Hub id `imdb`.

    datasets 2.x + recent huggingface_hub rewrites stanfordnlp/imdb to
    hf://datasets/imdb@<sha>/README.md, which is rejected. Download the
    parquet shards by namespaced repo id instead.
    """
    data_files = {}
    for split in ("train", "test"):
        data_files[split] = hf_hub_download(
            repo_id="stanfordnlp/imdb",
            filename=f"plain_text/{split}-00000-of-00001.parquet",
            repo_type="dataset",
        )
    return load_dataset("parquet", data_files=data_files)


def compute_metrics(eval_pred):
    logits, labels = eval_pred
    predictions = np.argmax(logits, axis=-1)
    return {"accuracy": float(accuracy_score(labels, predictions))}


def setup_training():
    """Setup and run the fine-tuning process"""

    print("=" * 60)
    print("πŸš€ TRACKMODEL - FINE-TUNING STARTED")
    print("=" * 60)

    # 1. Load Dataset
    print("\nπŸ“Š Step 1: Loading dataset...")
    try:
        dataset = load_imdb_dataset()
        print(f"βœ… Loaded {len(dataset['train'])} training samples")
        print(f"βœ… Loaded {len(dataset['test'])} test samples")

        # Use a subset for faster training
        print("\n⚑ Using subset for faster training (500 train, 100 test)")
        train_subset = dataset["train"].select(range(500))
        test_subset = dataset["test"].select(range(100))

    except Exception as e:
        print(f"❌ Failed to load dataset: {str(e)}")
        return None

    # 2. Load Model and Tokenizer
    print("\nπŸ€– Step 2: Loading pre-trained model...")
    model_name = "distilbert-base-uncased"
    print(f"πŸ“¦ Model: {model_name}")

    try:
        tokenizer = AutoTokenizer.from_pretrained(model_name)
        model = AutoModelForSequenceClassification.from_pretrained(
            model_name,
            num_labels=2,
            id2label={0: "NEGATIVE", 1: "POSITIVE"},
            label2id={"NEGATIVE": 0, "POSITIVE": 1},
        )
        print("βœ… Model and tokenizer loaded successfully")
    except Exception as e:
        print(f"❌ Failed to load model: {str(e)}")
        return None

    # 3. Tokenize the dataset
    print("\nβœ‚οΈ Step 3: Tokenizing dataset...")

    def tokenize_function(examples):
        return tokenizer(
            examples["text"],
            padding="max_length",
            truncation=True,
            max_length=256,
        )

    try:
        tokenized_train = train_subset.map(tokenize_function, batched=True)
        tokenized_test = test_subset.map(tokenize_function, batched=True)

        tokenized_train = tokenized_train.remove_columns(["text"])
        tokenized_test = tokenized_test.remove_columns(["text"])
        tokenized_train = tokenized_train.rename_column("label", "labels")
        tokenized_test = tokenized_test.rename_column("label", "labels")
        tokenized_train.set_format("torch")
        tokenized_test.set_format("torch")
        print("βœ… Tokenization complete")
    except Exception as e:
        print(f"❌ Tokenization failed: {str(e)}")
        return None

    # 4. Training Arguments
    print("\nβš™οΈ Step 4: Configuring training parameters...")

    training_args = TrainingArguments(
        output_dir="./results",
        num_train_epochs=2,
        per_device_train_batch_size=8,
        per_device_eval_batch_size=8,
        eval_strategy="epoch",
        save_strategy="epoch",
        logging_steps=10,
        load_best_model_at_end=True,
        metric_for_best_model="eval_accuracy",
        greater_is_better=True,
        save_total_limit=2,
        report_to="none",
        push_to_hub=False,
    )
    print("βœ… Training arguments configured")

    # 5. Train the model
    print("\nπŸ‹οΈ Step 5: Training the model...")
    print("⏳ This may take a few minutes...")

    try:
        trainer = Trainer(
            model=model,
            args=training_args,
            train_dataset=tokenized_train,
            eval_dataset=tokenized_test,
            processing_class=tokenizer,
            compute_metrics=compute_metrics,
        )

        trainer.train()
        print("βœ… Training complete!")
    except Exception as e:
        print(f"❌ Training failed: {str(e)}")
        return None

    # 6. Save the model
    print("\nπŸ’Ύ Step 6: Saving the model...")
    try:
        save_dir = os.environ.get("MODEL_SAVE_DIR", "./my_finetuned_model")
        os.makedirs(save_dir, exist_ok=True)
        model.save_pretrained(save_dir)
        tokenizer.save_pretrained(save_dir)
        print(f"βœ… Model saved to: {save_dir}")
        saved = os.listdir(save_dir)
        print(f"πŸ“ Saved files: {saved}")

        # Also write ./my_finetuned_model so the app can find it without /data.
        local_dir = os.path.abspath("./my_finetuned_model")
        if os.path.abspath(save_dir) != local_dir:
            os.makedirs(local_dir, exist_ok=True)
            model.save_pretrained(local_dir)
            tokenizer.save_pretrained(local_dir)
            print(f"βœ… Also saved to: {local_dir}")
    except Exception as e:
        print(f"❌ Failed to save model: {str(e)}")
        return None

    # 7. Evaluate
    print("\nπŸ“Š Step 7: Evaluating the model...")
    try:
        eval_results = trainer.evaluate()
        print(f"πŸ“ˆ Accuracy: {eval_results.get('eval_accuracy', 'N/A')}")
        print(f"πŸ“‰ Loss: {eval_results.get('eval_loss', 'N/A')}")
    except Exception as e:
        print(f"⚠️ Evaluation failed: {str(e)}")

    print("\n" + "=" * 60)
    print("βœ… FINE-TUNING COMPLETE!")
    print("=" * 60)

    return os.path.abspath("./my_finetuned_model")


HUB_REPO = "Surajnitp/sentiment-trackmodel"


def push_to_hub(model_path):
    """Push the fine-tuned model to Hugging Face Hub when HF_TOKEN is set."""
    print("\nπŸ“€ Step 8: Pushing model to Hugging Face Hub...")
    token = os.getenv("HF_TOKEN")
    if not token:
        print("⚠️ HF_TOKEN is not set. Skipping Hub upload.")
        print("Add a Space secret named HF_TOKEN, then run training again.")
        return False

    try:
        print(f"[push] logging in with HF_TOKEN (length={len(token)})", flush=True)
        login(token=token)
        api = HfApi(token=token)
        api.create_repo(repo_id=HUB_REPO, repo_type="model", exist_ok=True, private=False)
        print(f"[push] loading weights from {model_path}", flush=True)
        model = AutoModelForSequenceClassification.from_pretrained(model_path)
        tokenizer = AutoTokenizer.from_pretrained(model_path)
        print(f"[push] uploading to https://huggingface.co/{HUB_REPO}", flush=True)
        model.push_to_hub(HUB_REPO, token=token)
        tokenizer.push_to_hub(HUB_REPO, token=token)
        print("βœ… Model pushed successfully!")
        print(f"πŸ”— https://huggingface.co/{HUB_REPO}")
        return True
    except Exception as e:
        print(f"❌ Hub upload failed: {str(e)}")
        return False


if __name__ == "__main__":
    model_path = setup_training()

    if not model_path:
        print("\n❌ Training failed. Please check the errors above.")
        sys.exit(1)

    pushed = push_to_hub(model_path)
    print("\nβœ… All done! Your model is ready.")
    print(f"πŸ“ Model location: {model_path}")
    if pushed:
        print(f"☁️ Hub model: https://huggingface.co/{HUB_REPO}")
    else:
        print("☁️ Hub upload skipped or failed. Local weights are still saved.")
        sys.exit(0)