Download module_toxicdetection/train.py from orcheflow/flow-moderation: direct link, hf CLI and curl.
- Browser
- Download file 2.54 kB
-
https://huggingface.co/orcheflow/flow-moderation/resolve/main/module_toxicdetection/train.py
- Command line
-
hf download hf://orcheflow/flow-moderation/module_toxicdetection/train.py
-
curl -L -o train.py https://huggingface.co/orcheflow/flow-moderation/resolve/main/module_toxicdetection/train.py
2.54 kB
| import os | |
| import torch | |
| from transformers import ( | |
| AutoTokenizer, | |
| AutoModelForSequenceClassification, | |
| Trainer, | |
| TrainingArguments, | |
| DataCollatorWithPadding | |
| ) | |
| from sklearn.metrics import accuracy_score, f1_score | |
| from src.dataset import ToxicDataset | |
| MODEL_NAME = "beomi/KcELECTRA-base" | |
| DATA_PATH = "toxic_detection_model/data/train_toxic.txt" | |
| OUTPUT_DIR = "toxic_detection_model/models/checkpoints" | |
| LOG_DIR = "toxic_detection_model/models/logs" | |
| MAX_LEN = 128 | |
| BATCH_SIZE = 32 | |
| EPOCHS = 3 | |
| LEARNING_RATE = 2e-5 | |
| def compute_metrics(pred): | |
| labels = pred.label_ids | |
| preds = pred.predictions.argmax(-1) | |
| acc = accuracy_score(labels, preds) | |
| f1 = f1_score(labels, preds, average='weighted') | |
| return {'accuracy': acc, 'f1': f1} | |
| def main(): | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| print(f"Using device: {device}") | |
| print(f"Loading model: {MODEL_NAME}") | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=2).to(device) | |
| print("Loading datasets...") | |
| train_dataset = ToxicDataset(DATA_PATH, tokenizer, max_len=MAX_LEN, split='train') | |
| val_dataset = ToxicDataset(DATA_PATH, tokenizer, max_len=MAX_LEN, split='val') | |
| print(f"Train samples: {len(train_dataset)}") | |
| print(f"Val samples: {len(val_dataset)}") | |
| data_collator = DataCollatorWithPadding(tokenizer=tokenizer) | |
| training_args = TrainingArguments( | |
| output_dir=OUTPUT_DIR, | |
| num_train_epochs=EPOCHS, | |
| per_device_train_batch_size=BATCH_SIZE, | |
| per_device_eval_batch_size=BATCH_SIZE, | |
| warmup_steps=500, | |
| weight_decay=0.01, | |
| logging_dir=LOG_DIR, | |
| logging_steps=100, | |
| eval_strategy="epoch", | |
| save_strategy="epoch", | |
| load_best_model_at_end=True, | |
| metric_for_best_model="f1", | |
| fp16=True, | |
| dataloader_num_workers=2, | |
| report_to="none" | |
| ) | |
| trainer = Trainer( | |
| model=model, | |
| args=training_args, | |
| train_dataset=train_dataset, | |
| eval_dataset=val_dataset, | |
| compute_metrics=compute_metrics, | |
| data_collator=data_collator, | |
| ) | |
| print("Starting training...") | |
| trainer.train() | |
| print("Saving final model...") | |
| final_save_path = "toxic_detection_model/models/final_model" | |
| model.save_pretrained(final_save_path) | |
| tokenizer.save_pretrained(final_save_path) | |
| print(f"Model saved to {final_save_path}") | |
| if __name__ == "__main__": | |
| main() |