multimodalart HF staff commited on
Commit
70db990
1 Parent(s): daa5f41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -54
app.py CHANGED
@@ -118,63 +118,62 @@ with gr.Blocks() as demo:
118
  Note: The generation process might take a few moments.
119
  """)
120
 
121
- with gr.Tab("🧨 diffusers"):
122
  gr.Markdown("The way this works is combining the [IC LoRA](https://github.com/ali-vilab/In-Context-LoRA) with image-to-image + inpainting. Where the image on the left (the logo) is uploaded by the user, and the image on the right is masked and applied on the product by the LoRA. Based on the [ComfyUI workflow by WizardWhitebeard/klinter](https://civitai.com/articles/8779). Below is a diffusers implementation of the idea")
123
- gr.Code(language="python", value="""
124
- import torch
125
- from diffusers import FluxInpaintPipeline
126
 
127
- pipe = FluxInpaintPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
128
- pipe.to("cuda")
129
- pipe.load_lora_weights("ali-vilab/In-Context-LoRA", weight_name="visual-identity-design.safetensors")
130
-
131
- from PIL import Image
132
-
133
- def square_center_crop(img, target_size=768):
134
- if img.mode in ('RGBA', 'P'):
135
- img = img.convert('RGB')
136
-
137
- width, height = img.size
138
- crop_size = min(width, height)
139
-
140
- left = (width - crop_size) // 2
141
- top = (height - crop_size) // 2
142
- right = left + crop_size
143
- bottom = top + crop_size
144
-
145
- img_cropped = img.crop((left, top, right, bottom))
146
- return img_cropped.resize((target_size, target_size), Image.Resampling.LANCZOS)
147
-
148
- def duplicate_horizontally(img):
149
- width, height = img.size
150
- if width != height:
151
- raise ValueError(f"Input image must be square, got {width}x{height}")
152
-
153
- new_image = Image.new('RGB', (width * 2, height))
154
- new_image.paste(img, (0, 0))
155
- new_image.paste(img, (width, 0))
156
- return new_image
157
 
158
- mask = load_image("mask_square.png")
159
- image = load_image("the_logo.png")
160
- cropped_image = square_center_crop(image)
161
- logo_dupli = duplicate_horizontally(cropped_image)
162
-
163
- 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 "
164
- prompt = prompt_structure + "an coconut, engraved logo on a green coconut"
165
- out = pipe(
166
- prompt=prompt,
167
- image=logo_dupli,
168
- mask_image=mask,
169
- guidance_scale=6,
170
- height=768,
171
- width=1536,
172
- num_inference_steps=28,
173
- max_sequence_length=256,
174
- strength=1
175
- ).images[0]
176
- out
177
- """
 
 
 
 
 
 
178
  )
179
 
180
  # Set up the click event
 
118
  Note: The generation process might take a few moments.
119
  """)
120
 
121
+ with gr.Tab("🧨 diffusers implementation"):
122
  gr.Markdown("The way this works is combining the [IC LoRA](https://github.com/ali-vilab/In-Context-LoRA) with image-to-image + inpainting. Where the image on the left (the logo) is uploaded by the user, and the image on the right is masked and applied on the product by the LoRA. Based on the [ComfyUI workflow by WizardWhitebeard/klinter](https://civitai.com/articles/8779). Below is a diffusers implementation of the idea")
 
 
 
123
 
124
+ gr.Code(language="python", value="""# Support functions
125
+ def square_center_crop(img, target_size=768):
126
+ if img.mode in ('RGBA', 'P'):
127
+ img = img.convert('RGB')
128
+
129
+ width, height = img.size
130
+ crop_size = min(width, height)
131
+
132
+ left = (width - crop_size) // 2
133
+ top = (height - crop_size) // 2
134
+ right = left + crop_size
135
+ bottom = top + crop_size
136
+
137
+ img_cropped = img.crop((left, top, right, bottom))
138
+ return img_cropped.resize((target_size, target_size), Image.Resampling.LANCZOS)
139
+
140
+ def duplicate_horizontally(img):
141
+ width, height = img.size
142
+ if width != height:
143
+ raise ValueError(f"Input image must be square, got {width}x{height}")
144
+
145
+ new_image = Image.new('RGB', (width * 2, height))
146
+ new_image.paste(img, (0, 0))
147
+ new_image.paste(img, (width, 0))
148
+ return new_image"""
149
+ )
 
 
 
 
150
 
151
+ gr.Code(language="python", value="""import torch
152
+ from diffusers import FluxInpaintPipeline
153
+ from PIL import Image
154
+
155
+ pipe = FluxInpaintPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
156
+ pipe.to("cuda")
157
+ pipe.load_lora_weights("ali-vilab/In-Context-LoRA", weight_name="visual-identity-design.safetensors")
158
+
159
+ mask = load_image("mask_square.png")
160
+ image = load_image("the_logo.png")
161
+ cropped_image = square_center_crop(image) #crop the image you upload to square
162
+ logo_dupli = duplicate_horizontally(cropped_image) #duplicate it so the right side can be masked
163
+
164
+ 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 "
165
+ prompt = prompt_structure + "an coconut, engraved logo on a green coconut"
166
+ out = pipe(
167
+ prompt=prompt,
168
+ image=logo_dupli,
169
+ mask_image=mask,
170
+ guidance_scale=6,
171
+ height=768,
172
+ width=1536,
173
+ num_inference_steps=28,
174
+ max_sequence_length=256,
175
+ strength=1
176
+ ).images[0]"""
177
  )
178
 
179
  # Set up the click event