Datasets:
feat: add LoRA fine-tuning training script for dialects
Browse files- train_lora_colab.py +79 -0
train_lora_colab.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Dialects-AI-Foundry: Whisper-Base LoRA Fine-Tuning Script
|
| 3 |
+
Target: Algerian Arabic Dialects
|
| 4 |
+
Compatible with: Google Colab / Kaggle (T4 GPU or better)
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import torch
|
| 9 |
+
from datasets import load_dataset, Audio
|
| 10 |
+
from transformers import (
|
| 11 |
+
WhisperForConditionalGeneration,
|
| 12 |
+
WhisperProcessor,
|
| 13 |
+
Seq2SeqTrainingArguments,
|
| 14 |
+
Seq2SeqTrainer
|
| 15 |
+
)
|
| 16 |
+
from peft import LoraConfig, get_peft_model
|
| 17 |
+
import evaluate
|
| 18 |
+
|
| 19 |
+
REPO_ID = "Termuxphone/dialects-ai-foundry"
|
| 20 |
+
MODEL_ID = "openai/whisper-base"
|
| 21 |
+
|
| 22 |
+
print(f"[*] Loading dataset from: {REPO_ID}")
|
| 23 |
+
ds = load_dataset(REPO_ID)
|
| 24 |
+
ds = ds.cast_column("audio", Audio(sampling_rate=16000))
|
| 25 |
+
|
| 26 |
+
processor = WhisperProcessor.from_pretrained(MODEL_ID, language="arabic", task="transcribe")
|
| 27 |
+
model = WhisperForConditionalGeneration.from_pretrained(MODEL_ID)
|
| 28 |
+
|
| 29 |
+
# Configure LoRA for resource-efficient fine-tuning
|
| 30 |
+
peft_config = LoraConfig(
|
| 31 |
+
r=16,
|
| 32 |
+
lora_alpha=32,
|
| 33 |
+
target_modules=["q_proj", "v_proj"],
|
| 34 |
+
lora_dropout=0.05,
|
| 35 |
+
bias="none"
|
| 36 |
+
)
|
| 37 |
+
model = get_peft_model(model, peft_config)
|
| 38 |
+
model.print_trainable_parameters()
|
| 39 |
+
|
| 40 |
+
def prepare_dataset(batch):
|
| 41 |
+
audio = batch["audio"]
|
| 42 |
+
batch["input_features"] = processor.feature_extractor(
|
| 43 |
+
audio["array"], sampling_rate=audio["sampling_rate"]
|
| 44 |
+
).input_features[0]
|
| 45 |
+
|
| 46 |
+
# Text key normalization
|
| 47 |
+
text = batch.get("sentence") or batch.get("transcript") or batch.get("text") or ""
|
| 48 |
+
batch["labels"] = processor.tokenizer(text).input_ids
|
| 49 |
+
return batch
|
| 50 |
+
|
| 51 |
+
print("[*] Processing dataset features...")
|
| 52 |
+
tokenized_ds = ds.map(prepare_dataset, remove_columns=ds["train"].column_names)
|
| 53 |
+
|
| 54 |
+
training_args = Seq2SeqTrainingArguments(
|
| 55 |
+
output_dir="./whisper-base-algerian-lora",
|
| 56 |
+
per_device_train_batch_size=8,
|
| 57 |
+
gradient_accumulation_steps=2,
|
| 58 |
+
learning_rate=1e-3,
|
| 59 |
+
warmup_steps=50,
|
| 60 |
+
max_steps=500,
|
| 61 |
+
gradient_checkpointing=True,
|
| 62 |
+
fp16=torch.cuda.is_available(),
|
| 63 |
+
evaluation_strategy="steps",
|
| 64 |
+
eval_steps=100,
|
| 65 |
+
save_steps=100,
|
| 66 |
+
logging_steps=25,
|
| 67 |
+
report_to=["none"],
|
| 68 |
+
predict_with_generate=True
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
trainer = Seq2SeqTrainer(
|
| 72 |
+
args=training_args,
|
| 73 |
+
model=model,
|
| 74 |
+
train_dataset=tokenized_ds["train"],
|
| 75 |
+
eval_dataset=tokenized_ds["test"],
|
| 76 |
+
tokenizer=processor.feature_extractor,
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
print("[*] Ready to launch training: trainer.train()")
|