Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,69 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
|
|
4 |
|
5 |
-
#
|
6 |
model_name = "Dorian2B/Vera-Instruct"
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
-
model = AutoModelForCausalLM.from_pretrained(
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
def
|
13 |
-
|
14 |
prompt = ""
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
prompt
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
demo = gr.ChatInterface(
|
40 |
-
fn=
|
41 |
-
title="Chat avec
|
42 |
-
description="Discutez avec le modèle Vera-Instruct de
|
43 |
-
examples=["Bonjour
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
)
|
48 |
|
49 |
-
# Pour Hugging Face Spaces, utilisez launch()
|
50 |
if __name__ == "__main__":
|
51 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
+
from threading import Lock
|
5 |
|
6 |
+
# Chargement du modèle
|
7 |
model_name = "Dorian2B/Vera-Instruct"
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
10 |
+
model_name,
|
11 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
12 |
+
device_map="auto"
|
13 |
+
)
|
14 |
+
model.eval()
|
15 |
+
|
16 |
+
# Verrou pour éviter les conflits de threads
|
17 |
+
generate_lock = Lock()
|
18 |
|
19 |
+
def format_prompt(history, new_message):
|
20 |
+
"""Formate l'historique et le nouveau message pour le modèle."""
|
21 |
prompt = ""
|
22 |
+
for user_msg, bot_msg in history:
|
23 |
+
prompt += f"<|user|>{user_msg}</s>\n<|assistant|>{bot_msg}</s>\n"
|
24 |
+
prompt += f"<|user|>{new_message}</s>\n<|assistant|>"
|
25 |
+
return prompt
|
26 |
+
|
27 |
+
def generate_stream(history, new_message):
|
28 |
+
"""Génère une réponse en streaming avec contexte."""
|
29 |
+
prompt = format_prompt(history, new_message)
|
30 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
31 |
+
|
32 |
+
# Génération en streaming
|
33 |
+
with generate_lock:
|
34 |
+
with torch.no_grad():
|
35 |
+
for chunk in model.generate(
|
36 |
+
**inputs,
|
37 |
+
max_new_tokens=1024,
|
38 |
+
do_sample=True,
|
39 |
+
temperature=0.7,
|
40 |
+
top_p=0.9,
|
41 |
+
repetition_penalty=1.1,
|
42 |
+
eos_token_id=tokenizer.eos_token_id,
|
43 |
+
streamer=None, # (Remplacez par un vrai streamer si disponible)
|
44 |
+
):
|
45 |
+
decoded = tokenizer.decode(chunk[0], skip_special_tokens=True)
|
46 |
+
if decoded.startswith(prompt): # Supprime le prompt
|
47 |
+
decoded = decoded[len(prompt):]
|
48 |
+
yield decoded.strip()
|
49 |
+
|
50 |
+
def chat_interface(message, history):
|
51 |
+
"""Fonction pour Gradio ChatInterface."""
|
52 |
+
full_response = ""
|
53 |
+
for chunk in generate_stream(history, message):
|
54 |
+
full_response += chunk
|
55 |
+
yield full_response
|
56 |
+
|
57 |
+
# Interface Gradio
|
58 |
demo = gr.ChatInterface(
|
59 |
+
fn=chat_interface,
|
60 |
+
title="💬 Vera-Instruct Chat (avec Contexte & Streaming)",
|
61 |
+
description="Discutez avec le modèle **Dorian2B/Vera-Instruct**.<br>Le modèle conserve le contexte de la conversation.",
|
62 |
+
examples=["Bonjour ! Comment vas-tu ?", "Explique-moi l'IA générative."],
|
63 |
+
theme="soft",
|
64 |
+
retry_btn=None,
|
65 |
+
undo_btn=None,
|
66 |
)
|
67 |
|
|
|
68 |
if __name__ == "__main__":
|
69 |
+
demo.queue().launch(debug=True)
|