File size: 1,183 Bytes
f2ee9bf
73a7679
43a61d0
73a7679
 
f2ee9bf
73a7679
 
f2ee9bf
73a7679
 
43a61d0
 
 
 
cd42b03
43a61d0
 
 
73a7679
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2ee9bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from huggingface_hub import InferenceClient
import os

# Utilise le token depuis les secrets HF
HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")

def generate_email(model_id, to_email, context, tone):
    client = InferenceClient(token=HF_TOKEN)

    prompt = f"""
Tu es un assistant virtuel. Rédige un email destiné à {to_email}.

Contexte : {context}
Ton : {tone}

Réponds en deux parties :
1. Objet : ligne d'objet de l'email
2. Corps : contenu de l'email bien rédigé, en respectant le ton demandé.
Commence chaque partie par son étiquette.
""".strip()

    try:
        response = client.text_generation(
            model=model_id,
            prompt=prompt,
            max_new_tokens=512,
            temperature=0.7,
        )
        return parse_email(response)
    except Exception as e:
        return "Erreur", f"❌ Erreur de génération : {e}"

def parse_email(text):
    lines = text.strip().split("\n")
    subject = next((line for line in lines if line.lower().startswith("objet")), "Objet : (non trouvé)")
    body_lines = [line for line in lines if not line.lower().startswith("objet")]
    body = "\n".join(body_lines).strip()
    return subject, body