ChenoAi commited on
Commit
82213a5
1 Parent(s): bf9a0cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -166
app.py CHANGED
@@ -1,160 +1,91 @@
1
- #!/usr/bin/env python
2
-
3
  import os
4
  import random
5
  import uuid
 
6
 
7
  import gradio as gr
8
  import numpy as np
9
  from PIL import Image
10
  import spaces
11
- from typing import Tuple
12
  import torch
13
- from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler, AutoencoderKL
14
-
15
- DESCRIPTION = """
16
- # OpenDalle V1.1
17
- """
18
-
19
- def save_image(img):
20
- unique_name = str(uuid.uuid4()) + ".png"
21
- img.save(unique_name)
22
- return unique_name
23
-
24
- def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
25
- if randomize_seed:
26
- seed = random.randint(0, MAX_SEED)
27
- return seed
28
-
29
- MAX_SEED = np.iinfo(np.int32).max
30
 
31
  if not torch.cuda.is_available():
32
  DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
33
 
34
  MAX_SEED = np.iinfo(np.int32).max
 
 
 
 
35
 
36
- USE_TORCH_COMPILE = 0
37
- ENABLE_CPU_OFFLOAD = 0
38
-
39
 
40
  if torch.cuda.is_available():
41
- vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
42
  pipe = StableDiffusionXLPipeline.from_pretrained(
43
  "dataautogpt3/OpenDalleV1.1",
44
- vae=vae,
45
  torch_dtype=torch.float16,
46
  use_safetensors=True,
 
47
  )
 
48
  pipe.to("cuda")
49
 
50
 
51
- # by PixArt-alpha/PixArt-Sigma
52
- style_list = [
53
- {
54
- "name": "(No style)",
55
- "prompt": "{prompt}",
56
- "negative_prompt": "",
57
- },
58
- {
59
- "name": "Cinematic",
60
- "prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
61
- "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
62
- },
63
- {
64
- "name": "Photographic",
65
- "prompt": "cinematic photo {prompt} . 35mm photograph, film, bokeh, professional, 4k, highly detailed",
66
- "negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
67
- },
68
- {
69
- "name": "Anime",
70
- "prompt": "anime artwork {prompt} . anime style, key visual, vibrant, studio anime, highly detailed",
71
- "negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast",
72
- },
73
- {
74
- "name": "Manga",
75
- "prompt": "manga style {prompt} . vibrant, high-energy, detailed, iconic, Japanese comic style",
76
- "negative_prompt": "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, Western comic style",
77
- },
78
- {
79
- "name": "Digital Art",
80
- "prompt": "concept art {prompt} . digital artwork, illustrative, painterly, matte painting, highly detailed",
81
- "negative_prompt": "photo, photorealistic, realism, ugly",
82
- },
83
- {
84
- "name": "Pixel art",
85
- "prompt": "pixel-art {prompt} . low-res, blocky, pixel art style, 8-bit graphics",
86
- "negative_prompt": "sloppy, messy, blurry, noisy, highly detailed, ultra textured, photo, realistic",
87
- },
88
- {
89
- "name": "Fantasy art",
90
- "prompt": "ethereal fantasy concept art of {prompt} . magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy",
91
- "negative_prompt": "photographic, realistic, realism, 35mm film, dslr, cropped, frame, text, deformed, glitch, noise, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, sloppy, duplicate, mutated, black and white",
92
- },
93
- {
94
- "name": "Neonpunk",
95
- "prompt": "neonpunk style {prompt} . cyberpunk, vaporwave, neon, vibes, vibrant, stunningly beautiful, crisp, detailed, sleek, ultramodern, magenta highlights, dark purple shadows, high contrast, cinematic, ultra detailed, intricate, professional",
96
- "negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured",
97
- },
98
- {
99
- "name": "3D Model",
100
- "prompt": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting",
101
- "negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
102
- },
103
- ]
104
- styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
105
- STYLE_NAMES = list(styles.keys())
106
- DEFAULT_STYLE_NAME = "(No style)"
107
 
108
- def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
109
- p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
110
- if not negative:
111
- negative = ""
112
- return p.replace("{prompt}", positive), n + negative
113
 
114
- @spaces.GPU(enable_queue=True)
115
  def generate(
116
  prompt: str,
117
  negative_prompt: str = "",
118
- style: str = DEFAULT_STYLE_NAME,
119
  use_negative_prompt: bool = False,
120
- num_inference_steps: int = 30,
121
- num_images_per_prompt: int = 2,
122
- seed: int = 0,
123
  width: int = 1024,
124
  height: int = 1024,
125
  guidance_scale: float = 3,
 
126
  randomize_seed: bool = False,
 
127
  progress=gr.Progress(track_tqdm=True),
128
  ):
129
-
130
-
131
  seed = int(randomize_seed_fn(seed, randomize_seed))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
- if not use_negative_prompt:
134
- negative_prompt = "" # type: ignore
135
- prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
136
-
137
- images = pipe(
138
- prompt=prompt,
139
- negative_prompt=negative_prompt,
140
- width=width,
141
- height=height,
142
- guidance_scale=guidance_scale,
143
- num_inference_steps=num_inference_steps,
144
- num_images_per_prompt=num_images_per_prompt,
145
- cross_attention_kwargs={"scale": 0.65},
146
- output_type="pil",
147
- ).images
148
  image_paths = [save_image(img) for img in images]
