Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,19 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
3 |
-
import re
|
4 |
|
5 |
MODEL = "prithivMLmods/Llama-SmolTalk-3.2-1B-Instruct"
|
6 |
|
|
|
7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
8 |
|
|
|
9 |
model = AutoModelForCausalLM.from_pretrained(
|
10 |
MODEL,
|
11 |
device_map="auto",
|
12 |
load_in_8bit=True
|
13 |
)
|
14 |
|
|
|
15 |
chatbot = pipeline(
|
16 |
"text-generation",
|
17 |
model=model,
|
@@ -19,33 +21,41 @@ chatbot = pipeline(
|
|
19 |
device_map="auto"
|
20 |
)
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
text = re.sub(r"^.*?:", "", text, flags=re.DOTALL) # supprime jusqu'au premier ":"
|
30 |
-
return text.strip()
|
31 |
|
32 |
-
def chat(message):
|
33 |
-
|
|
|
|
|
34 |
|
35 |
resp = chatbot(
|
36 |
prompt,
|
37 |
-
max_new_tokens=
|
38 |
do_sample=True,
|
39 |
temperature=0.7,
|
40 |
-
top_p=0.9
|
|
|
41 |
)[0]["generated_text"]
|
42 |
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
|
46 |
with gr.Blocks() as demo:
|
|
|
|
|
47 |
msg = gr.Textbox(placeholder="Écris un message...")
|
48 |
-
|
49 |
-
msg.submit(
|
|
|
50 |
|
51 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
3 |
|
4 |
MODEL = "prithivMLmods/Llama-SmolTalk-3.2-1B-Instruct"
|
5 |
|
6 |
+
# Charger le tokenizer
|
7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
8 |
|
9 |
+
# Charger le modèle en 8 bits
|
10 |
model = AutoModelForCausalLM.from_pretrained(
|
11 |
MODEL,
|
12 |
device_map="auto",
|
13 |
load_in_8bit=True
|
14 |
)
|
15 |
|
16 |
+
# Pipeline
|
17 |
chatbot = pipeline(
|
18 |
"text-generation",
|
19 |
model=model,
|
|
|
21 |
device_map="auto"
|
22 |
)
|
23 |
|
24 |
+
# Prompt système
|
25 |
+
system_prompt = (
|
26 |
+
"Tu es Aria, une IA gentille, claire et polie. "
|
27 |
+
"Réponds toujours en phrases complètes. "
|
28 |
+
"Ne te lances pas dans un jeu de rôle, ne répète pas les messages précédents, "
|
29 |
+
"et donne uniquement ta réponse."
|
30 |
+
)
|
|
|
|
|
31 |
|
32 |
+
def chat(message, history):
|
33 |
+
history = history or []
|
34 |
+
context = "\n".join([f"{user}\n{bot}" for user, bot in history[-3:]])
|
35 |
+
prompt = f"{system_prompt}\n{context}\n{message}\nRéponse:"
|
36 |
|
37 |
resp = chatbot(
|
38 |
prompt,
|
39 |
+
max_new_tokens=250, # plus long pour éviter les coupures
|
40 |
do_sample=True,
|
41 |
temperature=0.7,
|
42 |
+
top_p=0.9,
|
43 |
+
repetition_penalty=1.1
|
44 |
)[0]["generated_text"]
|
45 |
|
46 |
+
# Couper dès qu'il repart sur un nouveau tour
|
47 |
+
reply = resp.split("Réponse:")[-1].strip()
|
48 |
+
reply = reply.split("Utilisateur:")[0].strip()
|
49 |
+
|
50 |
+
history.append((message, reply))
|
51 |
+
return history, history
|
52 |
|
53 |
with gr.Blocks() as demo:
|
54 |
+
chatbot_ui = gr.Chatbot()
|
55 |
+
state = gr.State([]) # sauvegarde de l'historique
|
56 |
msg = gr.Textbox(placeholder="Écris un message...")
|
57 |
+
|
58 |
+
msg.submit(chat, [msg, state], [chatbot_ui, state])
|
59 |
+
msg.submit(lambda: "", None, msg) # reset input après envoi
|
60 |
|
61 |
demo.launch()
|