Spaces:
Sleeping
Sleeping
æLtorio
commited on
use peft as an adapter
Browse files
app.py
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoProcessor, AutoTokenizer
|
| 3 |
from peft import AutoPeftModelForCausalLM
|
| 4 |
import torch
|
| 5 |
import os
|
|
@@ -7,117 +19,94 @@ import os
|
|
| 7 |
if os.environ.get('HF_TOKEN') is None:
|
| 8 |
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")
|
| 9 |
|
|
|
|
| 10 |
hugging_face_model_id = "eltorio/Llama-3.2-3B-appreciation"
|
| 11 |
base_model_path = "meta-llama/Llama-3.2-3B-Instruct"
|
| 12 |
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
| 13 |
|
| 14 |
-
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} 🐢."
|
| 15 |
# Define the title, description, and device description for the Gradio interface
|
|
|
|
| 16 |
title = f"Une intelligence artificielle pour écrire des appréciations qui tourne sur {device}"
|
| 17 |
desc = "Ce modèle vous propose une évaluation automatique."
|
| 18 |
|
| 19 |
# Define the long description for the Gradio interface
|
| 20 |
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"
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
processor = AutoProcessor.from_pretrained(base_model_path, trust_remote_code=True)
|
| 28 |
-
# Initialize the model from the base model path and set the torch dtype to bfloat16
|
| 29 |
-
peft_model = AutoPeftModelForCausalLM.from_pretrained(hugging_face_model_id,low_cpu_mem_usage=True)
|
| 30 |
-
merged_model = peft_model.merge_and_unload()
|
| 31 |
-
tokenizer = AutoTokenizer.from_pretrained(hugging_face_model_id)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# chat_template = "llama-3.1",
|
| 37 |
-
#)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
# Define a chat template for the model to respond to
|
| 53 |
-
_messages = [
|
| 54 |
-
{
|
| 55 |
-
"role": "system",
|
| 56 |
-
"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. Attention l'élément le plus important de votre analyse doit rester la moyenne du trimestre"},
|
| 57 |
-
{
|
| 58 |
-
"role": "user",
|
| 59 |
-
"content": user_question},
|
| 60 |
-
]
|
| 61 |
-
messages = [
|
| 62 |
-
{
|
| 63 |
-
"role": "system",
|
| 64 |
-
"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"},
|
| 65 |
-
{
|
| 66 |
-
"role": "user",
|
| 67 |
-
"content": user_question},
|
| 68 |
-
]
|
| 69 |
-
inputs = tokenizer.apply_chat_template(
|
| 70 |
messages,
|
| 71 |
tokenize = True,
|
| 72 |
-
add_generation_prompt = True,
|
| 73 |
return_tensors = "pt",).to(device)
|
| 74 |
-
|
|
|
|
| 75 |
max_new_tokens = 90,
|
| 76 |
use_cache = True,
|
| 77 |
temperature = 1.5,
|
| 78 |
min_p = 0.1,
|
| 79 |
pad_token_id=tokenizer.eos_token_id,)
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
else:
|
| 101 |
-
print("No GPU available")
|
| 102 |
-
device = torch.device('cpu')
|
| 103 |
-
def infere(trimestre: str, moyenne_1: float,moyenne_2: float,moyenne_3: float, comportement: float, participation: float, travail: float) -> str:
|
| 104 |
-
gr.Warning("No GPU available <br>Open a message in the <a href='https://huggingface.co/spaces/eltorio/Llama-3.2-3B-appreciation/discussions'>Community Discussion</a>")
|
| 105 |
-
return "No GPU available, please contact me on the community discussion"
|
| 106 |
|
| 107 |
-
# Create a Gradio interface with the infere function and specified title and descriptions
|
| 108 |
-
autoeval = gr.Interface(fn=infere, inputs=[
|
| 109 |
-
gr.Radio(
|
| 110 |
-
["1", "2", "3"], value="1", label="trimestre", info="Trimestre"
|
| 111 |
-
),
|
| 112 |
-
gr.Slider(0, 20,label="moyenne_1", value=10, info="Moyenne trimestre 1"),
|
| 113 |
-
gr.Slider(0, 20,label="moyenne_2", value=10, info="Moyenne trimestre 2"),
|
| 114 |
-
gr.Slider(0, 20,label="moyenne_3", value=10, info="Moyenne trimestre 3"),
|
| 115 |
-
gr.Slider(0, 10, value=5, label="comportement", info="Comportement (1 à 10)"),
|
| 116 |
-
gr.Slider(0, 10, value=5, label="participation", info="Participation (1 à 10)"),
|
| 117 |
-
gr.Slider(0, 10, value=5, label="travail", info="Travail (1 à 10)"),
|
| 118 |
-
|
| 119 |
-
], outputs="text", title=title,
|
| 120 |
-
description=desc, article=long_desc)
|
| 121 |
-
|
| 122 |
-
# Launch the Gradio interface and share it
|
| 123 |
-
autoeval.launch(server_name="0.0.0.0",share=True)
|
|
|
|
| 1 |
+
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
"""
|
| 4 |
+
app.py
|
| 5 |
+
|
| 6 |
+
This script creates a graphical interface to test an automatic evaluation generation model.
|
| 7 |
+
The model is available on Hugging Face and is based on the Llama 3.2 3B-instruct model.
|
| 8 |
+
model_id: "eltorio/Llama-3.2-3B-appreciation"
|
| 9 |
+
|
| 10 |
+
Author: Ronan Le Meillat
|
| 11 |
+
License: AGPL-3.0
|
| 12 |
+
"""
|
| 13 |
import gradio as gr
|
| 14 |
+
from transformers import AutoProcessor, AutoTokenizer, AutoModelForCausalLM
|
| 15 |
from peft import AutoPeftModelForCausalLM
|
| 16 |
import torch
|
| 17 |
import os
|
|
|
|
| 19 |
if os.environ.get('HF_TOKEN') is None:
|
| 20 |
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")
|
| 21 |
|
| 22 |
+
# sets the main paremeters
|
| 23 |
hugging_face_model_id = "eltorio/Llama-3.2-3B-appreciation"
|
| 24 |
base_model_path = "meta-llama/Llama-3.2-3B-Instruct"
|
| 25 |
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
| 26 |
|
|
|
|
| 27 |
# Define the title, description, and device description for the Gradio interface
|
| 28 |
+
device_desc = f"Cette I.A. fonctionne sur {device} 🚀." if device == torch.device('cuda') else f"🐢 Cette I.A. est très très lente sur {device} patientez plusieurs minutes 🐢."
|
| 29 |
title = f"Une intelligence artificielle pour écrire des appréciations qui tourne sur {device}"
|
| 30 |
desc = "Ce modèle vous propose une évaluation automatique."
|
| 31 |
|
| 32 |
# Define the long description for the Gradio interface
|
| 33 |
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"
|
| 34 |
|
| 35 |
+
# Load the model
|
| 36 |
+
processor = AutoProcessor.from_pretrained(
|
| 37 |
+
base_model_path,
|
| 38 |
+
do_image_splitting=False
|
| 39 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 42 |
+
base_model_path,
|
| 43 |
+
torch_dtype=torch.float16,
|
| 44 |
+
low_cpu_mem_usage=True,
|
| 45 |
+
).to(device)
|
| 46 |
+
model.load_adapter(hugging_face_model_id)
|
| 47 |
+
tokenizer = AutoTokenizer.from_pretrained(hugging_face_model_id)
|
| 48 |
|
| 49 |
+
# Define a function to create the conversation from the incoming parameters
|
| 50 |
+
def get_conversation(trimestre: str, moyenne_1: float,moyenne_2: float,moyenne_3: float, comportement: float, participation: float, travail: float):
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
if trimestre == "1":
|
| 53 |
+
trimestre_full = "premier trimestre"
|
| 54 |
+
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."
|
| 55 |
+
elif trimestre == "2":
|
| 56 |
+
trimestre_full = "deuxième trimestre"
|
| 57 |
+
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."
|
| 58 |
+
elif trimestre == "3":
|
| 59 |
+
trimestre_full = "troisième trimestre"
|
| 60 |
+
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."
|
| 61 |
+
messages = [
|
| 62 |
+
{
|
| 63 |
+
"role": "system",
|
| 64 |
+
"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"},
|
| 65 |
+
{
|
| 66 |
+
"role": "user",
|
| 67 |
+
"content": user_question},
|
| 68 |
+
]
|
| 69 |
+
return messages
|
| 70 |
|
| 71 |
+
# Define a function to infer a evaluation from the incoming parameters
|
| 72 |
+
def infere(trimestre: str, moyenne_1: float,moyenne_2: float,moyenne_3: float, comportement: float, participation: float, travail: float) -> str:
|
| 73 |
+
if not torch.cuda.is_available():
|
| 74 |
+
gr.Warning("""No GPU available <br>
|
| 75 |
+
Open a message in the <a href='https://huggingface.co/spaces/eltorio/Llama-3.2-3B-appreciation/discussions'>Community Discussion</a>
|
| 76 |
+
""")
|
| 77 |
+
messages = get_conversation(trimestre, moyenne_1, moyenne_2, moyenne_3, comportement, participation, travail)
|
| 78 |
+
# Tokenize the input
|
| 79 |
+
inputs = tokenizer.apply_chat_template(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
messages,
|
| 81 |
tokenize = True,
|
| 82 |
+
add_generation_prompt = True,
|
| 83 |
return_tensors = "pt",).to(device)
|
| 84 |
+
# Generate the output
|
| 85 |
+
outputs = model.generate(input_ids = inputs,
|
| 86 |
max_new_tokens = 90,
|
| 87 |
use_cache = True,
|
| 88 |
temperature = 1.5,
|
| 89 |
min_p = 0.1,
|
| 90 |
pad_token_id=tokenizer.eos_token_id,)
|
| 91 |
+
# Decodes the returned tokens
|
| 92 |
+
decoded_sequences = tokenizer.batch_decode(outputs[:, inputs.shape[1]:],skip_special_tokens=True)[0]
|
| 93 |
+
return decoded_sequences
|
| 94 |
|
| 95 |
+
# Create a Gradio interface with the infere function and specified title and descriptions
|
| 96 |
+
autoeval = gr.Interface(fn=infere, inputs=[
|
| 97 |
+
gr.Radio(
|
| 98 |
+
["1", "2", "3"], value="1", label="trimestre", info="Trimestre"
|
| 99 |
+
),
|
| 100 |
+
gr.Slider(0, 20,label="moyenne_1", value=10, info="Moyenne trimestre 1"),
|
| 101 |
+
gr.Slider(0, 20,label="moyenne_2", value=10, info="Moyenne trimestre 2"),
|
| 102 |
+
gr.Slider(0, 20,label="moyenne_3", value=10, info="Moyenne trimestre 3"),
|
| 103 |
+
gr.Slider(0, 10, value=5, label="comportement", info="Comportement (1 à 10)"),
|
| 104 |
+
gr.Slider(0, 10, value=5, label="participation", info="Participation (1 à 10)"),
|
| 105 |
+
gr.Slider(0, 10, value=5, label="travail", info="Travail (1 à 10)"),
|
| 106 |
+
|
| 107 |
+
], outputs="text", title=title,
|
| 108 |
+
description=desc, article=long_desc)
|
| 109 |
|
| 110 |
+
# Launch the Gradio interface and share it
|
| 111 |
+
autoeval.launch(server_name="0.0.0.0",share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|