Spaces:
Sleeping
Sleeping
æLtorio
commited on
working demo
Browse files
app.py
CHANGED
@@ -1,64 +1,81 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
|
|
3 |
|
4 |
-
""
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
yield response
|
41 |
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
),
|
59 |
-
|
60 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, AutoTokenizer
|
3 |
+
from peft import AutoPeftModelForCausalLM
|
4 |
+
import torch
|
5 |
|
6 |
+
hugging_face_model_id = "eltorio/Llama-3.2-3B-appreciation"
|
7 |
+
base_model_path = "meta-llama/Llama-3.2-3B-Instruct"
|
8 |
+
# Determine the device (GPU or CPU) to run the model on
|
9 |
+
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
10 |
+
print(f"Using device: {device}") # Log the device being used
|
11 |
+
# Initialize the processor from the base model path
|
12 |
+
processor = AutoProcessor.from_pretrained(base_model_path, trust_remote_code=True)
|
13 |
+
# Initialize the model from the base model path and set the torch dtype to bfloat16
|
14 |
+
peft_model = AutoPeftModelForCausalLM.from_pretrained(hugging_face_model_id)
|
15 |
+
merged_model = peft_model.merge_and_unload()
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(hugging_face_model_id)
|
17 |
|
18 |
|
19 |
+
#tokenizer = get_chat_template(
|
20 |
+
# tokenizer,
|
21 |
+
# chat_template = "llama-3.1",
|
22 |
+
#)
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
# Define a function to infer a evaluation from the incoming parameters
|
25 |
+
def infere(trimestre: str, moyenne_1: float,moyenne_2: float,moyenne_3: float, comportement: float, participation: float, travail: float) -> str:
|
|
|
|
|
|
|
26 |
|
27 |
+
if trimestre == "1":
|
28 |
+
trimestre_full = "premier trimestre"
|
29 |
+
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."
|
30 |
+
elif trimestre == "2":
|
31 |
+
trimestre_full = "deuxième trimestre"
|
32 |
+
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."
|
33 |
+
elif trimestre == "3":
|
34 |
+
trimestre_full = "troisième trimestre"
|
35 |
+
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."
|
36 |
|
37 |
+
# Define a chat template for the model to respond to
|
38 |
+
messages = [
|
39 |
+
{
|
40 |
+
"role": "system",
|
41 |
+
"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."},
|
42 |
+
{
|
43 |
+
"role": "user",
|
44 |
+
"content": user_question},
|
45 |
+
]
|
46 |
+
inputs = tokenizer.apply_chat_template(
|
47 |
+
messages,
|
48 |
+
tokenize = True,
|
49 |
+
add_generation_prompt = True, # Must add for generation
|
50 |
+
return_tensors = "pt",).to(device)
|
51 |
+
outputs = merged_model.generate(input_ids = inputs, max_new_tokens = 90, use_cache = True,
|
52 |
+
temperature = 1.5, min_p = 0.1)
|
53 |
+
decoded_sequences = tokenizer.batch_decode(outputs[:, inputs.shape[1]:],skip_special_tokens=True)[0]
|
54 |
+
return decoded_sequences
|
55 |
|
56 |
+
# Define the title, description, and device description for the Gradio interface
|
57 |
+
title = f"Une intelligence artificielle pour écrire des appréciationst <b>tourne sur {device}</b>"
|
58 |
+
desc = "Ce modèle vous propose une évaluation automatique."
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
device_desc = f"Ce fonctionne fonctionne sur {device} 🚀." if device == torch.device('cuda') else f"🐢 Cette I.A. fonctionne sur {device} 🐢."
|
|
|
61 |
|
62 |
+
# Define the long description for the Gradio interface
|
63 |
+
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"
|
64 |
|
65 |
+
# Create a Gradio interface with the infere function and specified title and descriptions
|
66 |
+
autoeval = gr.Interface(fn=infere, inputs=[
|
67 |
+
gr.Radio(
|
68 |
+
["1", "2", "3"], value="1", label="trimestre", info="Trimestre"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
),
|
70 |
+
gr.Slider(0, 20,label="moyenne_1", value=10, info="Moyenne trimestre 1"),
|
71 |
+
gr.Slider(0, 20,label="moyenne_2", value=10, info="Moyenne trimestre 2"),
|
72 |
+
gr.Slider(0, 20,label="moyenne_3", value=10, info="Moyenne trimestre 3"),
|
73 |
+
gr.Slider(0, 10, value=5, label="comportement", info="Comportement (1 à 10)"),
|
74 |
+
gr.Slider(0, 10, value=5, label="participation", info="Participation (1 à 10)"),
|
75 |
+
gr.Slider(0, 10, value=5, label="travail", info="Travail (1 à 10)"),
|
76 |
+
|
77 |
+
], outputs="text", title=title,
|
78 |
+
description=desc, article=long_desc)
|
79 |
|
80 |
+
# Launch the Gradio interface and share it
|
81 |
+
autoeval.launch(server_name="0.0.0.0",share=False)
|
|