prithivMLmods commited on
Commit
951c3d1
1 Parent(s): 75b1b60

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +296 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,296 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # DALL•E 3 XL v2
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
+ pipe = StableDiffusionXLPipeline.from_pretrained(
42
+ "fluently/Fluently-XL-v4",
43
+ torch_dtype=torch.float16,
44
+ use_safetensors=True,
45
+ )
46
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
47
+ pipe.load_lora_weights("ehristoforu/dalle-3-xl-v2", weight_name="dalle-3-xl-lora-v2.safetensors", adapter_name="dalle")
48
+ pipe.set_adapters("dalle")
49
+ pipe.to("cuda")
50
+
51
+
52
+
53
+ style_list = [
54
+ {
55
+ "name": "(No style)",
56
+ "prompt": "{prompt}",
57
+ "negative_prompt": "",
58
+ },
59
+ {
60
+ "name": "Cinematic",
61
+ "prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
62
+ "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
63
+ },
64
+ {
65
+ "name": "Photographic",
66
+ "prompt": "cinematic photo {prompt} . 35mm photograph, film, bokeh, professional, 4k, highly detailed",
67
+ "negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
68
+ },
69
+ {
70
+ "name": "Anime",
71
+ "prompt": "anime artwork {prompt} . anime style, key visual, vibrant, studio anime, highly detailed",
72
+ "negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast",
73
+ },
74
+ {
75
+ "name": "Manga",
76
+ "prompt": "manga style {prompt} . vibrant, high-energy, detailed, iconic, Japanese comic style",
77
+ "negative_prompt": "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, Western comic style",
78
+ },
79
+ {
80
+ "name": "Digital Art",
81
+ "prompt": "concept art {prompt} . digital artwork, illustrative, painterly, matte painting, highly detailed",
82
+ "negative_prompt": "photo, photorealistic, realism, ugly",
83
+ },
84
+ {
85
+ "name": "Pixel art",
86
+ "prompt": "pixel-art {prompt} . low-res, blocky, pixel art style, 8-bit graphics",
87
+ "negative_prompt": "sloppy, messy, blurry, noisy, highly detailed, ultra textured, photo, realistic",
88
+ },
89
+ {
90
+ "name": "Fantasy art",
91
+ "prompt": "ethereal fantasy concept art of {prompt} . magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy",
92
+ "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",
93
+ },
94
+ {
95
+ "name": "Neonpunk",
96
+ "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",
97
+ "negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured",
98
+ },
99
+ {
100
+ "name": "3D Model",
101
+ "prompt": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting",
102
+ "negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
103
+ },
104
+ ]
105
+ styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
106
+ STYLE_NAMES = list(styles.keys())
107
+ DEFAULT_STYLE_NAME = "(No style)"
108
+
109
+ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
110
+ p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
111
+ if not negative:
112
+ negative = ""
113
+ return p.replace("{prompt}", positive), n + negative
114
+
115
+ @spaces.GPU(enable_queue=True)
116
+ def generate(
117
+ prompt: str,
118
+ negative_prompt: str = "",
119
+ style: str = DEFAULT_STYLE_NAME,
120
+ use_negative_prompt: bool = False,
121
+ num_inference_steps: int = 30,
122
+ num_images_per_prompt: int = 2,
123
+ seed: int = 0,
124
+ width: int = 1024,
125
+ height: int = 1024,
126
+ guidance_scale: float = 3,
127
+ randomize_seed: bool = False,
128
+ progress=gr.Progress(track_tqdm=True),
129
+ ):
130
+
131
+
132
+ seed = int(randomize_seed_fn(seed, randomize_seed))
133
+
134
+ if not use_negative_prompt:
135
+ negative_prompt = "" # type: ignore
136
+ prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
137
+
138
+ images = pipe(
139
+ prompt=prompt,
140
+ negative_prompt=negative_prompt,
141
+ width=width,
142
+ height=height,
143
+ guidance_scale=guidance_scale,
144
+ num_inference_steps=num_inference_steps,
145
+ num_images_per_prompt=num_images_per_prompt,
146
+ cross_attention_kwargs={"scale": 0.65},
147
+ output_type="pil",
148
+ ).images
149
+ image_paths = [save_image(img) for img in images]
150
+ print(image_paths)
151
+ return image_paths, seed
152
+
153
+ examples = [
154
+ "neon holography crystal cat",
155
+ "a cat eating a piece of cheese",
156
+ "an astronaut riding a horse in space",
157
+ "a cartoon of a boy playing with a tiger",
158
+ "a cute robot artist painting on an easel, concept art",
159
+ "a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone"
160
+ ]
161
+
162
+ css = '''
163
+ .gradio-container{max-width: 560px !important}
164
+ h1{text-align:center}
165
+ footer {
166
+ visibility: hidden
167
+ }
168
+ '''
169
+ with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
170
+ gr.Markdown(DESCRIPTION)
171
+ gr.DuplicateButton(
172
+ value="Duplicate Space for private use",
173
+ elem_id="duplicate-button",
174
+ visible=False,
175
+ )
176
+
177
+ with gr.Group():
178
+ with gr.Row():
179
+ prompt = gr.Text(
180
+ label="Prompt",
181
+ show_label=False,
182
+ max_lines=1,
183
+ placeholder="Enter your prompt",
184
+ container=False,
185
+ )
186
+ run_button = gr.Button("Run")
187
+ result = gr.Gallery(label="Result", columns=1, preview=True)
188
+ with gr.Accordion("Advanced options", open=False):
189
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False, visible=True)
190
+ negative_prompt = gr.Text(
191
+ label="Negative prompt",
192
+ max_lines=1,
193
+ placeholder="Enter a negative prompt",
194
+ visible=True,
195
+ )
196
+ with gr.Row():
197
+ num_inference_steps = gr.Slider(
198
+ label="Steps",
199
+ minimum=10,
200
+ maximum=60,
201
+ step=1,
202
+ value=30,
203
+ )
204
+ with gr.Row():
205
+ num_images_per_prompt = gr.Slider(
206
+ label="Images",
207
+ minimum=1,
208
+ maximum=5,
209
+ step=1,
210
+ value=2,
211
+ )
212
+ seed = gr.Slider(
213
+ label="Seed",
214
+ minimum=0,
215
+ maximum=MAX_SEED,
216
+ step=1,
217
+ value=0,
218
+ visible=True
219
+ )
220
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
221
+ with gr.Row(visible=True):
222
+ width = gr.Slider(
223
+ label="Width",
224
+ minimum=512,
225
+ maximum=2048,
226
+ step=8,
227
+ value=1024,
228
+ )
229
+ height = gr.Slider(
230
+ label="Height",
231
+ minimum=512,
232
+ maximum=2048,
233
+ step=8,
234
+ value=1024,
235
+ )
236
+ with gr.Row():
237
+ guidance_scale = gr.Slider(
238
+ label="Guidance Scale",
239
+ minimum=0.1,
240
+ maximum=20.0,
241
+ step=0.1,
242
+ value=6,
243
+ )
244
+ with gr.Row(visible=True):
245
+ style_selection = gr.Radio(
246
+ show_label=True,
247
+ container=True,
248
+ interactive=True,
249
+ choices=STYLE_NAMES,
250
+ value=DEFAULT_STYLE_NAME,
251
+ label="Image Style",
252
+ )
253
+
254
+
255
+ gr.Examples(
256
+ examples=examples,
257
+ inputs=prompt,
258
+ outputs=[result, seed],
259
+ fn=generate,
260
+ cache_examples=False,
261
+ )
262
+
263
+ use_negative_prompt.change(
264
+ fn=lambda x: gr.update(visible=x),
265
+ inputs=use_negative_prompt,
266
+ outputs=negative_prompt,
267
+ api_name=False,
268
+ )
269
+
270
+
271
+ gr.on(
272
+ triggers=[
273
+ prompt.submit,
274
+ negative_prompt.submit,
275
+ run_button.click,
276
+ ],
277
+ fn=generate,
278
+ inputs=[
279
+ prompt,
280
+ negative_prompt,
281
+ style_selection,
282
+ use_negative_prompt,
283
+ num_inference_steps,
284
+ num_images_per_prompt,
285
+ seed,
286
+ width,
287
+ height,
288
+ guidance_scale,
289
+ randomize_seed,
290
+ ],
291
+ outputs=[result, seed],
292
+ api_name="run",
293
+ )
294
+
295
+ if __name__ == "__main__":
296
+ demo.queue(max_size=20).launch(show_api=False, debug=False)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch
2
+ diffusers
3
+ transformers
4
+ gradio
5
+ peft