|
import os |
|
from datasets import load_dataset, load_metric |
|
import numpy as np |
|
from transformers import AutoAdapterModel, AutoTokenizer, TrainingArguments, Trainer |
|
from dotenv import load_dotenv |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") |
|
HF_TOKEN = os.getenv("HF_TOKEN") |
|
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") |
|
WAND_API_KEY = os.getenv("WAND_API_KEY") |
|
|
|
|
|
|
|
|
|
|
|
dataset_pentesting = load_dataset("canstralian/pentesting-ai") |
|
dataset_redpajama = load_dataset("togethercomputer/RedPajama-Data-1T") |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("canstralian/rabbitredeux") |
|
|
|
def tokenize_function(examples): |
|
return tokenizer(examples['text'], padding="max_length", truncation=True) |
|
|
|
|
|
tokenized_dataset_pentesting = dataset_pentesting.map(tokenize_function, batched=True) |
|
tokenized_dataset_redpajama = dataset_redpajama.map(tokenize_function, batched=True) |
|
|
|
|
|
train_dataset_pentesting = tokenized_dataset_pentesting["train"] |
|
validation_dataset_pentesting = tokenized_dataset_pentesting["validation"] |
|
|
|
|
|
model = AutoAdapterModel.from_pretrained("canstralian/rabbitredeux") |
|
model.load_adapter("Canstralian/RabbitRedux", set_active=True) |
|
|
|
|
|
metric = load_metric("accuracy") |
|
|
|
|
|
training_args = TrainingArguments( |
|
output_dir="./results", |
|
num_train_epochs=3, |
|
per_device_train_batch_size=8, |
|
per_device_eval_batch_size=8, |
|
warmup_steps=500, |
|
weight_decay=0.01, |
|
logging_dir="./logs", |
|
logging_steps=10, |
|
evaluation_strategy="epoch" |
|
) |
|
|
|
|
|
trainer = Trainer( |
|
model=model, |
|
args=training_args, |
|
train_dataset=train_dataset_pentesting, |
|
eval_dataset=validation_dataset_pentesting, |
|
compute_metrics=lambda p: metric.compute(predictions=np.argmax(p.predictions, axis=1), references=p.label_ids) |
|
) |
|
|
|
|
|
trainer.train() |
|
|
|
|
|
eval_results = trainer.evaluate() |
|
print("Evaluation Results: ", eval_results) |
|
|
|
|
|
model.save_pretrained("./fine_tuned_model") |
|
|
|
|
|
new_data = """ |
|
I love the ocean. It is so peaceful and serene. |
|
""" |
|
|
|
|
|
tokenized_new_data = tokenize_function({"text": [new_data]}) |
|
input_ids = tokenized_new_data["input_ids"][0] |
|
attention_mask = tokenized_new_data["attention_mask"][0] |
|
|
|
|
|
outputs = model(input_ids=np.array([input_ids]), attention_mask=np.array([attention_mask])) |
|
prediction_scores = outputs.logits[0] |
|
|
|
|
|
predicted_label = np.argmax(prediction_scores) |
|
|
|
print(f"The predicted label is: {predicted_label}") |
|
|
|
|
|
actual_label = 1 |
|
|
|
accuracy = metric.compute(predictions=[predicted_label], references=[actual_label]) |
|
|
|
print(f"Accuracy on new data: {accuracy}") |
|
|