import torch import gradio as gr from PIL import Image import qrcode from pathlib import Path from multiprocessing import cpu_count import requests import io import os from PIL import Image import spaces from diffusers import ( StableDiffusionPipeline, StableDiffusionControlNetImg2ImgPipeline, ControlNetModel, DDIMScheduler, DPMSolverMultistepScheduler, DEISMultistepScheduler, HeunDiscreteScheduler, EulerDiscreteScheduler, ) qrcode_generator = qrcode.QRCode( version=1, error_correction=qrcode.ERROR_CORRECT_H, box_size=10, border=4, ) controlnet = ControlNetModel.from_pretrained( "DionTimmer/controlnet_qrcode-control_v1p_sd15", torch_dtype=torch.float16 ).to("cuda") pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained( # "runwayml/stable-diffusion-v1-5", "digiplay/GhostMixV1.2VAE", controlnet = controlnet, torch_dtype = torch.float16, safety_checker =None, ).to("cuda") #pipe.enable_xformers_memory_efficient_attention() def resize_for_condition_image(input_image: Image.Image, resolution: int): input_image = input_image.convert("RGB") W, H = input_image.size k = float(resolution) / min(H, W) H *= k W *= k H = int(round(H / 64.0)) * 64 W = int(round(W / 64.0)) * 64 img = input_image.resize((W, H), resample=Image.LANCZOS) return img SAMPLER_MAP = { "DPM++ Karras SDE": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True, algorithm_type="sde-dpmsolver++"), "DPM++ Karras": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True), "Heun": lambda config: HeunDiscreteScheduler.from_config(config), "Euler": lambda config: EulerDiscreteScheduler.from_config(config), "DDIM": lambda config: DDIMScheduler.from_config(config), "DEIS": lambda config: DEISMultistepScheduler.from_config(config), } @spaces.GPU() def inference( qr_code_content: str, prompt: str, negative_prompt: str, guidance_scale: float = 10.0, controlnet_conditioning_scale: float = 2.0, strength: float = 0.8, seed: int = -1, init_image: Image.Image | None = None, qrcode_image: Image.Image | None = None, use_qr_code_as_init_image = True, sampler = "DPM++ Karras SDE", ): if prompt is None or prompt == "": raise gr.Error("Prompt is required") if qrcode_image is None and qr_code_content == "": raise gr.Error("QR Code Image or QR Code Content is required") pipe.scheduler = SAMPLER_MAP[sampler](pipe.scheduler.config) generator = torch.manual_seed(seed) if seed != -1 else torch.Generator() if qr_code_content != "" or qrcode_image.size == (1, 1): print("Generating QR Code from content") qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=10, border=4, ) qr.add_data(qr_code_content) qr.make(fit=True) qrcode_image = qr.make_image(fill_color="black", back_color="white") qrcode_image = resize_for_condition_image(qrcode_image, 768) else: print("Using QR Code Image") qrcode_image = resize_for_condition_image(qrcode_image, 768) # hack due to gradio examples init_image = qrcode_image out = pipe( prompt=prompt, negative_prompt=negative_prompt, image=qrcode_image, control_image=qrcode_image, # type: ignore width=768, # type: ignore height=768, # type: ignore guidance_scale=float(guidance_scale), controlnet_conditioning_scale=float(controlnet_conditioning_scale), # type: ignore generator=generator, strength=float(strength), num_inference_steps=50, ) return out.images[0] # type: ignore with gr.Blocks(css=""" body { background-color: #000000; color: #ffffff; } .gradio-container { background-color: #000000; } .gr-button-primary { background-color: #ff0000 !important; border-color: #ff0000 !important; } .gr-button-primary:hover { background-color: #cc0000 !important; border-color: #cc0000 !important; } #markdown-content h1, #markdown-content h2, #markdown-content h3 { color: #ff0000; } .gr-form { background-color: #1a1a1a; border: 1px solid #333333; border-radius: 8px; padding: 20px; } .gr-input, .gr-select { background-color: #2a2a2a; border-color: #444444; color: #ffffff; } .gr-input:focus, .gr-select:focus { border-color: #ff0000; } .gr-form-label { color: #ff0000; } #markdown-content .center-content, #markdown-content .center-content * { text-align: center !important; } #markdown-content .center-content ul, #markdown-content .center-content ol { list-style-position: inside; padding-left: 0; } """) as blocks: gr.Markdown( """
# 🎨 Yamamoto QR Code Art Generator
## Transform Your QR Codes into Brand Masterpieces
Welcome to Yamamoto's innovative QR Code Art Generator! This cutting-edge tool empowers our creative team to craft
visually stunning, on-brand QR codes that perfectly blend functionality with artistic expression.
## 🚀 How It Works:
1. **Enter Your QR Code Content**: Start by inputting the URL or text for your QR code.
2. **Craft Your Prompt**: Describe the artistic style or theme you envision for your QR code.
3. **Fine-tune with Advanced Settings**: Adjust parameters to perfect your creation (see tips below).
4. **Generate and Iterate**: Click 'Run' to create your art, then refine as needed.
## 🌟 Tips for Spectacular Results:
- **Artistic Freedom**: Set between 0.8 and 0.95 for a balance of creativity and scannability.
- **QR Code Visibility**: Aim for 0.6 to 2.0 to ensure your code is both artistic and functional.
- **Prompt Crafting**: Use vivid, specific descriptions that align with your brand identity.
- **Experimentation**: Don't hesitate to try different settings and prompts to find your perfect style!
## 🎭 Prompt Ideas to Spark Your Creativity:
- "A serene Japanese garden with cherry blossoms and a koi pond"
- "A futuristic cityscape with neon lights and flying cars"
- "An abstract painting with swirling colors and geometric shapes"
- "A vintage-style travel poster featuring iconic landmarks"
Remember, the magic lies in the details of your prompt and the fine-tuning of your settings.
Happy creating!