padufour commited on
Commit
f8c1c74
·
verified ·
1 Parent(s): 2acdaa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -50
app.py CHANGED
@@ -1,15 +1,13 @@
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import torch
4
  import re
5
 
6
  # ==================== CHARGEMENT DU MODÈLE ====================
7
  print("🔄 Chargement du modèle...")
8
 
 
9
  model = AutoModelForCausalLM.from_pretrained(
10
  "padufour/mistral-accor-emails",
11
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
12
- device_map="auto",
13
  low_cpu_mem_usage=True
14
  )
15
 
@@ -21,7 +19,6 @@ print("✅ Modèle chargé et prêt !")
21
  def generate_email(email_type, client_name, company, objective,
22
  segment, priority, history, style, language):
23
 
24
- # Construire le prompt
25
  prompt = f"""### Instruction : Rédige un email du type "{email_type}" pour un client professionnel. L'email doit être complet, sans placeholder ni crochet [], et ne doit PAS mentionner le segment de clientèle dans le contenu.
26
 
27
  ### Input :
@@ -38,25 +35,22 @@ Important: Ne pas inclure de placeholders [], ne pas mentionner le segment, ne p
38
 
39
  ### Réponse :"""
40
 
41
- # Générer
42
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
43
 
44
- with torch.no_grad():
45
- outputs = model.generate(
46
- **inputs,
47
- max_new_tokens=800,
48
- temperature=0.7,
49
- top_p=0.9,
50
- top_k=50,
51
- repetition_penalty=1.1,
52
- do_sample=True,
53
- pad_token_id=tokenizer.pad_token_id,
54
- eos_token_id=tokenizer.eos_token_id
55
- )
56
 
57
  email = tokenizer.decode(outputs[0], skip_special_tokens=True)
58
 
59
- # Extraire la réponse
60
  if "### Réponse :" in email:
61
  email = email.split("### Réponse :")[1].strip()
62
 
@@ -66,11 +60,9 @@ Important: Ne pas inclure de placeholders [], ne pas mentionner le segment, ne p
66
  email = re.sub(r'at\s+[\w\.-]+@[\w\.-]+\.\w+', '', email)
67
  email = re.sub(r'or\s+\+?\d[\d\s\(\)\-\.]+', '', email)
68
 
69
- # Ajouter signature
70
  if "ACCOR Team" not in email and "Accor Team" not in email:
71
  email += "\n\nBest regards,\nThe ACCOR Team"
72
 
73
- # Nettoyer espaces
74
  email = re.sub(r'\n\s*\n\s*\n', '\n\n', email)
75
  email = re.sub(r' +', ' ', email)
76
 
@@ -95,34 +87,29 @@ with gr.Blocks(theme=gr.themes.Soft(), title="ACCOR Email Generator") as demo:
95
  email_type = gr.Dropdown(
96
  choices=["Newsletter", "New offers", "theme", "solution", "Hotel opening"],
97
  value="Newsletter",
98
- label="Email Type *",
99
- info="Type of marketing campaign"
100
  )
101
 
102
  segment = gr.Dropdown(
103
  choices=["MICE", "AHBO", "leisure group"],
104
  value="MICE",
105
- label="Segment",
106
- info="Customer segment (used internally, not displayed)"
107
  )
108
 
109
  gr.Markdown("### 👤 Client Information")
110
 
111
  client_name = gr.Textbox(
112
  label="Client Name *",
113
- placeholder="Marie Dupont",
114
  value="Marie Dupont"
115
  )
116
 
117
  company = gr.Textbox(
118
  label="Company *",
119
- placeholder="Event Solutions SA",
120
  value="Event Solutions SA"
121
  )
122
 
123
  objective = gr.Textbox(
124
  label="Objective *",
125
- placeholder="Promote winter events",
126
  value="Promote winter events",
127
  lines=2
128
  )
@@ -137,13 +124,11 @@ with gr.Blocks(theme=gr.themes.Soft(), title="ACCOR Email Generator") as demo:
137
 
138
  history = gr.Textbox(
139
  label="Client History",
140
- placeholder="New customer",
141
  value="New customer"
142
  )
143
 
144
  style = gr.Textbox(
145
  label="Style",
146
- placeholder="Professional and engaging",
147
  value="Professional and engaging"
148
  )
149
 
@@ -164,28 +149,11 @@ with gr.Blocks(theme=gr.themes.Soft(), title="ACCOR Email Generator") as demo:
164
  lines=25,
165
  show_copy_button=True
166
  )
167
-
168
- gr.Markdown(
169
- """
170
- ---
171
- **Model:** [padufour/mistral-accor-emails](https://huggingface.co/padufour/mistral-accor-emails)
172
-
173
- **Tips for best results:**
174
- - Provide detailed objectives
175
- - Specify client history for personalization
176
- - Use descriptive style preferences
177
- """
178
- )
179
-
180
- # Exemples
181
- gr.Markdown("### 💡 Quick Examples")
182
 
