Spaces:
Runtime error
Runtime error
File size: 4,166 Bytes
69b17de | 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 | import os
import pandas as pd
import numpy as np
import torch
import evaluate
from datasets import Dataset
from transformers import (
AutoTokenizer,
AutoModelForSequenceClassification,
TrainingArguments,
Trainer,
EarlyStoppingCallback
)
from sklearn.metrics import accuracy_score, precision_recall_fscore_support, confusion_matrix
# GPU Auto-Detection
DEVICE = "cpu"
def train_url_model():
print("Loading synthetic URL dataset...")
data_path = os.path.join("dataset", "data", "url_dataset.csv")
if not os.path.exists(data_path):
print(f"Dataset not found at {data_path}. Please run generate_dataset.py first.")
return
df = pd.read_csv(data_path)
# Map categories: benign -> 0, url_phishing -> 1
category_map = {
"benign": 0,
"url_phishing": 1
}
df = df[df["category"].isin(category_map.keys())].copy()
df["label"] = df["category"].map(category_map)
df = df.dropna(subset=["url", "label"])
print(f"Dataset Size: {len(df)} URLs")
# Convert to HuggingFace Dataset
hf_dataset = Dataset.from_pandas(df[["url", "label"]])
# Rename 'url' column to 'text' for standard tokenizer compatibility
hf_dataset = hf_dataset.rename_column("url", "text")
# Split 85/15 train/test with stratification
hf_dataset = hf_dataset.train_test_split(test_size=0.15, seed=42, stratify=hf_dataset["label"])
# UPGRADED: DistilRoBERTa-base → roberta-base for better performance
model_name = "roberta-base"
print(f"Loading Tokenizer: {model_name}")
tokenizer = AutoTokenizer.from_pretrained(model_name)
def tokenize_function(examples):
return tokenizer(examples["text"], padding="max_length", truncation=True, max_length=128)
print("Tokenizing URL dataset...")
tokenized_datasets = hf_dataset.map(tokenize_function, batched=True)
print(f"Loading Model: {model_name}")
model = AutoModelForSequenceClassification.from_pretrained(
model_name,
num_labels=2,
id2label={0: "benign", 1: "url_phishing"},
label2id={"benign": 0, "url_phishing": 1}
)
metric = evaluate.load("accuracy")
def compute_metrics(eval_pred):
logits, labels = eval_pred
predictions = np.argmax(logits, axis=-1)
accuracy = accuracy_score(labels, predictions)
precision, recall, f1, _ = precision_recall_fscore_support(
labels, predictions, average='weighted', zero_division=0
)
cm = confusion_matrix(labels, predictions)
return {
"accuracy": accuracy,
"precision": precision,
"recall": recall,
"f1": f1,
"confusion_matrix": cm.tolist()
}
output_dir = os.path.join("models", "url-scamdetect-finetuned")
training_args = TrainingArguments(
output_dir=output_dir,
eval_strategy="epoch",
save_strategy="epoch",
learning_rate=2e-5,
per_device_train_batch_size=32,
per_device_eval_batch_size=32,
num_train_epochs=5,
weight_decay=0.01,
warmup_ratio=0.1,
load_best_model_at_end=True,
metric_for_best_model="f1",
greater_is_better=True,
push_to_hub=False,
fp16=False,
logging_steps=100,
report_to=["none"],
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_datasets["train"],
eval_dataset=tokenized_datasets["test"],
processing_class=tokenizer,
compute_metrics=compute_metrics,
callbacks=[EarlyStoppingCallback(early_stopping_patience=2)]
)
print(f"Starting URL Training on {DEVICE.upper()}...")
trainer.train()
print("Evaluating URL Model Accuracy...")
eval_results = trainer.evaluate()
print(f"Final URL Evaluation Results: {eval_results}")
print(f"Saving Fine-Tuned URL Model to {output_dir}")
trainer.save_model(output_dir)
print("URL Training Complete!")
if __name__ == "__main__":
train_url_model()
|