Spaces:
Sleeping
Sleeping
File size: 3,445 Bytes
1c653a8 |
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 98 99 100 101 102 |
import gradio as gr
import smtplib
import numpy as np
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.utils import formatdate
import os
def send_email(email, selected_images):
# Email configuration (replace with your SMTP server details)
smtp_server = "smtp.gmail.com"
smtp_port = 587
# Create the email message
msg = MIMEMultipart()
msg['Subject'] = 'Selected Image'
msg['From'] = os.getenv("sender_email")
msg['To'] = email
msg['Date'] = formatdate()
# Add text to the email
text = MIMEText(f"""
今日はブースを訪れてくれてありがとうございました。。
以下は、あなたが生成した{len(selected_images)}枚の画像です。
""",_charset="utf-8")
msg.attach(text)
print(selected_images)
for i,image in enumerate(selected_images):
image=np.array(image)[0]
print(image)
with open(image, 'rb') as f:
img_data = f.read()
image = MIMEImage(img_data, name=f"image_{i+1}.jpg")
msg.attach(image)
# Send the email
try:
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login( os.getenv("sender_email"), os.getenv("sender_password"))
server.send_message(msg)
return "Email sent successfully!"
except smtplib.SMTPAuthenticationError:
raise Exception("SMTP Authentication failed. Please check your email and password.")
except smtplib.SMTPException as e:
raise Exception(f"SMTP error occurred: {str(e)}")
except Exception as e:
raise Exception(f"Failed to send email: {str(e)}")
examples= ["Test_images/Woman_1.png","Test_images/prompt_support_examples/Man_1.png"]
select_list=[]
from src.utils import convert_to_pil
def update_gallery(img,select_list=select_list):
#img=convert_to_pil(img)
if select_list is None:
select_list=[]
select_list.append(img)
return select_list
def clear_selection(select_list=select_list):
if select_list is not None:
select_list=[]
return select_list
def create_mail_tab():
with gr.TabItem("Mail", id="mail"):
email_input = gr.Textbox(label="Enter your email:")
image_gallery = gr.Gallery(label="Selected Images", show_label=True, elem_id="gallery")
with gr.Row():
clear_button = gr.Button("Clear Selection")
send_button = gr.Button("Send Email")
result_output = gr.Textbox(label="Result")
# Examples
with gr.Row():
print(examples)
examples=np.array(examples)
for example in examples:
print(example)
img=gr.Image(value=example, interactive=False, label=os.path.basename(example),type="filepath")
img.select(update_gallery,inputs=img,outputs=image_gallery)
# Connect the send button to the send_email function
send_button.click(
fn=send_email,
inputs=[email_input, image_gallery],
outputs=result_output
)
# Clear the selected images
clear_button.click(
fn=clear_selection,
inputs=None,
outputs=image_gallery,
) |