multimodalart HF staff commited on
Commit
f27dee7
1 Parent(s): 85875c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -55
app.py CHANGED
@@ -3,7 +3,6 @@ import torch
3
  import spaces
4
  from diffusers import FluxInpaintPipeline
5
  from PIL import Image, ImageFile
6
- import numpy as np
7
 
8
  #ImageFile.LOAD_TRUNCATED_IMAGES = True
9
 
@@ -18,57 +17,47 @@ pipe.load_lora_weights(
18
  weight_name="visual-identity-design.safetensors"
19
  )
20
 
21
- def square_center_crop_numpy(img, target_size=768):
22
  if img.mode in ('RGBA', 'P'):
23
  img = img.convert('RGB')
24
-
25
- # Convert PIL image to numpy array
26
- img_array = np.array(img)
27
-
28
- # Get dimensions
29
- height, width = img_array.shape[:2]
30
  crop_size = min(width, height)
31
-
32
- # Calculate crop coordinates
33
  left = (width - crop_size) // 2
34
  top = (height - crop_size) // 2
35
-
36
- # Perform the crop on numpy array
37
- img_cropped = img_array[top:top+crop_size, left:left+crop_size]
38
-
39
- # Convert back to PIL and resize
40
- img_pil = Image.fromarray(img_cropped)
41
- return img_pil.resize((target_size, target_size), Image.Resampling.LANCZOS)
42
 
43
  def duplicate_horizontally(img):
44
- # Convert PIL Image to numpy array
45
  width, height = img.size
46
  if width != height:
47
  raise ValueError(f"Input image must be square, got {width}x{height}")
48
-
49
- img_array = np.array(img)
50
- duplicated = np.concatenate([img_array, img_array], axis=1)
51
- return Image.fromarray(duplicated)
 
52
 
53
  # Load the mask image
54
  mask = Image.open("mask_square.png")
55
 
56
- def crop_input(image):
57
- cropped_image = square_center_crop(image)
58
- return cropped_image
59
-
60
  @spaces.GPU
61
  def generate(image, prompt_user, progress=gr.Progress(track_tqdm=True)):
62
  prompt_structure = "The two-panel image showcases the logo of a brand, [LEFT] the left panel is showing the logo [RIGHT] the right panel has this logo applied to "
63
  prompt = prompt_structure + prompt_user
64
- print(image)
65
- image = duplicate_horizontally(image)
66
-
 
67
  out = pipe(
68
  prompt=prompt,
69
- image=image,
70
  mask_image=mask,
71
- guidance_scale=3.75,
72
  height=768,
73
  width=1536,
74
  num_inference_steps=28,
@@ -89,34 +78,19 @@ with gr.Blocks() as demo:
89
  with gr.Column():
90
  input_image = gr.Image(
91
  label="Upload Logo Image",
92
- type="pil"
93
- )
94
- cropped_image = gr.Image(
95
- visible=False,
96
- type="pil"
97
  )
98
  prompt_input = gr.Textbox(
99
  label="Where should the logo be applied?",
100
- placeholder="e.g., a coffee cup on a wooden table"
 
101
  )
102
  generate_btn = gr.Button("Generate Application", variant="primary")
103
 
104
  with gr.Column():
105
  output_image = gr.Image(label="Generated Application")
106
  output_side = gr.Image(label="Side by side")
107
-
108
- gr.Examples(
109
- examples=[
110
- ["huggingface.png", "A hat"],
111
- ["awesome.png", "A tattoo on a leg"],
112
- ["dvd_logo.png", "a flower pot"]
113
- ],
114
- inputs=[input_image, prompt_input],
115
- outputs=[output_image, output_side],
116
- fn=generate,
117
- cache_examples="lazy"
118
- )
119
-
120
  with gr.Row():
121
  gr.Markdown("""
122
  ### Instructions:
@@ -129,12 +103,8 @@ with gr.Blocks() as demo:
129
 
130
  # Set up the click event
131
  generate_btn.click(
132
- fn=crop_input,
133
- inputs=[input_image],
134
- outputs=[cropped_image]
135
- ).then(
136
  fn=generate,
137
- inputs=[cropped_image, prompt_input],
138
  outputs=[output_image, output_side]
139
  )
140
 
 
3
  import spaces
4
  from diffusers import FluxInpaintPipeline
5
  from PIL import Image, ImageFile
 
6
 
7
  #ImageFile.LOAD_TRUNCATED_IMAGES = True
8
 
 
17
  weight_name="visual-identity-design.safetensors"
18
  )
19
 
20
+ def square_center_crop(img, target_size=768):
21
  if img.mode in ('RGBA', 'P'):
22
  img = img.convert('RGB')
23
+
24
+ width, height = img.size
 
 
 
 
25
  crop_size = min(width, height)
26
+
 
27
  left = (width - crop_size) // 2
28
  top = (height - crop_size) // 2
29
+ right = left + crop_size
30
+ bottom = top + crop_size
31
+
32
+ img_cropped = img.crop((left, top, right, bottom))
33
+ return img_cropped.resize((target_size, target_size), Image.Resampling.LANCZOS)
 
 
34
 
35
  def duplicate_horizontally(img):
 
36
  width, height = img.size
37
  if width != height:
38
  raise ValueError(f"Input image must be square, got {width}x{height}")
39
+
40
+ new_image = Image.new('RGB', (width * 2, height))
41
+ new_image.paste(img, (0, 0))
42
+ new_image.paste(img, (width, 0))
43
+ return new_image
44
 
45
  # Load the mask image
46
  mask = Image.open("mask_square.png")
47
 
 
 
 
 
48
  @spaces.GPU
49
  def generate(image, prompt_user, progress=gr.Progress(track_tqdm=True)):
50
  prompt_structure = "The two-panel image showcases the logo of a brand, [LEFT] the left panel is showing the logo [RIGHT] the right panel has this logo applied to "
51
  prompt = prompt_structure + prompt_user
52
+
53
+ cropped_image = square_center_crop(image)
54
+ logo_dupli = duplicate_horizontally(cropped_image)
55
+
56
  out = pipe(
57
  prompt=prompt,
58
+ image=logo_dupli,
59
  mask_image=mask,
60
+ guidance_scale=6,
61
  height=768,
62
  width=1536,
63
  num_inference_steps=28,
 
78
  with gr.Column():
79
  input_image = gr.Image(
80
  label="Upload Logo Image",
81
+ type="pil",
82
+ height=384
 
 
 
83
  )
84
  prompt_input = gr.Textbox(
85
  label="Where should the logo be applied?",
86
+ placeholder="e.g., a coffee cup on a wooden table",
87
+ lines=2
88
  )
89
  generate_btn = gr.Button("Generate Application", variant="primary")
90
 
91
  with gr.Column():
92
  output_image = gr.Image(label="Generated Application")
93
  output_side = gr.Image(label="Side by side")
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  with gr.Row():
95
  gr.Markdown("""
96
  ### Instructions:
 
103
 
104
  # Set up the click event
105
  generate_btn.click(
 
 
 
 
106
  fn=generate,
107
+ inputs=[input_image, prompt_input],
108
  outputs=[output_image, output_side]
109
  )
110