ChenoAi commited on
Commit
460d65d
·
verified ·
1 Parent(s): 8f8ab52
Files changed (1) hide show
  1. app.py +292 -1
app.py CHANGED
@@ -1,3 +1,294 @@
 
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- gr.load("models/dataautogpt3/OpenDalleV1.1").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 = '''
161
+ .gradio-container{max-width: 560px !important}
162
+ h1{text-align:center}
163
+ 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(
178
+ label="Prompt",
179
+ show_label=False,
180
+ max_lines=1,
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(
262
+ fn=lambda x: gr.update(visible=x),
263
+ inputs=use_negative_prompt,
264
+ outputs=negative_prompt,
265
+ api_name=False,
266
+ )
267
+
268
 
269
+ gr.on(
270
+ triggers=[
271
+ prompt.submit,
272
+ negative_prompt.submit,
273
+ run_button.click,
274
+ ],
275
+ fn=generate,
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=False, debug=False)