multimodalart HF staff commited on
Commit
9b0e1d1
1 Parent(s): bdede30

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -0
app.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+
5
+ import spaces
6
+ from diffusers import DiffusionPipeline
7
+ import torch
8
+
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ model_repo_id = "stabilityai/stable-diffusion-3.5-large"
11
+
12
+ if torch.cuda.is_available():
13
+ torch_dtype = torch.bfloat16
14
+ else:
15
+ torch_dtype = torch.float32
16
+
17
+ pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
+ pipe = pipe.to(device)
19
+
20
+ MAX_SEED = np.iinfo(np.int32).max
21
+ MAX_IMAGE_SIZE = 1024
22
+
23
+ @spaces.GPU
24
+ def infer(
25
+ prompt,
26
+ negative_prompt="",
27
+ seed=42,
28
+ randomize_seed=False,
29
+ width=1024,
30
+ height=1024,
31
+ guidance_scale=4.5,
32
+ num_inference_steps=28,
33
+ progress=gr.Progress(track_tqdm=True),
34
+ ):
35
+ if randomize_seed:
36
+ seed = random.randint(0, MAX_SEED)
37
+
38
+ generator = torch.Generator().manual_seed(seed)
39
+
40
+ image = pipe(
41
+ prompt=prompt,
42
+ negative_prompt=negative_prompt,
43
+ guidance_scale=guidance_scale,
44
+ num_inference_steps=num_inference_steps,
45
+ width=width,
46
+ height=height,
47
+ generator=generator,
48
+ ).images[0]
49
+
50
+ return image, seed
51
+
52
+
53
+ examples = [
54
+ "A capybara wearing a suit holding a sign that reads Hello World",
55
+ ]
56
+
57
+ css = """
58
+ #col-container {
59
+ margin: 0 auto;
60
+ max-width: 640px;
61
+ }
62
+ """
63
+
64
+ with gr.Blocks(css=css) as demo:
65
+ with gr.Column(elem_id="col-container"):
66
+ gr.Markdown(" # Stable Diffusion 3.5 Large (8B)")
67
+ with gr.Row():
68
+ prompt = gr.Text(
69
+ label="Prompt",
70
+ show_label=False,
71
+ max_lines=1,
72
+ placeholder="Enter your prompt",
73
+ container=False,
74
+ )
75
+
76
+ run_button = gr.Button("Run", scale=0, variant="primary")
77
+
78
+ result = gr.Image(label="Result", show_label=False)
79
+
80
+ with gr.Accordion("Advanced Settings", open=False):
81
+ negative_prompt = gr.Text(
82
+ label="Negative prompt",
83
+ max_lines=1,
84
+ placeholder="Enter a negative prompt",
85
+ visible=False,
86
+ )
87
+
88
+ seed = gr.Slider(
89
+ label="Seed",
90
+ minimum=0,
91
+ maximum=MAX_SEED,
92
+ step=1,
93
+ value=0,
94
+ )
95
+
96
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
97
+
98
+ with gr.Row():
99
+ width = gr.Slider(
100
+ label="Width",
101
+ minimum=512,
102
+ maximum=MAX_IMAGE_SIZE,
103
+ step=32,
104
+ value=1024,
105
+ )
106
+
107
+ height = gr.Slider(
108
+ label="Height",
109
+ minimum=512,
110
+ maximum=MAX_IMAGE_SIZE,
111
+ step=32,
112
+ value=1024,
113
+ )
114
+
115
+ with gr.Row():
116
+ guidance_scale = gr.Slider(
117
+ label="Guidance scale",
118
+ minimum=0.0,
119
+ maximum=7.5,
120
+ step=0.1,
121
+ value=4.5,
122
+ )
123
+
124
+ num_inference_steps = gr.Slider(
125
+ label="Number of inference steps",
126
+ minimum=1,
127
+ maximum=50,
128
+ step=1,
129
+ value=28,
130
+ )
131
+
132
+ gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=True, cache_mode="lazy")
133
+ gr.on(
134
+ triggers=[run_button.click, prompt.submit],
135
+ fn=infer,
136
+ inputs=[
137
+ prompt,
138
+ negative_prompt,
139
+ seed,
140
+ randomize_seed,
141
+ width,
142
+ height,
143
+ guidance_scale,
144
+ num_inference_steps,
145
+ ],
146
+ outputs=[result, seed],
147
+ )
148
+
149
+ if __name__ == "__main__":
150
+ demo.launch()