{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":10010677,"sourceType":"datasetVersion","datasetId":6161747}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":5,"nbformat":4,"cells":[{"id":"701975cd-049e-433f-972c-b1e32db51e98","cell_type":"markdown","source":"# Autoeval","metadata":{}},{"id":"1ac68e5b-bc0b-4687-880c-0ee82cf367c9","cell_type":"markdown","source":"## Install the required libraries","metadata":{}},{"id":"cd68281b-00d3-4e20-9a72-0c0f47a2a255","cell_type":"code","source":"%%capture\n\n!pip install unsloth\n\n# Also get the latest nightly Unsloth!\n\n!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir --no-deps git+https://github.com/unslothai/unsloth.git\n\n!pip install tf-keras","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-25T19:18:40.250680Z","iopub.execute_input":"2024-11-25T19:18:40.251338Z"}},"outputs":[],"execution_count":null},{"id":"17e6fb3a-5a9e-42b6-bf67-5e2f3ae429b8","cell_type":"markdown","source":"### Log in Hugging hub","metadata":{}},{"id":"a5682c8f-4f73-47b5-82c5-9bde4977c4ab","cell_type":"code","source":"from huggingface_hub import login\nfrom kaggle_secrets import UserSecretsClient\nimport os\n\nuser_secrets = UserSecretsClient()\nHF_TOKEN = user_secrets.get_secret(\"HF_TOKEN\")\n\nlogin(\n token=HF_TOKEN,\n add_to_git_credential=False\n)\n\nos.environ[\"HF_TOKEN\"] = HF_TOKEN","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"64e7825e-402d-4499-a3f4-7d891574dd82","cell_type":"markdown","source":"## Training parameters","metadata":{}},{"id":"521874af-e63c-4960-81b4-9cf7b4b45024","cell_type":"code","source":"from unsloth import FastLanguageModel\n\nimport torch\n\nmax_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!\n\ndtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+\n\nload_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"1ab798f8-6cd1-444d-bc35-5cf70b4e88c6","cell_type":"markdown","source":"## Load the source model","metadata":{}},{"id":"8c112ad2-fdc4-4558-91e4-7a56f2e0f7db","cell_type":"code","source":"model, tokenizer = FastLanguageModel.from_pretrained(\n\n model_name = \"unsloth/Llama-3.2-3B-Instruct\", # or choose \"unsloth/Llama-3.2-1B-Instruct\"\n\n max_seq_length = max_seq_length,\n\n dtype = dtype,\n\n load_in_4bit = load_in_4bit,\n\n token = HF_TOKEN,\n\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"a2d01782-536c-4a16-b12d-c63a37f297f1","cell_type":"markdown","source":"## Add the Peft model","metadata":{}},{"id":"3c4fe7e6-5902-44b6-9f88-837773e6b6fa","cell_type":"code","source":"model = FastLanguageModel.get_peft_model(\n\n model,\n\n r = 16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128\n\n target_modules = [\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\",\n\n \"gate_proj\", \"up_proj\", \"down_proj\",],\n\n lora_alpha = 16,\n\n lora_dropout = 0, # Supports any, but = 0 is optimized\n\n bias = \"none\", # Supports any, but = \"none\" is optimized\n\n # [NEW] \"unsloth\" uses 30% less VRAM, fits 2x larger batch sizes!\n\n use_gradient_checkpointing = \"unsloth\", # True or \"unsloth\" for very long context\n\n random_state = 3407,\n\n use_rslora = False, # We support rank stabilized LoRA\n\n loftq_config = None, # And LoftQ\n\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"2e671ab0-ae7b-43ba-8ea1-198391a17327","cell_type":"markdown","source":"### Read the data","metadata":{}},{"id":"ce42f3fa-81a7-468b-a17f-42bb5ca20839","cell_type":"code","source":"import pandas as pd\n\ndf_data = pd.read_csv(\"/kaggle/input/appreciation/appreciation.csv\")\n\ndf_data.head()\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"36567695-dd13-4e37-b299-a4b352be8ef7","cell_type":"markdown","source":"### Create the messages from the data\n\nThe data is in the form of a csv file with the following columns:\n\n```csv\n\nId,trimestre,résumé,note 1er trimestre,note 2ème trimestre,note 3ème trimestre,comportement 0-10,participation 0-10,travail 0-10,commentaire\n\n0,1,\"Mauvais trimestre, manque de travail\",5.0,,,5.0,5.0,5.0,X a beaucoup de difficultés dues à des lacunes mais aussi à un manque de travail qui ne permet pas de les combler. Il faut s'y mettre au prochain trimestre.\n\n```\n\nWe need to create HuggingFace's normal multiturn format ","metadata":{}},{"id":"7d35b181-8d08-4faf-82c6-1ecfe15e016b","cell_type":"code","source":"def create_training_turn(row):\n\n system_prompt = (\n\n \"Vous êtes une IA assistant les enseignants à rédiger des appréciations personnalisées \"\n\n \"pour leurs élèves en fonction de leurs performances. Vos commentaires doivent être en français, \"\n\n \"bienveillants, constructifs, et aider l'élève à comprendre ses points forts et les axes d'amélioration.\"\n\n )\n\n \n\n user_input = f\"\"\"Voici les données de l'élève :\n\n\n\n- Trimestre : {row['trimestre']}\n\n- Note du 1er trimestre : {row['note 1er trimestre'] if not pd.isna(row['note 1er trimestre']) else 'N/A'}\n\n- Note du 2ᵉ trimestre : {row['note 2ème trimestre'] if not pd.isna(row['note 2ème trimestre']) else 'N/A'}\n\n- Note du 3ᵉ trimestre : {row['note 3ème trimestre'] if not pd.isna(row['note 3ème trimestre']) else 'N/A'}\n\n- Comportement (0-10) : {row['comportement 0-10']}\n\n- Participation (0-10) : {row['participation 0-10']}\n\n- Travail (0-10) : {row['travail 0-10']}\n\n\n\nVeuillez rédiger une appréciation pour cet élève.\"\"\"\n\n \n\n assistant_response = row['commentaire']\n\n return [\n\n {\"role\": \"system\", \"content\":system_prompt},\n\n {\"role\": \"user\", \"content\":user_input},\n\n {\"role\": \"assistant\", \"content\":assistant_response}\n\n ]\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"158e640a-c82d-4049-8f34-451e0b03588b","cell_type":"markdown","source":"### Check the function","metadata":{}},{"id":"66a215ca-61ab-402e-9893-7d4c7e90be51","cell_type":"code","source":"test_row = df_data.iloc[208]\n\ncreate_training_turn(test_row)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"363d83e0-a30d-4afa-bc6c-d6925af2ed9d","cell_type":"markdown","source":"### Create the dataset","metadata":{}},{"id":"9ca401fa-e001-470f-8701-9415e5adbecc","cell_type":"code","source":"from datasets import Dataset\n\nmulti_turn_data = df_data.apply(create_training_turn, axis=1)\n\ndataset = Dataset.from_dict({\"conversations\": multi_turn_data.tolist()})","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"45e67437-9e49-45ef-9a92-5c1bb04b5831","cell_type":"markdown","source":"## Tokenize the data","metadata":{}},{"id":"243f922c-9c28-4acf-8bbd-644d33a9183d","cell_type":"code","source":"from unsloth.chat_templates import get_chat_template\n\n\n\ntokenizer = get_chat_template(\n\n tokenizer,\n\n chat_template = \"llama-3.1\",\n\n)\n\n\n\ndef formatting_prompts_func(messages):\n\n convos = messages[\"conversations\"]\n\n texts = [tokenizer.apply_chat_template(convo, tokenize = False, add_generation_prompt = False) for convo in convos]\n\n return { \"text\" : texts, }\n\npass\n\n\n\ndataset = dataset.map(formatting_prompts_func, batched = True,)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"53f18d73-3dfc-47a1-8d8f-17d3e3cf6732","cell_type":"markdown","source":"### Check the tokenized data","metadata":{}},{"id":"1a74df2c-0493-4cb1-a763-9ea03d1b9381","cell_type":"code","source":"dataset[\"text\"][0]","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"04cc4b63-51bd-4753-b056-ee2004d7a2ee","cell_type":"markdown","source":"### Parmeters for training","metadata":{}},{"id":"a03df12c-4b86-4aba-b375-75d8bc3bfd91","cell_type":"code","source":"from trl import SFTTrainer\n\nfrom transformers import TrainingArguments, DataCollatorForSeq2Seq\n\nfrom unsloth import is_bfloat16_supported\n\n\n\ntrainer = SFTTrainer(\n\n model = model,\n\n tokenizer = tokenizer,\n\n train_dataset = dataset,\n\n dataset_text_field = \"text\",\n\n max_seq_length = max_seq_length,\n\n data_collator = DataCollatorForSeq2Seq(tokenizer = tokenizer),\n\n dataset_num_proc = 2,\n\n packing = False, # Can make training 5x faster for short sequences.\n\n args = TrainingArguments(\n\n per_device_train_batch_size = 2,\n\n gradient_accumulation_steps = 4,\n\n warmup_steps = 5,\n\n # num_train_epochs = 1, # Set this for 1 full training run.\n\n max_steps = 60,\n\n learning_rate = 2e-4,\n\n fp16 = not is_bfloat16_supported(),\n\n bf16 = is_bfloat16_supported(),\n\n logging_steps = 1,\n\n optim = \"adamw_8bit\",\n\n weight_decay = 0.01,\n\n lr_scheduler_type = \"linear\",\n\n seed = 3407,\n\n output_dir = \"outputs\",\n\n report_to = \"none\", # Use this for WandB etc\n\n push_to_hub = True,\n\n push_to_hub_model_id = \"Llama-3.2-3B-appreciation\"\n\n ),\n\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"59d73e87-1604-490e-a9aa-344c2a09372d","cell_type":"code","source":"from unsloth.chat_templates import train_on_responses_only\n\ntrainer = train_on_responses_only(\n\n trainer,\n\n instruction_part = \"<|start_header_id|>user<|end_header_id|>\\n\\n\",\n\n response_part = \"<|start_header_id|>assistant<|end_header_id|>\\n\\n\",\n\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"fb247846-0fdd-48ab-b9e1-ff2a1afedc2b","cell_type":"code","source":"tokenizer.decode(trainer.train_dataset[5][\"input_ids\"])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"6a5ec80a-54fc-4aec-a4be-784d7065c1e8","cell_type":"code","source":"space = tokenizer(\" \", add_special_tokens = False).input_ids[0]\n\ntokenizer.decode([space if x == -100 else x for x in trainer.train_dataset[5][\"labels\"]])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"1f267205-81d2-458f-9355-f1ec6d6c2880","cell_type":"code","source":"trainer_stats = trainer.train()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"69df8fa9-6ee7-45e1-8998-1efa379b9570","cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}