183
  gr.Examples(
184
  examples=[
185
  ["Newsletter", "Marie Dupont", "Event Solutions SA", "Promote winter events", "MICE", "High", "Long-term client", "Professional and warm", "ENGLISH"],
186
- ["New offers", "Pierre Martin", "Travel Agency Plus", "Showcase summer destinations", "leisure group", "Medium", "Regular customer", "Friendly and inspiring", "ENGLISH"],
187
- ["theme", "Sophie Laurent", "Corporate Events Ltd", "Present emerging trends in 2025", "MICE", "High", "Industry leader", "Modern and innovative", "ENGLISH"],
188
- ["solution", "Thomas Bernard", "Business Travel Corp", "Promote loyalty program benefits", "AHBO", "High", "Frequent traveler", "Professional and convincing", "ENGLISH"],
189
  ],
190
  inputs=[email_type, client_name, company, objective, segment, priority, history, style, language],
191
  outputs=output,
@@ -193,12 +161,10 @@ with gr.Blocks(theme=gr.themes.Soft(), title="ACCOR Email Generator") as demo:
193
  cache_examples=False,
194
  )
195
 
196
- # Event
197
  generate_btn.click(
198
  fn=generate_email,
199
  inputs=[email_type, client_name, company, objective, segment, priority, history, style, language],
200
  outputs=output
201
  )
202
 
203
- # Lancer
204
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
 
3
  import re
4
 
5
  # ==================== CHARGEMENT DU MODÈLE ====================
6
  print("🔄 Chargement du modèle...")
7
 
8
+ # VERSION SIMPLE SANS QUANTIZATION
9
  model = AutoModelForCausalLM.from_pretrained(
10
  "padufour/mistral-accor-emails",
 
 
11
  low_cpu_mem_usage=True
12
  )
13
 
 
19
  def generate_email(email_type, client_name, company, objective,
20
  segment, priority, history, style, language):
21
 
 
22
  prompt = f"""### Instruction : Rédige un email du type "{email_type}" pour un client professionnel. L'email doit être complet, sans placeholder ni crochet [], et ne doit PAS mentionner le segment de clientèle dans le contenu.
23
 
24
  ### Input :
 
35
 
36
  ### Réponse :"""
37
 
38
+ inputs = tokenizer(prompt, return_tensors="pt")
 
39
 
40
+ outputs = model.generate(
41
+ **inputs,
42
+ max_new_tokens=800,
43
+ temperature=0.7,
44
+ top_p=0.9,
45
+ top_k=50,
46
+ repetition_penalty=1.1,
47
+ do_sample=True,
48
+ pad_token_id=tokenizer.pad_token_id,
49
+ eos_token_id=tokenizer.eos_token_id
50
+ )
 
51
 
52
  email = tokenizer.decode(outputs[0], skip_special_tokens=True)
53
 
 
54
  if "### Réponse :" in email:
55
  email = email.split("### Réponse :")[1].strip()
56
 
 
60
  email = re.sub(r'at\s+[\w\.-]+@[\w\.-]+\.\w+', '', email)
61
  email = re.sub(r'or\s+\+?\d[\d\s\(\)\-\.]+', '', email)
62
 
 
63
  if "ACCOR Team" not in email and "Accor Team" not in email:
64
  email += "\n\nBest regards,\nThe ACCOR Team"
65
 
 
66
  email = re.sub(r'\n\s*\n\s*\n', '\n\n', email)
67
  email = re.sub(r' +', ' ', email)
68
 
 
87
  email_type = gr.Dropdown(
88
  choices=["Newsletter", "New offers", "theme", "solution", "Hotel opening"],
89
  value="Newsletter",
90
+ label="Email Type *"
 
91
  )
92
 
93
  segment = gr.Dropdown(
94
  choices=["MICE", "AHBO", "leisure group"],
95
  value="MICE",
96
+ label="Segment"
 
97
  )
98
 
99
  gr.Markdown("### 👤 Client Information")
100
 
101
  client_name = gr.Textbox(
102
  label="Client Name *",
 
103
  value="Marie Dupont"
104
  )
105
 
106
  company = gr.Textbox(
107
  label="Company *",
 
108
  value="Event Solutions SA"
109
  )
110
 
111
  objective = gr.Textbox(
112
  label="Objective *",
 
113
  value="Promote winter events",
114
  lines=2
115
  )
 
124
 
125
  history = gr.Textbox(
126
  label="Client History",
 
127
  value="New customer"
128
  )
129
 
130
  style = gr.Textbox(
131
  label="Style",
 
132
  value="Professional and engaging"
133
  )
134
 
 
149
  lines=25,
150
  show_copy_button=True
151
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
  gr.Examples(
154
  examples=[
155
  ["Newsletter", "Marie Dupont", "Event Solutions SA", "Promote winter events", "MICE", "High", "Long-term client", "Professional and warm", "ENGLISH"],
156
+ ["New offers", "Pierre Martin", "Travel Agency Plus", "Showcase summer destinations", "leisure group", "Medium", "Regular customer", "Friendly", "ENGLISH"],
 
 
157
  ],
158
  inputs=[email_type, client_name, company, objective, segment, priority, history, style, language],
159
  outputs=output,
 
161
  cache_examples=False,
162
  )
163
 
 
164
  generate_btn.click(
165
  fn=generate_email,
166
  inputs=[email_type, client_name, company, objective, segment, priority, history, style, language],
167
  outputs=output
168
  )
169
 
 
170
  demo.launch()