Spaces:
Running
on
Zero
Running
on
Zero
Himanshu-AT
commited on
Commit
·
863ffef
1
Parent(s):
69d628b
revert to previous
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import spaces
|
|
|
2 |
import gradio as gr
|
3 |
import numpy as np
|
4 |
import os
|
@@ -9,20 +10,18 @@ import torch
|
|
9 |
from torchvision import transforms
|
10 |
import zipfile
|
11 |
|
12 |
-
from diffusers import FluxFillPipeline, AutoencoderKL
|
13 |
-
from diffusers.models.attention_processor import AttnProcessor2_0
|
14 |
from PIL import Image
|
|
|
15 |
|
16 |
MAX_SEED = np.iinfo(np.int32).max
|
17 |
MAX_IMAGE_SIZE = 2048
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
|
22 |
-
|
23 |
-
pipe.enable_xformers_memory_efficient_attention()
|
24 |
|
25 |
-
# Load LoRA models from JSON
|
26 |
with open("lora_models.json", "r") as f:
|
27 |
lora_models = json.load(f)
|
28 |
|
@@ -40,7 +39,7 @@ for model_name, model_path in lora_models.items():
|
|
40 |
|
41 |
lora_models["None"] = None
|
42 |
|
43 |
-
def calculate_optimal_dimensions(image: Image.Image
|
44 |
# Extract the original dimensions
|
45 |
original_width, original_height = image.size
|
46 |
|
@@ -60,10 +59,6 @@ def calculate_optimal_dimensions(image: Image.Image, scale_factor=1.0):
|
|
60 |
height = FIXED_DIMENSION
|
61 |
width = round(FIXED_DIMENSION * original_aspect_ratio)
|
62 |
|
63 |
-
# Apply scaling factor
|
64 |
-
width = round(width * scale_factor)
|
65 |
-
height = round(height * scale_factor)
|
66 |
-
|
67 |
# Ensure dimensions are multiples of 8
|
68 |
width = (width // 8) * 8
|
69 |
height = (height // 8) * 8
|
@@ -76,116 +71,21 @@ def calculate_optimal_dimensions(image: Image.Image, scale_factor=1.0):
|
|
76 |
height = (width / MIN_ASPECT_RATIO // 8) * 8
|
77 |
|
78 |
# Ensure width and height remain above the minimum dimensions
|
79 |
-
width = max(width, 576)
|
80 |
-
height = max(height, 576)
|
81 |
-
|
82 |
-
# Ensure dimensions don't exceed maximum
|
83 |
-
width = min(width, MAX_IMAGE_SIZE)
|
84 |
-
height = min(height, MAX_IMAGE_SIZE)
|
85 |
|
86 |
return width, height
|
87 |
|
88 |
-
def preprocess_mask(mask, blur_radius=10):
|
89 |
-
"""Apply blurring to create a soft mask for smoother transitions"""
|
90 |
-
from PIL import ImageFilter
|
91 |
-
if mask:
|
92 |
-
# Apply Gaussian blur to soften the mask edges
|
93 |
-
blurred_mask = mask.filter(ImageFilter.GaussianBlur(radius=blur_radius))
|
94 |
-
return blurred_mask
|
95 |
-
return mask
|
96 |
-
|
97 |
-
def enhance_detail(img, sharpness_factor=1.5):
|
98 |
-
"""Enhance the details in the generated image"""
|
99 |
-
from PIL import ImageEnhance
|
100 |
-
if img:
|
101 |
-
enhancer = ImageEnhance.Sharpness(img)
|
102 |
-
enhanced_img = enhancer.enhance(sharpness_factor)
|
103 |
-
return enhanced_img
|
104 |
-
return img
|
105 |
-
|
106 |
-
def layer_based_inference(pipe, image, mask, prompt,
|
107 |
-
structure_guidance_scale, texture_guidance_scale,
|
108 |
-
structure_steps, texture_steps, strength, seed,
|
109 |
-
lora_scale, width, height):
|
110 |
-
"""Perform a two-stage layer-based diffusion process for better quality"""
|
111 |
-
# Create generators with the same seed for reproducibility
|
112 |
-
structure_generator = torch.Generator(device='cuda').manual_seed(seed)
|
113 |
-
texture_generator = torch.Generator(device='cuda').manual_seed(seed+1)
|
114 |
-
|
115 |
-
# Configure structure pipeline (focus on shapes and composition)
|
116 |
-
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
117 |
-
|
118 |
-
# Stage 1: Generate the overall structure with more steps but lower guidance
|
119 |
-
structure_kwargs = {
|
120 |
-
"prompt": f"structure of {prompt}",
|
121 |
-
"image": image,
|
122 |
-
"mask_image": mask,
|
123 |
-
"height": height,
|
124 |
-
"width": width,
|
125 |
-
"guidance_scale": structure_guidance_scale,
|
126 |
-
"strength": strength * 0.9, # Less strength to preserve original structure
|
127 |
-
"num_inference_steps": structure_steps,
|
128 |
-
"generator": structure_generator,
|
129 |
-
}
|
130 |
-
|
131 |
-
# Add LoRA scale if supported
|
132 |
-
if pipe.is_lora_enabled():
|
133 |
-
structure_kwargs["cross_attention_kwargs"] = {"scale": lora_scale * 0.8} # Reduce LoRA impact for structure
|
134 |
-
|
135 |
-
# Generate the structural base
|
136 |
-
try:
|
137 |
-
intermediate_result = pipe(**structure_kwargs).images[0]
|
138 |
-
except Exception as e:
|
139 |
-
print(f"Structure generation error: {str(e)}")
|
140 |
-
return None
|
141 |
-
|
142 |
-
# Configure texture pipeline (focus on details and textures)
|
143 |
-
pipe.scheduler = DDPMScheduler.from_config(pipe.scheduler.config)
|
144 |
-
|
145 |
-
# Stage 2: Refine with texture details using the intermediate result
|
146 |
-
texture_kwargs = {
|
147 |
-
"prompt": f"detailed texture of {prompt}",
|
148 |
-
"image": intermediate_result,
|
149 |
-
"mask_image": mask,
|
150 |
-
"height": height,
|
151 |
-
"width": width,
|
152 |
-
"guidance_scale": texture_guidance_scale,
|
153 |
-
"strength": strength * 0.6, # Lower strength to maintain structure
|
154 |
-
"num_inference_steps": texture_steps,
|
155 |
-
"generator": texture_generator,
|
156 |
-
}
|
157 |
-
|
158 |
-
# Add LoRA scale if supported
|
159 |
-
if pipe.is_lora_enabled():
|
160 |
-
texture_kwargs["cross_attention_kwargs"] = {"scale": lora_scale * 1.2} # Increase LoRA impact for texture
|
161 |
-
|
162 |
-
# Generate the final result with detailed textures
|
163 |
-
try:
|
164 |
-
final_result = pipe(**texture_kwargs).images[0]
|
165 |
-
return final_result
|
166 |
-
except Exception as e:
|
167 |
-
print(f"Texture generation error: {str(e)}")
|
168 |
-
return intermediate_result # Return intermediate result if texture stage fails
|
169 |
-
|
170 |
@spaces.GPU(durations=300)
|
171 |
-
def infer(edit_images, prompt, lora_model, strength, seed=42, randomize_seed=False,
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
structure_guidance=2.5, texture_guidance=5.0,
|
176 |
-
structure_steps=20, texture_steps=15,
|
177 |
-
progress=gr.Progress(track_tqdm=True)):
|
178 |
-
|
179 |
-
gr.Info("Starting inference process")
|
180 |
-
|
181 |
-
# Load and enable LoRA if selected
|
182 |
if lora_model != "None":
|
183 |
pipe.load_lora_weights(lora_models[lora_model])
|
184 |
pipe.enable_lora()
|
185 |
-
else:
|
186 |
-
pipe.disable_lora()
|
187 |
|
188 |
-
gr.Info("
|
189 |
|
190 |
image = edit_images["background"]
|
191 |
mask = edit_images["layers"][0]
|
@@ -194,82 +94,34 @@ def infer(edit_images, prompt, lora_model, strength, seed=42, randomize_seed=Fal
|
|
194 |
gr.Info("Please upload an image.")
|
195 |
return None, None
|
196 |
|
197 |
-
|
198 |
-
width, height = calculate_optimal_dimensions(image
|
199 |
-
|
200 |
if randomize_seed:
|
201 |
seed = random.randint(0, MAX_SEED)
|
202 |
-
|
203 |
-
#
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
lora_scale=lora_scale,
|
223 |
-
width=width,
|
224 |
-
height=height
|
225 |
-
)
|
226 |
-
|
227 |
-
if result_image is None:
|
228 |
-
gr.Error("Layered diffusion failed. Falling back to standard diffusion.")
|
229 |
-
use_layered_diffusion = False
|
230 |
-
|
231 |
-
if not use_layered_diffusion:
|
232 |
-
# Standard diffusion as fallback
|
233 |
-
generator = torch.Generator(device='cuda').manual_seed(seed)
|
234 |
-
|
235 |
-
# Configure pipeline parameters
|
236 |
-
pipeline_kwargs = {
|
237 |
-
"prompt": prompt,
|
238 |
-
"prompt_2": prompt,
|
239 |
-
"image": image,
|
240 |
-
"mask_image": processed_mask,
|
241 |
-
"height": height,
|
242 |
-
"width": width,
|
243 |
-
"guidance_scale": guidance_scale,
|
244 |
-
"strength": strength,
|
245 |
-
"num_inference_steps": num_inference_steps,
|
246 |
-
"generator": generator,
|
247 |
-
}
|
248 |
-
|
249 |
-
# Add LoRA scale if model supports it
|
250 |
-
if lora_model != "None":
|
251 |
-
try:
|
252 |
-
pipeline_kwargs["cross_attention_kwargs"] = {"scale": lora_scale}
|
253 |
-
except:
|
254 |
-
gr.Info("LoRA scale not supported - using default scaling")
|
255 |
-
|
256 |
-
# Run the pipeline
|
257 |
-
try:
|
258 |
-
output = pipe(**pipeline_kwargs)
|
259 |
-
result_image = output.images[0]
|
260 |
-
except Exception as e:
|
261 |
-
gr.Error(f"Error during generation: {str(e)}")
|
262 |
-
return None, seed
|
263 |
-
|
264 |
-
# Enhance details based on user preference
|
265 |
-
if detail_level > 1.0:
|
266 |
-
gr.Info("Enhancing image details")
|
267 |
-
result_image = enhance_detail(result_image, sharpness_factor=detail_level)
|
268 |
-
|
269 |
-
output_image_jpg = result_image.convert("RGB")
|
270 |
output_image_jpg.save("output.jpg", "JPEG")
|
271 |
|
272 |
return output_image_jpg, seed
|
|
|
273 |
|
274 |
def download_image(image):
|
275 |
if isinstance(image, np.ndarray):
|
@@ -277,10 +129,7 @@ def download_image(image):
|
|
277 |
image.save("output.png", "PNG")
|
278 |
return "output.png"
|
279 |
|
280 |
-
def save_details(result, edit_image, prompt, lora_model, strength, seed, guidance_scale,
|
281 |
-
num_inference_steps, lora_scale, scale_factor,
|
282 |
-
use_layered_diffusion, blur_mask, detail_level,
|
283 |
-
structure_guidance, texture_guidance, structure_steps, texture_steps):
|
284 |
image = edit_image["background"]
|
285 |
mask = edit_image["layers"][0]
|
286 |
|
@@ -298,21 +147,10 @@ def save_details(result, edit_image, prompt, lora_model, strength, seed, guidanc
|
|
298 |
details = {
|
299 |
"prompt": prompt,
|
300 |
"lora_model": lora_model,
|
301 |
-
"lora_scale": lora_scale,
|
302 |
"strength": strength,
|
303 |
"seed": seed,
|
304 |
"guidance_scale": guidance_scale,
|
305 |
-
"num_inference_steps": num_inference_steps
|
306 |
-
"scale_factor": scale_factor,
|
307 |
-
"width": result.width,
|
308 |
-
"height": result.height,
|
309 |
-
"use_layered_diffusion": use_layered_diffusion,
|
310 |
-
"blur_mask": blur_mask,
|
311 |
-
"detail_level": detail_level,
|
312 |
-
"structure_guidance": structure_guidance,
|
313 |
-
"texture_guidance": texture_guidance,
|
314 |
-
"structure_steps": structure_steps,
|
315 |
-
"texture_steps": texture_steps
|
316 |
}
|
317 |
|
318 |
with open("details.json", "w") as f:
|
@@ -330,11 +168,16 @@ def save_details(result, edit_image, prompt, lora_model, strength, seed, guidanc
|
|
330 |
def set_image_as_inpaint(image):
|
331 |
return image
|
332 |
|
333 |
-
def
|
334 |
-
|
|
|
|
|
335 |
|
336 |
examples = [
|
337 |
-
"photography of a young woman,
|
|
|
|
|
|
|
338 |
]
|
339 |
|
340 |
css="""
|
@@ -342,20 +185,12 @@ css="""
|
|
342 |
margin: 0 auto;
|
343 |
max-width: 1000px;
|
344 |
}
|
345 |
-
.layer-settings {
|
346 |
-
border: 1px solid #ccc;
|
347 |
-
padding: 10px;
|
348 |
-
border-radius: 8px;
|
349 |
-
background-color: #f9f9f9;
|
350 |
-
margin-top: 10px;
|
351 |
-
}
|
352 |
"""
|
353 |
|
354 |
with gr.Blocks(css=css) as demo:
|
355 |
|
356 |
with gr.Column(elem_id="col-container"):
|
357 |
-
gr.Markdown(f"""# FLUX.1 [dev]
|
358 |
-
*Now with layered diffusion for improved quality and control*
|
359 |
""")
|
360 |
with gr.Row():
|
361 |
with gr.Column():
|
@@ -366,13 +201,14 @@ with gr.Blocks(css=css) as demo:
|
|
366 |
image_mode='RGB',
|
367 |
layers=False,
|
368 |
brush=gr.Brush(colors=["#FFFFFF"]),
|
|
|
369 |
)
|
370 |
prompt = gr.Text(
|
371 |
label="Prompt",
|
372 |
-
show_label=
|
373 |
max_lines=2,
|
374 |
placeholder="Enter your prompt",
|
375 |
-
container=
|
376 |
)
|
377 |
|
378 |
lora_model = gr.Dropdown(
|
@@ -385,148 +221,67 @@ with gr.Blocks(css=css) as demo:
|
|
385 |
|
386 |
result = gr.Image(label="Result", show_label=False)
|
387 |
|
388 |
-
with gr.Accordion("Basic Settings", open=True):
|
389 |
-
with gr.Row():
|
390 |
-
strength = gr.Slider(
|
391 |
-
label="Strength",
|
392 |
-
minimum=0,
|
393 |
-
maximum=1,
|
394 |
-
step=0.01,
|
395 |
-
value=0.85,
|
396 |
-
info="Controls how much to modify the original image"
|
397 |
-
)
|
398 |
-
|
399 |
-
lora_scale = gr.Slider(
|
400 |
-
label="LoRA Scale",
|
401 |
-
minimum=0,
|
402 |
-
maximum=2,
|
403 |
-
step=0.05,
|
404 |
-
value=0.75,
|
405 |
-
info="Controls the influence of the LoRA model"
|
406 |
-
)
|
407 |
-
|
408 |
-
with gr.Row():
|
409 |
-
scale_factor = gr.Slider(
|
410 |
-
label="Image Scale Factor",
|
411 |
-
minimum=0.5,
|
412 |
-
maximum=2.0,
|
413 |
-
step=0.1,
|
414 |
-
value=1.0,
|
415 |
-
info="Scale factor for image dimensions (1.0 = original, 2.0 = double size)"
|
416 |
-
)
|
417 |
-
|
418 |
-
use_layered_diffusion = gr.Checkbox(
|
419 |
-
label="Use Layered Diffusion",
|
420 |
-
value=True,
|
421 |
-
info="Process in structure and texture layers for better quality"
|
422 |
-
)
|
423 |
-
|
424 |
with gr.Accordion("Advanced Settings", open=False):
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
|
|
434 |
|
435 |
with gr.Row():
|
|
|
436 |
guidance_scale = gr.Slider(
|
437 |
-
label="Guidance Scale
|
438 |
minimum=1,
|
439 |
maximum=30,
|
440 |
step=0.5,
|
441 |
-
value=
|
442 |
)
|
443 |
|
444 |
num_inference_steps = gr.Slider(
|
445 |
-
label="
|
446 |
minimum=1,
|
447 |
maximum=50,
|
448 |
step=1,
|
449 |
value=28,
|
450 |
)
|
451 |
-
|
452 |
with gr.Row():
|
453 |
-
|
454 |
-
|
|
|
455 |
minimum=0,
|
456 |
-
maximum=
|
457 |
-
step=
|
458 |
-
value=
|
459 |
-
info="Higher values create smoother transitions at mask boundaries"
|
460 |
-
)
|
461 |
-
|
462 |
-
detail_level = gr.Slider(
|
463 |
-
label="Detail Enhancement",
|
464 |
-
minimum=0.5,
|
465 |
-
maximum=2.0,
|
466 |
-
step=0.1,
|
467 |
-
value=1.2,
|
468 |
-
info="Controls the sharpness of the final image"
|
469 |
)
|
470 |
-
|
471 |
-
#
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
label="Texture Guidance Scale",
|
487 |
-
minimum=1.0,
|
488 |
-
maximum=10.0,
|
489 |
-
step=0.1,
|
490 |
-
value=5.0,
|
491 |
-
info="Controls adherence to prompt during texture refinement (higher = more detailed)"
|
492 |
-
)
|
493 |
-
|
494 |
-
with gr.Row():
|
495 |
-
structure_steps = gr.Slider(
|
496 |
-
label="Structure Steps",
|
497 |
-
minimum=10,
|
498 |
-
maximum=40,
|
499 |
-
step=1,
|
500 |
-
value=20,
|
501 |
-
info="Inference steps for structure generation"
|
502 |
-
)
|
503 |
-
|
504 |
-
texture_steps = gr.Slider(
|
505 |
-
label="Texture Steps",
|
506 |
-
minimum=10,
|
507 |
-
maximum=40,
|
508 |
-
step=1,
|
509 |
-
value=15,
|
510 |
-
info="Inference steps for texture refinement"
|
511 |
-
)
|
512 |
-
|
513 |
-
# Toggle visibility of layered settings based on checkbox
|
514 |
-
use_layered_diffusion.change(
|
515 |
-
fn=toggle_layered_diffusion,
|
516 |
-
inputs=[use_layered_diffusion],
|
517 |
-
outputs=[layer_settings]
|
518 |
-
)
|
519 |
|
520 |
gr.on(
|
521 |
triggers=[run_button.click, prompt.submit],
|
522 |
-
fn=infer,
|
523 |
-
inputs=[
|
524 |
-
|
525 |
-
guidance_scale, num_inference_steps, lora_scale, scale_factor,
|
526 |
-
use_layered_diffusion, blur_mask, detail_level,
|
527 |
-
structure_guidance, texture_guidance, structure_steps, texture_steps
|
528 |
-
],
|
529 |
-
outputs=[result, seed]
|
530 |
)
|
531 |
|
532 |
download_button = gr.Button("Download Image as PNG")
|
@@ -547,25 +302,26 @@ with gr.Blocks(css=css) as demo:
|
|
547 |
|
548 |
save_button.click(
|
549 |
fn=save_details,
|
550 |
-
inputs=[
|
551 |
-
result, edit_image, prompt, lora_model, strength, seed, guidance_scale,
|
552 |
-
num_inference_steps, lora_scale, scale_factor,
|
553 |
-
use_layered_diffusion, blur_mask, detail_level,
|
554 |
-
structure_guidance, texture_guidance, structure_steps, texture_steps
|
555 |
-
],
|
556 |
outputs=gr.File(label="Download/Save Status")
|
557 |
)
|
558 |
|
559 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
560 |
PASSWORD = os.getenv("GRADIO_PASSWORD")
|
561 |
USERNAME = os.getenv("GRADIO_USERNAME")
|
562 |
-
|
563 |
-
# Create an authentication function
|
564 |
def authenticate(username, password):
|
565 |
if username == USERNAME and password == PASSWORD:
|
566 |
return True
|
|
|
567 |
else:
|
568 |
return False
|
569 |
-
|
570 |
# Launch the app with authentication
|
|
|
571 |
demo.launch(debug=True, auth=authenticate)
|
|
|
1 |
import spaces
|
2 |
+
|
3 |
import gradio as gr
|
4 |
import numpy as np
|
5 |
import os
|
|
|
10 |
from torchvision import transforms
|
11 |
import zipfile
|
12 |
|
13 |
+
from diffusers import FluxFillPipeline, AutoencoderKL
|
|
|
14 |
from PIL import Image
|
15 |
+
# from samgeo.text_sam import LangSAM
|
16 |
|
17 |
MAX_SEED = np.iinfo(np.int32).max
|
18 |
MAX_IMAGE_SIZE = 2048
|
19 |
|
20 |
+
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
21 |
+
# sam = LangSAM(model_type="sam2-hiera-large").to(device)
|
22 |
|
23 |
+
pipe = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16).to("cuda")
|
|
|
24 |
|
|
|
25 |
with open("lora_models.json", "r") as f:
|
26 |
lora_models = json.load(f)
|
27 |
|
|
|
39 |
|
40 |
lora_models["None"] = None
|
41 |
|
42 |
+
def calculate_optimal_dimensions(image: Image.Image):
|
43 |
# Extract the original dimensions
|
44 |
original_width, original_height = image.size
|
45 |
|
|
|
59 |
height = FIXED_DIMENSION
|
60 |
width = round(FIXED_DIMENSION * original_aspect_ratio)
|
61 |
|
|
|
|
|
|
|
|
|
62 |
# Ensure dimensions are multiples of 8
|
63 |
width = (width // 8) * 8
|
64 |
height = (height // 8) * 8
|
|
|
71 |
height = (width / MIN_ASPECT_RATIO // 8) * 8
|
72 |
|
73 |
# Ensure width and height remain above the minimum dimensions
|
74 |
+
width = max(width, 576) if width == FIXED_DIMENSION else width
|
75 |
+
height = max(height, 576) if height == FIXED_DIMENSION else height
|
|
|
|
|
|
|
|
|
76 |
|
77 |
return width, height
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
@spaces.GPU(durations=300)
|
80 |
+
def infer(edit_images, prompt, lora_model, strength, seed=42, randomize_seed=False, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
81 |
+
# pipe.enable_xformers_memory_efficient_attention()
|
82 |
+
gr.Info("Infering")
|
83 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
if lora_model != "None":
|
85 |
pipe.load_lora_weights(lora_models[lora_model])
|
86 |
pipe.enable_lora()
|
|
|
|
|
87 |
|
88 |
+
gr.Info("starting checks")
|
89 |
|
90 |
image = edit_images["background"]
|
91 |
mask = edit_images["layers"][0]
|
|
|
94 |
gr.Info("Please upload an image.")
|
95 |
return None, None
|
96 |
|
97 |
+
|
98 |
+
width, height = calculate_optimal_dimensions(image)
|
|
|
99 |
if randomize_seed:
|
100 |
seed = random.randint(0, MAX_SEED)
|
101 |
+
|
102 |
+
# controlImage = processor(image)
|
103 |
+
gr.Info("generating image")
|
104 |
+
image = pipe(
|
105 |
+
# mask_image_latent=vae.encode(controlImage),
|
106 |
+
prompt=prompt,
|
107 |
+
prompt_2=prompt,
|
108 |
+
image=image,
|
109 |
+
mask_image=mask,
|
110 |
+
height=height,
|
111 |
+
width=width,
|
112 |
+
guidance_scale=guidance_scale,
|
113 |
+
# strength=strength,
|
114 |
+
num_inference_steps=num_inference_steps,
|
115 |
+
generator=torch.Generator(device='cuda').manual_seed(seed),
|
116 |
+
# generator=torch.Generator().manual_seed(seed),
|
117 |
+
# lora_scale=0.75 // not supported in this version
|
118 |
+
).images[0]
|
119 |
+
|
120 |
+
output_image_jpg = image.convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
output_image_jpg.save("output.jpg", "JPEG")
|
122 |
|
123 |
return output_image_jpg, seed
|
124 |
+
# return image, seed
|
125 |
|
126 |
def download_image(image):
|
127 |
if isinstance(image, np.ndarray):
|
|
|
129 |
image.save("output.png", "PNG")
|
130 |
return "output.png"
|
131 |
|
132 |
+
def save_details(result, edit_image, prompt, lora_model, strength, seed, guidance_scale, num_inference_steps):
|
|
|
|
|
|
|
133 |
image = edit_image["background"]
|
134 |
mask = edit_image["layers"][0]
|
135 |
|
|
|
147 |
details = {
|
148 |
"prompt": prompt,
|
149 |
"lora_model": lora_model,
|
|
|
150 |
"strength": strength,
|
151 |
"seed": seed,
|
152 |
"guidance_scale": guidance_scale,
|
153 |
+
"num_inference_steps": num_inference_steps
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
}
|
155 |
|
156 |
with open("details.json", "w") as f:
|
|
|
168 |
def set_image_as_inpaint(image):
|
169 |
return image
|
170 |
|
171 |
+
# def generate_mask(image, click_x, click_y):
|
172 |
+
# text_prompt = "face"
|
173 |
+
# mask = sam.predict(image, text_prompt, box_threshold=0.24, text_threshold=0.24)
|
174 |
+
# return mask
|
175 |
|
176 |
examples = [
|
177 |
+
"photography of a young woman, accent lighting, (front view:1.4), "
|
178 |
+
# "a tiny astronaut hatching from an egg on the moon",
|
179 |
+
# "a cat holding a sign that says hello world",
|
180 |
+
# "an anime illustration of a wiener schnitzel",
|
181 |
]
|
182 |
|
183 |
css="""
|
|
|
185 |
margin: 0 auto;
|
186 |
max-width: 1000px;
|
187 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
"""
|
189 |
|
190 |
with gr.Blocks(css=css) as demo:
|
191 |
|
192 |
with gr.Column(elem_id="col-container"):
|
193 |
+
gr.Markdown(f"""# FLUX.1 [dev]
|
|
|
194 |
""")
|
195 |
with gr.Row():
|
196 |
with gr.Column():
|
|
|
201 |
image_mode='RGB',
|
202 |
layers=False,
|
203 |
brush=gr.Brush(colors=["#FFFFFF"]),
|
204 |
+
# height=600
|
205 |
)
|
206 |
prompt = gr.Text(
|
207 |
label="Prompt",
|
208 |
+
show_label=False,
|
209 |
max_lines=2,
|
210 |
placeholder="Enter your prompt",
|
211 |
+
container=False,
|
212 |
)
|
213 |
|
214 |
lora_model = gr.Dropdown(
|
|
|
221 |
|
222 |
result = gr.Image(label="Result", show_label=False)
|
223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
with gr.Accordion("Advanced Settings", open=False):
|
225 |
+
|
226 |
+
seed = gr.Slider(
|
227 |
+
label="Seed",
|
228 |
+
minimum=0,
|
229 |
+
maximum=MAX_SEED,
|
230 |
+
step=1,
|
231 |
+
value=0,
|
232 |
+
)
|
233 |
+
|
234 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
235 |
|
236 |
with gr.Row():
|
237 |
+
|
238 |
guidance_scale = gr.Slider(
|
239 |
+
label="Guidance Scale",
|
240 |
minimum=1,
|
241 |
maximum=30,
|
242 |
step=0.5,
|
243 |
+
value=50,
|
244 |
)
|
245 |
|
246 |
num_inference_steps = gr.Slider(
|
247 |
+
label="Number of inference steps",
|
248 |
minimum=1,
|
249 |
maximum=50,
|
250 |
step=1,
|
251 |
value=28,
|
252 |
)
|
253 |
+
|
254 |
with gr.Row():
|
255 |
+
|
256 |
+
strength = gr.Slider(
|
257 |
+
label="Strength",
|
258 |
minimum=0,
|
259 |
+
maximum=1,
|
260 |
+
step=0.01,
|
261 |
+
value=0.85,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
)
|
263 |
+
|
264 |
+
# width = gr.Slider(
|
265 |
+
# label="width",
|
266 |
+
# minimum=512,
|
267 |
+
# maximum=3072,
|
268 |
+
# step=1,
|
269 |
+
# value=1024,
|
270 |
+
# )
|
271 |
+
|
272 |
+
# height = gr.Slider(
|
273 |
+
# label="height",
|
274 |
+
# minimum=512,
|
275 |
+
# maximum=3072,
|
276 |
+
# step=1,
|
277 |
+
# value=1024,
|
278 |
+
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
|
280 |
gr.on(
|
281 |
triggers=[run_button.click, prompt.submit],
|
282 |
+
fn = infer,
|
283 |
+
inputs = [edit_image, prompt, lora_model, strength, seed, randomize_seed, guidance_scale, num_inference_steps],
|
284 |
+
outputs = [result, seed]
|
|
|
|
|
|
|
|
|
|
|
285 |
)
|
286 |
|
287 |
download_button = gr.Button("Download Image as PNG")
|
|
|
302 |
|
303 |
save_button.click(
|
304 |
fn=save_details,
|
305 |
+
inputs=[result, edit_image, prompt, lora_model, strength, seed, guidance_scale, num_inference_steps],
|
|
|
|
|
|
|
|
|
|
|
306 |
outputs=gr.File(label="Download/Save Status")
|
307 |
)
|
308 |
|
309 |
+
# edit_image.select(
|
310 |
+
# fn=generate_mask,
|
311 |
+
# inputs=[edit_image, gr.Number(), gr.Number()],
|
312 |
+
# outputs=[edit_image]
|
313 |
+
# )
|
314 |
+
|
315 |
+
# demo.launch()
|
316 |
PASSWORD = os.getenv("GRADIO_PASSWORD")
|
317 |
USERNAME = os.getenv("GRADIO_USERNAME")
|
318 |
+
# Create an authentication object
|
|
|
319 |
def authenticate(username, password):
|
320 |
if username == USERNAME and password == PASSWORD:
|
321 |
return True
|
322 |
+
|
323 |
else:
|
324 |
return False
|
|
|
325 |
# Launch the app with authentication
|
326 |
+
|
327 |
demo.launch(debug=True, auth=authenticate)
|