Spaces:
Sleeping
Sleeping
Change try another model
Browse files
agent.py
CHANGED
|
@@ -1,42 +1,46 @@
|
|
| 1 |
from huggingface_hub import InferenceClient
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
client
|
| 5 |
-
|
| 6 |
-
token=os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 7 |
-
)
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
prompt = f"""
|
|
|
|
| 14 |
|
| 15 |
Contexte : {context}
|
| 16 |
Ton : {tone}
|
| 17 |
|
| 18 |
-
Réponds
|
| 19 |
1. Objet : ligne d'objet de l'email
|
| 20 |
2. Corps : contenu de l'email bien rédigé, en respectant le ton demandé.
|
| 21 |
Commence chaque partie par son étiquette.
|
| 22 |
"""
|
| 23 |
|
| 24 |
-
response = client.
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
temperature=0.7,
|
| 28 |
-
|
| 29 |
-
return_full_text=False
|
| 30 |
)
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
lines = response.splitlines()
|
| 36 |
-
for i, line in enumerate(lines):
|
| 37 |
if line.lower().startswith("objet"):
|
| 38 |
subject = line.split(":", 1)[-1].strip()
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
return subject,
|
|
|
|
| 1 |
from huggingface_hub import InferenceClient
|
| 2 |
import os
|
| 3 |
|
| 4 |
+
# Initialise le client avec le modèle open-source gratuit
|
| 5 |
+
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
def generate_email(model, to: str, context: str, tone: str) -> tuple[str, str]:
|
| 8 |
+
"""
|
| 9 |
+
Génère un objet et un corps d'email à partir du destinataire, du contexte et du ton.
|
| 10 |
+
"""
|
| 11 |
+
prompt = f"""
|
| 12 |
+
Tu es un assistant virtuel. Rédige un email destiné à {to}.
|
| 13 |
|
| 14 |
Contexte : {context}
|
| 15 |
Ton : {tone}
|
| 16 |
|
| 17 |
+
Réponds en deux parties :
|
| 18 |
1. Objet : ligne d'objet de l'email
|
| 19 |
2. Corps : contenu de l'email bien rédigé, en respectant le ton demandé.
|
| 20 |
Commence chaque partie par son étiquette.
|
| 21 |
"""
|
| 22 |
|
| 23 |
+
response = client.chat_completion(
|
| 24 |
+
model="mistralai/Mistral-7B-Instruct-v0.1",
|
| 25 |
+
messages=[
|
| 26 |
+
{"role": "system", "content": "Tu es un assistant qui rédige des emails."},
|
| 27 |
+
{"role": "user", "content": prompt}
|
| 28 |
+
],
|
| 29 |
temperature=0.7,
|
| 30 |
+
max_tokens=800,
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
+
content = response.choices[0].message["content"]
|
| 34 |
+
|
| 35 |
+
# Extraire les deux parties de la réponse (objet + corps)
|
| 36 |
+
lines = content.strip().split("\n", 1)
|
| 37 |
+
subject = ""
|
| 38 |
+
body = ""
|
| 39 |
|
| 40 |
+
for line in lines:
|
|
|
|
|
|
|
| 41 |
if line.lower().startswith("objet"):
|
| 42 |
subject = line.split(":", 1)[-1].strip()
|
| 43 |
+
else:
|
| 44 |
+
body += line.strip() + "\n"
|
| 45 |
|
| 46 |
+
return subject, body.strip()
|