Spaces:
Paused
Paused
Download train.py from Surajnitp/Trackmodel: direct link, hf CLI and curl.
- Browser
- Download file 8.18 kB
-
https://huggingface.co/spaces/Surajnitp/Trackmodel/resolve/main/train.py
- Command line
-
hf download hf://spaces/Surajnitp/Trackmodel/train.py
-
curl -L -o train.py https://huggingface.co/spaces/Surajnitp/Trackmodel/resolve/main/train.py
8.18 kB
| """ | |
| 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) | |