Spaces:
Sleeping
Sleeping
Upload 6 files
Browse filesFiles changed (5)
- src/fal_api.py+6-6
- src/helpers.py+42-42
- src/mail.pyAdded+48
- src/rep_api.py+18-1
- src/utils.py+6
2 unmodified lines34567893 unmodified lines131415161718192021225 unmodified lines28293031323334353637383 unmodified lines42434445462 unmodified linesfrom src.helpers import resize_imagedef fal_ipadapter_api(input_image,ip_image,seg_prompt):print(input_image,ip_image,seg_prompt)handler = fal_client.submit("comfy/JarvisSan22/cloth_ipadapter",3 unmodified lines"groundingdinosamsegment (segment anything)_prompt":seg_prompt},)print(handler)result= handler.get()#image_urls=[]print(result["outputs"])"""for k,item in result["outputs"].items():if "images" in item:5 unmodified linesreturn result["outputs"]["20"]["images"][0]["url"]def fal_faceswap_api(input_image,face_image):print(input_image.shape,face_image.shape,type(face_image))#face_image.resize((1024, 1024))input_image=resize_image(input_image)face_image=resize_image(face_image)print(input_image.shape,face_image.shape)handler = fal_client.submit("fal-ai/face-swap",arguments={3 unmodified lines)result = handler.get()print(result)return result["image"]["url"]2 unmodified lines34567893 unmodified lines131415161718192021225 unmodified lines28293031323334353637383 unmodified lines42434445462 unmodified linesfrom src.helpers import resize_imagedef fal_ipadapter_api(input_image,ip_image,seg_prompt):#print(input_image,ip_image,seg_prompt)handler = fal_client.submit("comfy/JarvisSan22/cloth_ipadapter",3 unmodified lines"groundingdinosamsegment (segment anything)_prompt":seg_prompt},)#print(handler)result= handler.get()#image_urls=[]#print(result["outputs"])"""for k,item in result["outputs"].items():if "images" in item:5 unmodified linesreturn result["outputs"]["20"]["images"][0]["url"]def fal_faceswap_api(input_image,face_image):#print(input_image.shape,face_image.shape,type(face_image))#face_image.resize((1024, 1024))input_image=resize_image(input_image)face_image=resize_image(face_image)#print(input_image.shape,face_image.shape)handler = fal_client.submit("fal-ai/face-swap",arguments={3 unmodified lines)result = handler.get()#print(result)return result["image"]["url"]
12345678910111213141516171819202122232425262728293031323334353637383940414243import numpy as npfrom PIL import Imagedef resize_image(image_array, max_size=1048576):"""Resizes a NumPy image array to a maximum total size of 1048576 pixels while preserving the aspect ratio.Args:image_array (numpy.ndarray): The input image array.max_size (int): The maximum total size of the image in pixels (default is 1048576).Returns:numpy.ndarray: The resized image array."""# Get the current size of the imageheight, width, _ = image_array.shape# Calculate the total size of the imagetotal_size = height * width# If the image is already smaller than the max size, return the original imageif total_size <= max_size:return image_array# Calculate the aspect ratioaspect_ratio = width / height# Calculate the new dimensions while preserving the aspect ratioif aspect_ratio > 1:new_width = int(np.sqrt(max_size * aspect_ratio))new_height = int(new_width / aspect_ratio)else:new_height = int(np.sqrt(max_size / aspect_ratio))new_width = int(new_height * aspect_ratio)# Resize the image using Pillowimage = Image.fromarray(image_array)image = image.resize((new_width, new_height), resample=Image.BICUBIC)# Convert the resized image back to a NumPy arrayresized_image = np.array(image)return resized_image12345678910111213141516171819202122232425262728293031323334353637383940414243import numpy as npfrom PIL import Imagedef resize_image(image_array, max_size=1048576):"""Resizes a NumPy image array to a maximum total size of 1048576 pixels while preserving the aspect ratio.Args:image_array (numpy.ndarray): The input image array.max_size (int): The maximum total size of the image in pixels (default is 1048576).Returns:numpy.ndarray: The resized image array."""# Get the current size of the imageheight, width, _ = image_array.shape# Calculate the total size of the imagetotal_size = height * width# If the image is already smaller than the max size, return the original imageif total_size <= max_size:return image_array# Calculate the aspect ratioaspect_ratio = width / height# Calculate the new dimensions while preserving the aspect ratioif aspect_ratio > 1:new_width = int(np.sqrt(max_size * aspect_ratio))new_height = int(new_width / aspect_ratio)else:new_height = int(np.sqrt(max_size / aspect_ratio))new_width = int(new_height * aspect_ratio)# Resize the image using Pillowimage = Image.fromarray(image_array)image = image.resize((new_width, new_height), resample=Image.BICUBIC)# Convert the resized image back to a NumPy arrayresized_image = np.array(image)return resized_image
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
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
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)}")
88 unmodified lines89909192939495969788 unmodified lines)print(output)return output[0]def replicate_caption_api(image,model,context_text):base64_image = image_to_base64(image)if model=="blip":output = replicate.run(88 unmodified lines899091929394959697989910010110210310410510610710810911011111211311488 unmodified lines)print(output)return output[0]def replicate_bgcontrolnet(img,prompt,background_prompt, sampler_name= "DPM++ SDE Karras",negative_prompt="(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime, mutated hands and fingers:1.4), (deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, disconnected limbs, mutation, mutated, ugly, disgusting, amputation"):img=image_to_base64(img)prompt=prompt+" ," +background_promptoutput=replicate.run("wolverinn/realistic-background:9f020c55e037529bf20ed1cb799d7aa290404cfbd45157686717ffc7ee511eab",input={"seed": -1,"image":img,"prompt":prompt,"sampler_name":sampler_name,"negative_prompt":negative_prompt})print(output["output"]["image"])return output["output"]["image"]def replicate_caption_api(image,model,context_text):print(model,context_text)base64_image = image_to_base64(image)if model=="blip":output = replicate.run(
67 unmodified lines68697071727367 unmodified linesreturn f"{existing_prompt}, {new_prompt}"else:return new_prompt67 unmodified lines68697071727374757677787967 unmodified linesreturn f"{existing_prompt}, {new_prompt}"else:return new_promptdef update_gallery(img,gallery_list):img=convert_to_pil(img)gallery_list.append(img)return gallery_list
src/fal_api.py+6-6
src/helpers.py+42-42
src/mail.pyAdded+48
src/rep_api.py+18-1
src/utils.py+6