File size: 1,489 Bytes
febc836
 
153001e
 
 
febc836
153001e
 
 
 
 
 
febc836
 
153001e
febc836
153001e
 
febc836
153001e
 
 
 
 
 
 
febc836
 
153001e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
febc836
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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()