Instructions to use eltorio/Llama-3.2-3B-appreciation with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use eltorio/Llama-3.2-3B-appreciation with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("unsloth/llama-3.2-3b-instruct-bnb-4bit") model = PeftModel.from_pretrained(base_model, "eltorio/Llama-3.2-3B-appreciation") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -64,6 +64,122 @@ L'interface génère une appréciation de 1 à 20 mots adaptée au profil de l'
|
|
| 64 |
3. Génération de l'appréciation
|
| 65 |
4. Post-traitement (vérification longueur/ton/grammaire)
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
## Sécurité et éthique
|
| 68 |
|
| 69 |
- Il est hors de question de mettre des appréciations automatiques, elles devront être validée et eventuellement corrigée par l'enseignant.
|
|
|
|
| 64 |
3. Génération de l'appréciation
|
| 65 |
4. Post-traitement (vérification longueur/ton/grammaire)
|
| 66 |
|
| 67 |
+
## Exemple de code d'inference avec Gradio
|
| 68 |
+
|
| 69 |
+
Attention ce code ne fonctionne qu'avec un GPU Cuda.
|
| 70 |
+
|
| 71 |
+
```python
|
| 72 |
+
import gradio as gr
|
| 73 |
+
from transformers import AutoProcessor, AutoTokenizer
|
| 74 |
+
from peft import AutoPeftModelForCausalLM
|
| 75 |
+
import torch
|
| 76 |
+
import os
|
| 77 |
+
|
| 78 |
+
if os.environ.get('HF_TOKEN') is None:
|
| 79 |
+
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")
|
| 80 |
+
|
| 81 |
+
hugging_face_model_id = "eltorio/Llama-3.2-3B-appreciation"
|
| 82 |
+
base_model_path = "meta-llama/Llama-3.2-3B-Instruct"
|
| 83 |
+
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
| 84 |
+
|
| 85 |
+
device_desc = f"Cette I.A. fonctionne sur {device} 🚀." if device == torch.device('cuda') else f"🐢 Cette I.A. ne peut pas fonctionner sur {device} 🐢."
|
| 86 |
+
# Define the title, description, and device description for the Gradio interface
|
| 87 |
+
title = f"Une intelligence artificielle pour écrire des appréciations et tourne sur {device}"
|
| 88 |
+
desc = "Ce modèle vous propose une évaluation automatique."
|
| 89 |
+
|
| 90 |
+
# Define the long description for the Gradio interface
|
| 91 |
+
long_desc = f"Cette démonstration est basée sur le modèle <a href='https://huggingface.co/eltorio/Llama-3.2-3B-appreciation'>Llama-3.2-3B-appreciation</a>, c'est un LLM basé sur Llama 3.2 3B-instruct!<br><b>{device_desc}</b><br> 2024 - Ronan Le Meillat"
|
| 92 |
+
|
| 93 |
+
if torch.cuda.is_available():
|
| 94 |
+
# Determine the device (GPU or CPU) to run the model on
|
| 95 |
+
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
| 96 |
+
print(f"Using device: {device}") # Log the device being used
|
| 97 |
+
# Initialize the processor from the base model path
|
| 98 |
+
processor = AutoProcessor.from_pretrained(base_model_path, trust_remote_code=True)
|
| 99 |
+
# Initialize the model from the base model path and set the torch dtype to bfloat16
|
| 100 |
+
peft_model = AutoPeftModelForCausalLM.from_pretrained(hugging_face_model_id)
|
| 101 |
+
merged_model = peft_model.merge_and_unload()
|
| 102 |
+
tokenizer = AutoTokenizer.from_pretrained(hugging_face_model_id)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
#tokenizer = get_chat_template(
|
| 106 |
+
# tokenizer,
|
| 107 |
+
# chat_template = "llama-3.1",
|
| 108 |
+
#)
|
| 109 |
+
|
| 110 |
+
# Define a function to infer a evaluation from the incoming parameters
|
| 111 |
+
def infere(trimestre: str, moyenne_1: float,moyenne_2: float,moyenne_3: float, comportement: float, participation: float, travail: float) -> str:
|
| 112 |
+
|
| 113 |
+
if trimestre == "1":
|
| 114 |
+
trimestre_full = "premier trimestre"
|
| 115 |
+
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."
|
| 116 |
+
elif trimestre == "2":
|
| 117 |
+
trimestre_full = "deuxième trimestre"
|
| 118 |
+
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."
|
| 119 |
+
elif trimestre == "3":
|
| 120 |
+
trimestre_full = "troisième trimestre"
|
| 121 |
+
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."
|
| 122 |
+
|
| 123 |
+
# Define a chat template for the model to respond to
|
| 124 |
+
messages = [
|
| 125 |
+
{
|
| 126 |
+
"role": "system",
|
| 127 |
+
"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, bienveillante, constructive, et 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."},
|
| 128 |
+
{
|
| 129 |
+
"role": "user",
|
| 130 |
+
"content": user_question},
|
| 131 |
+
]
|
| 132 |
+
inputs = tokenizer.apply_chat_template(
|
| 133 |
+
messages,
|
| 134 |
+
tokenize = True,
|
| 135 |
+
add_generation_prompt = True, # Must add for generation
|
| 136 |
+
return_tensors = "pt",).to(device)
|
| 137 |
+
outputs = merged_model.generate(input_ids = inputs, max_new_tokens = 90, use_cache = True,
|
| 138 |
+
temperature = 1.5, min_p = 0.1)
|
| 139 |
+
decoded_sequences = tokenizer.batch_decode(outputs[:, inputs.shape[1]:],skip_special_tokens=True)[0]
|
| 140 |
+
return decoded_sequences
|
| 141 |
+
|
| 142 |
+
# Create a Gradio interface with the infere function and specified title and descriptions
|
| 143 |
+
autoeval = gr.Interface(fn=infere, inputs=[
|
| 144 |
+
gr.Radio(
|
| 145 |
+
["1", "2", "3"], value="1", label="trimestre", info="Trimestre"
|
| 146 |
+
),
|
| 147 |
+
gr.Slider(0, 20,label="moyenne_1", value=10, info="Moyenne trimestre 1"),
|
| 148 |
+
gr.Slider(0, 20,label="moyenne_2", value=10, info="Moyenne trimestre 2"),
|
| 149 |
+
gr.Slider(0, 20,label="moyenne_3", value=10, info="Moyenne trimestre 3"),
|
| 150 |
+
gr.Slider(0, 10, value=5, label="comportement", info="Comportement (1 à 10)"),
|
| 151 |
+
gr.Slider(0, 10, value=5, label="participation", info="Participation (1 à 10)"),
|
| 152 |
+
gr.Slider(0, 10, value=5, label="travail", info="Travail (1 à 10)"),
|
| 153 |
+
|
| 154 |
+
], outputs="text", title=title,
|
| 155 |
+
description=desc, article=long_desc)
|
| 156 |
+
|
| 157 |
+
# Launch the Gradio interface and share it
|
| 158 |
+
autoeval.launch(server_name="0.0.0.0",share=True)
|
| 159 |
+
else:
|
| 160 |
+
print("No GPU available")
|
| 161 |
+
device = torch.device('cpu')
|
| 162 |
+
def infere(trimestre: str, moyenne_1: float,moyenne_2: float,moyenne_3: float, comportement: float, participation: float, travail: float) -> str:
|
| 163 |
+
return "No GPU available, please contact me"
|
| 164 |
+
|
| 165 |
+
# Create a Gradio interface with the infere function and specified title and descriptions
|
| 166 |
+
autoeval = gr.Interface(fn=infere, inputs=[
|
| 167 |
+
gr.Radio(
|
| 168 |
+
["1", "2", "3"], value="1", label="trimestre", info="Trimestre"
|
| 169 |
+
),
|
| 170 |
+
gr.Slider(0, 20,label="moyenne_1", value=10, info="Moyenne trimestre 1"),
|
| 171 |
+
gr.Slider(0, 20,label="moyenne_2", value=10, info="Moyenne trimestre 2"),
|
| 172 |
+
gr.Slider(0, 20,label="moyenne_3", value=10, info="Moyenne trimestre 3"),
|
| 173 |
+
gr.Slider(0, 10, value=5, label="comportement", info="Comportement (1 à 10)"),
|
| 174 |
+
gr.Slider(0, 10, value=5, label="participation", info="Participation (1 à 10)"),
|
| 175 |
+
gr.Slider(0, 10, value=5, label="travail", info="Travail (1 à 10)"),
|
| 176 |
+
|
| 177 |
+
], outputs="text", title=title,
|
| 178 |
+
description=desc, article=long_desc)
|
| 179 |
+
|
| 180 |
+
# Launch the Gradio interface and share it
|
| 181 |
+
autoeval.launch(server_name="0.0.0.0",share=True)
|
| 182 |
+
```
|
| 183 |
## Sécurité et éthique
|
| 184 |
|
| 185 |
- Il est hors de question de mettre des appréciations automatiques, elles devront être validée et eventuellement corrigée par l'enseignant.
|