nonzeroexit's picture
Update app.py
15348d9 verified
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)