Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,6 +14,7 @@ import cv2
|
|
14 |
from pyzxing import BarCodeReader
|
15 |
from PIL import ImageOps
|
16 |
from huggingface_hub import hf_hub_download, snapshot_download
|
|
|
17 |
|
18 |
from diffusers import (
|
19 |
StableDiffusionPipeline,
|
@@ -45,31 +46,54 @@ CONTROLNET_MODELS = {
|
|
45 |
}
|
46 |
|
47 |
DIFFUSION_MODELS = {
|
48 |
-
"Stable Diffusion v1.5": "ckpt/sd15",
|
49 |
"GhostMix": "sinkinai/GhostMix-V2-BakedVae",
|
50 |
-
"Real-Dream": "stablediffusionapi/edge-of-realism",
|
51 |
-
"Real-Dream": "stablediffusionapi/edge-of-realism",
|
52 |
# Add more diffusion models here
|
53 |
}
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
59 |
controlnet_path,
|
60 |
torch_dtype=torch.float16
|
61 |
).to("cuda")
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
66 |
diffusion_path,
|
67 |
-
controlnet=
|
68 |
torch_dtype=torch.float16,
|
69 |
safety_checker=None,
|
70 |
).to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
def resize_for_condition_image(input_image: Image.Image, resolution: int):
|
75 |
input_image = input_image.convert("RGB")
|
@@ -230,13 +254,13 @@ def inference(
|
|
230 |
negative_prompt=negative_prompt,
|
231 |
image=init_image,
|
232 |
control_image=control_image,
|
233 |
-
width=
|
234 |
-
height=
|
235 |
guidance_scale=float(guidance_scale),
|
236 |
controlnet_conditioning_scale=float(controlnet_conditioning_scale),
|
237 |
generator=generator,
|
238 |
strength=float(strength),
|
239 |
-
num_inference_steps=
|
240 |
)
|
241 |
|
242 |
final_image = out.images[0]
|
@@ -247,7 +271,7 @@ def inference(
|
|
247 |
return final_image, seed
|
248 |
except Exception as e:
|
249 |
print(f"Error in inference: {str(e)}")
|
250 |
-
return Image.new('RGB', (
|
251 |
|
252 |
def invert_init_image_display(image):
|
253 |
if image is None:
|
@@ -331,22 +355,23 @@ with gr.Blocks(theme='Hev832/Applio') as blocks:
|
|
331 |
minimum=0.0,
|
332 |
maximum=5.0,
|
333 |
step=0.01,
|
334 |
-
value=
|
335 |
label="QR Code Visibility",
|
336 |
)
|
337 |
-
gr.
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
|
|
350 |
|
351 |
strength = gr.Slider(
|
352 |
minimum=0.0,
|
@@ -355,17 +380,18 @@ with gr.Blocks(theme='Hev832/Applio') as blocks:
|
|
355 |
value=0.9,
|
356 |
label="Artistic Freedom",
|
357 |
)
|
358 |
-
gr.
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
|
|
369 |
|
370 |
guidance_scale = gr.Slider(
|
371 |
minimum=0.0,
|
@@ -374,18 +400,19 @@ with gr.Blocks(theme='Hev832/Applio') as blocks:
|
|
374 |
value=7.5,
|
375 |
label="Follow the Prompt",
|
376 |
)
|
377 |
-
gr.
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
|
|
389 |
)
|
390 |
|
391 |
sampler = gr.Dropdown(
|
@@ -393,19 +420,20 @@ with gr.Blocks(theme='Hev832/Applio') as blocks:
|
|
393 |
value="DPM++ Karras SDE",
|
394 |
label="Art Style",
|
395 |
)
|
396 |
-
gr.
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
|
|
409 |
|
410 |
seed = gr.Slider(
|
411 |
minimum=-1,
|
@@ -415,17 +443,18 @@ with gr.Blocks(theme='Hev832/Applio') as blocks:
|
|
415 |
label="Creative Seed",
|
416 |
randomize=False,
|
417 |
)
|
418 |
-
gr.
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
|
|
429 |
with gr.Accordion("QR Code Customization", open=True):
|
430 |
bg_color = gr.ColorPicker(
|
431 |
label="Background Color",
|
@@ -451,7 +480,7 @@ with gr.Blocks(theme='Hev832/Applio') as blocks:
|
|
451 |
)
|
452 |
diffusion_model_dropdown = gr.Dropdown(
|
453 |
choices=list(DIFFUSION_MODELS.keys()),
|
454 |
-
value="
|
455 |
label="Diffusion Model",
|
456 |
info="Select the main diffusion model for image generation"
|
457 |
)
|
@@ -461,9 +490,16 @@ with gr.Blocks(theme='Hev832/Applio') as blocks:
|
|
461 |
with gr.Column():
|
462 |
result_image = gr.Image(label="Your Artistic QR Code")
|
463 |
used_seed = gr.Number(label="Seed Used", interactive=False)
|
|
|
464 |
with gr.Row():
|
465 |
scan_button = gr.Button("Scan QR Code")
|
466 |
invert_button = gr.Button("Invert Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
467 |
scan_result = gr.Textbox(label="Scan Result", interactive=False)
|
468 |
|
469 |
gr.Markdown(
|
@@ -512,6 +548,22 @@ with gr.Blocks(theme='Hev832/Applio') as blocks:
|
|
512 |
outputs=[init_image]
|
513 |
)
|
514 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
515 |
run_btn.click(
|
516 |
inference,
|
517 |
inputs=[
|
@@ -537,5 +589,8 @@ with gr.Blocks(theme='Hev832/Applio') as blocks:
|
|
537 |
concurrency_limit=20
|
538 |
)
|
539 |
|
|
|
|
|
|
|
540 |
blocks.queue(max_size=20)
|
541 |
-
blocks.launch(share=
|
|
|
14 |
from pyzxing import BarCodeReader
|
15 |
from PIL import ImageOps
|
16 |
from huggingface_hub import hf_hub_download, snapshot_download
|
17 |
+
from PIL import ImageEnhance
|
18 |
|
19 |
from diffusers import (
|
20 |
StableDiffusionPipeline,
|
|
|
46 |
}
|
47 |
|
48 |
DIFFUSION_MODELS = {
|
|
|
49 |
"GhostMix": "sinkinai/GhostMix-V2-BakedVae",
|
|
|
|
|
50 |
# Add more diffusion models here
|
51 |
}
|
52 |
|
53 |
+
# Global variables to store loaded models
|
54 |
+
loaded_controlnet = None
|
55 |
+
loaded_pipe = None
|
56 |
+
|
57 |
+
def load_models_on_launch():
|
58 |
+
global loaded_controlnet, loaded_pipe
|
59 |
+
print("Loading models on launch...")
|
60 |
+
controlnet_path = snapshot_download(CONTROLNET_MODELS["QR Code Monster"])
|
61 |
+
loaded_controlnet = ControlNetModel.from_pretrained(
|
62 |
controlnet_path,
|
63 |
torch_dtype=torch.float16
|
64 |
).to("cuda")
|
65 |
|
66 |
+
diffusion_path = snapshot_download(DIFFUSION_MODELS["GhostMix"])
|
67 |
+
loaded_pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
|
|
68 |
diffusion_path,
|
69 |
+
controlnet=loaded_controlnet,
|
70 |
torch_dtype=torch.float16,
|
71 |
safety_checker=None,
|
72 |
).to("cuda")
|
73 |
+
print("Models loaded successfully!")
|
74 |
+
|
75 |
+
# Modify the load_models function to use global variables
|
76 |
+
def load_models(controlnet_model, diffusion_model):
|
77 |
+
global loaded_controlnet, loaded_pipe
|
78 |
+
if loaded_controlnet is None or loaded_pipe is None:
|
79 |
+
load_models_on_launch()
|
80 |
+
return loaded_pipe
|
81 |
+
|
82 |
+
# Add new functions for image adjustments
|
83 |
+
def adjust_image(image, brightness, contrast, saturation):
|
84 |
+
if image is None:
|
85 |
+
return None
|
86 |
|
87 |
+
img = Image.fromarray(image) if isinstance(image, np.ndarray) else image
|
88 |
+
|
89 |
+
if brightness != 1:
|
90 |
+
img = ImageEnhance.Brightness(img).enhance(brightness)
|
91 |
+
if contrast != 1:
|
92 |
+
img = ImageEnhance.Contrast(img).enhance(contrast)
|
93 |
+
if saturation != 1:
|
94 |
+
img = ImageEnhance.Color(img).enhance(saturation)
|
95 |
+
|
96 |
+
return np.array(img)
|
97 |
|
98 |
def resize_for_condition_image(input_image: Image.Image, resolution: int):
|
99 |
input_image = input_image.convert("RGB")
|
|
|
254 |
negative_prompt=negative_prompt,
|
255 |
image=init_image,
|
256 |
control_image=control_image,
|
257 |
+
width=1024,
|
258 |
+
height=1024,
|
259 |
guidance_scale=float(guidance_scale),
|
260 |
controlnet_conditioning_scale=float(controlnet_conditioning_scale),
|
261 |
generator=generator,
|
262 |
strength=float(strength),
|
263 |
+
num_inference_steps=100,
|
264 |
)
|
265 |
|
266 |
final_image = out.images[0]
|
|
|
271 |
return final_image, seed
|
272 |
except Exception as e:
|
273 |
print(f"Error in inference: {str(e)}")
|
274 |
+
return Image.new('RGB', (1024, 1024), color='white'), -1
|
275 |
|
276 |
def invert_init_image_display(image):
|
277 |
if image is None:
|
|
|
355 |
minimum=0.0,
|
356 |
maximum=5.0,
|
357 |
step=0.01,
|
358 |
+
value=2,
|
359 |
label="QR Code Visibility",
|
360 |
)
|
361 |
+
with gr.Accordion("See Details", open=False):
|
362 |
+
gr.Markdown(
|
363 |
+
"""
|
364 |
+
**QR Code Visibility** determines how much the QR code itself stands out in the final design. Think of it like balancing between how "artistic" the image looks and how "functional" the QR code is.
|
365 |
+
|
366 |
+
- **Low settings (0.0-1)**: If you choose a lower value, the QR code will blend more into the art, and it might be hard to scan with a phone. This setting is great if you want the image to look amazing, but you might lose some of the scannability. Try this if you care more about art and less about the QR code being easily recognized.
|
367 |
+
|
368 |
+
- **Medium settings (1-3)**: This is the sweet spot where the QR code remains clearly visible while still blending in with the art. You can still scan it easily with a phone, but it looks more creative. For most users, setting it around **1.1** is a great start to balance both art and function.
|
369 |
+
|
370 |
+
- **High settings (3-5.0)**: If you need to make sure that the QR code is super easy to scan, even if it means the image looks less like art and more like a regular QR code, then choose a higher value. This is ideal when functionality is the main goal, and the artistic side can take a backseat.
|
371 |
+
|
372 |
+
Start with **1.3** if you're unsure, and adjust up or down depending on whether you want the QR code to be more artistic or more functional.
|
373 |
+
"""
|
374 |
+
)
|
375 |
|
376 |
strength = gr.Slider(
|
377 |
minimum=0.0,
|
|
|
380 |
value=0.9,
|
381 |
label="Artistic Freedom",
|
382 |
)
|
383 |
+
with gr.Accordion("See Details", open=False):
|
384 |
+
gr.Markdown(
|
385 |
+
"""
|
386 |
+
**Artistic Freedom** controls how much the AI is allowed to change the QR code's look to match your description. It's like telling the AI how creative it can get with your QR code:
|
387 |
+
|
388 |
+
- **Low settings (0.0-0.3)**: If you set this low, the AI will make small changes and your QR code will look more like a regular, plain QR code. This is useful if you want something that is still creative but not too wild, keeping it simple and easy to scan.
|
389 |
+
|
390 |
+
- **Medium settings (0.4-0.7)**: Here, the AI will add more artistic touches but keep the QR code recognizable. You get the best of both worlds—your QR code will have some creative flair, but it will still be easy to scan. For most cases, setting it to **0.6** is a great way to keep the code functional and artistic.
|
391 |
+
|
392 |
+
- **High settings (0.8-1.0)**: If you set this high, the AI will go all-out creative. The QR code will look amazing, but it might be difficult to scan because the art can start to take over the code. This setting is perfect if you're aiming for a highly creative piece of art and don't mind if it's a bit harder to scan. Start at **0.9** to explore creative but functional designs.
|
393 |
+
"""
|
394 |
+
)
|
395 |
|
396 |
guidance_scale = gr.Slider(
|
397 |
minimum=0.0,
|
|
|
400 |
value=7.5,
|
401 |
label="Follow the Prompt",
|
402 |
)
|
403 |
+
with gr.Accordion("See Details", open=False):
|
404 |
+
gr.Markdown(
|
405 |
+
"""
|
406 |
+
**Follow the Prompt** tells the AI how closely it should follow your description when creating the QR code art. Think of it like giving the AI instructions on how strict or flexible it can be with your design ideas:
|
407 |
+
|
408 |
+
- **Low settings (0-5)**: If you choose a lower value, the AI has more freedom to get creative on its own and may not stick too closely to your description. This is great if you want to see how the AI interprets your ideas in unexpected ways.
|
409 |
+
|
410 |
+
- **Medium settings (5-15)**: This is a good balance where the AI will mostly follow your prompt but will also add some of its own creative touches. If you want to see some surprises but still want the design to look like what you described, start at around **7.5**.
|
411 |
+
|
412 |
+
- **High settings (15+)**: If you choose a higher value, the AI will stick very closely to what you wrote in the description. This is good if you have a very specific idea and don't want the AI to change much. Just keep in mind that this might limit the AI's creativity.
|
413 |
+
|
414 |
+
Start at **7.5** for a balanced approach where the AI follows your ideas but still adds some artistic flair.
|
415 |
+
"""
|
416 |
)
|
417 |
|
418 |
sampler = gr.Dropdown(
|
|
|
420 |
value="DPM++ Karras SDE",
|
421 |
label="Art Style",
|
422 |
)
|
423 |
+
with gr.Accordion("See Details", open=False):
|
424 |
+
gr.Markdown(
|
425 |
+
"""
|
426 |
+
**Art Style** changes how the AI creates the image, using different methods (or "samplers"). Each method has a different effect on how detailed or artistic the final QR code looks:
|
427 |
+
|
428 |
+
- **DPM++ Karras SDE**: This is a great all-around option for creating high-quality, detailed images. It's a good place to start if you want a balance of sharpness and creativity.
|
429 |
+
|
430 |
+
- **Euler**: This method creates very sharp, detailed images, making the QR code look crisp and clear. Choose this if you want a precise, well-defined design.
|
431 |
+
|
432 |
+
- **DDIM**: This method is better if you want the QR code to have a more artistic, abstract style. It's great for when you want the QR code to look like a piece of modern art.
|
433 |
+
|
434 |
+
Feel free to experiment with different samplers to see what works best for the look you're going for!
|
435 |
+
"""
|
436 |
+
)
|
437 |
|
438 |
seed = gr.Slider(
|
439 |
minimum=-1,
|
|
|
443 |
label="Creative Seed",
|
444 |
randomize=False,
|
445 |
)
|
446 |
+
with gr.Accordion("See Details", open=False):
|
447 |
+
gr.Markdown(
|
448 |
+
"""
|
449 |
+
**Creative Seed** controls whether the AI creates a completely new design each time or sticks to a specific design. Think of it like a recipe: with the same seed number, you get the same "recipe" for your QR code every time.
|
450 |
+
|
451 |
+
- **-1**: This setting makes the AI create something completely new every time you run it. Use this if you want to explore different design ideas with each attempt.
|
452 |
+
|
453 |
+
- **Any other number**: If you set a specific number, the AI will always create the same image based on that number. This is useful if you find a design you like and want to recreate it exactly.
|
454 |
+
|
455 |
+
Try **-1** if you want to explore and generate different designs. If you find something you really love, write down the seed number and use it again to recreate the same design.
|
456 |
+
"""
|
457 |
+
)
|
458 |
with gr.Accordion("QR Code Customization", open=True):
|
459 |
bg_color = gr.ColorPicker(
|
460 |
label="Background Color",
|
|
|
480 |
)
|
481 |
diffusion_model_dropdown = gr.Dropdown(
|
482 |
choices=list(DIFFUSION_MODELS.keys()),
|
483 |
+
value="GhostMix",
|
484 |
label="Diffusion Model",
|
485 |
info="Select the main diffusion model for image generation"
|
486 |
)
|
|
|
490 |
with gr.Column():
|
491 |
result_image = gr.Image(label="Your Artistic QR Code")
|
492 |
used_seed = gr.Number(label="Seed Used", interactive=False)
|
493 |
+
|
494 |
with gr.Row():
|
495 |
scan_button = gr.Button("Scan QR Code")
|
496 |
invert_button = gr.Button("Invert Image")
|
497 |
+
|
498 |
+
with gr.Row():
|
499 |
+
brightness = gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="Brightness")
|
500 |
+
contrast = gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="Contrast")
|
501 |
+
saturation = gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="Saturation")
|
502 |
+
|
503 |
scan_result = gr.Textbox(label="Scan Result", interactive=False)
|
504 |
|
505 |
gr.Markdown(
|
|
|
548 |
outputs=[init_image]
|
549 |
)
|
550 |
|
551 |
+
brightness.change(
|
552 |
+
adjust_image,
|
553 |
+
inputs=[result_image, brightness, contrast, saturation],
|
554 |
+
outputs=[result_image]
|
555 |
+
)
|
556 |
+
contrast.change(
|
557 |
+
adjust_image,
|
558 |
+
inputs=[result_image, brightness, contrast, saturation],
|
559 |
+
outputs=[result_image]
|
560 |
+
)
|
561 |
+
saturation.change(
|
562 |
+
adjust_image,
|
563 |
+
inputs=[result_image, brightness, contrast, saturation],
|
564 |
+
outputs=[result_image]
|
565 |
+
)
|
566 |
+
|
567 |
run_btn.click(
|
568 |
inference,
|
569 |
inputs=[
|
|
|
589 |
concurrency_limit=20
|
590 |
)
|
591 |
|
592 |
+
# Load models on launch
|
593 |
+
load_models_on_launch()
|
594 |
+
|
595 |
blocks.queue(max_size=20)
|
596 |
+
blocks.launch(share=False, show_api=True)
|