Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,4 @@
|
|
1 |
#!/usr/bin/env python
|
2 |
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
3 |
-
# of this software and associated documentation files (the "Software"), to deal
|
4 |
-
# in the Software without restriction, including without limitation the rights
|
5 |
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
6 |
-
# copies of the Software, and to permit persons to whom the Software is
|
7 |
-
|
8 |
import os
|
9 |
import random
|
10 |
import uuid
|
@@ -14,6 +8,7 @@ from PIL import Image
|
|
14 |
import spaces
|
15 |
import torch
|
16 |
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
|
|
17 |
|
18 |
css = '''
|
19 |
.gradio-container{max-width: 570px !important}
|
@@ -26,15 +21,15 @@ footer {
|
|
26 |
DESCRIPTIONXX = """
|
27 |
## REALVISXL V5 + LIGHTNING ⚡
|
28 |
"""
|
29 |
-
examples = [
|
30 |
|
|
|
31 |
"Illustration of A starry night camp in the mountains, 4k, cinematic --ar 85:128 --v 6.0 --style raw",
|
32 |
"A delicious ceviche cheesecake slice, 4k, octane render, ray tracing, Ultra-High-Definition"
|
33 |
]
|
34 |
|
35 |
MODEL_OPTIONS = {
|
36 |
"REALVISXL V5.0": "SG161222/RealVisXL_V5.0",
|
37 |
-
#"LIGHTNING V5.0": "SG161222/RealVisXL_V5.0_Lightning",
|
38 |
}
|
39 |
|
40 |
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
@@ -44,6 +39,43 @@ BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1"))
|
|
44 |
|
45 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
def load_and_prepare_model(model_id):
|
48 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
49 |
model_id,
|
@@ -82,6 +114,7 @@ def generate(
|
|
82 |
prompt: str,
|
83 |
negative_prompt: str = "",
|
84 |
use_negative_prompt: bool = False,
|
|
|
85 |
seed: int = 1,
|
86 |
width: int = 1024,
|
87 |
height: int = 1024,
|
@@ -98,6 +131,8 @@ def generate(
|
|
98 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
99 |
generator = torch.Generator(device=device).manual_seed(seed)
|
100 |
|
|
|
|
|
101 |
options = {
|
102 |
"prompt": [prompt] * num_images,
|
103 |
"negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
|
@@ -123,35 +158,6 @@ def generate(
|
|
123 |
image_paths = [save_image(img) for img in images]
|
124 |
return image_paths, seed
|
125 |
|
126 |
-
#def load_predefined_images():
|
127 |
-
# predefined_images = [
|
128 |
-
# "assets/1.png",
|
129 |
-
# "assets/2.png",
|
130 |
-
# "assets/3.png",
|
131 |
-
# "assets/4.png",
|
132 |
-
# "assets/5.png",
|
133 |
-
# "assets/6.png",
|
134 |
-
# "assets/7.png",
|
135 |
-
#"assets/8.png",
|
136 |
-
#"assets/9.png",
|
137 |
-
#]
|
138 |
-
#return predefined_images
|
139 |
-
|
140 |
-
|
141 |
-
# def load_predefined_images():
|
142 |
-
# predefined_images = [
|
143 |
-
# "assets2/11.png",
|
144 |
-
# "assets2/22.png",
|
145 |
-
# "assets2/33.png",
|
146 |
-
# "assets2/44.png",
|
147 |
-
# "assets2/55.png",
|
148 |
-
# "assets2/66.png",
|
149 |
-
# "assets2/77.png",
|
150 |
-
# "assets2/88.png",
|
151 |
-
# "assets2/99.png",
|
152 |
-
# ]
|
153 |
-
# return predefined_images
|
154 |
-
|
155 |
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
156 |
gr.Markdown(DESCRIPTIONXX)
|
157 |
with gr.Row():
|
@@ -172,7 +178,15 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
172 |
value="REALVISXL V5.0"
|
173 |
)
|
174 |
|
175 |
-
with gr.Accordion("Advanced options", open=False, visible=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
num_images = gr.Slider(
|
177 |
label="Number of Images",
|
178 |
minimum=1,
|
@@ -255,46 +269,17 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
255 |
prompt,
|
256 |
negative_prompt,
|
257 |
use_negative_prompt,
|
|
|
258 |
seed,
|
259 |
width,
|
260 |
height,
|
261 |
guidance_scale,
|
262 |
num_inference_steps,
|
263 |
-
randomize_seed,
|
264 |
-
num_images
|
265 |
],
|
266 |
outputs=[result, seed],
|
267 |
-
api_name="run",
|
268 |
)
|
269 |
|
270 |
-
|
271 |
-
#gr.Markdown("### REALVISXL V5.0")
|
272 |
-
#predefined_gallery = gr.Gallery(label="REALVISXL V5.0", columns=3, show_label=False, value=load_predefined_images1())
|
273 |
-
|
274 |
-
#gr.Markdown("### LIGHTNING V5.0")
|
275 |
-
#predefined_gallery = gr.Gallery(label="LIGHTNING V5.0", columns=3, show_label=False, value=load_predefined_images())
|
276 |
-
|
277 |
-
gr.Markdown(
|
278 |
-
"""
|
279 |
-
<div style="text-align: justify;">
|
280 |
-
⚡Models used in the playground <a href="https://huggingface.co/SG161222/RealVisXL_V5.0">[REALVISXL V5.0]</a>, <a href="https://huggingface.co/SG161222/RealVisXL_V5.0_Lightning">[REALVISXL V5.0 LIGHTNING]</a> for image generation. Stable Diffusion XL piped (SDXL) model HF. This is the demo space for generating images using the Stable Diffusion XL models, with multiple different variants available.
|
281 |
-
</div>
|
282 |
-
""")
|
283 |
-
|
284 |
-
gr.Markdown(
|
285 |
-
"""
|
286 |
-
<div style="text-align: justify;">
|
287 |
-
⚡This is the demo space for generating images using Stable Diffusion XL with quality styles, different models, and types. Try the sample prompts to generate higher quality images. Try the sample prompts for generating higher quality images.
|
288 |
-
<a href='https://huggingface.co/spaces/prithivMLmods/Top-Prompt-Collection' target='_blank'>Try prompts</a>.
|
289 |
-
</div>
|
290 |
-
""")
|
291 |
-
|
292 |
-
gr.Markdown(
|
293 |
-
"""
|
294 |
-
<div style="text-align: justify;">
|
295 |
-
⚠️ Users are accountable for the content they generate and are responsible for ensuring it meets appropriate ethical standards.
|
296 |
-
</div>
|
297 |
-
""")
|
298 |
-
|
299 |
if __name__ == "__main__":
|
300 |
-
demo.queue(max_size=50).launch(show_api=False)
|
|
|
1 |
#!/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
import random
|
4 |
import uuid
|
|
|
8 |
import spaces
|
9 |
import torch
|
10 |
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
11 |
+
from typing import Tuple
|
12 |
|
13 |
css = '''
|
14 |
.gradio-container{max-width: 570px !important}
|
|
|
21 |
DESCRIPTIONXX = """
|
22 |
## REALVISXL V5 + LIGHTNING ⚡
|
23 |
"""
|
|
|
24 |
|
25 |
+
examples = [
|
26 |
"Illustration of A starry night camp in the mountains, 4k, cinematic --ar 85:128 --v 6.0 --style raw",
|
27 |
"A delicious ceviche cheesecake slice, 4k, octane render, ray tracing, Ultra-High-Definition"
|
28 |
]
|
29 |
|
30 |
MODEL_OPTIONS = {
|
31 |
"REALVISXL V5.0": "SG161222/RealVisXL_V5.0",
|
32 |
+
# "LIGHTNING V5.0": "SG161222/RealVisXL_V5.0_Lightning",
|
33 |
}
|
34 |
|
35 |
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
|
|
39 |
|
40 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
41 |
|
42 |
+
style_list = [
|
43 |
+
{
|
44 |
+
"name": "3840 x 2160",
|
45 |
+
"prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
46 |
+
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
47 |
+
},
|
48 |
+
{
|
49 |
+
"name": "2560 x 1440",
|
50 |
+
"prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
51 |
+
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
52 |
+
},
|
53 |
+
{
|
54 |
+
"name": "HD+",
|
55 |
+
"prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
56 |
+
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
57 |
+
},
|
58 |
+
{
|
59 |
+
"name": "Style Zero",
|
60 |
+
"prompt": "{prompt}",
|
61 |
+
"negative_prompt": "",
|
62 |
+
},
|
63 |
+
]
|
64 |
+
|
65 |
+
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
66 |
+
DEFAULT_STYLE_NAME = "3840 x 2160"
|
67 |
+
STYLE_NAMES = list(styles.keys())
|
68 |
+
|
69 |
+
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
70 |
+
if style_name in styles:
|
71 |
+
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
72 |
+
else:
|
73 |
+
p, n = styles[DEFAULT_STYLE_NAME]
|
74 |
+
|
75 |
+
if not negative:
|
76 |
+
negative = ""
|
77 |
+
return p.replace("{prompt}", positive), n + negative
|
78 |
+
|
79 |
def load_and_prepare_model(model_id):
|
80 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
81 |
model_id,
|
|
|
114 |
prompt: str,
|
115 |
negative_prompt: str = "",
|
116 |
use_negative_prompt: bool = False,
|
117 |
+
style_selection: str = DEFAULT_STYLE_NAME,
|
118 |
seed: int = 1,
|
119 |
width: int = 1024,
|
120 |
height: int = 1024,
|
|
|
131 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
132 |
generator = torch.Generator(device=device).manual_seed(seed)
|
133 |
|
134 |
+
prompt, negative_prompt = apply_style(style_selection, prompt, negative_prompt)
|
135 |
+
|
136 |
options = {
|
137 |
"prompt": [prompt] * num_images,
|
138 |
"negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
|
|
|
158 |
image_paths = [save_image(img) for img in images]
|
159 |
return image_paths, seed
|
160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
162 |
gr.Markdown(DESCRIPTIONXX)
|
163 |
with gr.Row():
|
|
|
178 |
value="REALVISXL V5.0"
|
179 |
)
|
180 |
|
181 |
+
with gr.Accordion("Advanced options", open=False, visible=False):
|
182 |
+
style_selection = gr.Radio(
|
183 |
+
show_label=True,
|
184 |
+
container=True,
|
185 |
+
interactive=True,
|
186 |
+
choices=STYLE_NAMES,
|
187 |
+
value=DEFAULT_STYLE_NAME,
|
188 |
+
label="Quality Style",
|
189 |
+
)
|
190 |
num_images = gr.Slider(
|
191 |
label="Number of Images",
|
192 |
minimum=1,
|
|
|
269 |
prompt,
|
270 |
negative_prompt,
|
271 |
use_negative_prompt,
|
272 |
+
style_selection,
|
273 |
seed,
|
274 |
width,
|
275 |
height,
|
276 |
guidance_scale,
|
277 |
num_inference_steps,
|
278 |
+
randomize_seed,
|
279 |
+
num_images,
|
280 |
],
|
281 |
outputs=[result, seed],
|
|
|
282 |
)
|
283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
284 |
if __name__ == "__main__":
|
285 |
+
demo.queue(max_size=50).launch(show_api=False)
|