Spaces:
Running
Running
Create tools/email_server.py
Browse files- tools/email_server.py +129 -0
tools/email_server.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from utils.llm_utils import get_llm_response
|
| 2 |
+
from email.mime.text import MIMEText
|
| 3 |
+
from email.mime.multipart import MIMEMultipart
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
async def draft_email(
|
| 8 |
+
recipient: str,
|
| 9 |
+
subject: str,
|
| 10 |
+
context: str,
|
| 11 |
+
tone: str = 'professional'
|
| 12 |
+
) -> dict:
|
| 13 |
+
"""
|
| 14 |
+
Draft an email based on context
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
recipient: Email recipient
|
| 18 |
+
subject: Email subject
|
| 19 |
+
context: Context/purpose of email
|
| 20 |
+
tone: Tone (professional, friendly, formal, casual)
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
Dict with drafted email content
|
| 24 |
+
"""
|
| 25 |
+
try:
|
| 26 |
+
prompt = f"""Draft a {tone} email with the following details:
|
| 27 |
+
|
| 28 |
+
Recipient: {recipient}
|
| 29 |
+
Subject: {subject}
|
| 30 |
+
|
| 31 |
+
Context/Purpose:
|
| 32 |
+
{context}
|
| 33 |
+
|
| 34 |
+
Write a complete, well-formatted email including:
|
| 35 |
+
- Appropriate greeting
|
| 36 |
+
- Clear body paragraphs
|
| 37 |
+
- Professional closing
|
| 38 |
+
- Signature placeholder
|
| 39 |
+
|
| 40 |
+
Tone should be: {tone}
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
email_body = await get_llm_response(prompt, temperature=0.7)
|
| 44 |
+
|
| 45 |
+
# Create formatted email
|
| 46 |
+
email_html = f"""
|
| 47 |
+
<!DOCTYPE html>
|
| 48 |
+
<html>
|
| 49 |
+
<head>
|
| 50 |
+
<style>
|
| 51 |
+
body {{ font-family: Arial, sans-serif; line-height: 1.6; }}
|
| 52 |
+
.email-container {{ max-width: 600px; margin: 20px; }}
|
| 53 |
+
.header {{ border-bottom: 2px solid #333; padding-bottom: 10px; }}
|
| 54 |
+
.body {{ margin: 20px 0; }}
|
| 55 |
+
.footer {{ border-top: 1px solid #ccc; padding-top: 10px; color: #666; }}
|
| 56 |
+
</style>
|
| 57 |
+
</head>
|
| 58 |
+
<body>
|
| 59 |
+
<div class="email-container">
|
| 60 |
+
<div class="header">
|
| 61 |
+
<strong>To:</strong> {recipient}<br>
|
| 62 |
+
<strong>Subject:</strong> {subject}
|
| 63 |
+
</div>
|
| 64 |
+
<div class="body">
|
| 65 |
+
{email_body.replace(chr(10), '<br>')}
|
| 66 |
+
</div>
|
| 67 |
+
<div class="footer">
|
| 68 |
+
<em>Drafted by LifeAdmin AI</em>
|
| 69 |
+
</div>
|
| 70 |
+
</div>
|
| 71 |
+
</body>
|
| 72 |
+
</html>
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
# Save to file
|
| 76 |
+
output_path = f"data/outputs/email_draft_{datetime.now().strftime('%Y%m%d_%H%M%S')}.html"
|
| 77 |
+
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
|
| 78 |
+
|
| 79 |
+
with open(output_path, 'w', encoding='utf-8') as f:
|
| 80 |
+
f.write(email_html)
|
| 81 |
+
|
| 82 |
+
# Also save plain text version
|
| 83 |
+
txt_path = output_path.replace('.html', '.txt')
|
| 84 |
+
with open(txt_path, 'w', encoding='utf-8') as f:
|
| 85 |
+
f.write(f"To: {recipient}\nSubject: {subject}\n\n{email_body}")
|
| 86 |
+
|
| 87 |
+
return {
|
| 88 |
+
'success': True,
|
| 89 |
+
'output_path_html': output_path,
|
| 90 |
+
'output_path_txt': txt_path,
|
| 91 |
+
'email_body': email_body,
|
| 92 |
+
'recipient': recipient,
|
| 93 |
+
'subject': subject
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
except Exception as e:
|
| 97 |
+
return {'error': str(e), 'success': False}
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
async def draft_reply_email(original_email: str, reply_context: str, tone: str = 'professional') -> dict:
|
| 101 |
+
"""Draft a reply to an email"""
|
| 102 |
+
try:
|
| 103 |
+
prompt = f"""Draft a reply to this email:
|
| 104 |
+
|
| 105 |
+
ORIGINAL EMAIL:
|
| 106 |
+
{original_email}
|
| 107 |
+
|
| 108 |
+
REPLY CONTEXT/INSTRUCTIONS:
|
| 109 |
+
{reply_context}
|
| 110 |
+
|
| 111 |
+
Tone: {tone}
|
| 112 |
+
|
| 113 |
+
Write a professional reply email."""
|
| 114 |
+
|
| 115 |
+
reply = await get_llm_response(prompt, temperature=0.7)
|
| 116 |
+
|
| 117 |
+
output_path = f"data/outputs/email_reply_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
|
| 118 |
+
with open(output_path, 'w', encoding='utf-8') as f:
|
| 119 |
+
f.write(reply)
|
| 120 |
+
|
| 121 |
+
return {
|
| 122 |
+
'success': True,
|
| 123 |
+
'output_path': output_path,
|
| 124 |
+
'reply': reply
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
except Exception as e:
|
| 128 |
+
return {'error': str(e), 'success': False}
|
| 129 |
+
|