Upload colab_fsociety_finetune.ipynb with huggingface_hub
Browse files- colab_fsociety_finetune.ipynb +40 -43
colab_fsociety_finetune.ipynb
CHANGED
|
@@ -13,11 +13,14 @@
|
|
| 13 |
"cell_type": "code",
|
| 14 |
"metadata": {"id": "install"},
|
| 15 |
"source": [
|
| 16 |
-
"# Instalar dependencias\n",
|
| 17 |
-
"!pip install -qU transformers datasets accelerate peft bitsandbytes trl\n",
|
| 18 |
"!pip install -qU unsloth\n",
|
| 19 |
"!pip install -q huggingface_hub\n",
|
| 20 |
"\n",
|
|
|
|
|
|
|
|
|
|
| 21 |
"import torch, os, json\n",
|
| 22 |
"from huggingface_hub import notebook_login\n",
|
| 23 |
"notebook_login()"
|
|
@@ -25,52 +28,43 @@
|
|
| 25 |
"execution_count": null,
|
| 26 |
"outputs": []
|
| 27 |
},
|
| 28 |
-
|
| 29 |
"cell_type": "code",
|
| 30 |
-
"metadata": {"id": "
|
| 31 |
"source": [
|
| 32 |
-
"from
|
| 33 |
"\n",
|
| 34 |
-
"
|
| 35 |
-
"
|
| 36 |
-
"
|
| 37 |
-
"
|
| 38 |
-
"
|
| 39 |
-
"
|
| 40 |
-
"print(f\"
|
| 41 |
-
"print(f\"
|
| 42 |
],
|
| 43 |
"execution_count": null,
|
| 44 |
"outputs": []
|
| 45 |
},
|
| 46 |
-
|
| 47 |
"cell_type": "code",
|
| 48 |
-
"metadata": {"id": "
|
| 49 |
"source": [
|
| 50 |
-
"
|
| 51 |
-
"
|
| 52 |
-
"
|
| 53 |
-
"
|
| 54 |
-
"
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
"
|
| 58 |
-
"
|
| 59 |
-
"
|
| 60 |
-
"
|
| 61 |
-
"
|
| 62 |
-
"
|
| 63 |
-
"
|
| 64 |
-
"
|
| 65 |
-
" \n",
|
| 66 |
-
" if system:\n",
|
| 67 |
-
" prompt = f\"<bos><start_of_turn>user\\\\n{system}\\\\n\\\\n{user}<end_of_turn>\\\\n<start_of_turn>model\\\\n\"\n",
|
| 68 |
-
" else:\n",
|
| 69 |
-
" prompt = f\"<bos><start_of_turn>user\\\\n{user}<end_of_turn>\\\\n<start_of_turn>model\\\\n\"\n",
|
| 70 |
-
" response = f\"{assistant}<end_of_turn>\"\n",
|
| 71 |
-
" return {\"text\": prompt + response}\n",
|
| 72 |
-
"\n",
|
| 73 |
-
"ds = ds.map(format_chatml)\n",
|
| 74 |
"ds = ds.train_test_split(test_size=0.05, seed=42)\n",
|
| 75 |
"train_ds = ds['train']\n",
|
| 76 |
"eval_ds = ds['test']\n",
|
|
@@ -162,6 +156,7 @@
|
|
| 162 |
"cell_type": "code",
|
| 163 |
"metadata": {"id": "save"},
|
| 164 |
"source": [
|
|
|
|
| 165 |
"HF_USER = \"murdok1982\"\n",
|
| 166 |
"MODEL_NAME = \"fsociety-LoRA\"\n",
|
| 167 |
"\n",
|
|
@@ -186,10 +181,12 @@
|
|
| 186 |
"# Probar el modelo fine-tuned\n",
|
| 187 |
"from IPython.display import display, Markdown\n",
|
| 188 |
"\n",
|
| 189 |
-
"
|
| 190 |
-
"Eres un experto programador.
|
| 191 |
-
"
|
| 192 |
-
"\
|
|
|
|
|
|
|
| 193 |
"\n",
|
| 194 |
"inputs = tokenizer(test_prompt, return_tensors=\"pt\").to(\"cuda\")\n",
|
| 195 |
"outputs = model.generate(\n",
|
|
|
|
| 13 |
"cell_type": "code",
|
| 14 |
"metadata": {"id": "install"},
|
| 15 |
"source": [
|
| 16 |
+
"# Instalar dependencias (versiones compatibles con Unsloth)\n",
|
| 17 |
+
"!pip install -qU transformers==5.5.0 datasets==4.3.0 accelerate peft bitsandbytes trl==0.24.0\n",
|
| 18 |
"!pip install -qU unsloth\n",
|
| 19 |
"!pip install -q huggingface_hub\n",
|
| 20 |
"\n",
|
| 21 |
+
"import warnings\n",
|
| 22 |
+
"warnings.filterwarnings('ignore', category=UserWarning, module='huggingface_hub')",
|
| 23 |
+
"\n",
|
| 24 |
"import torch, os, json\n",
|
| 25 |
"from huggingface_hub import notebook_login\n",
|
| 26 |
"notebook_login()"
|
|
|
|
| 28 |
"execution_count": null,
|
| 29 |
"outputs": []
|
| 30 |
},
|
| 31 |
+
{
|
| 32 |
"cell_type": "code",
|
| 33 |
+
"metadata": {"id": "load_tokenizer"},
|
| 34 |
"source": [
|
| 35 |
+
"from transformers import AutoTokenizer\n",
|
| 36 |
"\n",
|
| 37 |
+
"MODEL_ID = \"google/gemma-4-E4B-it\"\n",
|
| 38 |
+
"MAX_SEQ_LENGTH = 2048\n",
|
| 39 |
+
"\n",
|
| 40 |
+
"print(\"Cargando tokenizer...\")\n",
|
| 41 |
+
"tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=True)\n",
|
| 42 |
+
"tokenizer.padding_side = 'right'\n",
|
| 43 |
+
"print(f\"Tokenizer listo. Pad token: {tokenizer.pad_token_id}\")\n",
|
| 44 |
+
"print(f\"Template: {tokenizer.chat_template[:100]}...\")"
|
| 45 |
],
|
| 46 |
"execution_count": null,
|
| 47 |
"outputs": []
|
| 48 |
},
|
| 49 |
+
{
|
| 50 |
"cell_type": "code",
|
| 51 |
+
"metadata": {"id": "load_dataset"},
|
| 52 |
"source": [
|
| 53 |
+
"from datasets import load_dataset\n",
|
| 54 |
+
"\n",
|
| 55 |
+
"print(\"Cargando dataset...\")\n",
|
| 56 |
+
"ds = load_dataset('murdok1982/gemma4-programacion-seguridad', split='train', streaming=False)\n",
|
| 57 |
+
"print(f\"Dataset: {len(ds):,} ejemplos\")\n",
|
| 58 |
+
"\n",
|
| 59 |
+
"# Convertir ChatML a texto usando el template de Gemma 4\n",
|
| 60 |
+
"def format_chatml(examples):\n",
|
| 61 |
+
" texts = []\n",
|
| 62 |
+
" for messages in examples['messages']:\n",
|
| 63 |
+
" text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False)\n",
|
| 64 |
+
" texts.append(text)\n",
|
| 65 |
+
" return {'text': texts}\n",
|
| 66 |
+
"\n",
|
| 67 |
+
"ds = ds.map(format_chatml, batched=True, batch_size=1000)\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
"ds = ds.train_test_split(test_size=0.05, seed=42)\n",
|
| 69 |
"train_ds = ds['train']\n",
|
| 70 |
"eval_ds = ds['test']\n",
|
|
|
|
| 156 |
"cell_type": "code",
|
| 157 |
"metadata": {"id": "save"},
|
| 158 |
"source": [
|
| 159 |
+
"from pathlib import Path\n",
|
| 160 |
"HF_USER = \"murdok1982\"\n",
|
| 161 |
"MODEL_NAME = \"fsociety-LoRA\"\n",
|
| 162 |
"\n",
|
|
|
|
| 181 |
"# Probar el modelo fine-tuned\n",
|
| 182 |
"from IPython.display import display, Markdown\n",
|
| 183 |
"\n",
|
| 184 |
+
"messages = [\n",
|
| 185 |
+
" {\"role\": \"system\", \"content\": \"Eres un experto programador.\"},\n",
|
| 186 |
+
" {\"role\": \"user\", \"content\": \"Escribe una funcion en Python que implemente busqueda binaria.\"},\n",
|
| 187 |
+
"]\n",
|
| 188 |
+
"test_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
|
| 189 |
+
"print(\"Prompt:\", test_prompt[:150])\n",
|
| 190 |
"\n",
|
| 191 |
"inputs = tokenizer(test_prompt, return_tensors=\"pt\").to(\"cuda\")\n",
|
| 192 |
"outputs = model.generate(\n",
|