vilarin commited on
Commit
37acca7
·
verified ·
1 Parent(s): 6ab5cc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -30
app.py CHANGED
@@ -73,7 +73,73 @@ def inpaintGen(
73
  ).images[0]
74
 
75
  return result, seed
76
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  with gr.Blocks(theme="ocean", title="Flux.1 Fill dev", css=CSS) as demo:
79
  gr.HTML("<h1><center>Flux.1 Fill dev</center></h1>")
@@ -84,36 +150,77 @@ with gr.Blocks(theme="ocean", title="Flux.1 Fill dev", css=CSS) as demo:
84
  </center>
85
  </p>
86
  """)
87
- with gr.Row():
88
- with gr.Column():
89
- imgMask = gr.ImageMask(type="filepath", label="Image", layers=False, height=800)
90
- inpaint_prompt = gr.Textbox(label='Prompts ✏️', placeholder="A hat...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  with gr.Row():
92
- Inpaint_sendBtn = gr.Button(value="Submit", variant='primary')
93
- Inpaint_clearBtn = gr.ClearButton([imgMask, inpaint_prompt], value="Clear")
94
- image_out = gr.Image(type="pil", label="Output", height=960)
95
- with gr.Accordion("Advanced ⚙️", open=False):
96
- guidance = gr.Slider(label="Guidance scale", minimum=1, maximum=50, value=30.0, step=0.1)
97
- num_steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=20, step=1)
98
- seed = gr.Number(label="Seed", value=42, precision=0)
99
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
100
-
101
- gr.on(
102
- triggers = [
103
- inpaint_prompt.submit,
104
- Inpaint_sendBtn.click,
105
- ],
106
- fn = inpaintGen,
107
- inputs = [
108
- imgMask,
109
- inpaint_prompt,
110
- guidance,
111
- num_steps,
112
- seed,
113
- randomize_seed
114
- ],
115
- outputs = [image_out, seed]
116
- )
 
 
 
 
117
 
118
  if __name__ == "__main__":
119
  demo.queue(api_open=False).launch(show_api=False, share=False)
 
73
  ).images[0]
74
 
75
  return result, seed
76
+
77
+
78
+
79
+ @spaces.GPU():
80
+ def outpaintGen(
81
+ img,
82
+ outpaint_prompt: str,
83
+ overlay_top: int,
84
+ overlay_right: int,
85
+ overlay_bottom: int,
86
+ overlay_left: int,
87
+ op_guidance: float,
88
+ op_num_steps: int,
89
+ op_seed: int,
90
+ op_randomize_seed: bool,
91
+ progress=gr.Progress(track_tqdm=True)
92
+ ):
93
+ image = Image.open(img)
94
+
95
+ # Convert input to PIL Image if it's a numpy array
96
+ if isinstance(image, np.ndarray):
97
+ image = Image.fromarray(image)
98
+
99
+ # Get original dimensions
100
+ original_width, original_height = image.size
101
+
102
+ # Calculate new dimensions
103
+ new_width = original_width + overlap_left + overlap_right
104
+ new_height = original_height + overlap_top + overlap_bottom
105
+
106
+ # Create new blank mask image (black background)
107
+ mask_image = Image.new('RGB', (new_width, new_height), color='black')
108
+
109
+ # Create white rectangle for original image area
110
+ white_area = Image.new('RGB', (original_width, original_height), color='white')
111
+
112
+ # Paste white rectangle at the appropriate position
113
+ mask_image.paste(white_area, (overlap_left, overlap_top))
114
+
115
+ # Convert to grayscale
116
+ mask_image = mask_image.convert('L')
117
+
118
+ fix_width = (new_width // 16) * 16
119
+ fix_height = (new_height // 16) * 16
120
+
121
+ # If the image size is not already divisible by 16, resize it
122
+ if new_width != fix_width or new_height != fix_height:
123
+ mask_image = mask_image.resize((fix_width, fix_height), Image.LANCZOS)
124
+
125
+ if randomize_seed:
126
+ seed = random.randint(0, MAX_SEED)
127
+ generator = torch.Generator("cpu").manual_seed(seed)
128
+
129
+ result = pipe(
130
+ prompt=inpaint_prompt,
131
+ image=image,
132
+ mask_image=mask_image,
133
+ width=fix_width,
134
+ height=fix_height,
135
+ num_inference_steps=num_steps,
136
+ generator=generator,
137
+ guidance_scale=guidance,
138
+ max_sequence_length=512,
139
+ ).images[0]
140
+
141
+ return result, seed
142
+
143
 
144
  with gr.Blocks(theme="ocean", title="Flux.1 Fill dev", css=CSS) as demo:
145
  gr.HTML("<h1><center>Flux.1 Fill dev</center></h1>")
 
150
  </center>
151
  </p>
152
  """)
