Update app.py
Browse files
app.py
CHANGED
@@ -1,170 +1,496 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import time
|
4 |
-
from llama_cpp import Llama
|
5 |
import os
|
|
|
|
|
6 |
from huggingface_hub import hf_hub_download
|
|
|
7 |
|
8 |
# Configuration du modèle
|
9 |
MODEL_NAME = "Dorian2B/Vera-v1.5-Instruct-2B-GGUF"
|
10 |
MODEL_FILE = "vera-v1.5-instruct-q8_0.gguf"
|
|
|
11 |
|
|
|
|
|
|
|
|
|
|
|
12 |
def download_model():
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
return model_path
|
15 |
|
16 |
-
|
|
|
|
|
17 |
model_path = download_model()
|
18 |
|
19 |
-
# Paramètres pour le modèle
|
20 |
model = Llama(
|
21 |
model_path=model_path,
|
22 |
-
n_ctx=4096,
|
23 |
-
|
24 |
-
|
|
|
|
|
25 |
)
|
26 |
-
|
27 |
|
28 |
-
# Format du template pour Vera
|
29 |
def format_prompt(message, history):
|
30 |
-
|
|
|
31 |
|
32 |
# Ajout de l'historique
|
33 |
for user_msg, assistant_msg in history:
|
34 |
-
|
35 |
-
|
36 |
|
37 |
# Ajout du message actuel
|
38 |
-
|
39 |
-
prompt += "<|assistant|>\n"
|
40 |
|
41 |
-
return
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
#
|
49 |
-
|
50 |
|
51 |
-
prompt
|
|
|
52 |
|
53 |
response_text = ""
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
# Fonction pour réinitialiser la conversation
|
71 |
def reset_conversation():
|
72 |
-
return [], ""
|
73 |
|
74 |
-
#
|
|
|
|
|
|
|
|
|
75 |
custom_css = """
|
76 |
-
|
|
|
|
|
|
|
77 |
.gradio-container {
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
}
|
|
|
|
|
80 |
.chatbot-container {
|
81 |
-
border-radius:
|
82 |
-
box-shadow: 0
|
|
|
|
|
83 |
}
|
|
|
|
|
84 |
.chatbot .user-message {
|
85 |
-
background: linear-gradient(135deg, #
|
86 |
color: white;
|
87 |
-
border-radius:
|
|
|
|
|
|
|
|
|
88 |
}
|
|
|
|
|
89 |
.chatbot .bot-message {
|
90 |
-
background: #
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
}
|
93 |
"""
|
94 |
|
95 |
-
#
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
103 |
|
104 |
with gr.Row():
|
|
|
105 |
with gr.Column(scale=4):
|
|
|
|
|
|
|
|
|
106 |
chatbot = gr.Chatbot(
|
107 |
-
height=
|
108 |
show_copy_button=True,
|
109 |
avatar_images=("👤", "🤖"),
|
110 |
-
|
111 |
elem_id="chatbot",
|
112 |
container=True,
|
113 |
elem_classes="chatbot-container",
|
114 |
)
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
|
|
126 |
with gr.Row():
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
-
with gr.Accordion("À propos du modèle", open=False):
|
131 |
gr.Markdown("""
|
132 |
-
|
133 |
-
Le modèle est optimisé pour les conversations en français.
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
""")
|
140 |
|
141 |
# Configuration des événements
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
submit_btn.click(
|
143 |
fn=generate_response,
|
144 |
-
inputs=[message, chatbot],
|
145 |
-
outputs=[chatbot]
|
146 |
-
queue=True
|
147 |
).then(
|
148 |
fn=lambda: "",
|
149 |
outputs=[message]
|
|
|
|
|
|
|
150 |
)
|
151 |
|
|
|
152 |
message.submit(
|
153 |
fn=generate_response,
|
154 |
-
inputs=[message, chatbot],
|
155 |
-
outputs=[chatbot]
|
156 |
-
queue=True
|
157 |
).then(
|
158 |
fn=lambda: "",
|
159 |
outputs=[message]
|
|
|
|
|
|
|
160 |
)
|
161 |
|
|
|
162 |
reset_btn.click(
|
163 |
fn=reset_conversation,
|
164 |
outputs=[chatbot, message]
|
165 |
)
|
166 |
|
|
|
|
|
|
|
167 |
# Lancement de l'interface
|
168 |
if __name__ == "__main__":
|
169 |
-
demo.queue()
|
170 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import time
|
|
|
4 |
import os
|
5 |
+
from functools import lru_cache
|
6 |
+
from llama_cpp import Llama
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
+
import threading
|
9 |
|
10 |
# Configuration du modèle
|
11 |
MODEL_NAME = "Dorian2B/Vera-v1.5-Instruct-2B-GGUF"
|
12 |
MODEL_FILE = "vera-v1.5-instruct-q8_0.gguf"
|
13 |
+
MODEL_CACHE_DIR = os.path.join(os.path.expanduser("~"), ".cache", "vera-model")
|
14 |
|
15 |
+
# Créer le répertoire de cache s'il n'existe pas
|
16 |
+
os.makedirs(MODEL_CACHE_DIR, exist_ok=True)
|
17 |
+
|
18 |
+
# Télécharge le modèle une seule fois et met en cache le résultat
|
19 |
+
@lru_cache(maxsize=1)
|
20 |
def download_model():
|
21 |
+
print("Téléchargement du modèle en cours...")
|
22 |
+
model_path = hf_hub_download(
|
23 |
+
repo_id=MODEL_NAME,
|
24 |
+
filename=MODEL_FILE,
|
25 |
+
cache_dir=MODEL_CACHE_DIR
|
26 |
+
)
|
27 |
+
print(f"Modèle téléchargé à {model_path}")
|
28 |
return model_path
|
29 |
|
30 |
+
# Charge le modèle dans un thread séparé pour ne pas bloquer l'interface
|
31 |
+
def load_model_async():
|
32 |
+
global model
|
33 |
model_path = download_model()
|
34 |
|
35 |
+
# Paramètres optimisés pour le modèle
|
36 |
model = Llama(
|
37 |
model_path=model_path,
|
38 |
+
n_ctx=4096,
|
39 |
+
n_batch=512, # Augmente la taille du batch pour de meilleures performances
|
40 |
+
n_gpu_layers=-1,
|
41 |
+
verbose=False,
|
42 |
+
seed=42 # Pour des résultats reproductibles
|
43 |
)
|
44 |
+
print("Modèle chargé avec succès!")
|
45 |
|
46 |
+
# Format du template pour Vera avec optimisation
|
47 |
def format_prompt(message, history):
|
48 |
+
# Construction optimisée du prompt en utilisant join au lieu de concaténation
|
49 |
+
prompt_parts = ["<|system|>\nTu es Vera, une assistante IA utile, honnête et inoffensive.\n</s>\n"]
|
50 |
|
51 |
# Ajout de l'historique
|
52 |
for user_msg, assistant_msg in history:
|
53 |
+
prompt_parts.append(f"<|user|>\n{user_msg}\n</s>\n")
|
54 |
+
prompt_parts.append(f"<|assistant|>\n{assistant_msg}\n</s>\n")
|
55 |
|
56 |
# Ajout du message actuel
|
57 |
+
prompt_parts.append(f"<|user|>\n{message}\n</s>\n<|assistant|>\n")
|
|
|
58 |
|
59 |
+
return "".join(prompt_parts)
|
60 |
|
61 |
+
# Variables globales pour la gestion du modèle
|
62 |
+
model = None
|
63 |
+
is_model_loading = False
|
64 |
+
model_lock = threading.Lock()
|
65 |
+
|
66 |
+
# Fonction d'inférence avec streaming optimisé
|
67 |
+
def generate_response(message, history, temperature=0.7, top_p=0.95, max_tokens=2048):
|
68 |
+
global model, is_model_loading
|
69 |
+
|
70 |
+
# Vérifier si le modèle est déjà chargé
|
71 |
+
with model_lock:
|
72 |
+
if model is None:
|
73 |
+
if not is_model_loading:
|
74 |
+
is_model_loading = True
|
75 |
+
# Lancer le chargement asynchrone
|
76 |
+
yield [{"role": "user", "content": message},
|
77 |
+
{"role": "assistant", "content": "Chargement du modèle en cours... Veuillez patienter."}]
|
78 |
+
load_model_async()
|
79 |
+
is_model_loading = False
|
80 |
+
else:
|
81 |
+
yield [{"role": "user", "content": message},
|
82 |
+
{"role": "assistant", "content": "Le modèle est en cours de chargement. Veuillez patienter."}]
|
83 |
+
return
|
84 |
+
|
85 |
+
# Si le message est vide, ne rien faire
|
86 |
+
if not message.strip():
|
87 |
+
return
|
88 |
+
|
89 |
+
# Conversion de l'historique si nécessaire (format messages -> format tuples pour le traitement)
|
90 |
+
history_tuples = []
|
91 |
+
if history:
|
92 |
+
for msg in history:
|
93 |
+
if isinstance(msg, dict):
|
94 |
+
# Conversion du format messages (dict) vers tuples
|
95 |
+
if msg["role"] == "user":
|
96 |
+
user_msg = msg["content"]
|
97 |
+
if len(history_tuples) == 0 or len(history_tuples[-1]) < 2:
|
98 |
+
history_tuples.append([user_msg, ""])
|
99 |
+
else:
|
100 |
+
history_tuples.append([user_msg, ""])
|
101 |
+
elif msg["role"] == "assistant":
|
102 |
+
if history_tuples and len(history_tuples[-1]) == 2:
|
103 |
+
history_tuples[-1][1] = msg["content"]
|
104 |
+
elif isinstance(msg, tuple) or isinstance(msg, list):
|
105 |
+
# Déjà au format tuple
|
106 |
+
history_tuples.append(msg)
|
107 |
|
108 |
+
# Préparation de l'historique pour la réponse (format tuples)
|
109 |
+
new_history_tuples = history_tuples + [(message, "")]
|
110 |
|
111 |
+
# Formatage du prompt avec l'historique en tuples
|
112 |
+
prompt = format_prompt(message, history_tuples)
|
113 |
|
114 |
response_text = ""
|
115 |
|
116 |
+
try:
|
117 |
+
# Utilise le stream pour générer la réponse progressivement
|
118 |
+
for token in model.create_completion(
|
119 |
+
prompt,
|
120 |
+
max_tokens=max_tokens,
|
121 |
+
temperature=temperature,
|
122 |
+
top_p=top_p,
|
123 |
+
top_k=40, # Ajout du paramètre top_k pour diversité
|
124 |
+
repeat_penalty=1.1, # Pénalise la répétition
|
125 |
+
stop=["</s>", "<|user|>", "<|system|>"],
|
126 |
+
stream=True,
|
127 |
+
):
|
128 |
+
chunk = token["choices"][0]["text"]
|
129 |
+
response_text += chunk
|
130 |
+
|
131 |
+
# Conversion de l'historique en format messages pour l'affichage
|
132 |
+
new_history_messages = []
|
133 |
+
for i, (usr_msg, ast_msg) in enumerate(new_history_tuples):
|
134 |
+
new_history_messages.append({"role": "user", "content": usr_msg})
|
135 |
+
# Pour le dernier message de l'assistant, utiliser le texte généré
|
136 |
+
if i == len(new_history_tuples) - 1:
|
137 |
+
new_history_messages.append({"role": "assistant", "content": response_text})
|
138 |
+
elif ast_msg: # Éviter d'ajouter les messages vides
|
139 |
+
new_history_messages.append({"role": "assistant", "content": ast_msg})
|
140 |
+
|
141 |
+
yield new_history_messages
|
142 |
+
time.sleep(0.01) # Légère pause pour un affichage fluide
|
143 |
+
except Exception as e:
|
144 |
+
# Gestion des erreurs pendant la génération
|
145 |
+
error_message = f"Erreur pendant la génération: {str(e)}"
|
146 |
+
yield [{"role": "user", "content": message},
|
147 |
+
{"role": "assistant", "content": error_message}]
|
148 |
|
149 |
# Fonction pour réinitialiser la conversation
|
150 |
def reset_conversation():
|
151 |
+
return [], "" # Retourne un historique vide et un message vide
|
152 |
|
153 |
+
# Fonction pour ajuster les paramètres du modèle
|
154 |
+
def update_params(temp, top_p, max_len):
|
155 |
+
return gr.update(value=f"Température: {temp}, Top-p: {top_p}, Longueur max: {max_len}")
|
156 |
+
|
157 |
+
# CSS personnalisé pour une interface moderne et professionnelle
|
158 |
custom_css = """
|
159 |
+
/* Masquer le pied de page Gradio */
|
160 |
+
footer {display: none !important}
|
161 |
+
|
162 |
+
/* Style global du conteneur */
|
163 |
.gradio-container {
|
164 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
165 |
+
background: #050f24;
|
166 |
+
min-height: 100vh;
|
167 |
+
}
|
168 |
+
|
169 |
+
/* En-tête */
|
170 |
+
h1, h2, h3 {
|
171 |
+
font-weight: 700;
|
172 |
+
color: #f3f4f6;
|
173 |
}
|
174 |
+
|
175 |
+
/* Style de la zone de chat */
|
176 |
.chatbot-container {
|
177 |
+
border-radius: 16px;
|
178 |
+
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
|
179 |
+
background-color: white;
|
180 |
+
overflow: hidden;
|
181 |
}
|
182 |
+
|
183 |
+
/* Messages utilisateur */
|
184 |
.chatbot .user-message {
|
185 |
+
background: linear-gradient(135deg, #6366f1, #8b5cf6);
|
186 |
color: white;
|
187 |
+
border-radius: 16px 16px 2px 16px;
|
188 |
+
padding: 12px 16px;
|
189 |
+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
|
190 |
+
max-width: 80%;
|
191 |
+
margin: 8px 0;
|
192 |
}
|
193 |
+
|
194 |
+
/* Messages de l'assistant */
|
195 |
.chatbot .bot-message {
|
196 |
+
background: #f3f4f6;
|
197 |
+
color: #111827;
|
198 |
+
border-radius: 16px 16px 16px 2px;
|
199 |
+
padding: 12px 16px;
|
200 |
+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
|
201 |
+
max-width: 80%;
|
202 |
+
margin: 8px 0;
|
203 |
+
}
|
204 |
+
|
205 |
+
/* Zone de texte */
|
206 |
+
.input-area {
|
207 |
+
border-radius: 12px;
|
208 |
+
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
|
209 |
+
border: 1px solid #e5e7eb;
|
210 |
+
transition: all 0.3s ease;
|
211 |
+
}
|
212 |
+
.input-area:focus {
|
213 |
+
border-color: #6366f1;
|
214 |
+
box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
|
215 |
+
}
|
216 |
+
|
217 |
+
/* Boutons */
|
218 |
+
.primary-btn {
|
219 |
+
background: linear-gradient(135deg, #6366f1, #8b5cf6);
|
220 |
+
border: none;
|
221 |
+
border-radius: 8px;
|
222 |
+
color: white;
|
223 |
+
font-weight: 600;
|
224 |
+
padding: 10px 16px;
|
225 |
+
cursor: pointer;
|
226 |
+
transition: all 0.3s ease;
|
227 |
+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
|
228 |
+
}
|
229 |
+
.primary-btn:hover {
|
230 |
+
transform: translateY(-2px);
|
231 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
|
232 |
+
}
|
233 |
+
.secondary-btn {
|
234 |
+
background-color: #f3f4f6;
|
235 |
+
border: 1px solid #e5e7eb;
|
236 |
+
border-radius: 8px;
|
237 |
+
color: #4b5563;
|
238 |
+
font-weight: 500;
|
239 |
+
padding: 10px 16px;
|
240 |
+
cursor: pointer;
|
241 |
+
transition: all 0.3s ease;
|
242 |
+
}
|
243 |
+
.secondary-btn:hover {
|
244 |
+
background-color: #e5e7eb;
|
245 |
+
}
|
246 |
+
|
247 |
+
/* Accordéon personnalisé */
|
248 |
+
.accordion {
|
249 |
+
border-radius: 8px;
|
250 |
+
border: 1px solid #e5e7eb;
|
251 |
+
margin-top: 16px;
|
252 |
+
}
|
253 |
+
.accordion-header {
|
254 |
+
padding: 12px;
|
255 |
+
font-weight: 600;
|
256 |
+
color: #4b5563;
|
257 |
+
}
|
258 |
+
|
259 |
+
/* Animation de chargement */
|
260 |
+
@keyframes pulse {
|
261 |
+
0% {opacity: 0.6;}
|
262 |
+
50% {opacity: 1;}
|
263 |
+
100% {opacity: 0.6;}
|
264 |
+
}
|
265 |
+
.loading-indicator {
|
266 |
+
animation: pulse 1.5s infinite;
|
267 |
+
display: inline-block;
|
268 |
+
margin-right: 8px;
|
269 |
+
}
|
270 |
+
|
271 |
+
/* Personnalisation des paramètres */
|
272 |
+
.parameter-container {
|
273 |
+
background-color: #111827;
|
274 |
+
border-radius: 12px;
|
275 |
+
padding: 16px;
|
276 |
+
margin-top: 12px;
|
277 |
+
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
|
278 |
+
}
|
279 |
+
.parameter-slider {
|
280 |
+
margin: 8px 0;
|
281 |
+
}
|
282 |
+
|
283 |
+
/* Style des badges */
|
284 |
+
.model-badge {
|
285 |
+
display: inline-block;
|
286 |
+
background-color: #818cf8;
|
287 |
+
color: white;
|
288 |
+
padding: 4px 8px;
|
289 |
+
border-radius: 6px;
|
290 |
+
font-size: 0.8rem;
|
291 |
+
font-weight: 500;
|
292 |
+
margin-right: 8px;
|
293 |
+
}
|
294 |
+
|
295 |
+
/* État du modèle */
|
296 |
+
.model-status {
|
297 |
+
font-size: 0.9rem;
|
298 |
+
color: #4b5563;
|
299 |
+
margin-bottom: 12px;
|
300 |
+
}
|
301 |
+
.status-loaded {
|
302 |
+
color: #10b981;
|
303 |
+
}
|
304 |
+
.status-loading {
|
305 |
+
color: #f59e0b;
|
306 |
+
}
|
307 |
+
|
308 |
+
/* Ajustements pour responsive */
|
309 |
+
@media (max-width: 768px) {
|
310 |
+
.gradio-container {
|
311 |
+
padding: 12px;
|
312 |
+
}
|
313 |
+
.chatbot .user-message, .chatbot .bot-message {
|
314 |
+
max-width: 90%;
|
315 |
+
}
|
316 |
}
|
317 |
"""
|
318 |
|
319 |
+
# Créer un thème personnalisé
|
320 |
+
theme = gr.themes.Base(
|
321 |
+
primary_hue="indigo",
|
322 |
+
secondary_hue="purple",
|
323 |
+
neutral_hue="slate",
|
324 |
+
font=["Inter", "ui-sans-serif", "system-ui", "sans-serif"],
|
325 |
+
font_mono=["Fira Code", "ui-monospace", "monospace"],
|
326 |
+
).set(
|
327 |
+
button_primary_background_fill="*primary_500",
|
328 |
+
button_primary_background_fill_hover="*primary_600",
|
329 |
+
button_primary_text_color="white",
|
330 |
+
button_secondary_background_fill="*neutral_100",
|
331 |
+
button_secondary_background_fill_hover="*neutral_200",
|
332 |
+
button_secondary_text_color="*neutral_800",
|
333 |
+
block_radius="12px",
|
334 |
+
block_shadow="0 2px 8px rgba(0, 0, 0, 0.1)",
|
335 |
+
input_radius="8px",
|
336 |
+
input_shadow="0 2px 4px rgba(0, 0, 0, 0.05)",
|
337 |
+
input_border_width="1px"
|
338 |
+
)
|
339 |
+
|
340 |
+
# Interface Gradio améliorée
|
341 |
+
with gr.Blocks(css=custom_css, theme=theme) as demo:
|
342 |
+
# Variables d'état pour l'interface
|
343 |
+
model_status = gr.State("non chargé")
|
344 |
|
345 |
+
with gr.Row():
|
346 |
+
gr.Markdown("""
|
347 |
+
# 🌟 Vera - Assistant IA Français
|
348 |
+
|
349 |
+
Un assistant conversationnel basé sur le modèle **Vera-v1.5-Instruct** optimisé pour le français.
|
350 |
+
""")
|
351 |
|
352 |
with gr.Row():
|
353 |
+
# Colonne principale pour le chat
|
354 |
with gr.Column(scale=4):
|
355 |
+
# Indicateur de statut du modèle
|
356 |
+
status_indicator = gr.Markdown("💤 **Modèle**: En attente de chargement", elem_id="model-status")
|
357 |
+
|
358 |
+
# Zone de chat avec design amélioré
|
359 |
chatbot = gr.Chatbot(
|
360 |
+
height=600,
|
361 |
show_copy_button=True,
|
362 |
avatar_images=("👤", "🤖"),
|
363 |
+
type="messages", # Utilisation du format messages au lieu de tuples (déprécié)
|
364 |
elem_id="chatbot",
|
365 |
container=True,
|
366 |
elem_classes="chatbot-container",
|
367 |
)
|
368 |
+
|
369 |
+
# Zone de saisie et boutons
|
370 |
+
with gr.Row():
|
371 |
+
message = gr.Textbox(
|
372 |
+
placeholder="Posez votre question à Vera...",
|
373 |
+
lines=2,
|
374 |
+
max_lines=10,
|
375 |
+
container=True,
|
376 |
+
elem_classes="input-area",
|
377 |
+
scale=4,
|
378 |
+
autofocus=True,
|
379 |
+
)
|
380 |
+
|
381 |
with gr.Row():
|
382 |
+
with gr.Column(scale=3):
|
383 |
+
submit_btn = gr.Button("Envoyer", elem_classes="primary-btn")
|
384 |
+
with gr.Column(scale=1):
|
385 |
+
reset_btn = gr.Button("Nouvelle conversation", elem_classes="secondary-btn")
|
386 |
+
|
387 |
+
# Colonne pour les paramètres et informations
|
388 |
+
with gr.Column(scale=1):
|
389 |
+
with gr.Group(elem_classes="parameter-container"):
|
390 |
+
gr.Markdown("### ⚙️ Paramètres")
|
391 |
+
|
392 |
+
temperature = gr.Slider(
|
393 |
+
minimum=0.1,
|
394 |
+
maximum=1.0,
|
395 |
+
value=0.7,
|
396 |
+
step=0.1,
|
397 |
+
label="Température",
|
398 |
+
info="Contrôle la créativité (plus élevé = plus créatif)",
|
399 |
+
elem_classes="parameter-slider"
|
400 |
+
)
|
401 |
+
|
402 |
+
top_p = gr.Slider(
|
403 |
+
minimum=0.5,
|
404 |
+
maximum=1.0,
|
405 |
+
value=0.95,
|
406 |
+
step=0.05,
|
407 |
+
label="Top-p",
|
408 |
+
info="Contrôle la diversité des réponses",
|
409 |
+
elem_classes="parameter-slider"
|
410 |
+
)
|
411 |
+
|
412 |
+
max_tokens = gr.Slider(
|
413 |
+
minimum=256,
|
414 |
+
maximum=4096,
|
415 |
+
value=2048,
|
416 |
+
step=256,
|
417 |
+
label="Longueur maximale",
|
418 |
+
info="Nombre maximum de tokens générés",
|
419 |
+
elem_classes="parameter-slider"
|
420 |
+
)
|
421 |
+
|
422 |
+
apply_params = gr.Button("Appliquer", elem_classes="secondary-btn")
|
423 |
+
params_info = gr.Markdown("Température: 0.7, Top-p: 0.95, Longueur max: 2048")
|
424 |
|
425 |
+
with gr.Accordion("ℹ️ À propos du modèle", open=False, elem_classes="accordion"):
|
426 |
gr.Markdown("""
|
427 |
+
### Vera v1.5 Instruct
|
|
|
428 |
|
429 |
+
<div class="model-badge">GGUF</div> <div class="model-badge">Français</div> <div class="model-badge">2B</div>
|
430 |
+
|
431 |
+
Ce modèle est basé sur **Vera-v1.5-Instruct-GGUF** développé par [Dorian2B](https://huggingface.co/Dorian2B/Vera-v1.5-Instruct-GGUF).
|
432 |
+
|
433 |
+
**Caractéristiques:**
|
434 |
+
- Modèle optimisé pour les conversations en français
|
435 |
+
- Basé sur l'architecture Gemma2
|
436 |
+
- Support de contexte jusqu'à 8192 tokens
|
437 |
+
- Quantifié en 8-bit pour de meilleures performances
|
438 |
+
|
439 |
+
**Conseils d'utilisation:**
|
440 |
+
- Posez des questions claires et précises
|
441 |
+
- Pour de meilleurs résultats, ajustez la température selon vos besoins
|
442 |
+
- Utilisez le bouton "Nouvelle conversation" pour réinitialiser le contexte
|
443 |
""")
|
444 |
|
445 |
# Configuration des événements
|
446 |
+
# Mettre à jour les paramètres
|
447 |
+
apply_params.click(
|
448 |
+
fn=update_params,
|
449 |
+
inputs=[temperature, top_p, max_tokens],
|
450 |
+
outputs=[params_info]
|
451 |
+
)
|
452 |
+
|
453 |
+
# Gérer la soumission de message (bouton Envoyer)
|
454 |
submit_btn.click(
|
455 |
fn=generate_response,
|
456 |
+
inputs=[message, chatbot, temperature, top_p, max_tokens],
|
457 |
+
outputs=[chatbot]
|
|
|
458 |
).then(
|
459 |
fn=lambda: "",
|
460 |
outputs=[message]
|
461 |
+
).then(
|
462 |
+
fn=lambda: gr.update(value="🟢 **Modèle**: Chargé et prêt"),
|
463 |
+
outputs=[status_indicator]
|
464 |
)
|
465 |
|
466 |
+
# Gérer la soumission par la touche Entrée
|
467 |
message.submit(
|
468 |
fn=generate_response,
|
469 |
+
inputs=[message, chatbot, temperature, top_p, max_tokens],
|
470 |
+
outputs=[chatbot]
|
|
|
471 |
).then(
|
472 |
fn=lambda: "",
|
473 |
outputs=[message]
|
474 |
+
).then(
|
475 |
+
fn=lambda: gr.update(value="🟢 **Modèle**: Chargé et prêt"),
|
476 |
+
outputs=[status_indicator]
|
477 |
)
|
478 |
|
479 |
+
# Réinitialiser la conversation
|
480 |
reset_btn.click(
|
481 |
fn=reset_conversation,
|
482 |
outputs=[chatbot, message]
|
483 |
)
|
484 |
|
485 |
+
# Lancer le modèle en parallèle au démarrage
|
486 |
+
threading.Thread(target=load_model_async, daemon=True).start()
|
487 |
+
|
488 |
# Lancement de l'interface
|
489 |
if __name__ == "__main__":
|
490 |
+
demo.queue() # Active la file d'attente pour les requêtes
|
491 |
+
demo.launch(
|
492 |
+
share=True,
|
493 |
+
show_error=True,
|
494 |
+
debug=False,
|
495 |
+
max_threads=4, # Nombre maximum de threads pour traiter les requêtes
|
496 |
+
)
|