import gradio as gr import torch from diffusers import AutoPipelineForText2Image import spaces import random pipe = AutoPipelineForText2Image.from_pretrained( "stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16" ) pipe.to("cuda") @spaces.GPU() def infer(prompt, seed=42, randomize_seed=True, width=512, height=896, num_inference_steps=2): if randomize_seed: seed = random.randint(0, 2147483647) generator = torch.Generator("cuda").manual_seed(seed) image = pipe( prompt=prompt, width=width, height=height, num_inference_steps=num_inference_steps, guidance_scale=0.0, generator=generator ).images[0] return image, seed with gr.Blocks() as demo: gr.Markdown("# Fast AI Cartoon Image Generator") with gr.Row(): prompt = gr.Textbox(label="Prompt", placeholder="Enter prompt...") run_btn = gr.Button("Generate") result = gr.Image(label="Result") seed = gr.Number(label="Seed", value=42) randomize_seed = gr.Checkbox(label="Randomize", value=True) width = gr.Slider(label="Width", minimum=256, maximum=1024, step=64, value=512) height = gr.Slider(label="Height", minimum=256, maximum=1024, step=64, value=896) steps = gr.Slider(label="Steps", minimum=1, maximum=4, step=1, value=2) run_btn.click( fn=infer, inputs=[prompt, seed, randomize_seed, width, height, steps], outputs=[result, seed] ) demo.launch()