JarvisLabs commited on
Commit
064aede
·
verified ·
1 Parent(s): 9c21ed0

Upload 6 files

Browse files
Files changed (5) hide show
  1. src/fal_api.py +6 -6
  2. src/helpers.py +42 -42
  3. src/mail.py +48 -0
  4. src/rep_api.py +18 -1
  5. src/utils.py +6 -0
src/fal_api.py CHANGED
@@ -3,7 +3,7 @@ from src.utils import numpy_to_base64
3
  from src.helpers import resize_image
4
 
5
  def fal_ipadapter_api(input_image,ip_image,seg_prompt):
6
- print(input_image,ip_image,seg_prompt)
7
 
8
  handler = fal_client.submit(
9
  "comfy/JarvisSan22/cloth_ipadapter",
@@ -13,10 +13,10 @@ def fal_ipadapter_api(input_image,ip_image,seg_prompt):
13
  "groundingdinosamsegment (segment anything)_prompt":seg_prompt
14
  },
15
  )
16
- print(handler)
17
  result= handler.get()
18
  #image_urls=[]
19
- print(result["outputs"])
20
  """
21
  for k,item in result["outputs"].items():
22
  if "images" in item:
@@ -28,11 +28,11 @@ def fal_ipadapter_api(input_image,ip_image,seg_prompt):
28
  return result["outputs"]["20"]["images"][0]["url"]
29
 
30
  def fal_faceswap_api(input_image,face_image):
31
- print(input_image.shape,face_image.shape,type(face_image))
32
  #face_image.resize((1024, 1024))
33
  input_image=resize_image(input_image)
34
  face_image=resize_image(face_image)
35
- print(input_image.shape,face_image.shape)
36
  handler = fal_client.submit(
37
  "fal-ai/face-swap",
38
  arguments={
@@ -42,5 +42,5 @@ def fal_faceswap_api(input_image,face_image):
42
  )
43
 
44
  result = handler.get()
45
- print(result)
46
  return result["image"]["url"]
 
3
  from src.helpers import resize_image
4
 
5
  def fal_ipadapter_api(input_image,ip_image,seg_prompt):
6
+ #print(input_image,ip_image,seg_prompt)
7
 
8
  handler = fal_client.submit(
9
  "comfy/JarvisSan22/cloth_ipadapter",
 
13
  "groundingdinosamsegment (segment anything)_prompt":seg_prompt
14
  },
15
  )
16
+ #print(handler)
17
  result= handler.get()
18
  #image_urls=[]
19
+ #print(result["outputs"])
20
  """
21
  for k,item in result["outputs"].items():
22
  if "images" in item:
 
28
  return result["outputs"]["20"]["images"][0]["url"]
29
 
30
  def fal_faceswap_api(input_image,face_image):
31
+ #print(input_image.shape,face_image.shape,type(face_image))
32
  #face_image.resize((1024, 1024))
33
  input_image=resize_image(input_image)
34
  face_image=resize_image(face_image)
