Mohuu0601 commited on
Commit
a25e199
·
verified ·
1 Parent(s): 455cba7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +181 -0
app.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import spaces
4
+ from diffusers import FluxInpaintPipeline
5
+ from PIL import Image, ImageFile
6
+
7
+ #ImageFile.LOAD_TRUNCATED_IMAGES = True
8
+
9
+ # Initialize the pipeline
10
+ pipe = FluxInpaintPipeline.from_pretrained(
11
+ "black-forest-labs/FLUX.1-dev",
12
+ torch_dtype=torch.bfloat16
13
+ )
14
+ pipe.to("cuda")
15
+ pipe.load_lora_weights(
16
+ "ali-vilab/In-Context-LoRA",
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
+
46
+ @spaces.GPU
47
+ def generate(image, prompt_description, prompt_user, progress=gr.Progress(track_tqdm=True)):
48
+ prompt_structure = "The two-panel image showcases the logo on the left and the application on the right, [LEFT] the left panel is showing "+prompt_description+" [RIGHT] this logo is applied to "
49
+ prompt = prompt_structure + prompt_user
50
+
51
+ mask = Image.open("mask_square.png")
52
+ cropped_image = square_center_crop(image)
53
+ logo_dupli = duplicate_horizontally(cropped_image)
54
+
55
+ out = pipe(
56
+ prompt=prompt,
57
+ image=logo_dupli,
58
+ mask_image=mask,
59
+ guidance_scale=3.5,
60
+ height=768,
61
+ width=1536,
62
+ num_inference_steps=28,
63
+ max_sequence_length=256,
64
+ strength=1
65
+ ).images[0]
66
+
67
+ width, height = out.size
68
+ half_width = width // 2
69
+ image_2 = out.crop((half_width, 0, width, height))
70
+ return image_2, out
71
+
72
+ with gr.Blocks() as demo:
73
+ gr.Markdown("# Logo in Context")
74
+ gr.Markdown("### [In-Context LoRA](https://huggingface.co/ali-vilab/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)")
75
+
76
+ with gr.Tab("Demo"):
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 coconut, engraved logo on a green coconut"]
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
+ with gr.Tab("🧨 diffusers implementation"):
121
+ 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")
122
+
123
+ gr.Code(language="python", value="""# Support functions
124
+ def square_center_crop(img, target_size=768):
125
+ if img.mode in ('RGBA', 'P'):
126
+ img = img.convert('RGB')
127
+
128
+ width, height = img.size
129
+ crop_size = min(width, height)
130
+
131
+ left = (width - crop_size) // 2
132
+ top = (height - crop_size) // 2
133
+ right = left + crop_size
134
+ bottom = top + crop_size
135
+
136
+ img_cropped = img.crop((left, top, right, bottom))
137
+ return img_cropped.resize((target_size, target_size), Image.Resampling.LANCZOS)
138
+ def duplicate_horizontally(img):
139
+ width, height = img.size
140
+ if width != height:
141
+ raise ValueError(f"Input image must be square, got {width}x{height}")
142
+
143
+ new_image = Image.new('RGB', (width * 2, height))
144
+ new_image.paste(img, (0, 0))
145
+ new_image.paste(img, (width, 0))
146
+ return new_image"""
147
+ )
148
+
149
+ gr.Code(language="python", value="""import torch
150
+ from diffusers import FluxInpaintPipeline
151
+ from PIL import Image
152
+ pipe = FluxInpaintPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
153
+ pipe.to("cuda")
154
+ pipe.load_lora_weights("ali-vilab/In-Context-LoRA", weight_name="visual-identity-design.safetensors")
155
+ mask = load_image("mask_square.png")
156
+ image = load_image("the_logo.png")
157
+ cropped_image = square_center_crop(image) #crop the image you upload to square
158
+ logo_dupli = duplicate_horizontally(cropped_image) #duplicate it so the right side can be masked
159
+ 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 "
160
+ prompt = prompt_structure + "an coconut, engraved logo on a green coconut"
161
+ out = pipe(
162
+ prompt=prompt,
163
+ image=logo_dupli,
164
+ mask_image=mask,
165
+ guidance_scale=6,
166
+ height=768,
167
+ width=1536,
168
+ num_inference_steps=28,
169
+ max_sequence_length=256,
170
+ strength=1
171
+ ).images[0]"""
172
+ )
173
+
174
+ # Set up the click event
175
+ generate_btn.click(
176
+ fn=generate,
177
+ inputs=[input_image, prompt_description, prompt_input],
178
+ outputs=[output_image, output_side]
179
+ )
180
+
181
+ demo.launch()