Spaces:
Running
on
Zero
Running
on
Zero
import gradio as gr | |
import numpy as np | |
import random | |
import spaces | |
from diffusers import StableDiffusionXLImg2ImgPipeline | |
from diffusers.utils import load_image | |
import torch | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
model_repo_id = "stabilityai/stable-diffusion-xl-refiner-1.0" | |
if torch.cuda.is_available(): | |
torch_dtype = torch.float16 | |
else: | |
torch_dtype = torch.float32 | |
pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained( | |
model_repo_id, | |
torch_dtype=torch_dtype, | |
variant="fp16" if torch.cuda.is_available() else None, | |
use_safetensors=True | |
) | |
pipe = pipe.to(device) | |
MAX_SEED = np.iinfo(np.int32).max | |
MAX_IMAGE_SIZE = 1024 | |
def infer( | |
prompt, | |
input_image, | |
negative_prompt, | |
seed, | |
randomize_seed, | |
strength, | |
guidance_scale, | |
num_inference_steps, | |
progress=gr.Progress(track_tqdm=True), | |
): | |
if input_image is None: | |
return None, seed | |
if randomize_seed: | |
seed = random.randint(0, MAX_SEED) | |
generator = torch.Generator().manual_seed(seed) | |
# Process the image | |
if input_image is not None: | |
width, height = input_image.size | |
# Ensure width and height are valid for the model | |
if width > MAX_IMAGE_SIZE: | |
width = MAX_IMAGE_SIZE | |
if height > MAX_IMAGE_SIZE: | |
height = MAX_IMAGE_SIZE | |
image = pipe( | |
prompt=prompt, | |
image=input_image, | |
negative_prompt=negative_prompt, | |
guidance_scale=guidance_scale, | |
num_inference_steps=num_inference_steps, | |
strength=strength, | |
generator=generator, | |
).images[0] | |
return image, seed | |
examples = [ | |
["Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", "https://huggingface.co/datasets/patrickvonplaten/images/resolve/main/aa_xl/000000009.png"], | |
["An astronaut riding a green horse", "https://huggingface.co/datasets/patrickvonplaten/images/resolve/main/aa_xl/000000009.png"], | |
["A delicious ceviche cheesecake slice", "https://huggingface.co/datasets/patrickvonplaten/images/resolve/main/aa_xl/000000009.png"], | |
] | |
css = """ | |
#col-container { | |
margin: 0 auto; | |
max-width: 840px; | |
} | |
""" | |
with gr.Blocks(css=css) as demo: | |
with gr.Column(elem_id="col-container"): | |
gr.Markdown(" # SDXL Refiner - Image-to-Image") | |
with gr.Row(): | |
with gr.Column(scale=1): | |
input_image = gr.Image( | |
label="Input Image", | |
type="pil", | |
height=400 | |
) | |
with gr.Column(scale=1): | |
result = gr.Image(label="Result", height=400) | |
prompt = gr.Text( | |
label="Prompt", | |
placeholder="Enter your prompt", | |
) | |
run_button = gr.Button("Run", variant="primary") | |
with gr.Accordion("Advanced Settings", open=False): | |
negative_prompt = gr.Text( | |
label="Negative prompt", | |
max_lines=1, | |
placeholder="Enter a negative prompt", | |
) | |
strength = gr.Slider( | |
label="Strength", | |
minimum=0.0, | |
maximum=1.0, | |
step=0.05, | |
value=0.7, | |
) | |
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.Row(): | |
guidance_scale = gr.Slider( | |
label="Guidance scale", | |
minimum=1.0, | |
maximum=20.0, | |
step=0.1, | |
value=7.5, | |
) | |
num_inference_steps = gr.Slider( | |
label="Number of inference steps", | |
minimum=1, | |
maximum=100, | |
step=1, | |
value=30, | |
) | |
gr.Examples( | |
examples=examples, | |
inputs=[prompt, input_image], | |
outputs=[result, seed], | |
fn=infer, | |
cache_examples=True, | |
) | |
gr.on( | |
triggers=[run_button.click], | |
fn=infer, | |
inputs=[ | |
prompt, | |
input_image, | |
negative_prompt, | |
seed, | |
randomize_seed, | |
strength, | |
guidance_scale, | |
num_inference_steps, | |
], | |
outputs=[result, seed], | |
) | |
if __name__ == "__main__": | |
demo.launch() | |