35
+ #print(input_image.shape,face_image.shape)
36
  handler = fal_client.submit(
37
  "fal-ai/face-swap",
38
  arguments={
 
42
  )
43
 
44
  result = handler.get()
45
+ #print(result)
46
  return result["image"]["url"]
src/helpers.py CHANGED
@@ -1,43 +1,43 @@
1
- import numpy as np
2
- from PIL import Image
3
-
4
- def resize_image(image_array, max_size=1048576):
5
- """
6
- Resizes a NumPy image array to a maximum total size of 1048576 pixels while preserving the aspect ratio.
7
-
8
- Args:
9
- image_array (numpy.ndarray): The input image array.
10
- max_size (int): The maximum total size of the image in pixels (default is 1048576).
11
-
12
- Returns:
13
- numpy.ndarray: The resized image array.
14
- """
15
- # Get the current size of the image
16
- height, width, _ = image_array.shape
17
-
18
- # Calculate the total size of the image
19
- total_size = height * width
20
-
21
- # If the image is already smaller than the max size, return the original image
22
- if total_size <= max_size:
23
- return image_array
24
-
25
- # Calculate the aspect ratio
26
- aspect_ratio = width / height
27
-
28
- # Calculate the new dimensions while preserving the aspect ratio
29
- if aspect_ratio > 1:
30
- new_width = int(np.sqrt(max_size * aspect_ratio))
31
- new_height = int(new_width / aspect_ratio)
32
- else:
33
- new_height = int(np.sqrt(max_size / aspect_ratio))
34
- new_width = int(new_height * aspect_ratio)
35
-
36
- # Resize the image using Pillow
37
- image = Image.fromarray(image_array)
38
- image = image.resize((new_width, new_height), resample=Image.BICUBIC)
39
-
40
- # Convert the resized image back to a NumPy array
41
- resized_image = np.array(image)
42
-
43
  return resized_image
 
1
+ import numpy as np
2
+ from PIL import Image
3
+
4
+ def resize_image(image_array, max_size=1048576):
5
+ """
6
+ Resizes a NumPy image array to a maximum total size of 1048576 pixels while preserving the aspect ratio.
7
+
8
+ Args:
9
+ image_array (numpy.ndarray): The input image array.
10
+ max_size (int): The maximum total size of the image in pixels (default is 1048576).
11
+
12
+ Returns:
13
+ numpy.ndarray: The resized image array.
14
+ """
15
+ # Get the current size of the image
16
+ height, width, _ = image_array.shape
17
+
18
+ # Calculate the total size of the image
19
+ total_size = height * width
20
+
21
+ # If the image is already smaller than the max size, return the original image
22
+ if total_size <= max_size:
23
+ return image_array
24
+
25
+ # Calculate the aspect ratio
26
+ aspect_ratio = width / height
27
+
28
+ # Calculate the new dimensions while preserving the aspect ratio
29
+ if aspect_ratio > 1:
30
+ new_width = int(np.sqrt(max_size * aspect_ratio))
31
+ new_height = int(new_width / aspect_ratio)
32
+ else:
33
+ new_height = int(np.sqrt(max_size / aspect_ratio))
34
+ new_width = int(new_height * aspect_ratio)
35
+
36
+ # Resize the image using Pillow
37
+ image = Image.fromarray(image_array)
38
+ image = image.resize((new_width, new_height), resample=Image.BICUBIC)
39
+
40
+ # Convert the resized image back to a NumPy array
41
+ resized_image = np.array(image)
42
+
43
  return resized_image
src/mail.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import smtplib
3
+ import numpy as np
4
+ from email.mime.text import MIMEText
5
+ from email.mime.image import MIMEImage
6
+ from email.mime.multipart import MIMEMultipart
7
+ from email.utils import formatdate
8
+
9
+ def send_email(email, selected_images):
10
+ # Email configuration (replace with your SMTP server details)
11
+ smtp_server = "smtp.gmail.com"
12
+ smtp_port = 587
13
+ # Create the email message
14
+ msg = MIMEMultipart()
15
+ msg['Subject'] = 'Selected Image'
16
+ msg['From'] = os.getenv("sender_email")
17
+ msg['To'] = email
18
+ msg['Date'] = formatdate()
19
+
20
+ # Add text to the email
21
+ text = MIMEText(f"""
22
+ 今日はブースを訪れてくれてありがとうございました。。
23
+  以下は、あなたが生成した{len(selected_images)}枚の画像です。
24
+ """,_charset="utf-8")
25
+ msg.attach(text)
26
+ print(selected_images)
27
+ for i,image in enumerate(selected_images):
28
+ image=np.array(image)[0]
29
+ print(image)
30
+ with open(image, 'rb') as f:
31
+ img_data = f.read()
32
+ image = MIMEImage(img_data, name=f"image_{i+1}.jpg")
33
+ msg.attach(image)
34
+
35
+ # Send the email
36
+ try:
37
+ with smtplib.SMTP(smtp_server, smtp_port) as server:
38
+ server.starttls()
39
+ server.login( os.getenv("sender_email"), os.getenv("sender_password"))
40
+ server.send_message(msg)
41
+ return "Email sent successfully!"
42
+ except smtplib.SMTPAuthenticationError:
43
+ raise Exception("SMTP Authentication failed. Please check your email and password.")
44
+ except smtplib.SMTPException as e:
45
+ raise Exception(f"SMTP error occurred: {str(e)}")
46
+ except Exception as e:
47
+ raise Exception(f"Failed to send email: {str(e)}")
48
+
src/rep_api.py CHANGED
@@ -89,9 +89,26 @@ def generate_image_replicate(prompt,lora_model,api_path,aspect_ratio,model,lora_
89
  )
90
  print(output)
91
  return output[0]
92
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
  def replicate_caption_api(image,model,context_text):
 
95
  base64_image = image_to_base64(image)
96
  if model=="blip":
97
  output = replicate.run(
 
89
  )
90
  print(output)
91
  return output[0]
92
+ def replicate_bgcontrolnet(img,prompt,background_prompt, sampler_name= "DPM++ SDE Karras",
93
+ 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"
94
+ ):
95
+ img=image_to_base64(img)
96
+ prompt=prompt+" ," +background_prompt
97
+ output=replicate.run(
98
+ "wolverinn/realistic-background:9f020c55e037529bf20ed1cb799d7aa290404cfbd45157686717ffc7ee511eab",
99
+ input={
100
+ "seed": -1,
101
+ "image":img,
102
+ "prompt":prompt,
103
+ "sampler_name":sampler_name,
104
+ "negative_prompt":negative_prompt
105
+ }
106
+ )
107
+ print(output["output"]["image"])
108
+ return output["output"]["image"]
109
 
110
  def replicate_caption_api(image,model,context_text):
111
+ print(model,context_text)
112
  base64_image = image_to_base64(image)
113
  if model=="blip":
114
  output = replicate.run(
src/utils.py CHANGED
@@ -68,6 +68,12 @@ def add_to_prompt(existing_prompt, new_prompt):
68
  return f"{existing_prompt}, {new_prompt}"
69
  else:
70
  return new_prompt
 
 
 
 
 
 
71
 
72
 
73
 
 
68
  return f"{existing_prompt}, {new_prompt}"
69
  else:
70
  return new_prompt
71
+
72
+ def update_gallery(img,gallery_list):
73
+ img=convert_to_pil(img)
74
+
75
+ gallery_list.append(img)
76
+ return gallery_list
77
 
78
 
79