File size: 5,120 Bytes
9939244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
{
  "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",
        "login(token=os.getenv('HF_TOKEN'))  # Set HF_TOKEN in Colab secrets (left sidebar 🔑)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# @title Prepare dataset\n",
        "import json\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|><call_tool>get_crypto_price({\\\"symbol\\\": \\\"BTC\\\"})</call_tool><|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|><call_tool>multiply_numbers({\\\"a\\\": 42, \\\"b\\\": 58})</call_tool><|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|><call_tool>multiply_numbers({\\\"a\\\": 144, \\\"b\\\": 37})</call_tool><|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|><call_tool>get_crypto_price({\\\"symbol\\\": \\\"ETH\\\"})</call_tool><|eot_id|>\"}\n",
        "]\n",
        "with open('dataset.jsonl', 'w') as f:\n",
        "    for ex in dataset:\n",
        "        f.write(json.dumps(ex) + '\\n')\n",
        "print('Dataset written.')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# @title Upload dataset to HF\n",
        "from huggingface_hub import HfApi\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 (QLoRA)\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 $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",
        "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",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}