File size: 11,069 Bytes
83b9995 c805d4c 83b9995 c805d4c 83b9995 c805d4c 83b9995 |
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
{
"cells": [
{
"cell_type": "markdown",
"id": "ea62ee81-8904-492e-a840-3664cf27e8fb",
"metadata": {},
"source": [
"# Autoeval inference testing"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa3d9de4-4e59-468f-92f0-b5f2ec55858d",
"metadata": {},
"outputs": [],
"source": [
"from transformers import AutoProcessor, AutoTokenizer, AutoModelForCausalLM\n",
"import torch\n",
"import os\n",
"\n",
"try:\n",
" from google.colab import userdata\n",
" HF_TOKEN = userdata.get('HF_TOKEN')\n",
" os.environ['HF_TOKEN'] = HF_TOKEN\n",
"except:\n",
" print(\"Not running in Google Colab, trying to get the HF_TOKEN from the environment\")\n",
"\n",
"\n",
"if os.environ.get('HF_TOKEN') is None:\n",
" raise ValueError(\"You must set the HF_TOKEN environment variable to use this script, you also need to have access to the Llama 3.2 model family\")\n",
"\n",
"hugging_face_model_id = \"eltorio/Llama-3.2-3B-appreciation\"\n",
"base_model_path = \"meta-llama/Llama-3.2-3B-Instruct\"\n",
"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n"
]
},
{
"cell_type": "markdown",
"id": "a7696fc5-7c8e-4c3c-a5e5-8b88dcdaa2de",
"metadata": {},
"source": [
"## Load the model"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a0668894-d42e-4e56-8448-4b83af04b213",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1c7303ddd88143a99b18d04f0def5efc",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"processor = AutoProcessor.from_pretrained(\n",
" base_model_path,\n",
" do_image_splitting=False\n",
")\n",
"\n",
"model = AutoModelForCausalLM.from_pretrained(\n",
" base_model_path,\n",
" torch_dtype=torch.float16,\n",
" low_cpu_mem_usage=True,\n",
").to(device)\n",
"model.load_adapter(hugging_face_model_id)\n",
"tokenizer = AutoTokenizer.from_pretrained(hugging_face_model_id)"
]
},
{
"cell_type": "markdown",
"id": "75ed038d-649d-4c91-8804-ad9bbe3c5963",
"metadata": {},
"source": [
"## Define a function for getting a multiturn conversation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "07cb81ed-7190-405a-8aea-be139cf24bc9",
"metadata": {},
"outputs": [],
"source": [
"# Define a function to infer a evaluation from the incoming parameters\n",
"def infere(trimestre: str, moyenne_1: float,moyenne_2: float,moyenne_3: float, comportement: float, participation: float, travail: float) -> str:\n",
"\n",
" if trimestre == \"1\":\n",
" trimestre_full = \"premier trimestre\"\n",
" user_question = f\"Veuillez rédiger une appréciation en moins de 40 mots pour le {trimestre_full} pour cet élève qui a eu {moyenne_1} de moyenne, j'ai évalué son comportement à {comportement}/10, sa participation à {participation}/10 et son travail à {travail}/10. Les notes ne doivent pas apparaître dans l'appréciation.\"\n",
" elif trimestre == \"2\":\n",
" trimestre_full = \"deuxième trimestre\"\n",
" user_question = f\"Veuillez rédiger une appréciation en moins de 40 mots pour le {trimestre_full} pour cet élève qui a eu {moyenne_2} de moyenne ce trimestre et {moyenne_1} au premier trimestre, j'ai évalué son comportement à {comportement}/10, sa participation à {participation}/10 et son travail à {travail}/10. Les notes ne doivent pas apparaître dans l'appréciation.\"\n",
" elif trimestre == \"3\":\n",
" trimestre_full = \"troisième trimestre\"\n",
" user_question= f\"Veuillez rédiger une appréciation en moins de 40 mots pour le {trimestre_full} pour cet élève qui a eu {moyenne_3} de moyenne ce trimestre, {moyenne_2} au deuxième trimestre et {moyenne_1} au premier trimestre, j'ai évalué son comportement à {comportement}/10, sa participation à {participation}/10 et son travail à {travail}/10. Les notes ne doivent pas apparaître dans l'appréciation.\"\n",
" messages = [\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": \"Vous êtes une IA assistant les enseignants d'histoire-géographie en rédigeant à leur place une appréciation personnalisée pour leur élève en fonction de ses performances. Votre appreciation doit être en français, et doit aider l'élève à comprendre ses points forts et les axes d'amélioration. Votre appréciation doit comporter de 1 à 40 mots. Votre appréciation ne doit jamais comporter la valeur de la note. Votre appréciation doit utiliser le style impersonnel.Attention l'élément le plus important de votre analyse doit rester la moyenne du trimestre\"},\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": user_question},\n",
" ]\n",
" return messages"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0874967f-ddcc-4b3f-9625-e934cff38d44",
"metadata": {},
"outputs": [],
"source": [
"messages = infere(\"1\", 3, float('nan'), float('nan'), 10, 10, 10)"
]
},
{
"cell_type": "markdown",
"id": "2686f92a-2de6-420e-a36a-3581f4df3ed8",
"metadata": {},
"source": [
"## Tokenize the input"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b58c8308-f5df-48d1-b872-2b539f1eef19",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[128000, 128006, 9125, 128007, 271, 38766, 1303, 33025, 2696,\n",
" 25, 6790, 220, 2366, 18, 198, 15724, 2696, 25,\n",
" 220, 1627, 5887, 220, 2366, 19, 271, 43273, 62299,\n",
" 6316, 44190, 18328, 3625, 68061, 625, 1821, 294, 6,\n",
" 90446, 2427, 978, 3257, 648, 665, 9517, 67, 7404,\n",
" 519, 3869, 28130, 2035, 6316, 917, 43711, 5979, 367,\n",
" 97252, 35965, 8047, 5019, 28130, 33013, 79351, 665, 34501,\n",
" 409, 15907, 24601, 13, 650, 52262, 35996, 42182, 23761,\n",
" 665, 55467, 11, 1880, 42182, 91878, 326, 6, 19010,\n",
" 79351, 3869, 60946, 265, 15907, 3585, 75652, 1880, 3625,\n",
" 25776, 294, 58591, 73511, 7769, 13, 650, 52262, 917,\n",
" 43711, 5979, 367, 42182, 52962, 261, 409, 220, 16,\n",
" 3869, 220, 1272, 78199, 13, 650, 52262, 917, 43711,\n",
" 5979, 367, 841, 42182, 56316, 52962, 261, 1208, 51304,\n",
" 409, 1208, 5296, 13, 650, 52262, 917, 43711, 5979,\n",
" 367, 42182, 75144, 514, 1742, 60849, 8301, 47472, 3012,\n",
" 326, 6, 29982, 479, 514, 5636, 3062, 409, 15265,\n",
" 49586, 42182, 2800, 261, 1208, 52138, 26193, 3930, 75110,\n",
" 265, 128009, 128006, 882, 128007, 271, 53, 89025, 9517,\n",
" 67, 7420, 6316, 917, 43711, 5979, 367, 665, 40970,\n",
" 409, 220, 1272, 78199, 5019, 514, 21134, 75110, 265,\n",
" 5019, 42067, 33013, 79351, 7930, 264, 15925, 220, 18,\n",
" 409, 52138, 26193, 11, 503, 34155, 4046, 26591, 978,\n",
" 4538, 52962, 1133, 3869, 220, 605, 14, 605, 11,\n",
" 829, 20852, 3869, 220, 605, 14, 605, 1880, 4538,\n",
" 42775, 3869, 220, 605, 14, 605, 13, 11876, 8554,\n",
" 841, 97569, 6502, 917, 5169, 66014, 7010, 326, 6,\n",
" 391, 652, 978, 5979, 367, 13, 128009, 128006, 78191,\n",
" 128007, 271]], device='cuda:0')"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inputs = tokenizer.apply_chat_template(\n",
" messages,\n",
" tokenize = True,\n",
" add_generation_prompt = True,\n",
" return_tensors = \"pt\",).to(device)\n",
"inputs"
]
},
{
"cell_type": "markdown",
"id": "49d35c86-4d5d-4aaa-8e49-6b6e7386d4d6",
"metadata": {},
"source": [
"## Generate the output"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "94723775-3774-4e5f-b9bb-53b6f16fb432",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"The attention mask is not set and cannot be inferred from input because pad token is same as eos token. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.\n"
]
},
{
"data": {
"text/plain": [
"\"Quel changement (positif) par rapport à l'an passé! X travaille plus sérieusement, il fait davantage d'effort, il participe. Son redoublement lui permettra d'avoir une deuxième année plus confortable. Continuer ainsi devrait payer au final.\""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"outputs = model.generate(input_ids = inputs, \n",
" max_new_tokens = 90, \n",
" use_cache = True,\n",
" temperature = 1.5,\n",
" min_p = 0.1,\n",
" pad_token_id=tokenizer.eos_token_id,)\n",
"decoded_sequences = tokenizer.batch_decode(outputs[:, inputs.shape[1]:],skip_special_tokens=True)[0]\n",
"decoded_sequences"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5d1ba3fb-2e62-4486-9c45-3567d4d3a6f0",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|