import torch import gradio as gr from PIL import Image import qrcode from diffusers import ( StableDiffusionControlNetImg2ImgPipeline, ControlNetModel, DDIMScheduler, DPMSolverMultistepScheduler, DEISMultistepScheduler, HeunDiscreteScheduler, EulerDiscreteScheduler, ) controlnet = ControlNetModel.from_pretrained( "DionTimmer/controlnet_qrcode-control_v1p_sd15", torch_dtype=torch.float16 ) pipe= StableDiffusionControlNetImg2ImgPipeline.from_pretrained( "runwayml/stable-diffusion-v1-5", controlnet=controlnet, use_safetensors=True, torch_dtype=torch.float16, ).to("cuda") 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), } 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, 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): 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 = qrcode_image.resize((768, 768)) else: qrcode_image = qrcode_image.resize((768, 768)) # hack due to gradio examples init_image = qrcode_image out = pipe( prompt=prompt, negative_prompt=negative_prompt, image=init_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=40, ) return out.images[0] # type: ignore def inference_ui_demo(): return None # https://www.kaggle.com/code/aisuko/text-to-image-qr-code-generator/notebook # image=inference(qr_code_content="https://www.kaggle.com/aisuko", # prompt="A sky view of a colorful lakes and rivers flowing through the mountains", # negative_prompt="ugly, disfigured, low quality, blurry, nsfw", # guidance_scale=7.5, # controlnet_conditioning_scale=1.3, # strength=0.9, # seed=5392011833, # init_image=None, # qrcode_image=None, # sampler="DPM++ Karras SDE") with gr.Blocks() as blocks: gr.Markdown( """ # QR Code Image to Image UI Demo This code cannot be runable because of the low resource. So, it is aimed to show the the componnets of the UI only. If you want to run the Code, please go to Kaggle """ ) with gr.Row(): with gr.Column(): qrcode_content=gr.Textbox( label="QR Code Content", info="QR Code Content or URL", value="", ) with gr.Accordion(label="QR Code Image (Optional)", open=False): qr_code_image=gr.Image( label="QR Code Image (Optional). Leave blank to automatically generate QR Code", type="pil", ) prompt=gr.Textbox( label="Prompt", info="Prompt that guides the generation towards", ) negative_prompt=gr.Textbox( label="Negative Prompt", value="ugly, disfigured, low quality, blurry, nsfw", ) use_qr_code_as_init_image=gr.Checkbox( label="Use QR code as init image", value=True, interactive=False, info="Whether init image should be QR code. Unclick to pass init image or generate init image with Stable Diffusion 1.5" ) with gr.Accordion(label="Init Image (Optional)", open=False) as init_image_acc: init_image=gr.Image( label="Init Image (Optional). Leave blank to generate image with SD 1.5", type="pil", ) with gr.Accordion( label="Params: The generated QR Code functionality is largely influenced by the parameters detailed below", open=True,): controlnet_conditioning_scale=gr.Slider( minimum=0.0, maximum=5.0, step=0.1, value=1.1, label="Controlnet Conditioning Scale", ) strength=gr.Slider( minimum=0.0, maximum=1.0, step=0.1, value=0.9, label="Strength", ) guidance_scale=gr.Slider( minimum=0.0, maximum=10.0, step=0.1, value=7.5, label="Guidance Scale", ) sampler=gr.Dropdown( choices=list(SAMPLER_MAP.keys()), value="DPM++ Karras SDE", label="Sampler" ) seed=gr.Slider( minimum=-1, maximum=9999999999, step=1, value=2313123, label="Seed", randomize=True, ) with gr.Row(): btn=gr.Button("Run") with gr.Column(): result_image=gr.Image(label="Result Image") btn.click( inference_ui_demo, inputs=[ qrcode_content, prompt, negative_prompt, guidance_scale, controlnet_conditioning_scale, strength, seed, init_image, qr_code_image, sampler, ], outputs=[result_image], ) blocks.queue(concurrency_count=1, max_size=2) blocks.launch()