lea97338 commited on
Commit
ae20b70
·
verified ·
1 Parent(s): 27e0523

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -11
app.py CHANGED
@@ -3,7 +3,7 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
  import torch
4
 
5
  # =========================
6
- # MODEL (Microsoft CPU léger)
7
  # =========================
8
  MODEL_NAME = "microsoft/phi-2"
9
 
@@ -11,7 +11,7 @@ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
11
 
12
  model = AutoModelForCausalLM.from_pretrained(
13
  MODEL_NAME,
14
- torch_dtype=torch.float32,
15
  device_map="cpu"
16
  )
17
 
@@ -27,7 +27,6 @@ generator = pipeline(
27
  # =========================
28
  def chat(user_message, history):
29
 
30
- # ✅ prompt système
31
  system_prompt = """
32
  You are an AI assistant.
33
 
@@ -39,15 +38,21 @@ you MUST answer ONLY with:
39
  Otherwise respond normally.
40
  """
41
 
42
- # ✅ contexte court (rapide)
43
  conversation = system_prompt + "\n"
44
 
45
- for user, bot in history[-3:]:
46
- conversation += f"User: {user}\nAssistant: {bot}\n"
 
 
 
 
 
 
 
 
47
 
48
  conversation += f"User: {user_message}\nAssistant:"
49
 
50
- # ✅ génération
51
  output = generator(
52
  conversation,
53
  max_new_tokens=100,
@@ -57,7 +62,9 @@ Otherwise respond normally.
57
 
58
  response = output.split("Assistant:")[-1].strip()
59
 
60
- history.append((user_message, response))
 
 
61
 
62
  return history, ""
63
 
@@ -68,16 +75,23 @@ with gr.Blocks() as demo:
68
 
69
  gr.Markdown("# 🤖 Microsoft Phi-2 Chatbot")
70
 
71
- chatbot = gr.Chatbot(height=500)
 
 
 
72
 
73
  msg = gr.Textbox(
74
- placeholder="Pose une question ou demande une image...",
75
  label="Message"
76
  )
77
 
78
- clear = gr.Button("🗑️ Clear")
 
 
79
 
 
80
  msg.submit(chat, [msg, chatbot], [chatbot, msg])
 
81
  clear.click(lambda: [], None, chatbot)
82
 
83
  # =========================
 
3
  import torch
4
 
5
  # =========================
6
+ # MODEL (Microsoft Phi-2 CPU)
7
  # =========================
8
  MODEL_NAME = "microsoft/phi-2"
9
 
 
11
 
12
  model = AutoModelForCausalLM.from_pretrained(
13
  MODEL_NAME,
14
+ dtype=torch.float32, # ✅ remplace torch_dtype
15
  device_map="cpu"
16
  )
17
 
 
27
  # =========================
28
  def chat(user_message, history):
29
 
 
30
  system_prompt = """
31
  You are an AI assistant.
32
 
 
38
  Otherwise respond normally.
39
  """
40
 
 
41
  conversation = system_prompt + "\n"
42
 
43
+ # ✅ nouveau format messages
44
+ for msg in history[-3:]:
45
+ role = msg["role"]
46
+ content = msg["content"]
47
+
48
+ if isinstance(content, str):
49
+ if role == "user":
50
+ conversation += f"User: {content}\n"
51
+ else:
52
+ conversation += f"Assistant: {content}\n"
53
 
54
  conversation += f"User: {user_message}\nAssistant:"
55
 
 
56
  output = generator(
57
  conversation,
58
  max_new_tokens=100,
 
62
 
63
  response = output.split("Assistant:")[-1].strip()
64
 
65
+ # ✅ format correct Gradio
66
+ history.append({"role": "user", "content": user_message})
67
+ history.append({"role": "assistant", "content": response})
68
 
69
  return history, ""
70
 
 
75
 
76
  gr.Markdown("# 🤖 Microsoft Phi-2 Chatbot")
77
 
78
+ chatbot = gr.Chatbot(
79
+ height=500,
80
+ type="messages" # ✅ IMPORTANT
81
+ )
82
 
83
  msg = gr.Textbox(
84
+ placeholder="Parle ou demande une image...",
85
  label="Message"
86
  )
87
 
88
+ with gr.Row():
89
+ send = gr.Button("⚡ Envoyer")
90
+ clear = gr.Button("🗑️ Clear")
91
 
92
+ send.click(chat, [msg, chatbot], [chatbot, msg])
93
  msg.submit(chat, [msg, chatbot], [chatbot, msg])
94
+
95
  clear.click(lambda: [], None, chatbot)
96
 
97
  # =========================