bhaskartripathi commited on
Commit
0a56024
·
verified ·
1 Parent(s): 7d5bec8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -0
app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import torch
5
+ from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, FlowMatchEulerDiscreteScheduler
6
+ import spaces
7
+
8
+ device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ dtype = torch.float16
10
+
11
+ repo = "stabilityai/stable-diffusion-3-medium-diffusers"
12
+ pipe = StableDiffusion3Pipeline.from_pretrained(repo, torch_dtype=torch.float16).to(device)
13
+
14
+ MAX_SEED = np.iinfo(np.int32).max
15
+ MAX_IMAGE_SIZE = 1344
16
+
17
+ @spaces.GPU
18
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
19
+
20
+ if randomize_seed:
21
+ seed = random.randint(0, MAX_SEED)
22
+
23
+ generator = torch.Generator().manual_seed(seed)
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 = generator
33
+ ).images[0]
34
+
35
+ return image, seed
36
+
37
+ examples = [
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
+
43
+ css="""
44
+ #col-container {
45
+ margin: 0 auto;
46
+ max-width: 580px;
47
+ }
48
+ """
49
+
50
+ with gr.Blocks(css=css) as demo:
51
+
52
+ with gr.Column(elem_id="col-container"):
53
+ gr.Markdown(f"""
54
+ # [Stable Diffusion 3 Medium]""")
55
+
56
+ with gr.Row():
57
+
58
+ prompt = gr.Text(
59
+ label="Prompt",
60
+ show_label=False,
61
+ max_lines=1,
62
+ placeholder="Enter your prompt",
63
+ container=False,
64
+ )
65
+
66
+ run_button = gr.Button("Run", scale=0)
67
+
68
+ result = gr.Image(label="Result", show_label=False)
69
+
70
+ with gr.Accordion("Advanced Settings", open=False):
71
+
72
+ negative_prompt = gr.Text(
73
+ label="Negative prompt",
74
+ max_lines=1,
75
+ placeholder="Enter a negative prompt",
76
+ )
77
+
78
+ seed = gr.Slider(
79
+ label="Seed",
80
+ minimum=0,
81
+ maximum=MAX_SEED,
82
+ step=1,
83
+ value=0,
84
+ )
85
+
86
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
87
+
88
+ with gr.Row():
89
+
90
+ width = gr.Slider(
91
+ label="Width",
92
+ minimum=256,
93
+ maximum=MAX_IMAGE_SIZE,
94
+ step=64,
95
+ value=1024,
96
+ )
97
+
98
+ height = gr.Slider(
99
+ label="Height",
100
+ minimum=256,
101
+ maximum=MAX_IMAGE_SIZE,
102
+ step=64,
103
+ value=1024,
104
+ )
105
+
106
+ with gr.Row():
107
+
108
+ guidance_scale = gr.Slider(
109
+ label="Guidance scale",
110
+ minimum=0.0,
111
+ maximum=10.0,
112
+ step=0.1,
113
+ value=5.0,
114
+ )
115
+
116
+ num_inference_steps = gr.Slider(
117
+ label="Number of inference steps",
118
+ minimum=1,
119
+ maximum=50,
120
+ step=1,
121
+ value=28,
122
+ )
123
+
124
+ gr.Examples(
125
+ examples = examples,
126
+ inputs = [prompt]
127
+ )
128
+ gr.on(
129
+ triggers=[run_button.click, prompt.submit, negative_prompt.submit],
130
+ fn = infer,
131
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
132
+ outputs = [result, seed]
133
+ )
134
+
135
+ demo.launch()