153
+ with gr.Tab("Inpainting"):
154
+ with gr.Row():
155
+ with gr.Column():
156
+ imgMask = gr.ImageMask(type="filepath", label="Image", layers=False, height=800)
157
+ inpaint_prompt = gr.Textbox(label='Prompts ✏️', placeholder="A hat...")
158
+ with gr.Row():
159
+ Inpaint_sendBtn = gr.Button(value="Submit", variant='primary')
160
+ Inpaint_clearBtn = gr.ClearButton([imgMask, inpaint_prompt], value="Clear")
161
+ image_out = gr.Image(type="pil", label="Output", height=960)
162
+ with gr.Accordion("Advanced ⚙️", open=False):
163
+ guidance = gr.Slider(label="Guidance scale", minimum=1, maximum=50, value=30.0, step=0.1)
164
+ num_steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=20, step=1)
165
+ seed = gr.Number(label="Seed", value=42, precision=0)
166
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
167
+
168
+ gr.on(
169
+ triggers = [
170
+ inpaint_prompt.submit,
171
+ Inpaint_sendBtn.click,
172
+ ],
173
+ fn = inpaintGen,
174
+ inputs = [
175
+ imgMask,
176
+ inpaint_prompt,
177
+ guidance,
178
+ num_steps,
179
+ seed,
180
+ randomize_seed
181
+ ],
182
+ outputs = [image_out, seed]
183
+ )
184
+ with gr.Tab("Outpainting"):
185
+ with gr.Row():
186
+ with gr.Column():
187
+ img = gr.Image(type="filepath", label="Image", height=800)
188
+ outpaint_prompt = gr.Textbox(label='Prompts ✏️', placeholder="In city...")
189
+ with gr.Row():
190
+ outpaint_sendBtn = gr.Button(value="Submit", variant='primary')
191
+ outpaint_clearBtn = gr.ClearButton([img, outpaint_prompt], value="Clear")
192
+ image_exp = gr.Image(type="pil", label="Output", height=960)
193
+ with gr.Accordion("Advanced ⚙️", open=False):
194
  with gr.Row():
195
+ overlay_top = gr.Number(label="Top", value=60, precision=0)
196
+ overlay_right = gr.Number(label="Right", value=60, precision=0)
197
+ overlay_bottom = gr.Number(label="Bottom", value=60, precision=0)
198
+ overlay_left = gr.Number(label="Left", value=60, precision=0)
199
+ op_guidance = gr.Slider(label="Guidance scale", minimum=1, maximum=50, value=30.0, step=0.1)
200
+ op_num_steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=20, step=1)
201
+ op_seed = gr.Number(label="Seed", value=42, precision=0)
202
+ op_randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
203
+
204
+ gr.on(
205
+ triggers = [
206
+ outpaint_prompt.submit,
207
+ outpaint_sendBtn.click,
208
+ ],
209
+ fn = outpaintGen,
210
+ inputs = [
211
+ img,
212
+ outpaint_prompt,
213
+ overlay_top,
214
+ overlay_right,
215
+ overlay_bottom,
216
+ overlay_left,
217
+ op_guidance,
218
+ op_num_steps,
219
+ op_seed,
220
+ op_randomize_seed
221
+ ],
222
+ outputs = [image_exp, op_seed]
223
+ )
224
 
225
  if __name__ == "__main__":
226
  demo.queue(api_open=False).launch(show_api=False, share=False)