superlazycoder/zeroGPU
0
1import gradio as gr2import numpy as np3import random4from diffusers import DiffusionPipeline5import torch6import spaces7 8device = "cuda"9 10 11MAX_SEED = np.iinfo(np.int32).max12MAX_IMAGE_SIZE = 102413 14@spaces.GPU15def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):16 17 if randomize_seed:18 seed = random.randint(0, MAX_SEED)19 20 generator = torch.Generator().manual_seed(seed)21 pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)22 pipe.enable_xformers_memory_efficient_attention()23 pipe = pipe.to(device)24 25 image = pipe(26 prompt = prompt, 27 negative_prompt = negative_prompt,28 guidance_scale = guidance_scale, 29 num_inference_steps = num_inference_steps, 30 width = width, 31 height = height,32 generator = generator33 ).images[0] 34 35 return image36 37examples = [38 "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",39 "An astronaut riding a green horse",40 "A delicious ceviche cheesecake slice",41]42 43css="""44#col-container {45 margin: 0 auto;46 max-width: 520px;47}48"""49 50 51power_device = "GPU"52 53with gr.Blocks(css=css) as demo:54 55 with gr.Column(elem_id="col-container"):56 gr.Markdown(f"""57 # ZeroGPU Text-to-Image Gradio Template58 Currently running on {power_device}.59 """)60 61 with gr.Row():62 63 prompt = gr.Text(64 label="Prompt",65 show_label=False,66 max_lines=1,67 placeholder="Enter your prompt",68 container=False,69 )70 71 run_button = gr.Button("Run", scale=0)72 73 result = gr.Image(label="Result", show_label=False)74 75 with gr.Accordion("Advanced Settings", open=False):76 77 negative_prompt = gr.Text(78 label="Negative prompt",79 max_lines=1,80 placeholder="Enter a negative prompt",81 visible=False,82 )83 84 seed = gr.Slider(85 label="Seed",86 minimum=0,87 maximum=MAX_SEED,88 step=1,89 value=0,90 )91 92 randomize_seed = gr.Checkbox(label="Randomize seed", value=True)93 94 with gr.Row():95 96 width = gr.Slider(97 label="Width",98 minimum=256,99 maximum=MAX_IMAGE_SIZE,100 step=32,101 value=512,102 )103 104 height = gr.Slider(105 label="Height",106 minimum=256,107 maximum=MAX_IMAGE_SIZE,108 step=32,109 value=512,110 )111 112 with gr.Row():113 114 guidance_scale = gr.Slider(115 label="Guidance scale",116 minimum=0.0,117 maximum=10.0,118 step=0.1,119 value=0.0,120 )121 122 num_inference_steps = gr.Slider(123 label="Number of inference steps",124 minimum=1,125 maximum=12,126 step=1,127 value=2,128 )129 130 gr.Examples(131 examples = examples,132 inputs = [prompt]133 )134 135 run_button.click(136 fn = infer,137 inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],138 outputs = [result]139 )140 141demo.queue().launch()