Niansuh commited on
Commit
f77ff83
1 Parent(s): 8bb0f0d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +200 -0
app.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ import uuid
4
+ import json
5
+
6
+ import gradio as gr
7
+ import numpy as np
8
+ from PIL import Image
9
+ import spaces
10
+ import torch
11
+ from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
12
+
13
+ if not torch.cuda.is_available():
14
+ DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
15
+
16
+ MAX_SEED = np.iinfo(np.int32).max
17
+ CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "1") == "1"
18
+ MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
19
+ USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
20
+ ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
21
+
22
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
23
+
24
+ if torch.cuda.is_available():
25
+ pipe = StableDiffusionXLPipeline.from_pretrained(
26
+ "sd-community/sdxl-flash",
27
+ torch_dtype=torch.float16,
28
+ use_safetensors=True,
29
+ add_watermarker=False
30
+ )
31
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
32
+ pipe.to("cuda")
33
+
34
+
35
+ def save_image(img):
36
+ unique_name = str(uuid.uuid4()) + ".png"
37
+ img.save(unique_name)
38
+ return unique_name
39
+
40
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
41
+ if randomize_seed:
42
+ seed = random.randint(0, MAX_SEED)
43
+ return seed
44
+
45
+ @spaces.GPU(duration=30, queue=False)
46
+ def generate(
47
+ prompt: str,
48
+ negative_prompt: str = "",
49
+ use_negative_prompt: bool = False,
50
+ seed: int = 1,
51
+ width: int = 1024,
52
+ height: int = 1024,
53
+ guidance_scale: float = 3,
54
+ num_inference_steps: int = 30,
55
+ randomize_seed: bool = False,
56
+ use_resolution_binning: bool = True,
57
+ progress=gr.Progress(track_tqdm=True),
58
+ ):
59
+ pipe.to(device)
60
+ seed = int(randomize_seed_fn(seed, randomize_seed))
61
+ generator = torch.Generator().manual_seed(seed)
62
+
63
+ options = {
64
+ "prompt":prompt,
65
+ "negative_prompt":negative_prompt,
66
+ "width":width,
67
+ "height":height,
68
+ "guidance_scale":guidance_scale,
69
+ "num_inference_steps":num_inference_steps,
70
+ "generator":generator,
71
+ "use_resolution_binning":use_resolution_binning,
72
+ "output_type":"pil",
73
+
74
+ }
75
+
76
+ images = pipe(**options).images
77
+
78
+ image_paths = [save_image(img) for img in images]
79
+ return image_paths, seed
80
+
81
+
82
+ examples = [
83
+ "a cat eating a piece of cheese",
84
+ "a ROBOT riding a BLUE horse on Mars, photorealistic, 4k",
85
+ "Ironman VS Hulk, ultrarealistic",
86
+ "Astronaut in a jungle, cold color palette, oil pastel, detailed, 8k",
87
+ "An alien holding sign board contain word 'Flash', futuristic, neonpunk",
88
+ "Kids going to school, Anime style"
89
+ ]
90
+
91
+ css = '''
92
+ .gradio-container{max-width: 560px !important}
93
+ h1{text-align:center}
94
+ footer {
95
+ visibility: hidden
96
+ }
97
+ '''
98
+ with gr.Blocks(css=css) as demo:
99
+ gr.Markdown("""# SDXL Flash
100
+ ### First Image processing takes time then images generate faster.""")
101
+ with gr.Group():
102
+ with gr.Row():
103
+ prompt = gr.Text(
104
+ label="Prompt",
105
+ show_label=False,
106
+ max_lines=1,
107
+ placeholder="Enter your prompt",
108
+ container=False,
109
+ )
110
+ run_button = gr.Button("Run", scale=0)
111
+ result = gr.Gallery(label="Result", columns=1)
112
+ with gr.Accordion("Advanced options", open=False):
113
+ with gr.Row():
114
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
115
+ negative_prompt = gr.Text(
116
+ label="Negative prompt",
117
+ max_lines=5,
118
+ lines=4,
119
+ placeholder="Enter a negative prompt",
120
+ value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, NSFW",
121
+ visible=True,
122
+ )
123
+ seed = gr.Slider(
124
+ label="Seed",
125
+ minimum=0,
126
+ maximum=MAX_SEED,
127
+ step=1,
128
+ value=0,
129
+ )
130
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
131
+ with gr.Row(visible=True):
132
+ width = gr.Slider(
133
+ label="Width",
134
+ minimum=512,
135
+ maximum=MAX_IMAGE_SIZE,
136
+ step=64,
137
+ value=1024,
138
+ )
139
+ height = gr.Slider(
140
+ label="Height",
141
+ minimum=512,
142
+ maximum=MAX_IMAGE_SIZE,
143
+ step=64,
144
+ value=1024,
145
+ )
146
+ with gr.Row():
147
+ guidance_scale = gr.Slider(
148
+ label="Guidance Scale",
149
+ minimum=0.1,
150
+ maximum=6,
151
+ step=0.1,
152
+ value=3.0,
153
+ )
154
+ num_inference_steps = gr.Slider(
155
+ label="Number of inference steps",
156
+ minimum=1,
157
+ maximum=15,
158
+ step=1,
159
+ value=8,
160
+ )
161
+
162
+ gr.Examples(
163
+ examples=examples,
164
+ inputs=prompt,
165
+ outputs=[result, seed],
166
+ fn=generate,
167
+ cache_examples=CACHE_EXAMPLES,
168
+ )
169
+
170
+ use_negative_prompt.change(
171
+ fn=lambda x: gr.update(visible=x),
172
+ inputs=use_negative_prompt,
173
+ outputs=negative_prompt,
174
+ api_name=False,
175
+ )
176
+
177
+ gr.on(
178
+ triggers=[
179
+ prompt.submit,
180
+ negative_prompt.submit,
181
+ run_button.click,
182
+ ],
183
+ fn=generate,
184
+ inputs=[
185
+ prompt,
186
+ negative_prompt,
187
+ use_negative_prompt,
188
+ seed,
189
+ width,
190
+ height,
191
+ guidance_scale,
192
+ num_inference_steps,
193
+ randomize_seed,
194
+ ],
195
+ outputs=[result, seed],
196
+ api_name="run",
197
+ )
198
+
199
+ if __name__ == "__main__":
200
+ demo.queue(max_size=20).launch()