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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained("padufour/mistral-accor-emails")
|
| 17 |
+
|
| 18 |
+
print("✅ Modèle chargé et prêt !")
|
| 19 |
+
|
| 20 |
+
# ==================== FONCTION DE GÉNÉRATION ====================
|
| 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 :
|
| 28 |
+
Client Information:
|
| 29 |
+
* Name: {client_name}
|
| 30 |
+
* Company: {company}
|
| 31 |
+
* Objective: {objective}
|
| 32 |
+
* Priority: {priority}
|
| 33 |
+
* History: {history}
|
| 34 |
+
* Style: {style}
|
| 35 |
+
* Language: {language}
|
| 36 |
+
|
| 37 |
+
Important: Ne pas inclure de placeholders [], ne pas mentionner le segment, ne pas mettre les coordonnées du client.
|
| 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 |
+
|
| 63 |
+
# Nettoyer
|
| 64 |
+
email = re.sub(r'\[.*?\]', '', email)
|
| 65 |
+
email = re.sub(r'\b(MICE|AHBO|leisure group)\b', '', email, flags=re.IGNORECASE)
|
| 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 |
+
|
| 77 |
+
return email.strip()
|
| 78 |
+
|
| 79 |
+
# ==================== INTERFACE GRADIO ====================
|
| 80 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="ACCOR Email Generator") as demo:
|
| 81 |
+
|
| 82 |
+
gr.Markdown(
|
| 83 |
+
"""
|
| 84 |
+
# 🎯 ACCOR Email Generator
|
| 85 |
+
**Powered by Mistral-7B Fine-tuned**
|
| 86 |
+
|
| 87 |
+
Generate professional marketing emails for ACCOR clients in seconds.
|
| 88 |
+
"""
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
with gr.Row():
|
| 92 |
+
with gr.Column(scale=1):
|
| 93 |
+
gr.Markdown("### 📋 Email Configuration")
|
| 94 |
+
|
| 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 |
+
)
|
| 129 |
+
|
| 130 |
+
gr.Markdown("### ⚙️ Advanced Options")
|
| 131 |
+
|
| 132 |
+
priority = gr.Radio(
|
| 133 |
+
choices=["High", "Medium", "Low"],
|
| 134 |
+
value="High",
|
| 135 |
+
label="Priority"
|
| 136 |
+
)
|
| 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 |
+
|
| 150 |
+
language = gr.Radio(
|
| 151 |
+
choices=["ENGLISH", "FRENCH"],
|
| 152 |
+
value="ENGLISH",
|
| 153 |
+
label="Language"
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
generate_btn = gr.Button("🚀 Generate Email", variant="primary", size="lg")
|
| 157 |
+
|
| 158 |
+
with gr.Column(scale=1):
|
| 159 |
+
gr.Markdown("### 📧 Generated Email")
|
| 160 |
+
|
| 161 |
+
output = gr.Textbox(
|
| 162 |
+
label="Result",
|
| 163 |
+
placeholder="Your generated email will appear here...",
|
| 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,
|
| 192 |
+
fn=generate_email,
|
| 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()
|