Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -28,6 +28,13 @@ from diffusers import (
|
|
28 |
EulerDiscreteScheduler,
|
29 |
)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
|
33 |
qrcode_generator = qrcode.QRCode(
|
@@ -68,7 +75,7 @@ def load_models_on_launch():
|
|
68 |
loaded_controlnet = ControlNetModel.from_pretrained(
|
69 |
controlnet_path,
|
70 |
torch_dtype=torch.float16
|
71 |
-
).to("
|
72 |
|
73 |
diffusion_path = snapshot_download(DIFFUSION_MODELS["GhostMix"])
|
74 |
loaded_pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
@@ -76,7 +83,7 @@ def load_models_on_launch():
|
|
76 |
controlnet=loaded_controlnet,
|
77 |
torch_dtype=torch.float16,
|
78 |
safety_checker=None,
|
79 |
-
).to("
|
80 |
print("Models loaded successfully!")
|
81 |
|
82 |
# Modify the load_models function to use global variables
|
@@ -193,9 +200,9 @@ def inference(
|
|
193 |
qr_code_content: str,
|
194 |
prompt: str,
|
195 |
negative_prompt: str,
|
196 |
-
guidance_scale: float = 15.0,
|
197 |
-
controlnet_conditioning_scale: float = 1.5,
|
198 |
-
strength: float = 0.6,
|
199 |
seed: int = -1,
|
200 |
init_image: Image.Image | None = None,
|
201 |
qrcode_image: Image.Image | None = None,
|
@@ -208,13 +215,14 @@ def inference(
|
|
208 |
controlnet_model: str = "QR Code Monster",
|
209 |
diffusion_model: str = "GhostMix",
|
210 |
reference_image_strength: float = 0.6,
|
|
|
211 |
):
|
212 |
try:
|
213 |
-
progress =
|
214 |
# Load models based on user selection
|
215 |
-
progress(0, desc="
|
216 |
pipe = load_models(controlnet_model, diffusion_model)
|
217 |
-
progress(0.
|
218 |
|
219 |
if prompt is None or prompt == "":
|
220 |
raise gr.Error("Prompt is required")
|
@@ -226,7 +234,7 @@ def inference(
|
|
226 |
if count_tokens(prompt) > MAX_TOKENS:
|
227 |
raise gr.Error(f"Prompt exceeds the maximum allowed tokens of {MAX_TOKENS}")
|
228 |
|
229 |
-
if count_tokens(negative_prompt) > MAX_TOKENS:
|
230 |
raise gr.Error(f"Negative prompt exceeds the maximum allowed tokens of {MAX_TOKENS}")
|
231 |
|
232 |
pipe.scheduler = SAMPLER_MAP[sampler](pipe.scheduler.config)
|
@@ -235,7 +243,7 @@ def inference(
|
|
235 |
seed = torch.seed() # Generate a truly random seed
|
236 |
generator = torch.manual_seed(seed)
|
237 |
|
238 |
-
if qr_code_content != "" or qrcode_image.size == (1, 1):
|
239 |
print("Generating QR Code from content")
|
240 |
qr = qrcode.QRCode(
|
241 |
version=1,
|
@@ -248,9 +256,11 @@ def inference(
|
|
248 |
|
249 |
qrcode_image = qr.make_image(fill_color=qr_color, back_color=bg_color)
|
250 |
qrcode_image = resize_for_condition_image(qrcode_image, 1024)
|
251 |
-
|
252 |
print("Using QR Code Image")
|
253 |
qrcode_image = resize_for_condition_image(qrcode_image, 1024)
|
|
|
|
|
254 |
|
255 |
# Determine which image to use as init_image and control_image
|
256 |
if use_qr_code_as_init_image:
|
@@ -274,10 +284,10 @@ def inference(
|
|
274 |
if invert_init_image and init_image is not None:
|
275 |
init_image = invert_image(init_image)
|
276 |
|
277 |
-
|
278 |
out = pipe(
|
279 |
-
prompt=prompt,
|
280 |
-
negative_prompt=negative_prompt,
|
281 |
image=init_image,
|
282 |
control_image=control_image,
|
283 |
width=1024,
|
@@ -287,19 +297,18 @@ def inference(
|
|
287 |
generator=generator,
|
288 |
strength=float(strength),
|
289 |
num_inference_steps=50,
|
|
|
290 |
)
|
291 |
-
final_image = out.images[0]
|
292 |
|
293 |
if invert_final_image:
|
294 |
final_image = invert_image(final_image)
|
295 |
|
296 |
-
|
297 |
-
|
298 |
-
print(f"Gradio error in inference: {str(e)}")
|
299 |
-
return Image.new('RGB', (1024, 1024), color='white'), -1
|
300 |
except Exception as e:
|
301 |
print(f"Unexpected error in inference: {str(e)}")
|
302 |
-
return Image.new('RGB', (1024, 1024), color='white'), -1
|
303 |
|
304 |
|
305 |
|
@@ -333,132 +342,301 @@ def invert_init_image_display(image):
|
|
333 |
return inverted
|
334 |
|
335 |
css = """
|
336 |
-
h1 {
|
337 |
text-align: center;
|
338 |
-
display:block;
|
|
|
|
|
|
|
|
|
|
|
339 |
}
|
340 |
"""
|
341 |
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
|
|
|
|
|
|
370 |
with gr.Column():
|
371 |
-
|
372 |
-
label="
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
377 |
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
|
|
|
|
390 |
|
391 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
392 |
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
label="Uncheck to use your own image for generation",
|
398 |
-
value=True,
|
399 |
-
interactive=True,
|
400 |
-
info="Allows you to use your own image for generation, otherwise a generic QR Code is created automatically as the base image"
|
401 |
)
|
402 |
-
invert_init_image_button = gr.Button("Invert Init Image")
|
403 |
|
404 |
-
with gr.
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
409 |
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
|
|
|
|
|
|
416 |
)
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
421 |
)
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
427 |
)
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
434 |
)
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
440 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
441 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
442 |
with gr.Column():
|
443 |
-
|
|
|
444 |
|
445 |
-
|
446 |
-
|
447 |
scan_button = gr.Button("Verify QR Code Works")
|
448 |
scan_result = gr.Textbox(label="Validation Result of QR Code", interactive=False)
|
449 |
|
450 |
-
|
451 |
with gr.Row():
|
452 |
brightness = gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="Brightness")
|
453 |
contrast = gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="Contrast")
|
454 |
saturation = gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="Saturation")
|
455 |
with gr.Row():
|
456 |
-
invert_button = gr.Button("Invert Image")
|
457 |
|
458 |
used_seed = gr.Number(label="Seed Used", interactive=False)
|
459 |
|
460 |
gr.Markdown(
|
461 |
-
|
462 |
"""
|
463 |
### 🔍 Analyzing Your Creation
|
464 |
- Is the QR code scannable? Check with your phone camera to see if it can scan it.
|
@@ -469,221 +647,119 @@ with gr.Blocks(theme='Hev832/Applio', css=css) as blocks:
|
|
469 |
"""
|
470 |
)
|
471 |
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
maximum=5,
|
500 |
-
step=0.10,
|
501 |
-
value=2,
|
502 |
-
label="Artistic Freedom for the AI When Generating",
|
503 |
-
)
|
504 |
-
with gr.Accordion("How Much Artistic Freedom the AI has When Generating Image (Click For Explanation)", open=False):
|
505 |
-
gr.Markdown(
|
506 |
-
"""
|
507 |
-
**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:
|
508 |
-
|
509 |
-
- **Low settings (0.10-2)**: 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.
|
510 |
-
|
511 |
-
- **Medium settings (2-3)**: 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.
|
512 |
-
|
513 |
-
- **High settings (3-5)**: 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.
|
514 |
-
"""
|
515 |
-
)
|
516 |
-
|
517 |
-
with gr.Row():
|
518 |
-
guidance_scale = gr.Slider(
|
519 |
-
minimum=0.0,
|
520 |
-
maximum=100.0,
|
521 |
-
step=0.25,
|
522 |
-
value=7.5,
|
523 |
-
label="How Closely the AI Follows the Prompt",
|
524 |
-
|
525 |
-
)
|
526 |
-
with gr.Accordion("How Closely the AI Follows the Prompt (Click For Explanation)", open=False):
|
527 |
-
gr.Markdown(
|
528 |
-
"""
|
529 |
-
**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:
|
530 |
-
|
531 |
-
- **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.
|
532 |
-
|
533 |
-
- **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**.
|
534 |
-
|
535 |
-
- **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.
|
536 |
-
|
537 |
-
Start at **7.5** for a balanced approach where the AI follows your ideas but still adds some artistic flair.
|
538 |
-
"""
|
539 |
-
)
|
540 |
-
|
541 |
-
with gr.Row():
|
542 |
-
sampler = gr.Dropdown(
|
543 |
-
choices=list(SAMPLER_MAP.keys()),
|
544 |
-
value="DPM++ Karras SDE",
|
545 |
-
label="Art Style Used to Create the Image",
|
546 |
-
|
547 |
-
)
|
548 |
-
with gr.Accordion("Details on Art Style the AI Uses to Create the Image (Click For Explanation)", open=False):
|
549 |
-
gr.Markdown(
|
550 |
-
"""
|
551 |
-
**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:
|
552 |
-
|
553 |
-
- **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.
|
554 |
-
|
555 |
-
- **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.
|
556 |
-
|
557 |
-
- **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.
|
558 |
-
|
559 |
-
Feel free to experiment with different samplers to see what works best for the look you're going for!
|
560 |
-
"""
|
561 |
-
)
|
562 |
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
return "No image to scan"
|
598 |
-
|
599 |
-
scanned_text = scan_qr_code(image)
|
600 |
-
if scanned_text:
|
601 |
-
return f"Scanned successfully: {scanned_text}"
|
602 |
-
else:
|
603 |
-
return "Failed to scan QR code. Try adjusting the settings for better visibility."
|
604 |
-
|
605 |
-
def invert_displayed_image(image):
|
606 |
-
if image is None:
|
607 |
-
return None
|
608 |
-
return invert_image(image)
|
609 |
-
|
610 |
-
scan_button.click(
|
611 |
-
scan_and_display,
|
612 |
-
inputs=[result_image],
|
613 |
-
outputs=[scan_result]
|
614 |
-
)
|
615 |
-
|
616 |
-
invert_button.click(
|
617 |
-
invert_displayed_image,
|
618 |
-
inputs=[result_image],
|
619 |
-
outputs=[result_image]
|
620 |
-
)
|
621 |
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
|
628 |
-
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
|
641 |
-
|
642 |
-
|
643 |
|
644 |
-
|
645 |
-
|
646 |
-
|
|
|
|
|
647 |
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
|
|
658 |
)
|
659 |
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
-
|
664 |
-
|
665 |
-
negative_prompt,
|
666 |
-
guidance_scale,
|
667 |
-
controlnet_conditioning_scale,
|
668 |
-
strength,
|
669 |
-
seed,
|
670 |
-
init_image,
|
671 |
-
qr_code_image,
|
672 |
-
use_qr_code_as_init_image,
|
673 |
-
sampler,
|
674 |
-
bg_color,
|
675 |
-
qr_color,
|
676 |
-
invert_final_image,
|
677 |
-
controlnet_model_dropdown,
|
678 |
-
diffusion_model_dropdown,
|
679 |
-
reference_image_strength,
|
680 |
-
],
|
681 |
-
outputs=[result_image, used_seed],
|
682 |
-
concurrency_limit=20
|
683 |
)
|
684 |
|
685 |
# Load models on launch
|
686 |
load_models_on_launch()
|
687 |
|
688 |
blocks.queue(max_size=20)
|
689 |
-
blocks.launch(share=
|
|
|
28 |
EulerDiscreteScheduler,
|
29 |
)
|
30 |
|
31 |
+
from dotenv import load_dotenv
|
32 |
+
|
33 |
+
# Load environment variables from .env file
|
34 |
+
load_dotenv()
|
35 |
+
|
36 |
+
USERNAME = os.getenv("USERNAME")
|
37 |
+
PASSWORD = os.getenv("PASSWORD")
|
38 |
|
39 |
|
40 |
qrcode_generator = qrcode.QRCode(
|
|
|
75 |
loaded_controlnet = ControlNetModel.from_pretrained(
|
76 |
controlnet_path,
|
77 |
torch_dtype=torch.float16
|
78 |
+
).to("mps")
|
79 |
|
80 |
diffusion_path = snapshot_download(DIFFUSION_MODELS["GhostMix"])
|
81 |
loaded_pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
|
|
83 |
controlnet=loaded_controlnet,
|
84 |
torch_dtype=torch.float16,
|
85 |
safety_checker=None,
|
86 |
+
).to("mps")
|
87 |
print("Models loaded successfully!")
|
88 |
|
89 |
# Modify the load_models function to use global variables
|
|
|
200 |
qr_code_content: str,
|
201 |
prompt: str,
|
202 |
negative_prompt: str,
|
203 |
+
guidance_scale: float = 15.0,
|
204 |
+
controlnet_conditioning_scale: float = 1.5,
|
205 |
+
strength: float = 0.6,
|
206 |
seed: int = -1,
|
207 |
init_image: Image.Image | None = None,
|
208 |
qrcode_image: Image.Image | None = None,
|
|
|
215 |
controlnet_model: str = "QR Code Monster",
|
216 |
diffusion_model: str = "GhostMix",
|
217 |
reference_image_strength: float = 0.6,
|
218 |
+
progress: gr.Progress = gr.Progress()
|
219 |
):
|
220 |
try:
|
221 |
+
progress(0, desc="Initializing...")
|
222 |
# Load models based on user selection
|
223 |
+
progress(0.1, desc="Loading models...")
|
224 |
pipe = load_models(controlnet_model, diffusion_model)
|
225 |
+
progress(0.2, desc="Models loaded, preparing for inference...")
|
226 |
|
227 |
if prompt is None or prompt == "":
|
228 |
raise gr.Error("Prompt is required")
|
|
|
234 |
if count_tokens(prompt) > MAX_TOKENS:
|
235 |
raise gr.Error(f"Prompt exceeds the maximum allowed tokens of {MAX_TOKENS}")
|
236 |
|
237 |
+
if negative_prompt and count_tokens(negative_prompt) > MAX_TOKENS:
|
238 |
raise gr.Error(f"Negative prompt exceeds the maximum allowed tokens of {MAX_TOKENS}")
|
239 |
|
240 |
pipe.scheduler = SAMPLER_MAP[sampler](pipe.scheduler.config)
|
|
|
243 |
seed = torch.seed() # Generate a truly random seed
|
244 |
generator = torch.manual_seed(seed)
|
245 |
|
246 |
+
if qr_code_content != "" or (qrcode_image is not None and qrcode_image.size == (1, 1)):
|
247 |
print("Generating QR Code from content")
|
248 |
qr = qrcode.QRCode(
|
249 |
version=1,
|
|
|
256 |
|
257 |
qrcode_image = qr.make_image(fill_color=qr_color, back_color=bg_color)
|
258 |
qrcode_image = resize_for_condition_image(qrcode_image, 1024)
|
259 |
+
elif qrcode_image is not None:
|
260 |
print("Using QR Code Image")
|
261 |
qrcode_image = resize_for_condition_image(qrcode_image, 1024)
|
262 |
+
else:
|
263 |
+
raise gr.Error("No valid QR code content or image provided")
|
264 |
|
265 |
# Determine which image to use as init_image and control_image
|
266 |
if use_qr_code_as_init_image:
|
|
|
284 |
if invert_init_image and init_image is not None:
|
285 |
init_image = invert_image(init_image)
|
286 |
|
287 |
+
progress(0.5, desc="Generating image...")
|
288 |
out = pipe(
|
289 |
+
prompt=prompt,
|
290 |
+
negative_prompt=negative_prompt,
|
291 |
image=init_image,
|
292 |
control_image=control_image,
|
293 |
width=1024,
|
|
|
297 |
generator=generator,
|
298 |
strength=float(strength),
|
299 |
num_inference_steps=50,
|
300 |
+
callback=lambda step, timestep, latents: progress(0.5 + step / 100, desc=f"Step {step}/50")
|
301 |
)
|
302 |
+
final_image = out.images[0]
|
303 |
|
304 |
if invert_final_image:
|
305 |
final_image = invert_image(final_image)
|
306 |
|
307 |
+
progress(1.0, desc="Done!")
|
308 |
+
return final_image, seed, gr.update(visible=False) # Add the third output here
|
|
|
|
|
309 |
except Exception as e:
|
310 |
print(f"Unexpected error in inference: {str(e)}")
|
311 |
+
return Image.new('RGB', (1024, 1024), color='white'), -1, gr.update(visible=False)
|
312 |
|
313 |
|
314 |
|
|
|
342 |
return inverted
|
343 |
|
344 |
css = """
|
345 |
+
h1, h2, h3, h4, h5, h6, p, li, ul, ol, a {
|
346 |
text-align: center;
|
347 |
+
display: block;
|
348 |
+
}
|
349 |
+
ul, ol {
|
350 |
+
margin-left: auto;
|
351 |
+
margin-right: auto;
|
352 |
+
display: table;
|
353 |
}
|
354 |
"""
|
355 |
|
356 |
+
# Add login function
|
357 |
+
def login(username, password):
|
358 |
+
if username == USERNAME and password == PASSWORD:
|
359 |
+
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
360 |
+
else:
|
361 |
+
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
|
362 |
+
|
363 |
+
# Add login elements to the Gradio interface
|
364 |
+
with gr.Blocks(theme='Hev832/Applio', css=css, fill_width=True, fill_height=True) as blocks:
|
365 |
+
generated_images = gr.State([])
|
366 |
+
|
367 |
+
with gr.Tab("Welcome"):
|
368 |
+
gr.Markdown(
|
369 |
+
"""
|
370 |
+

|
371 |
+
# 🎨 Yamamoto QR Code Art Generator
|
372 |
+
## Transform Your QR Codes into Brand Masterpieces
|
373 |
+
Welcome to Yamamoto's innovative QR Code Art Generator! This cutting-edge tool empowers our creative team to craft
|
374 |
+
visually stunning, on-brand QR codes that perfectly blend functionality with artistic expression.
|
375 |
+
## 🚀 How It Works:
|
376 |
+
1. **Enter Your QR Code Content**: Start by inputting the URL or text for your QR code.
|
377 |
+
2. **Craft Your Prompt**: Describe the artistic style or theme you envision for your QR code.
|
378 |
+
3. **Fine-tune with Advanced Settings**: Adjust parameters to perfect your creation (see tips below).
|
379 |
+
4. **Generate and Iterate**: Click 'Run' to create your art, then refine as needed.
|
380 |
+
---
|
381 |
+
## Login below using the Yamamoto internal ussername and password to access the full app.
|
382 |
+
### Once logging in, a new tab will appear allowing you to access the QR Code Art Generator.
|
383 |
+
"""
|
384 |
+
)
|
385 |
+
|
386 |
+
# Add login form
|
387 |
with gr.Column():
|
388 |
+
username = gr.Textbox(label="Username", placeholder="Enter your username", value="UDG")
|
389 |
+
password = gr.Textbox(label="Password", type="password", placeholder="Enter your password", value="UDG!")
|
390 |
+
with gr.Column():
|
391 |
+
login_button = gr.Button("Login")
|
392 |
+
login_error = gr.Markdown("Invalid username or password. Please try again.", visible=False)
|
393 |
+
|
394 |
+
|
395 |
+
with gr.Tab("QR Code and Prompt Input", visible=False) as app_container:
|
396 |
+
with gr.Row():
|
397 |
+
with gr.Column():
|
398 |
+
qr_code_content = gr.Textbox(
|
399 |
+
label="QR Code Content",
|
400 |
+
placeholder="Enter URL or text for your QR code",
|
401 |
+
info="This is what your QR code will link to or display when scanned.",
|
402 |
+
value="https://theunderground.digital/",
|
403 |
+
lines=1,
|
404 |
+
)
|
405 |
|
406 |
+
prompt = gr.Textbox(
|
407 |
+
label="Artistic Prompt",
|
408 |
+
placeholder="Describe the style or theme for your QR code art (For best results, keep the prompt to 75 characters or less as seen below)",
|
409 |
+
value="A high-res, photo-realistic minimalist rendering of Mount Fuji as a sharp, semi-realistic silhouette on the horizon. The mountain conveys strength and motion with clean, crisp lines and natural flow. Features detailed snow textures, subtle ridge highlights, and a powerful yet serene atmosphere. Emphasizes strength with clarity and precision in texture and light. (Sharp outlines:1.5), (Photo-realistic:1.4), (Detailed textures:1.3), (Minimalist:1.3), (Semi-realistic:1.3), (Monochrome contrast:1.2), (Crisp detail:1.2), (Evoking strength:1.2). Inspired by traditional Japanese woodblock prints, nature photography, and minimalist design principles.",
|
410 |
+
info="Describe the style or theme for your QR code art (For best results, keep the prompt to 75 characters or less as seen in the example)",
|
411 |
+
lines=8,
|
412 |
+
)
|
413 |
+
negative_prompt = gr.Textbox(
|
414 |
+
label="Elements to Avoid",
|
415 |
+
placeholder="Describe what you don't want in the image",
|
416 |
+
value="ugly, disfigured, low quality, blurry, nsfw, bad_pictures, (bad_prompt_version2:0.8), EasyNegative, 3d, cartoon, anime, sketches, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), poorly drawn, distorted, overexposed, flat shading, bad proportions, deformed, pixelated, messy details, lack of contrast, unrealistic textures, bad anatomy, rough edges, low resolution, text artifacts.",
|
417 |
+
info="List elements or styles you want to avoid in your QR code art.",
|
418 |
+
lines=4,
|
419 |
+
)
|
420 |
|
421 |
+
run_btn = gr.Button("🎨 Create Your QR Art", variant="primary")
|
422 |
+
|
423 |
+
with gr.Accordion(label="Needs Some Prompting Help?", open=False, visible=True):
|
424 |
+
gr.Markdown(
|
425 |
+
"""
|
426 |
+
## 🌟 Tips for Spectacular Results:
|
427 |
+
- Use concise details in your prompt to help the AI understand your vision.
|
428 |
+
- Use negative prompts to avoid unwanted elements in your image.
|
429 |
+
- Experiment with different ControlNet models and diffusion models to find the best combination for your prompt.
|
430 |
+
|
431 |
+
## 🎭 Prompt Ideas to Spark Your Creativity:
|
432 |
+
- "A serene Japanese garden with cherry blossoms and a koi pond"
|
433 |
+
- "A futuristic cityscape with neon lights and flying cars"
|
434 |
+
- "An abstract painting with swirling colors and geometric shapes"
|
435 |
+
- "A vintage-style travel poster featuring iconic landmarks"
|
436 |
+
|
437 |
+
Remember, the magic lies in the details of your prompt and the fine-tuning of your settings.
|
438 |
+
Happy creating!
|
439 |
+
"""
|
440 |
+
)
|
441 |
+
|
442 |
+
with gr.Accordion("Set Custom QR Code Colors", open=False):
|
443 |
+
bg_color = gr.ColorPicker(
|
444 |
+
label="Background Color",
|
445 |
+
value="#FFFFFF",
|
446 |
+
info="Choose the background color for the QR code"
|
447 |
+
)
|
448 |
+
qr_color = gr.ColorPicker(
|
449 |
+
label="QR Code Color",
|
450 |
+
value="#000000",
|
451 |
+
info="Choose the color for the QR code pattern"
|
452 |
+
)
|
453 |
+
invert_final_image = gr.Checkbox(
|
454 |
+
label="Invert Final Image",
|
455 |
+
value=False,
|
456 |
+
info="Check this to invert the colors of the final image",
|
457 |
+
visible=False,
|
458 |
+
)
|
459 |
+
with gr.Accordion("AI Model Selection", open=False):
|
460 |
+
controlnet_model_dropdown = gr.Dropdown(
|
461 |
+
choices=list(CONTROLNET_MODELS.keys()),
|
462 |
+
value="QR Code Monster",
|
463 |
+
label="ControlNet Model",
|
464 |
+
info="Select the ControlNet model for QR code generation"
|
465 |
+
)
|
466 |
+
diffusion_model_dropdown = gr.Dropdown(
|
467 |
+
choices=list(DIFFUSION_MODELS.keys()),
|
468 |
+
value="GhostMix",
|
469 |
+
label="Diffusion Model",
|
470 |
+
info="Select the main diffusion model for image generation"
|
471 |
+
)
|
472 |
+
|
473 |
|
474 |
+
with gr.Accordion(label="QR Code Image (Optional)", open=False, visible=False):
|
475 |
+
qr_code_image = gr.Image(
|
476 |
+
label="QR Code Image (Optional). Leave blank to automatically generate QR code",
|
477 |
+
type="pil",
|
|
|
|
|
|
|
|
|
478 |
)
|
|
|
479 |
|
480 |
+
with gr.Column():
|
481 |
+
result_image = gr.Image(label="Your Artistic QR Code", show_download_button=True, show_fullscreen_button=True)
|
482 |
+
result_image_loading = gr.Markdown("Generating image...", visible=False)
|
483 |
+
scan_button = gr.Button("Verify QR Code Works", visible=False)
|
484 |
+
scan_result = gr.Textbox(label="Validation Result of QR Code", interactive=False, visible=False)
|
485 |
+
used_seed = gr.Number(label="Seed Used", interactive=False)
|
486 |
+
|
487 |
+
with gr.Accordion(label="Use Your Own Image as a Reference", open=True, visible=True) as init_image_acc:
|
488 |
+
init_image = gr.Image(label="Reference Image", type="pil")
|
489 |
+
with gr.Row():
|
490 |
+
use_qr_code_as_init_image = gr.Checkbox(
|
491 |
+
label="Uncheck to use your own image for generation",
|
492 |
+
value=True,
|
493 |
+
interactive=True,
|
494 |
+
info="Allows you to use your own image for generation, otherwise a generic QR Code is created automatically as the base image"
|
495 |
+
)
|
496 |
+
invert_init_image_button = gr.Button("Invert Init Image", size="sm", visible=False)
|
497 |
|
498 |
+
with gr.Tab("Advanced Settings"):
|
499 |
+
with gr.Accordion("Advanced Art Controls", open=True):
|
500 |
+
with gr.Row():
|
501 |
+
controlnet_conditioning_scale = gr.Slider(
|
502 |
+
minimum=0.0,
|
503 |
+
maximum=5.0,
|
504 |
+
step=0.01,
|
505 |
+
value=2,
|
506 |
+
label="QR Code Visibility in Image",
|
507 |
)
|
508 |
+
with gr.Accordion("How Much QR Code Visibility is in Final Image (Click For Explanation)", open=False):
|
509 |
+
gr.Markdown(
|
510 |
+
"""
|
511 |
+
**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.
|
512 |
+
|
513 |
+
- **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.
|
514 |
+
|
515 |
+
- **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.
|
516 |
+
|
517 |
+
- **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.
|
518 |
+
|
519 |
+
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.
|
520 |
+
"""
|
521 |
+
)
|
522 |
+
|
523 |
+
with gr.Row():
|
524 |
+
strength = gr.Slider(
|
525 |
+
minimum=0.0,
|
526 |
+
maximum=5,
|
527 |
+
step=0.10,
|
528 |
+
value=2,
|
529 |
+
label="Artistic Freedom for the AI When Generating",
|
530 |
)
|
531 |
+
with gr.Accordion("How Much Artistic Freedom the AI has When Generating Image (Click For Explanation)", open=False):
|
532 |
+
gr.Markdown(
|
533 |
+
"""
|
534 |
+
**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:
|
535 |
+
|
536 |
+
- **Low settings (0.10-2)**: 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.
|
537 |
+
|
538 |
+
- **Medium settings (2-3)**: 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.
|
539 |
+
|
540 |
+
- **High settings (3-5)**: 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.
|
541 |
+
"""
|
542 |
+
)
|
543 |
+
|
544 |
+
with gr.Row():
|
545 |
+
guidance_scale = gr.Slider(
|
546 |
+
minimum=0.0,
|
547 |
+
maximum=100.0,
|
548 |
+
step=0.25,
|
549 |
+
value=7.5,
|
550 |
+
label="How Closely the AI Follows the Prompt",
|
551 |
)
|
552 |
+
with gr.Accordion("How Closely the AI Follows the Prompt (Click For Explanation)", open=False):
|
553 |
+
gr.Markdown(
|
554 |
+
"""
|
555 |
+
**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:
|
556 |
+
|
557 |
+
- **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.
|
558 |
+
|
559 |
+
- **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**.
|
560 |
+
|
561 |
+
- **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.
|
562 |
+
|
563 |
+
Start at **7.5** for a balanced approach where the AI follows your ideas but still adds some artistic flair.
|
564 |
+
"""
|
565 |
+
)
|
566 |
+
|
567 |
+
with gr.Row():
|
568 |
+
sampler = gr.Dropdown(
|
569 |
+
choices=list(SAMPLER_MAP.keys()),
|
570 |
+
value="DPM++ Karras SDE",
|
571 |
+
label="Art Style Used to Create the Image",
|
572 |
)
|
573 |
+
with gr.Accordion("Details on Art Style the AI Uses to Create the Image (Click For Explanation)", open=False):
|
574 |
+
gr.Markdown(
|
575 |
+
"""
|
576 |
+
**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:
|
577 |
+
|
578 |
+
- **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.
|
579 |
+
|
580 |
+
- **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.
|
581 |
+
|
582 |
+
- **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.
|
583 |
+
|
584 |
+
Feel free to experiment with different samplers to see what works best for the look you're going for!
|
585 |
+
"""
|
586 |
+
)
|
587 |
+
|
588 |
+
with gr.Row():
|
589 |
+
seed = gr.Slider(
|
590 |
+
minimum=-1,
|
591 |
+
maximum=9999999999,
|
592 |
+
step=1,
|
593 |
+
value=-1,
|
594 |
+
label="Creative Seed for the Image Generation",
|
595 |
)
|
596 |
+
with gr.Accordion("How Creative Seed Works for Generating New and Unique Images (Click For Explanation)", open=False):
|
597 |
+
gr.Markdown(
|
598 |
+
"""
|
599 |
+
**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.
|
600 |
+
|
601 |
+
- **-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.
|
602 |
+
|
603 |
+
- **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.
|
604 |
|
605 |
+
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.
|
606 |
+
"""
|
607 |
+
)
|
608 |
+
|
609 |
+
with gr.Row():
|
610 |
+
reference_image_strength = gr.Slider(
|
611 |
+
minimum=0.0,
|
612 |
+
maximum=5.0,
|
613 |
+
step=0.05,
|
614 |
+
value=0.6,
|
615 |
+
label="Reference Image Influence",
|
616 |
+
info="Controls how much the reference image influences the final result (0 = ignore, 5 = copy exactly)",
|
617 |
+
visible=False # We'll make this visible when a reference image is uploaded
|
618 |
+
)
|
619 |
+
|
620 |
+
with gr.Tab("Image Editing"):
|
621 |
with gr.Column():
|
622 |
+
image_selector = gr.Dropdown(label="Select Image to Edit", choices=[], interactive=True)
|
623 |
+
image_to_edit = gr.Image(label="Your Artistic QR Code", show_download_button=True, show_fullscreen_button=True)
|
624 |
|
625 |
+
with gr.Row():
|
626 |
+
edited_image = gr.Image(label="Edited QR Code", show_download_button=True, show_fullscreen_button=True)
|
627 |
scan_button = gr.Button("Verify QR Code Works")
|
628 |
scan_result = gr.Textbox(label="Validation Result of QR Code", interactive=False)
|
629 |
|
|
|
630 |
with gr.Row():
|
631 |
brightness = gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="Brightness")
|
632 |
contrast = gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="Contrast")
|
633 |
saturation = gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="Saturation")
|
634 |
with gr.Row():
|
635 |
+
invert_button = gr.Button("Invert Image", size="sm")
|
636 |
|
637 |
used_seed = gr.Number(label="Seed Used", interactive=False)
|
638 |
|
639 |
gr.Markdown(
|
|
|
640 |
"""
|
641 |
### 🔍 Analyzing Your Creation
|
642 |
- Is the QR code scannable? Check with your phone camera to see if it can scan it.
|
|
|
647 |
"""
|
648 |
)
|
649 |
|
650 |
+
def update_image_list(image, seed):
|
651 |
+
generated_images.value.append((image, seed))
|
652 |
+
return gr.Dropdown(choices=[f"Image {i+1} (Seed: {s})" for i, (_, s) in enumerate(generated_images.value)])
|
653 |
+
|
654 |
+
def load_image_for_editing(choice):
|
655 |
+
if not choice:
|
656 |
+
return None
|
657 |
+
index = int(choice.split()[1]) - 1
|
658 |
+
return generated_images.value[index][0]
|
659 |
+
|
660 |
+
def edit_image(image, brightness, contrast, saturation):
|
661 |
+
if image is None:
|
662 |
+
return None
|
663 |
+
img = Image.fromarray(image) if isinstance(image, np.ndarray) else image
|
664 |
+
img = ImageEnhance.Brightness(img).enhance(brightness)
|
665 |
+
img = ImageEnhance.Contrast(img).enhance(contrast)
|
666 |
+
img = ImageEnhance.Color(img).enhance(saturation)
|
667 |
+
return np.array(img)
|
668 |
+
|
669 |
+
def invert_image(image):
|
670 |
+
if image is None:
|
671 |
+
return None
|
672 |
+
return ImageOps.invert(Image.fromarray(image) if isinstance(image, np.ndarray) else image)
|
673 |
+
|
674 |
+
def scan_and_display(image):
|
675 |
+
if image is None:
|
676 |
+
return "No image to scan"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
677 |
|
678 |
+
scanned_text = scan_qr_code(image)
|
679 |
+
if scanned_text:
|
680 |
+
return f"Scanned successfully: {scanned_text}"
|
681 |
+
else:
|
682 |
+
return "Failed to scan QR code. Try adjusting the settings for better visibility."
|
683 |
+
|
684 |
+
run_btn.click(
|
685 |
+
inference,
|
686 |
+
inputs=[
|
687 |
+
qr_code_content,
|
688 |
+
prompt,
|
689 |
+
negative_prompt,
|
690 |
+
guidance_scale,
|
691 |
+
controlnet_conditioning_scale,
|
692 |
+
strength,
|
693 |
+
seed,
|
694 |
+
init_image,
|
695 |
+
qr_code_image,
|
696 |
+
use_qr_code_as_init_image,
|
697 |
+
sampler,
|
698 |
+
bg_color,
|
699 |
+
qr_color,
|
700 |
+
invert_final_image,
|
701 |
+
controlnet_model_dropdown,
|
702 |
+
diffusion_model_dropdown,
|
703 |
+
reference_image_strength,
|
704 |
+
],
|
705 |
+
outputs=[result_image, used_seed, result_image_loading],
|
706 |
+
show_progress=True
|
707 |
+
).then(
|
708 |
+
update_image_list,
|
709 |
+
inputs=[result_image, used_seed],
|
710 |
+
outputs=[image_selector]
|
711 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
712 |
|
713 |
+
image_selector.change(
|
714 |
+
load_image_for_editing,
|
715 |
+
inputs=[image_selector],
|
716 |
+
outputs=[image_to_edit]
|
717 |
+
)
|
718 |
|
719 |
+
brightness.change(
|
720 |
+
edit_image,
|
721 |
+
inputs=[image_to_edit, brightness, contrast, saturation],
|
722 |
+
outputs=[edited_image]
|
723 |
+
)
|
724 |
+
contrast.change(
|
725 |
+
edit_image,
|
726 |
+
inputs=[image_to_edit, brightness, contrast, saturation],
|
727 |
+
outputs=[edited_image]
|
728 |
+
)
|
729 |
+
saturation.change(
|
730 |
+
edit_image,
|
731 |
+
inputs=[image_to_edit, brightness, contrast, saturation],
|
732 |
+
outputs=[edited_image]
|
733 |
+
)
|
734 |
|
735 |
+
invert_button.click(
|
736 |
+
invert_image,
|
737 |
+
inputs=[image_to_edit],
|
738 |
+
outputs=[edited_image]
|
739 |
+
)
|
740 |
|
741 |
+
scan_button.click(
|
742 |
+
scan_and_display,
|
743 |
+
inputs=[edited_image],
|
744 |
+
outputs=[scan_result]
|
745 |
+
)
|
746 |
|
747 |
+
# Define login button click behavior
|
748 |
+
login_button.click(
|
749 |
+
login,
|
750 |
+
inputs=[username, password],
|
751 |
+
outputs=[app_container, login_error, login_button]
|
752 |
)
|
753 |
|
754 |
+
# Define password textbox submit behavior
|
755 |
+
password.submit(
|
756 |
+
login,
|
757 |
+
inputs=[username, password],
|
758 |
+
outputs=[app_container, login_error, login_button]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
759 |
)
|
760 |
|
761 |
# Load models on launch
|
762 |
load_models_on_launch()
|
763 |
|
764 |
blocks.queue(max_size=20)
|
765 |
+
blocks.launch(share=False, show_api=True)
|