File size: 4,024 Bytes
15348d9
0ff7b30
1a54e21
15348d9
 
0ff7b30
15348d9
 
1a54e21
15348d9
750c75a
15348d9
 
 
 
750c75a
15348d9
 
 
 
 
 
 
 
 
 
 
 
 
750c75a
15348d9
1a54e21
15348d9
1a54e21
15348d9
1a54e21
15348d9
 
 
 
 
 
 
 
 
1a54e21
 
750c75a
15348d9
 
 
 
 
 
 
 
 
45a55f0
1a54e21
15348d9
 
 
 
 
 
 
3959ff2
15348d9
1a54e21
15348d9
 
1a54e21
15348d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
750c75a
15348d9
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import gradio as gr
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Email, To, Attachment, FileContent, FileName, FileType, Disposition
import base64

# Load API key and sender email from Hugging Face Space Secrets
# These are loaded as environment variables by the Space runtime.
SENDGRID_API_KEY = os.environ.get("SENDGRID_API_KEY")
SENDGRID_SENDER_EMAIL = os.environ.get("SENDGRID_SENDER_EMAIL")

if not SENDGRID_API_KEY:
    print("Warning: SENDGRID_API_KEY not found. Email sending will fail.")
if not SENDGRID_SENDER_EMAIL:
    print("Warning: SENDGRID_SENDER_EMAIL not found. Email sending will fail.")

def send_email_report(
    recipient_email: str,
    pdf_content_base64: str,
    file_name: str,
    sequence: str,
    prediction: str,
    confidence: float
) -> str:
    """
    Sends an email with an attached PDF report using SendGrid.
    """
    if not SENDGRID_API_KEY or not SENDGRID_SENDER_EMAIL:
        return "Error: SendGrid API key or sender email not configured on the backend."

    # Create SendGrid Mail object
    message = Mail(
        from_email=SENDGRID_SENDER_EMAIL,
        to_emails=recipient_email,
        subject=f"EPIC-AMP Prediction Report for {sequence[:20]}...",
        html_content=f"""
            <p>Dear User,</p>
            <p>Thank you for using EPIC-AMP! Your prediction report is attached.</p>
            <p><strong>Input Sequence:</strong> {sequence}</p>
            <p><strong>Prediction:</strong> <span style="color: {'#d32f2f' if 'non-amp' in prediction.lower() else '#4caf50'}; font-weight: bold;">{prediction}</span></p>
            <p><strong>Confidence:</strong> {(confidence * 100):.2f}%</p>
            <p>Please find the detailed PDF report attached to this email.</p>
            <p>If you have any questions or feedback, please reach out to us at <a href="mailto:[email protected]">[email protected]</a>.</p>
            <p>Sincerely,</p>
            <p>The EPIC-AMP Team</p>
        """
    )

    # Create and add the PDF attachment [4, 5, 7, 13]
    encoded_file = pdf_content_base64
    attachedFile = Attachment(
        FileContent(encoded_file),
        FileName(file_name),
        FileType('application/pdf'),
        Disposition('attachment')
    )
    message.add_attachment(attachedFile)

    try:
        sendgrid_client = SendGridAPIClient(SENDGRID_API_KEY)
        response = sendgrid_client.send(message)
        
        # Check SendGrid response status [6]
        if 200 <= response.status_code < 300:
            print(f"Email sent successfully to {recipient_email}. Status Code: {response.status_code}")
            return "Email sent successfully!"
        else:
            error_msg = f"Failed to send email. Status Code: {response.status_code}. Body: {response.body}"
            print(error_msg)
            return f"Failed to send email: {response.status_code}"

    except Exception as e:
        print(f"An error occurred while sending email: {e}")
        return f"An error occurred while sending email: {str(e)}"

# Gradio Interface for the email sender
# We use gr.Interface to expose this as an API endpoint callable by the frontend
email_interface = gr.Interface(
    fn=send_email_report,
    inputs=[
        gr.Textbox(label="Recipient Email"),
        gr.Textbox(label="PDF Content (Base64)"),
        gr.Textbox(label="File Name"),
        gr.Textbox(label="Sequence"),
        gr.Textbox(label="Prediction"),
        gr.Number(label="Confidence")
    ],
    outputs=gr.Textbox(label="Email Status"),
    title="EPIC-AMP Email Sender Backend",
    description="This endpoint handles sending detailed AMP reports via email."
)

# You can also integrate this into an existing Gradio Blocks app.
# If you have an existing prediction app, you can export this email_interface
# and use it within your Blocks as a tab or just expose it as another API route.
# For simplicity, here it's a standalone interface.
email_interface.launch(server_name="0.0.0.0", server_port=7860)