Spaces:
Runtime error
Runtime error
#!/usr/bin/env python | |
import random | |
import gradio as gr | |
import numpy as np | |
import PIL.Image | |
import torch | |
import torchvision.transforms.functional as TF | |
from diffusers import DDPMScheduler, StableDiffusionXLAdapterPipeline, T2IAdapter | |
DESCRIPTION = "# T2I-Adapter-SDXL Sketch" | |
if not torch.cuda.is_available(): | |
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>" | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
if torch.cuda.is_available(): | |
model_id = "stabilityai/stable-diffusion-xl-base-1.0" | |
adapter = T2IAdapter.from_pretrained("TencentARC/t2i-adapter-sketch-sdxl-1.0", torch_dtype=torch.float16, variant="fp16") | |
scheduler = DDPMScheduler.from_pretrained(model_id, subfolder="scheduler") | |
pipe = StableDiffusionXLAdapterPipeline.from_pretrained( | |
model_id, | |
adapter=adapter, | |
safety_checker=None, | |
torch_dtype=torch.float16, | |
variant="fp16", | |
scheduler=scheduler, | |
) | |
pipe.to(device) | |
else: | |
pipe = None | |
MAX_SEED = np.iinfo(np.int32).max | |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int: | |
if randomize_seed: | |
seed = random.randint(0, MAX_SEED) | |
return seed | |
def run( | |
image: PIL.Image.Image, | |
prompt: str, | |
negative_prompt: str, | |
num_steps=25, | |
guidance_scale=5, | |
adapter_conditioning_scale=0.8, | |
seed=0, | |
) -> PIL.Image.Image: | |
image = image.convert("RGB").resize((1024, 1024)) | |
image = TF.to_tensor(image) > 0.5 | |
image = TF.to_pil_image(image.to(torch.float32)) | |
generator = torch.Generator(device=device).manual_seed(seed) | |
out = pipe( | |
prompt=prompt, | |
negative_prompt=negative_prompt, | |
image=image, | |
num_inference_steps=num_steps, | |
generator=generator, | |
guidance_scale=guidance_scale, | |
adapter_conditioning_scale=adapter_conditioning_scale, | |
).images[0] | |
return out | |
with gr.Blocks() as demo: | |
gr.Markdown(DESCRIPTION) | |
with gr.Row(): | |
with gr.Column(): | |
image = gr.Image( | |
source="canvas", | |
tool="sketch", | |
type="pil", | |
image_mode="1", | |
invert_colors=True, | |
# shape=(1024, 1024), | |
brush_radius=4, | |
height=1024, | |
width=1024, | |
) | |
prompt = gr.Textbox(label="Prompt") | |
run_button = gr.Button("Run") | |
with gr.Accordion("Advanced options", open=False): | |
negative_prompt = gr.Textbox( | |
label="Negative prompt", value="extra digit, fewer digits, cropped, worst quality, low quality" | |
) | |
num_steps = gr.Slider( | |
label="Number of steps", | |
minimum=1, | |
maximum=50, | |
step=1, | |
value=25, | |
) | |
guidance_scale = gr.Slider( | |
label="Guidance scale", | |
minimum=0.1, | |
maximum=10.0, | |
step=0.1, | |
value=5, | |
) | |
adapter_conditioning_scale = gr.Slider( | |
label="Adapter Conditioning Ccale", | |
minimum=0.5, | |
maximum=1, | |
step=0.1, | |
value=.85, | |
) | |
seed = gr.Slider( | |
label="Seed", | |
minimum=0, | |
maximum=MAX_SEED, | |
step=1, | |
value=0, | |
) | |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True) | |
with gr.Column(): | |
result = gr.Image(label="Result", height=600) | |
inputs = [ | |
image, | |
prompt, | |
negative_prompt, | |
num_steps, | |
guidance_scale, | |
adapter_conditioning_scale, | |
seed, | |
] | |
prompt.submit( | |
fn=randomize_seed_fn, | |
inputs=[seed, randomize_seed], | |
outputs=seed, | |
queue=False, | |
api_name=False, | |
).then( | |
fn=run, | |
inputs=inputs, | |
outputs=result, | |
api_name=False, | |
) | |
run_button.click( | |
fn=randomize_seed_fn, | |
inputs=[seed, randomize_seed], | |
outputs=seed, | |
queue=False, | |
api_name=False, | |
).then( | |
fn=run, | |
inputs=inputs, | |
outputs=result, | |
api_name="run", | |
) | |
if __name__ == "__main__": | |
demo.queue(max_size=20).launch() | |