{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# @title Install dependencies\n",
"!pip install -q -U autotrain-advanced huggingface_hub huggingface_hub[cli] optimum transformers accelerate peft bitsandbytes datasets"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# @title Login to Hugging Face\n",
"from huggingface_hub import login\n",
"import os\n",
"token = os.getenv('HF_TOKEN')\n",
"if not token:\n",
" raise ValueError('Set HF_TOKEN in Colab secrets (left sidebar 🔑)')\n",
"login(token=token)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# @title Upload dataset to HF Hub\n",
"from huggingface_hub import HfApi\n",
"import json\n",
"\n",
"dataset = [\n",
" {\"text\": \"<|begin_of_text|><|start_header_id|>user<|end_header_id|>What is the current price of Bitcoin?<|eot_id|><|start_header_id|>assistant<|end_header_id|>get_crypto_price({\\\"symbol\\\": \\\"BTC\\\"})<|eot_id|>\"},\n",
" {\"text\": \"<|begin_of_text|><|start_header_id|>user<|end_header_id|>Multiply 42 by 58 for me.<|eot_id|><|start_header_id|>assistant<|end_header_id|>multiply_numbers({\\\"a\\\": 42, \\\"b\\\": 58})<|eot_id|>\"},\n",
" {\"text\": \"<|begin_of_text|><|start_header_id|>user<|end_header_id|>What is the capital of France?<|eot_id|><|start_header_id|>assistant<|end_header_id|>The capital of France is Paris.<|eot_id|>\"},\n",
" {\"text\": \"<|begin_of_text|><|start_header_id|>user<|end_header_id|>Calculate 144 multiplied by 37.<|eot_id|><|start_header_id|>assistant<|end_header_id|>multiply_numbers({\\\"a\\\": 144, \\\"b\\\": 37})<|eot_id|>\"},\n",
" {\"text\": \"<|begin_of_text|><|start_header_id|>user<|end_header_id|>What's the current Ethereum price?<|eot_id|><|start_header_id|>assistant<|end_header_id|>get_crypto_price({\\\"symbol\\\": \\\"ETH\\\"})<|eot_id|>\"}\n",
"]\n",
"\n",
"with open('dataset.jsonl', 'w') as f:\n",
" for ex in dataset:\n",
" f.write(json.dumps(ex) + '\\n')\n",
"\n",
"api = HfApi()\n",
"api.upload_file(\n",
" path_or_fileobj='dataset.jsonl',\n",
" path_in_repo='dataset.jsonl',\n",
" repo_id='yummyfiles/Bob',\n",
" repo_type='dataset',\n",
" token=os.getenv('HF_TOKEN')\n",
")\n",
"print('Dataset uploaded to yummyfiles/Bob')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# @title Fine-tune with AutoTrain (runs ~15 min on T4)\n",
"!autotrain llm --train \\\n",
" --project-name Bob \\\n",
" --model meta-llama/Llama-3.2-1B-Instruct \\\n",
" --train-data yummyfiles/Bob \\\n",
" --data-path dataset.jsonl \\\n",
" --text-column text \\\n",
" --trainer peft \\\n",
" --peft-r 16 \\\n",
" --peft-alpha 32 \\\n",
" --peft-dropout 0.05 \\\n",
" --learning-rate 2e-4 \\\n",
" --batch-size 1 \\\n",
" --epochs 3 \\\n",
" --block-size 2048 \\\n",
" --warmup-ratio 0.03 \\\n",
" --optimizer paged_adamw_8bit \\\n",
" --scheduler cosine \\\n",
" --gradient-accumulation 4 \\\n",
" --mixed-precision bf16 \\\n",
" --quantization 4bit \\\n",
" --push-to-hub \\\n",
" --repo-id yummyfiles/Bob \\\n",
" --token {os.getenv('HF_TOKEN')}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# @title Export to ONNX (4-bit) and upload\n",
"!pip install -q optimum[onnxruntime]\n",
"!optimum-cli export onnx \\\n",
" --model yummyfiles/Bob \\\n",
" --task text-generation \\\n",
" --quantize int4 \\\n",
" --output /content/web_model\n",
"\n",
"from huggingface_hub import HfApi\n",
"api = HfApi()\n",
"api.upload_folder(\n",
" folder_path='/content/web_model',\n",
" path_in_repo='web_model',\n",
" repo_id='yummyfiles/Bob',\n",
" repo_type='model',\n",
" token=os.getenv('HF_TOKEN')\n",
")\n",
"print('ONNX model uploaded to yummyfiles/Bob/web_model')"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 4
}