File size: 4,946 Bytes
458776d
 
 
 
5614c61
 
458776d
 
5614c61
458776d
5614c61
 
458776d
 
5614c61
458776d
5614c61
 
 
 
 
458776d
5614c61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
458776d
 
5614c61
 
 
458776d
 
 
 
 
5614c61
458776d
 
 
 
5614c61
458776d
 
5614c61
458776d
 
5614c61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
458776d
 
 
5614c61
458776d
 
5614c61
458776d
 
 
5614c61
458776d
 
5614c61
458776d
 
 
 
5614c61
458776d
5614c61
458776d
 
5614c61
458776d
5614c61
458776d
5614c61
 
 
 
 
 
 
 
458776d
 
5614c61
 
 
 
 
 
 
 
 
 
458776d
 
5614c61
 
 
 
 
458776d
5614c61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import gradio as gr
import numpy as np
import random
import torch
from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, FlowMatchEulerDiscreteScheduler
import spaces

device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16

repo = "stabilityai/stable-diffusion-3-medium-diffusers"
pipe = StableDiffusion3Pipeline.from_pretrained(repo, torch_dtype=torch.float16).to(device)

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

def infer(prompts, negative_prompts, seeds, randomize_seeds, widths, heights, guidance_scales, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
    images = []
    for i, prompt in enumerate(prompts):
        if randomize_seeds[i]:
            seeds[i] = random.randint(0, MAX_SEED)

        generator = torch.Generator().manual_seed(seeds[i])

        image = pipe(
            prompt=prompt,
            negative_prompt=negative_prompts[i],
            guidance_scale=guidance_scales[i],
            num_inference_steps=num_inference_steps[i],
            width=widths[i],
            height=heights[i],
            generator=generator
        ).images[0]

        images.append(image)

    return images, seeds

examples = [
    ["Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", "A blurry astronaut", 0, True, 512, 512, 7.5, 28],
    ["An astronaut riding a green horse", "Astronaut on a regular horse", 0, True, 512, 512, 7.5, 28],
    ["A delicious ceviche cheesecake slice", "A cheesecake that looks boring", 0, True, 512, 512, 7.5, 28],
]

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

with gr.Blocks(css=css) as demo:

    with gr.Column(elem_id="col-container"):
        gr.Markdown(f"""
        # Demo [Automated Stable Diffusion 3 Medium](https://huggingface.co/stabilityai/stable-diffusion-3-medium)
        """)

        with gr.Row():
            prompt_group = gr.Group(elem_id="prompt_group")
            with prompt_group:
                prompt_input = gr.Text(
                    label="Prompt",
                    show_label=False,
                    max_lines=1,
                    placeholder="Enter your prompt",
                    container=False,
                )
                negative_prompt_input = gr.Text(
                    label="Negative prompt",
                    max_lines=1,
                    placeholder="Enter a negative prompt",
                )
                seed_input = gr.Slider(
                    label="Seed",
                    minimum=0,
                    maximum=MAX_SEED,
                    step=1,
                    value=0,
                )
                randomize_seed_input = gr.Checkbox(label="Randomize seed", value=True)
                width_input = gr.Slider(
                    label="Width",
                    minimum=256,
                    maximum=MAX_IMAGE_SIZE,
                    step=64,
                    value=512,
                )
                height_input = gr.Slider(
                    label="Height",
                    minimum=256,
                    maximum=MAX_IMAGE_SIZE,
                    step=64,
                    value=512,
                )
                guidance_scale_input = gr.Slider(
                    label="Guidance scale",
                    minimum=0.0,
                    maximum=10.0,
                    step=0.1,
                    value=7.5,
                )
                num_inference_steps_input = gr.Slider(
                    label="Number of inference steps",
                    minimum=1,
                    maximum=50,
                    step=1,
                    value=28,
                )
            run_button = gr.Button("Run", scale=0)

        result = gr.Gallery(label="Results", show_label=False, columns=4, rows=1)
        add_button = gr.Button("Add Prompt")

        with gr.Accordion("Advanced Settings", open=False):
            pass

        gr.Examples(
            examples = examples,
            inputs = [
                prompt_input,
                negative_prompt_input,
                seed_input,
                randomize_seed_input,
                width_input,
                height_input,
                guidance_scale_input,
                num_inference_steps_input
            ]
        )

    def add_prompt():
        prompt_group.duplicate()
    
    def clear_prompts():
        prompt_group.clear()

    add_button.click(add_prompt)
    gr.on(
        triggers=[run_button.click, prompt_input.submit, negative_prompt_input.submit],
        fn=infer,
        inputs=[
            prompt_input,
            negative_prompt_input,
            seed_input,
            randomize_seed_input,
            width_input,
            height_input,
            guidance_scale_input,
            num_inference_steps_input
        ],
        outputs=[result, seed_input],
        api_name="infer"
    )
    demo.launch()