El-Alberto67 commited on
Commit
f434a00
·
verified ·
1 Parent(s): d383b03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -24
app.py CHANGED
@@ -3,17 +3,14 @@ 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,41 +18,42 @@ chatbot = pipeline(
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()
 
3
 
4
  MODEL = "prithivMLmods/Llama-SmolTalk-3.2-1B-Instruct"
5
 
 
6
  tokenizer = AutoTokenizer.from_pretrained(MODEL)
7
 
 
8
  model = AutoModelForCausalLM.from_pretrained(
9
  MODEL,
10
  device_map="auto",
11
  load_in_8bit=True
12
  )
13
 
 
14
  chatbot = pipeline(
15
  "text-generation",
16
  model=model,
 
18
  device_map="auto"
19
  )
20
 
21
+ system_prompt = """Tu es Aria, une IA gentille, claire et polie.
22
+ Réponds toujours en phrases complètes.
23
+ Ne fais pas de jeu de rôle.
24
+ Ne répète pas le message de l'utilisateur.
25
+ Réponds directement à la question."""
 
 
26
 
27
+ def chat(message, history=[]):
28
+ history = history[-3:] # garder 3 derniers échanges
29
+
30
+ context = "\n".join([f"Utilisateur: {m[0]}\nAria: {m[1]}" for m in history])
31
+ prompt = f"{system_prompt}\n{context}\nUtilisateur: {message}\nAria:"
32
 
33
  resp = chatbot(
34
  prompt,
35
+ max_new_tokens=120, # plus long
36
  do_sample=True,
37
  temperature=0.7,
38
  top_p=0.9,
39
  repetition_penalty=1.1
40
  )[0]["generated_text"]
41
 
42
+ # Garde seulement après "Aria:"
43
+ reply = resp.split("Aria:")[-1].strip()
44
+
45
+ # Supprime si "Utilisateur:" est revenu dans la génération
46
+ if "Utilisateur:" in reply:
47
+ reply = reply.split("Utilisateur:")[0].strip()
48
 
49
+ history.append([message, reply])
50
+
51
+ # Retourner au format Gradio Chatbot
52
  return history, history
53
 
54
  with gr.Blocks() as demo:
55
+ chat_ui = gr.Chatbot()
 
56
  msg = gr.Textbox(placeholder="Écris un message...")
57
+ msg.submit(chat, [msg, chat_ui], [chat_ui, chat_ui])
 
 
58
 
59
  demo.launch()