Spaces:
Sleeping
Sleeping
| import torch | |
| import gradio as gr | |
| from diffusers import ( | |
| StableDiffusionControlNetPipeline, | |
| ControlNetModel, | |
| EulerAncestralDiscreteScheduler, | |
| ) | |
| import random | |
| from types import SimpleNamespace | |
| from qr_creator import generate_qr_art | |
| cache_dir = '.cache' | |
| model = 'stable-diffusion-v1-5/stable-diffusion-v1-5' | |
| # @spaces.GPU #[uncomment to use ZeroGPU] | |
| device = 'cuda' if torch.cuda.is_available() else 'cpu' | |
| if device == 'cuda': | |
| torch_dtype=torch.float16 | |
| else: | |
| torch_dtype='auto' | |
| controlnet = ControlNetModel.from_pretrained( | |
| "monster-labs/control_v1p_sd15_qrcode_monster", | |
| torch_dtype=torch_dtype, | |
| cache_dir=cache_dir, | |
| ) | |
| pipe = StableDiffusionControlNetPipeline.from_pretrained( | |
| model, | |
| controlnet=controlnet, | |
| safety_checker=None, | |
| torch_dtype=torch_dtype, | |
| cache_dir=cache_dir, | |
| ).to(device) | |
| pipe.scheduler = EulerAncestralDiscreteScheduler.from_config( | |
| pipe.scheduler.config) | |
| # pipe.enable_xformers_memory_efficient_attention() # Commented out to fix ModuleNotFoundError | |
| def infer( | |
| content, | |
| prompt, | |
| negative_prompt, | |
| progress=gr.Progress(track_tqdm=True), | |
| ): | |
| args = SimpleNamespace( | |
| content=content, | |
| prompt=prompt, | |
| negative=negative_prompt, | |
| scale=round(random.uniform(5, 9), 1), | |
| control=round(random.uniform(0.9, 1.2), 1), | |
| seed=random.randint(1, 12345), | |
| ) | |
| img = generate_qr_art( | |
| pipe, | |
| args.content, | |
| args.prompt, | |
| args.negative, | |
| args.scale, | |
| args.control, | |
| args.seed, | |
| ) | |
| return img | |
| examples = [ | |
| [ | |
| 'https://sabitech.co.jp/', | |
| 'A peaceful Japanese countryside in anime style, with traditional wooden houses, cherry blossom trees, stone paths, and soft sunlight. The QR code is seamlessly formed by natural elements of the scene: intertwined sakura branches, wooden planks, rice field patterns, and gently curved roof tiles. The QR pattern remains fully intact and scannable, but blends harmoniously into the environment as if it naturally belongs there. Warm color palette, soft anime shading, clean linework, gentle breeze moving the leaves, serene rural atmosphere. High resolution, detailed textures, balanced contrast, smooth integration between QR structure and Japanese countryside elements.', | |
| 'Unscannable QR code, distorted QR pattern, broken squares, missing modules, chaotic branches, overly abstract shapes, blurry, low contrast, messy composition, unnatural geometry, disconnected elements, glitch artifacts, low resolution.', | |
| ], | |
| ] | |
| css = """ | |
| #col-container { | |
| margin: 0 auto; | |
| max-width: 640px; | |
| } | |
| """ | |
| with gr.Blocks(css=css) as demo: | |
| with gr.Column(elem_id="col-container"): | |
| gr.Markdown(" # Create QR Code fingerprints") | |
| with gr.Row(): | |
| content = gr.Text( | |
| label="Content", | |
| show_label=False, | |
| max_lines=1, | |
| placeholder="Enter your content", | |
| container=False, | |
| ) | |
| with gr.Row(): | |
| prompt = gr.Text( | |
| label="Prompt", | |
| show_label=False, | |
| max_lines=1, | |
| placeholder="Enter your prompt", | |
| container=False, | |
| ) | |
| run_button = gr.Button("Run", scale=0, variant="primary") | |
| with gr.Row(): | |
| negative_prompt = gr.Text( | |
| label="Negative prompt", | |
| show_label=False, | |
| max_lines=1, | |
| placeholder="Enter a negative prompt", | |
| container=False, | |
| ) | |
| result = gr.Image(label="Result", show_label=False) | |
| gr.Examples(examples=examples, inputs=[ | |
| content, prompt, negative_prompt]) | |
| gr.on( | |
| triggers=[run_button.click, prompt.submit], | |
| fn=infer, | |
| inputs=[ | |
| content, | |
| prompt, | |
| negative_prompt, | |
| ], | |
| outputs=[result], | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |