File size: 1,301 Bytes
3c00266
 
 
 
 
 
 
ba59d75
3c00266
ba59d75
3c00266
 
 
 
 
ba59d75
 
3c00266
ba59d75
3c00266
ba59d75
 
 
 
 
 
3c00266
 
ba59d75
 
3c00266
 
 
 
 
 
 
 
 
 
ba59d75
 
 
 
 
3c00266
 
 
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
49
import gradio as gr
import numpy as np
import random
from diffusers import DiffusionPipeline
import torch

device = "cuda" if torch.cuda.is_available() else "cpu"
model_repo_id = "stabilityai/sdxl-turbo"  # Replace with the model you want to use

torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
pipe = pipe.to(device)

MAX_SEED = np.iinfo(np.int32).max

def generate_cat_image():
    seed = random.randint(0, MAX_SEED)
    generator = torch.Generator().manual_seed(seed)
    
    image = pipe(
        prompt="A cute cat, highly detailed, 8k",
        negative_prompt="",
        guidance_scale=7.5,
        num_inference_steps=20,
        width=512,
        height=512,
        generator=generator,
    ).images[0]
    
    return image

css = """
#col-container {
    margin: 0 auto;
    max-width: 640px;
}
"""

with gr.Blocks(css=css) as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown(" # Cat Image Generator 🐱")
        result = gr.Image(label="Generated Cat Image", show_label=False)
        run_button = gr.Button("Generate New Cat")
    
    run_button.click(fn=generate_cat_image, inputs=[], outputs=[result])

if __name__ == "__main__":
    demo.launch()