149
- print(image_paths)
150
  return image_paths, seed
151
 
 
152
  examples = [
153
- "black fluffy gorgeous dangerous cat animal creature, large orange eyes, big fluffy ears, piercing gaze, full moon, dark ambiance, best quality, extremely detailed",
154
- "an anime female general laughing, with a military cap, evil smile, sadistic, grim",
155
- "Super Closeup Portrait, action shot, Profoundly dark whitish meadow, glass flowers, Stains, space grunge style, Jeanne d'Arc wearing White Olive green used styled Cotton frock, Wielding thin silver sword, Sci-fi vibe, dirty, noisy, Vintage monk style, very detailed, hd",
156
- "((OpenDAlle!)text logo:1), ~*~aesthetic~*~",
157
- "John Berkey Style page,ral-oilspill, There is no road ahead,no land, Strangely,the river is still flowing,crossing the void into the mysterious unknown, The end of nothingness,a huge ripple,it is a kind of wave,and it is the law of time that lasts forever in that void, At the end of the infinite void,there is a colorful world,very hazy and mysterious,and it cannot be seen clearly,but it is real, And that's where the river goes",
 
158
  ]
159
 
160
  css = '''
@@ -164,14 +95,9 @@ footer {
164
  visibility: hidden
165
  }
166
  '''
167
- with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
168
- gr.Markdown(DESCRIPTION)
169
- gr.DuplicateButton(
170
- value="Duplicate Space for private use",
171
- elem_id="duplicate-button",
172
- visible=False,
173
- )
174
-
175
  with gr.Group():
176
  with gr.Row():
177
  prompt = gr.Text(
@@ -181,81 +107,64 @@ with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
181
  placeholder="Enter your prompt",
182
  container=False,
183
  )
184
- run_button = gr.Button("Run")
185
- result = gr.Gallery(label="Result", columns=1, preview=True)
186
  with gr.Accordion("Advanced options", open=False):
187
- use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False, visible=True)
188
- negative_prompt = gr.Text(
 
189
  label="Negative prompt",
190
- max_lines=1,
 
191
  placeholder="Enter a negative prompt",
 
192
  visible=True,
193
  )
194
- with gr.Row():
195
- num_inference_steps = gr.Slider(
196
- label="Steps",
197
- minimum=10,
198
- maximum=60,
199
- step=1,
200
- value=30,
201
- )
202
- with gr.Row():
203
- num_images_per_prompt = gr.Slider(
204
- label="Images",
205
- minimum=1,
206
- maximum=5,
207
- step=1,
208
- value=2,
209
- )
210
  seed = gr.Slider(
211
  label="Seed",
212
  minimum=0,
213
  maximum=MAX_SEED,
214
  step=1,
215
  value=0,
216
- visible=True
217
  )
218
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
219
  with gr.Row(visible=True):
220
  width = gr.Slider(
221
  label="Width",
222
  minimum=512,
223
- maximum=2048,
224
- step=8,
225
  value=1024,
226
  )
227
  height = gr.Slider(
228
  label="Height",
229
  minimum=512,
230
- maximum=2048,
231
- step=8,
232
  value=1024,
233
  )
234
  with gr.Row():
235
  guidance_scale = gr.Slider(
236
  label="Guidance Scale",
237
  minimum=0.1,
238
- maximum=20.0,
239
  step=0.1,
240
- value=6,
241
  )
242
- with gr.Row(visible=True):
243
- style_selection = gr.Radio(
244
- show_label=True,
245
- container=True,
246
- interactive=True,
247
- choices=STYLE_NAMES,
248
- value=DEFAULT_STYLE_NAME,
249
- label="Image Style",
250
  )
251
-
252
 
253
  gr.Examples(
254
  examples=examples,
255
  inputs=prompt,
256
  outputs=[result, seed],
257
  fn=generate,
258
- cache_examples=False,
259
  )
260
 
261
  use_negative_prompt.change(
@@ -264,7 +173,6 @@ with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
264
  outputs=negative_prompt,
265
  api_name=False,
266
  )
267
-
268
 
269
  gr.on(
270
  triggers=[
@@ -276,19 +184,17 @@ with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
276
  inputs=[
277
  prompt,
278
  negative_prompt,
279
- style_selection,
280
  use_negative_prompt,
281
- num_inference_steps,
282
- num_images_per_prompt,
283
  seed,
284
  width,
285
  height,
286
  guidance_scale,
 
287
  randomize_seed,
288
  ],
289
  outputs=[result, seed],
290
  api_name="run",
291
  )
292
-
293
  if __name__ == "__main__":
294
- demo.queue(max_size=20).launch(show_api=True, debug=False, share=True)
 
 
 
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
  "dataautogpt3/OpenDalleV1.1",
 
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 = '''
 
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(
 
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(
 
173
  outputs=negative_prompt,
174
  api_name=False,
175
  )
 
176
 
177
  gr.on(
178
  triggers=[
 
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()