Spaces:
Sleeping
Sleeping
Essai sup
Browse files- agent.py +39 -15
- app.py +15 -19
- email_sender.py +2 -4
agent.py
CHANGED
|
@@ -1,19 +1,43 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
return subject, body
|
|
|
|
| 1 |
+
from smolagent import SmolAgent, OpenAIProvider
|
| 2 |
+
from smolagent.schema import ChatPrompt, Prompt
|
| 3 |
|
| 4 |
+
# Crée le système de prompt soigné pour générer email professionnel
|
| 5 |
+
system_prompt = Prompt(
|
| 6 |
+
role="system",
|
| 7 |
+
content=(
|
| 8 |
+
"Tu es un assistant IA qui génère des emails professionnels adaptés au contexte, "
|
| 9 |
+
"au destinataire et au ton donné. "
|
| 10 |
+
"Rédige d’abord un objet court et pertinent, puis le corps du mail complet."
|
| 11 |
+
),
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Prompt utilisateur (input)
|
| 15 |
+
user_prompt = Prompt(
|
| 16 |
+
role="user",
|
| 17 |
+
content=(
|
| 18 |
+
"Destinataire : {to_email}\n"
|
| 19 |
+
"Contexte : {context}\n"
|
| 20 |
+
"Ton souhaité : {tone}\n\n"
|
| 21 |
+
"Génère un objet et un corps d'email."
|
| 22 |
+
),
|
| 23 |
+
)
|
| 24 |
|
| 25 |
+
def build_email_agent():
|
| 26 |
+
provider = OpenAIProvider(model="gpt-4o-mini") # ou autre modèle disponible
|
| 27 |
+
prompt = ChatPrompt(prompts=[system_prompt, user_prompt])
|
| 28 |
+
agent = SmolAgent(provider=provider, prompt=prompt)
|
| 29 |
+
return agent
|
| 30 |
|
| 31 |
+
def generate_email(agent, to_email, context, tone):
|
| 32 |
+
# Formate le prompt avec les valeurs utilisateur
|
| 33 |
+
inputs = {
|
| 34 |
+
"to_email": to_email,
|
| 35 |
+
"context": context,
|
| 36 |
+
"tone": tone,
|
| 37 |
+
}
|
| 38 |
+
response = agent.chat(inputs)
|
| 39 |
+
# La réponse devrait contenir objet + corps, on sépare à la première ligne vide
|
| 40 |
+
parts = response.strip().split('\n\n', 1)
|
| 41 |
+
subject = parts[0].strip() if parts else "Objet automatique"
|
| 42 |
+
body = parts[1].strip() if len(parts) > 1 else response.strip()
|
| 43 |
return subject, body
|
app.py
CHANGED
|
@@ -1,39 +1,35 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
from agent import build_email_agent, generate_email
|
| 4 |
from email_sender import send_email
|
| 5 |
|
| 6 |
agent = build_email_agent()
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
f"Contexte : {context}\n" \
|
| 11 |
-
f"Ton : {tone}\n\nObjet et corps du mail :"
|
| 12 |
-
subject, body = generate_email(agent, prompt)
|
| 13 |
return subject, body
|
| 14 |
|
| 15 |
-
def
|
| 16 |
-
|
| 17 |
-
return
|
| 18 |
|
| 19 |
-
with gr.Blocks(title="Assistant Email
|
| 20 |
-
gr.Markdown("# ✉️ Assistant Email Professionnel")
|
| 21 |
|
| 22 |
-
to_email = gr.Textbox(label="Destinataire", placeholder="
|
| 23 |
-
context = gr.Textbox(label="Contexte ou objectif
|
| 24 |
-
placeholder="
|
| 25 |
-
tone = gr.Dropdown(choices=["Formel", "Chaleureux", "Direct", "Poli"], value="Formel", label="Ton
|
| 26 |
|
| 27 |
subject = gr.Textbox(label="Objet de l'email")
|
| 28 |
body = gr.Textbox(label="Corps de l'email", lines=10)
|
| 29 |
|
| 30 |
with gr.Row():
|
| 31 |
-
generate_btn = gr.Button("📝 Générer
|
| 32 |
-
send_btn = gr.Button("📨 Envoyer
|
| 33 |
|
| 34 |
status = gr.Markdown()
|
| 35 |
|
| 36 |
-
generate_btn.click(
|
| 37 |
-
send_btn.click(
|
| 38 |
|
| 39 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from agent import build_email_agent, generate_email
|
| 3 |
from email_sender import send_email
|
| 4 |
|
| 5 |
agent = build_email_agent()
|
| 6 |
|
| 7 |
+
def on_generate(to_email, context, tone):
|
| 8 |
+
subject, body = generate_email(agent, to_email, context, tone)
|
|
|
|
|
|
|
|
|
|
| 9 |
return subject, body
|
| 10 |
|
| 11 |
+
def on_send(to_email, subject, body):
|
| 12 |
+
success, message = send_email(to_email, subject, body)
|
| 13 |
+
return message
|
| 14 |
|
| 15 |
+
with gr.Blocks(title="Assistant Email Pro avec SmolAgent") as demo:
|
| 16 |
+
gr.Markdown("# ✉️ Assistant Email Professionnel SmolAgent")
|
| 17 |
|
| 18 |
+
to_email = gr.Textbox(label="Destinataire", placeholder="exemple@exemple.com")
|
| 19 |
+
context = gr.Textbox(label="Contexte ou objectif", lines=4,
|
| 20 |
+
placeholder="Demander un rendez-vous, relancer un client...")
|
| 21 |
+
tone = gr.Dropdown(choices=["Formel", "Chaleureux", "Direct", "Poli"], value="Formel", label="Ton")
|
| 22 |
|
| 23 |
subject = gr.Textbox(label="Objet de l'email")
|
| 24 |
body = gr.Textbox(label="Corps de l'email", lines=10)
|
| 25 |
|
| 26 |
with gr.Row():
|
| 27 |
+
generate_btn = gr.Button("📝 Générer l'email")
|
| 28 |
+
send_btn = gr.Button("📨 Envoyer l'email")
|
| 29 |
|
| 30 |
status = gr.Markdown()
|
| 31 |
|
| 32 |
+
generate_btn.click(on_generate, inputs=[to_email, context, tone], outputs=[subject, body])
|
| 33 |
+
send_btn.click(on_send, inputs=[to_email, subject, body], outputs=status)
|
| 34 |
|
| 35 |
demo.launch()
|
email_sender.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# email_sender.py
|
| 2 |
import os
|
| 3 |
import base64
|
| 4 |
import pickle
|
|
@@ -10,7 +9,6 @@ from googleapiclient.discovery import build
|
|
| 10 |
TOKEN_PATH = "token.pkl"
|
| 11 |
CREDENTIALS_PATH = "client_secret.json"
|
| 12 |
|
| 13 |
-
# Écrire client_secret.json depuis variable d'environnement (Hugging Face Secret)
|
| 14 |
client_secret_content = os.getenv("CLIENT_SECRET_JSON")
|
| 15 |
if client_secret_content and not os.path.exists(CREDENTIALS_PATH):
|
| 16 |
with open(CREDENTIALS_PATH, "w") as f:
|
|
@@ -42,6 +40,6 @@ def send_email(to_email, subject, body):
|
|
| 42 |
send_message = {"raw": raw_message}
|
| 43 |
try:
|
| 44 |
sent = service.users().messages().send(userId="me", body=send_message).execute()
|
| 45 |
-
return f"✅ Email envoyé à {to_email} (ID: {sent['id']})"
|
| 46 |
except Exception as e:
|
| 47 |
-
return f"❌ Échec de l'envoi : {e}"
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import base64
|
| 3 |
import pickle
|
|
|
|
| 9 |
TOKEN_PATH = "token.pkl"
|
| 10 |
CREDENTIALS_PATH = "client_secret.json"
|
| 11 |
|
|
|
|
| 12 |
client_secret_content = os.getenv("CLIENT_SECRET_JSON")
|
| 13 |
if client_secret_content and not os.path.exists(CREDENTIALS_PATH):
|
| 14 |
with open(CREDENTIALS_PATH, "w") as f:
|
|
|
|
| 40 |
send_message = {"raw": raw_message}
|
| 41 |
try:
|
| 42 |
sent = service.users().messages().send(userId="me", body=send_message).execute()
|
| 43 |
+
return True, f"✅ Email envoyé à {to_email} (ID: {sent['id']})"
|
| 44 |
except Exception as e:
|
| 45 |
+
return False, f"❌ Échec de l'envoi : {e}"
|