""" 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@/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)