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