Spaces:
Runtime error

test-error / app.py
nico2rdj's picture
Update app.py
53a48a0
Raw History Blame
5.85 kB
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
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
)
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
controlnet=controlnet,
safety_checker=None,
torch_dtype=torch.float16,
).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++"),
}
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("")
if qrcode_image is None and qr_code_content == "":
raise gr.Error("")
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("")
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("")
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=40,
)
return out.images[0] # type: ignore
with gr.Blocks() as blocks:
with gr.Row():
with gr.Column():
qr_code_content = gr.Textbox(
label="error",
info="",
value="",
)
with gr.Accordion(label="", open=False):
qr_code_image = gr.Image(
label="",
type="pil",
)
prompt = gr.Textbox(
label="",
info="",
)
negative_prompt = gr.Textbox(
label="",
value="",
)
use_qr_code_as_init_image = gr.Checkbox(label="", value=True, interactive=False, info="")
with gr.Accordion(label="", open=False, visible=False) as init_image_acc:
init_image = gr.Image(label="", type="pil")
with gr.Accordion(
label="",
open=True,
):
controlnet_conditioning_scale = gr.Slider(
minimum=0.0,
maximum=5.0,
step=0.01,
value=1.1,
label="",
)
strength = gr.Slider(
minimum=0.0, maximum=1.0, step=0.01, value=0.9, label=""
)
guidance_scale = gr.Slider(
minimum=0.0,
maximum=50.0,
step=0.25,
value=7.5,
label="",
)
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():
run_btn = gr.Button("")
with gr.Column():
result_image = gr.Image(label="")
run_btn.click(
inference,
inputs=[
qr_code_content,
prompt,
negative_prompt,
guidance_scale,
controlnet_conditioning_scale,
strength,
seed,
init_image,
qr_code_image,
use_qr_code_as_init_image,
sampler,
],
outputs=[result_image],
)
blocks.queue(concurrency_count=1, max_size=20)
blocks.launch(share=bool(os.environ.get("SHARE", False)))