multimodalart HF staff commited on
Commit
daa5f41
1 Parent(s): 16553e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -43
app.py CHANGED
@@ -72,51 +72,111 @@ def generate(image, prompt_description, prompt_user, progress=gr.Progress(track_
72
 
73
  with gr.Blocks() as demo:
74
  gr.Markdown("# Logo in Context")
75
- gr.Markdown("### In-Context LoRA + Image-to-Image, apply your logo to anything")
76
-
77
- with gr.Row():
78
- with gr.Column():
79
- input_image = gr.Image(
80
- label="Upload Logo Image",
81
- type="pil",
82
- height=384
83
- )
84
- prompt_description = gr.Textbox(
85
- label="Describe your logo",
86
- placeholder="A Hugging Face emoji logo",
87
- )
88
- prompt_input = gr.Textbox(
89
- label="Where should the logo be applied?",
90
- placeholder="e.g., a coffee cup on a wooden table"
91
- )
92
- generate_btn = gr.Button("Generate Application", variant="primary")
93
-
94
- with gr.Column():
95
- output_image = gr.Image(label="Generated Application")
96
- output_side = gr.Image(label="Side by side")
97
 
98
- gr.Examples(
99
- examples=[
100
- ["huggingface.png", "A Hugging Face emoji logo", "An embroidered hat"],
101
- ["awesome.png", "An awesome face logo", "A tattoo on a leg"],
102
- ["dvd_logo.png", "A DVD logo", "a flower pot"]
103
- ],
104
- inputs=[input_image, prompt_description, prompt_input],
105
- outputs=[output_image, output_side],
106
- fn=generate,
107
- cache_examples="lazy"
108
- )
 
 
 
 
 
 
 
 
 
 
 
109
 
110
- with gr.Row():
111
- gr.Markdown("""
112
- ### Instructions:
113
- 1. Upload a logo image (preferably square)
114
- 2. Describe where you'd like to see the logo applied
115
- 3. Click 'Generate Application' and wait for the result
116
-
117
- Note: The generation process might take a few moments.
118
- """)
119
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  # Set up the click event
121
  generate_btn.click(
122
  fn=generate,
 
72
 
73
  with gr.Blocks() as demo:
74
  gr.Markdown("# Logo in Context")
75
+ gr.Markdown("### In-Context LoRA + Image-to-Image + Inpainting, apply your logo to anything. diffusers implementation based on the [workflow by WizardWhitebeard/klinter](https://civitai.com/articles/8779)")
76
+
77
+ with gr.Tab("Demo"):
78
+ with gr.Row():
79
+ with gr.Column():
80
+ input_image = gr.Image(
81
+ label="Upload Logo Image",
82
+ type="pil",
83
+ height=384
84
+ )
85
+ prompt_description = gr.Textbox(
86
+ label="Describe your logo",
87
+ placeholder="A Hugging Face emoji logo",
88
+ )
89
+ prompt_input = gr.Textbox(
90
+ label="Where should the logo be applied?",
91
+ placeholder="e.g., a coffee cup on a wooden table"
92
+ )
93
+ generate_btn = gr.Button("Generate Application", variant="primary")
 
 
 
94
 
95
+ with gr.Column():
96
+ output_image = gr.Image(label="Generated Application")
97
+ output_side = gr.Image(label="Side by side")
98
+
99
+ gr.Examples(
100
+ examples=[
101
+ ["huggingface.png", "A Hugging Face emoji logo", "An embroidered hat"],
102
+ ["awesome.png", "An awesome face logo", "A tattoo on a leg"],
103
+ ["dvd_logo.png", "A DVD logo", "a flower pot"]
104
+ ],
105
+ inputs=[input_image, prompt_description, prompt_input],
106
+ outputs=[output_image, output_side],
107
+ fn=generate,
108
+ cache_examples="lazy"
109
+ )
110
+
111
+ with gr.Row():
112
+ gr.Markdown("""
113
+ ### Instructions:
114
+ 1. Upload a logo image (preferably square)
115
+ 2. Describe where you'd like to see the logo applied
116
+ 3. Click 'Generate Application' and wait for the result
117
 
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
181
  generate_btn.click(
182
  fn=generate,