Upload train_aya_sft.py with huggingface_hub
Browse files- train_aya_sft.py +91 -0
train_aya_sft.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.10"
|
| 3 |
+
# dependencies = [
|
| 4 |
+
# "trl>=0.12.0",
|
| 5 |
+
# "peft>=0.7.0",
|
| 6 |
+
# "trackio",
|
| 7 |
+
# "datasets",
|
| 8 |
+
# "transformers",
|
| 9 |
+
# "torch"
|
| 10 |
+
# ]
|
| 11 |
+
# ///
|
| 12 |
+
|
| 13 |
+
import os
|
| 14 |
+
from datasets import load_dataset
|
| 15 |
+
from peft import LoraConfig
|
| 16 |
+
from trl import SFTTrainer, SFTConfig
|
| 17 |
+
import trackio
|
| 18 |
+
|
| 19 |
+
def format_chatml(example):
|
| 20 |
+
# Language map based on subset
|
| 21 |
+
lang_map = {
|
| 22 |
+
'Lug_Uga': 'Luganda',
|
| 23 |
+
'Swa_Ken': 'Kiswahili',
|
| 24 |
+
'Aka_Gha': 'Akan',
|
| 25 |
+
'Amh_Eth': 'Amharic',
|
| 26 |
+
'Eng_Uga': 'English',
|
| 27 |
+
'Eng_Gha': 'English',
|
| 28 |
+
'Eng_Eth': 'English',
|
| 29 |
+
'Eng_Ken': 'English'
|
| 30 |
+
}
|
| 31 |
+
lang = lang_map.get(example['subset'], 'the Target Language')
|
| 32 |
+
|
| 33 |
+
system_prompt = f"You are a helpful, medically accurate AI assistant fluent in {lang}. Answer the following health question accurately and completely."
|
| 34 |
+
|
| 35 |
+
messages = [
|
| 36 |
+
{"role": "system", "content": system_prompt},
|
| 37 |
+
{"role": "user", "content": str(example['input'])},
|
| 38 |
+
{"role": "assistant", "content": str(example['output'])}
|
| 39 |
+
]
|
| 40 |
+
return {"messages": messages}
|
| 41 |
+
|
| 42 |
+
def main():
|
| 43 |
+
print("Loading dataset...")
|
| 44 |
+
dataset = load_dataset("Bedru/zindi-multilingual-health-qa")
|
| 45 |
+
|
| 46 |
+
print("Formatting dataset...")
|
| 47 |
+
train_dataset = dataset['train'].map(format_chatml, remove_columns=dataset['train'].column_names)
|
| 48 |
+
eval_dataset = dataset['validation'].map(format_chatml, remove_columns=dataset['validation'].column_names)
|
| 49 |
+
|
| 50 |
+
print("Initializing trainer...")
|
| 51 |
+
trainer = SFTTrainer(
|
| 52 |
+
model="CohereForAI/aya-23-8B",
|
| 53 |
+
train_dataset=train_dataset,
|
| 54 |
+
eval_dataset=eval_dataset,
|
| 55 |
+
peft_config=LoraConfig(
|
| 56 |
+
r=32,
|
| 57 |
+
lora_alpha=64,
|
| 58 |
+
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
|
| 59 |
+
task_type="CAUSAL_LM"
|
| 60 |
+
),
|
| 61 |
+
args=SFTConfig(
|
| 62 |
+
output_dir="zindi-health-qa-aya",
|
| 63 |
+
push_to_hub=True,
|
| 64 |
+
hub_model_id="sujanadh/zindi-health-qa-aya-23",
|
| 65 |
+
num_train_epochs=2,
|
| 66 |
+
per_device_train_batch_size=2,
|
| 67 |
+
gradient_accumulation_steps=8,
|
| 68 |
+
learning_rate=2e-5,
|
| 69 |
+
eval_strategy="steps",
|
| 70 |
+
eval_steps=100,
|
| 71 |
+
save_strategy="steps",
|
| 72 |
+
save_steps=100,
|
| 73 |
+
logging_steps=10,
|
| 74 |
+
report_to="trackio",
|
| 75 |
+
project="zindi-health-qa",
|
| 76 |
+
run_name="aya-23-8b-sft-run-1",
|
| 77 |
+
bf16=True,
|
| 78 |
+
max_length=1024,
|
| 79 |
+
hub_strategy="every_save",
|
| 80 |
+
)
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
print("Starting training...")
|
| 84 |
+
trainer.train()
|
| 85 |
+
|
| 86 |
+
print("Pushing final model to hub...")
|
| 87 |
+
trainer.push_to_hub()
|
| 88 |
+
print("Training complete!")
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
main()
|