Spaces:
Paused
Paused
Commit
•
70db990
1
Parent(s):
daa5f41
Update app.py
Browse files
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 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
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 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
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
|