Spaces:
Running
on
Zero
Running
on
Zero
Delete last-commit
Browse files- last-commit/03082024.txt +0 -263
- last-commit/app.txt +0 -262
- last-commit/doc.txt +0 -320
- last-commit/file.txt +0 -274
last-commit/03082024.txt
DELETED
@@ -1,263 +0,0 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
import os
|
3 |
-
import random
|
4 |
-
import uuid
|
5 |
-
import gradio as gr
|
6 |
-
import numpy as np
|
7 |
-
from PIL import Image
|
8 |
-
import spaces
|
9 |
-
import torch
|
10 |
-
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
11 |
-
|
12 |
-
css = '''
|
13 |
-
.gradio-container{max-width: 570px !important}
|
14 |
-
h1{text-align:center}
|
15 |
-
footer {
|
16 |
-
visibility: hidden
|
17 |
-
}
|
18 |
-
'''
|
19 |
-
|
20 |
-
DESCRIPTIONXX = """
|
21 |
-
## TEXT 2 IMAGE🥠
|
22 |
-
"""
|
23 |
-
examples = [
|
24 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
25 |
-
"Chocolate dripping from a donut against a yellow background, 8k",
|
26 |
-
"Illustration of A starry night camp in the mountains, 4k, cinematic --ar 85:128 --v 6.0 --style raw",
|
27 |
-
"A photo of a lavender cat, hdr, 4k, --ar 85:128 --v 6.0 --style raw",
|
28 |
-
"A delicious ceviche cheesecake slice, 4k, octane render, ray tracing, Ultra-High-Definition"
|
29 |
-
]
|
30 |
-
|
31 |
-
MODEL_OPTIONS = {
|
32 |
-
"Lightning": "SG161222/RealVisXL_V4.0_Lightning",
|
33 |
-
"Turbovision": "SG161222/RealVisXL_V3.0_Turbo",
|
34 |
-
|
35 |
-
}
|
36 |
-
|
37 |
-
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
38 |
-
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
39 |
-
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
40 |
-
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1"))
|
41 |
-
|
42 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
43 |
-
|
44 |
-
def load_and_prepare_model(model_id):
|
45 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
46 |
-
model_id,
|
47 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
48 |
-
use_safetensors=True,
|
49 |
-
add_watermarker=False,
|
50 |
-
).to(device)
|
51 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
52 |
-
|
53 |
-
if USE_TORCH_COMPILE:
|
54 |
-
pipe.compile()
|
55 |
-
|
56 |
-
if ENABLE_CPU_OFFLOAD:
|
57 |
-
pipe.enable_model_cpu_offload()
|
58 |
-
|
59 |
-
return pipe
|
60 |
-
|
61 |
-
# Preload and compile both models
|
62 |
-
models = {key: load_and_prepare_model(value) for key, value in MODEL_OPTIONS.items()}
|
63 |
-
|
64 |
-
MAX_SEED = np.iinfo(np.int32).max
|
65 |
-
|
66 |
-
def save_image(img):
|
67 |
-
unique_name = str(uuid.uuid4()) + ".png"
|
68 |
-
img.save(unique_name)
|
69 |
-
return unique_name
|
70 |
-
|
71 |
-
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
72 |
-
if randomize_seed:
|
73 |
-
seed = random.randint(0, MAX_SEED)
|
74 |
-
return seed
|
75 |
-
|
76 |
-
@spaces.GPU(duration=60, enable_queue=True)
|
77 |
-
def generate(
|
78 |
-
model_choice: str,
|
79 |
-
prompt: str,
|
80 |
-
negative_prompt: str = "",
|
81 |
-
use_negative_prompt: bool = False,
|
82 |
-
seed: int = 1,
|
83 |
-
width: int = 1024,
|
84 |
-
height: int = 1024,
|
85 |
-
guidance_scale: float = 3,
|
86 |
-
num_inference_steps: int = 25,
|
87 |
-
randomize_seed: bool = False,
|
88 |
-
use_resolution_binning: bool = True,
|
89 |
-
num_images: int = 1,
|
90 |
-
progress=gr.Progress(track_tqdm=True),
|
91 |
-
):
|
92 |
-
global models
|
93 |
-
pipe = models[model_choice]
|
94 |
-
|
95 |
-
seed = int(randomize_seed_fn(seed, randomize_seed))
|
96 |
-
generator = torch.Generator(device=device).manual_seed(seed)
|
97 |
-
|
98 |
-
options = {
|
99 |
-
"prompt": [prompt] * num_images,
|
100 |
-
"negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
|
101 |
-
"width": width,
|
102 |
-
"height": height,
|
103 |
-
"guidance_scale": guidance_scale,
|
104 |
-
"num_inference_steps": num_inference_steps,
|
105 |
-
"generator": generator,
|
106 |
-
"output_type": "pil",
|
107 |
-
}
|
108 |
-
|
109 |
-
if use_resolution_binning:
|
110 |
-
options["use_resolution_binning"] = True
|
111 |
-
|
112 |
-
images = []
|
113 |
-
for i in range(0, num_images, BATCH_SIZE):
|
114 |
-
batch_options = options.copy()
|
115 |
-
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
116 |
-
if "negative_prompt" in batch_options:
|
117 |
-
batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
|
118 |
-
images.extend(pipe(**batch_options).images)
|
119 |
-
|
120 |
-
image_paths = [save_image(img) for img in images]
|
121 |
-
return image_paths, seed
|
122 |
-
|
123 |
-
def load_predefined_images():
|
124 |
-
predefined_images = [
|
125 |
-
"assets/1.png",
|
126 |
-
"assets/2.png",
|
127 |
-
"assets/3.png",
|
128 |
-
"assets/4.png",
|
129 |
-
"assets/5.png",
|
130 |
-
"assets/6.png",
|
131 |
-
"assets/7.png",
|
132 |
-
"assets/8.png",
|
133 |
-
"assets/9.png",
|
134 |
-
"assets/10.png",
|
135 |
-
"assets/11.png",
|
136 |
-
"assets/12.png",
|
137 |
-
]
|
138 |
-
return predefined_images
|
139 |
-
|
140 |
-
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
141 |
-
gr.Markdown(DESCRIPTIONXX)
|
142 |
-
with gr.Row():
|
143 |
-
prompt = gr.Text(
|
144 |
-
label="Prompt",
|
145 |
-
show_label=False,
|
146 |
-
max_lines=1,
|
147 |
-
placeholder="Enter your prompt",
|
148 |
-
container=False,
|
149 |
-
)
|
150 |
-
run_button = gr.Button("Run⚡", scale=0)
|
151 |
-
result = gr.Gallery(label="Result", columns=1, show_label=False)
|
152 |
-
|
153 |
-
with gr.Row():
|
154 |
-
model_choice = gr.Dropdown(
|
155 |
-
label="Model Selection",
|
156 |
-
choices=list(MODEL_OPTIONS.keys()),
|
157 |
-
value="Lightning"
|
158 |
-
)
|
159 |
-
|
160 |
-
with gr.Accordion("Advanced options", open=True, visible=False):
|
161 |
-
num_images = gr.Slider(
|
162 |
-
label="Number of Images",
|
163 |
-
minimum=1,
|
164 |
-
maximum=1,
|
165 |
-
step=1,
|
166 |
-
value=1,
|
167 |
-
)
|
168 |
-
with gr.Row():
|
169 |
-
with gr.Column(scale=1):
|
170 |
-
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
171 |
-
negative_prompt = gr.Text(
|
172 |
-
label="Negative prompt",
|
173 |
-
max_lines=5,
|
174 |
-
lines=4,
|
175 |
-
placeholder="Enter a negative prompt",
|
176 |
-
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",
|
177 |
-
visible=True,
|
178 |
-
)
|
179 |
-
seed = gr.Slider(
|
180 |
-
label="Seed",
|
181 |
-
minimum=0,
|
182 |
-
maximum=MAX_SEED,
|
183 |
-
step=1,
|
184 |
-
value=0,
|
185 |
-
)
|
186 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
187 |
-
with gr.Row():
|
188 |
-
width = gr.Slider(
|
189 |
-
label="Width",
|
190 |
-
minimum=512,
|
191 |
-
maximum=MAX_IMAGE_SIZE,
|
192 |
-
step=64,
|
193 |
-
value=1024,
|
194 |
-
)
|
195 |
-
height = gr.Slider(
|
196 |
-
label="Height",
|
197 |
-
minimum=512,
|
198 |
-
maximum=MAX_IMAGE_SIZE,
|
199 |
-
step=64,
|
200 |
-
value=1024,
|
201 |
-
)
|
202 |
-
with gr.Row():
|
203 |
-
guidance_scale = gr.Slider(
|
204 |
-
label="Guidance Scale",
|
205 |
-
minimum=0.1,
|
206 |
-
maximum=6,
|
207 |
-
step=0.1,
|
208 |
-
value=3.0,
|
209 |
-
)
|
210 |
-
num_inference_steps = gr.Slider(
|
211 |
-
label="Number of inference steps",
|
212 |
-
minimum=1,
|
213 |
-
maximum=35,
|
214 |
-
step=1,
|
215 |
-
value=20,
|
216 |
-
)
|
217 |
-
|
218 |
-
gr.Examples(
|
219 |
-
examples=examples,
|
220 |
-
inputs=prompt,
|
221 |
-
cache_examples=False
|
222 |
-
)
|
223 |
-
|
224 |
-
use_negative_prompt.change(
|
225 |
-
fn=lambda x: gr.update(visible=x),
|
226 |
-
inputs=use_negative_prompt,
|
227 |
-
outputs=negative_prompt,
|
228 |
-
api_name=False,
|
229 |
-
)
|
230 |
-
|
231 |
-
gr.on(
|
232 |
-
triggers=[
|
233 |
-
prompt.submit,
|
234 |
-
negative_prompt.submit,
|
235 |
-
run_button.click,
|
236 |
-
],
|
237 |
-
fn=generate,
|
238 |
-
inputs=[
|
239 |
-
model_choice,
|
240 |
-
prompt,
|
241 |
-
negative_prompt,
|
242 |
-
use_negative_prompt,
|
243 |
-
seed,
|
244 |
-
width,
|
245 |
-
height,
|
246 |
-
guidance_scale,
|
247 |
-
num_inference_steps,
|
248 |
-
randomize_seed,
|
249 |
-
num_images
|
250 |
-
],
|
251 |
-
outputs=[result, seed],
|
252 |
-
api_name="run",
|
253 |
-
)
|
254 |
-
gr.Markdown("🥠Models used in the playground [[Lightning]](https://huggingface.co/SG161222/RealVisXL_V4.0_Lightning), [[Turbo]](https://huggingface.co/SG161222/RealVisXL_V3.0_Turbo) 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 multi different variants available.")
|
255 |
-
gr.Markdown("🥠This is the demo space for generating images using Stable Diffusion 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.<a href='https://huggingface.co/spaces/prithivMLmods/Top-Prompt-Collection' target='_blank'>Try prompts</a>.")
|
256 |
-
gr.Markdown("⚠️ users are accountable for the content they generate and are responsible for ensuring it meets appropriate ethical standards.")
|
257 |
-
|
258 |
-
with gr.Column(scale=3):
|
259 |
-
gr.Markdown("### Image Gallery")
|
260 |
-
predefined_gallery = gr.Gallery(label="Image Gallery", columns=3, show_label=False, value=load_predefined_images())
|
261 |
-
|
262 |
-
if __name__ == "__main__":
|
263 |
-
demo.queue(max_size=20).launch(show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
last-commit/app.txt
DELETED
@@ -1,262 +0,0 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
import os
|
3 |
-
import random
|
4 |
-
import uuid
|
5 |
-
import gradio as gr
|
6 |
-
import numpy as np
|
7 |
-
from PIL import Image
|
8 |
-
import spaces
|
9 |
-
import torch
|
10 |
-
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
11 |
-
|
12 |
-
DESCRIPTIONx = """
|
13 |
-
|
14 |
-
|
15 |
-
"""
|
16 |
-
|
17 |
-
css = '''
|
18 |
-
.gradio-container{max-width: 560px !important}
|
19 |
-
h1{text-align:center}
|
20 |
-
footer {
|
21 |
-
visibility: hidden
|
22 |
-
}
|
23 |
-
'''
|
24 |
-
|
25 |
-
#examples = [
|
26 |
-
# "3d image, cute girl, in the style of Pixar --ar 1:2 --stylize 750, 4K resolution highlights, Sharp focus, octane render, ray tracing, Ultra-High-Definition, 8k, UHD, HDR, (Masterpiece:1.5), (best quality:1.5)",
|
27 |
-
# "Chocolate dripping from a donut against a yellow background, in the style of brocore, hyper-realistic oil --ar 2:3 --q 2 --s 750 --v 5 --ar 2:3 --q 2 --s 750 --v 5",
|
28 |
-
# "Illustration of A starry night camp in the mountains. Low-angle view, Minimal background, Geometric shapes theme, Pottery, Split-complementary colors, Bicolored light, UHD",
|
29 |
-
# "Man in brown leather jacket posing for camera, in the style of sleek and stylized, clockpunk, subtle shades, exacting precision, ferrania p30 --ar 67:101 --v 5",
|
30 |
-
# "Commercial photography, giant burger, white lighting, studio light, 8k octane rendering, high resolution photography, insanely detailed, fine details, on white isolated plain, 8k, commercial photography, stock photo, professional color grading, --v 4 --ar 9:16 "
|
31 |
-
#]
|
32 |
-
|
33 |
-
MODEL_OPTIONS = {
|
34 |
-
"Lightning": "SG161222/RealVisXL_V4.0_Lightning",
|
35 |
-
"Realvision": "SG161222/RealVisXL_V4.0",
|
36 |
-
}
|
37 |
-
|
38 |
-
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
39 |
-
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
40 |
-
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
41 |
-
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1"))
|
42 |
-
|
43 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
44 |
-
|
45 |
-
def load_and_prepare_model(model_id):
|
46 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
47 |
-
model_id,
|
48 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
49 |
-
use_safetensors=True,
|
50 |
-
add_watermarker=False,
|
51 |
-
).to(device)
|
52 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
53 |
-
|
54 |
-
if USE_TORCH_COMPILE:
|
55 |
-
pipe.compile()
|
56 |
-
|
57 |
-
if ENABLE_CPU_OFFLOAD:
|
58 |
-
pipe.enable_model_cpu_offload()
|
59 |
-
|
60 |
-
return pipe
|
61 |
-
|
62 |
-
# Preload and compile both models
|
63 |
-
models = {key: load_and_prepare_model(value) for key, value in MODEL_OPTIONS.items()}
|
64 |
-
|
65 |
-
MAX_SEED = np.iinfo(np.int32).max
|
66 |
-
|
67 |
-
def save_image(img):
|
68 |
-
unique_name = str(uuid.uuid4()) + ".png"
|
69 |
-
img.save(unique_name)
|
70 |
-
return unique_name
|
71 |
-
|
72 |
-
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
73 |
-
if randomize_seed:
|
74 |
-
seed = random.randint(0, MAX_SEED)
|
75 |
-
return seed
|
76 |
-
|
77 |
-
@spaces.GPU(duration=60, enable_queue=True)
|
78 |
-
def generate(
|
79 |
-
model_choice: str,
|
80 |
-
prompt: str,
|
81 |
-
negative_prompt: str = "",
|
82 |
-
use_negative_prompt: bool = False,
|
83 |
-
seed: int = 1,
|
84 |
-
width: int = 1024,
|
85 |
-
height: int = 1024,
|
86 |
-
guidance_scale: float = 3,
|
87 |
-
num_inference_steps: int = 25,
|
88 |
-
randomize_seed: bool = False,
|
89 |
-
use_resolution_binning: bool = True,
|
90 |
-
num_images: int = 1,
|
91 |
-
progress=gr.Progress(track_tqdm=True),
|
92 |
-
):
|
93 |
-
global models
|
94 |
-
pipe = models[model_choice]
|
95 |
-
|
96 |
-
seed = int(randomize_seed_fn(seed, randomize_seed))
|
97 |
-
generator = torch.Generator(device=device).manual_seed(seed)
|
98 |
-
|
99 |
-
options = {
|
100 |
-
"prompt": [prompt] * num_images,
|
101 |
-
"negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
|
102 |
-
"width": width,
|
103 |
-
"height": height,
|
104 |
-
"guidance_scale": guidance_scale,
|
105 |
-
"num_inference_steps": num_inference_steps,
|
106 |
-
"generator": generator,
|
107 |
-
"output_type": "pil",
|
108 |
-
}
|
109 |
-
|
110 |
-
if use_resolution_binning:
|
111 |
-
options["use_resolution_binning"] = True
|
112 |
-
|
113 |
-
images = []
|
114 |
-
for i in range(0, num_images, BATCH_SIZE):
|
115 |
-
batch_options = options.copy()
|
116 |
-
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
117 |
-
if "negative_prompt" in batch_options:
|
118 |
-
batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
|
119 |
-
images.extend(pipe(**batch_options).images)
|
120 |
-
|
121 |
-
image_paths = [save_image(img) for img in images]
|
122 |
-
return image_paths, seed
|
123 |
-
|
124 |
-
def load_predefined_images():
|
125 |
-
predefined_images = [
|
126 |
-
"assets/1.png",
|
127 |
-
"assets/2.png",
|
128 |
-
"assets/3.png",
|
129 |
-
"assets/4.png",
|
130 |
-
"assets/5.png",
|
131 |
-
"assets/6.png",
|
132 |
-
"assets/7.png",
|
133 |
-
"assets/8.png",
|
134 |
-
"assets/9.png",
|
135 |
-
"assets/10.png",
|
136 |
-
"assets/11.png",
|
137 |
-
"assets/12.png",
|
138 |
-
]
|
139 |
-
return predefined_images
|
140 |
-
|
141 |
-
with gr.Blocks(css=css) as demo:
|
142 |
-
gr.Markdown(DESCRIPTIONx)
|
143 |
-
with gr.Row():
|
144 |
-
prompt = gr.Text(
|
145 |
-
label="Prompt",
|
146 |
-
show_label=False,
|
147 |
-
max_lines=1,
|
148 |
-
placeholder="Enter your prompt",
|
149 |
-
value="A cartoon of a Ironman fighting with Hulk, wall painting",
|
150 |
-
container=False,
|
151 |
-
)
|
152 |
-
run_button = gr.Button("Run⚡", scale=0)
|
153 |
-
result = gr.Gallery(label="Result", columns=1, show_label=False)
|
154 |
-
|
155 |
-
with gr.Row():
|
156 |
-
model_choice = gr.Dropdown(
|
157 |
-
label="Model Selection",
|
158 |
-
choices=list(MODEL_OPTIONS.keys()),
|
159 |
-
value="Lightning"
|
160 |
-
)
|
161 |
-
|
162 |
-
with gr.Accordion("Advanced options", open=True, visible=False):
|
163 |
-
num_images = gr.Slider(
|
164 |
-
label="Number of Images",
|
165 |
-
minimum=1,
|
166 |
-
maximum=1,
|
167 |
-
step=1,
|
168 |
-
value=1,
|
169 |
-
)
|
170 |
-
with gr.Row():
|
171 |
-
with gr.Column(scale=1):
|
172 |
-
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
173 |
-
negative_prompt = gr.Text(
|
174 |
-
label="Negative prompt",
|
175 |
-
max_lines=5,
|
176 |
-
lines=4,
|
177 |
-
placeholder="Enter a negative prompt",
|
178 |
-
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",
|
179 |
-
visible=True,
|
180 |
-
)
|
181 |
-
seed = gr.Slider(
|
182 |
-
label="Seed",
|
183 |
-
minimum=0,
|
184 |
-
maximum=MAX_SEED,
|
185 |
-
step=1,
|
186 |
-
value=0,
|
187 |
-
)
|
188 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
189 |
-
with gr.Row():
|
190 |
-
width = gr.Slider(
|
191 |
-
label="Width",
|
192 |
-
minimum=512,
|
193 |
-
maximum=MAX_IMAGE_SIZE,
|
194 |
-
step=64,
|
195 |
-
value=1024,
|
196 |
-
)
|
197 |
-
height = gr.Slider(
|
198 |
-
label="Height",
|
199 |
-
minimum=512,
|
200 |
-
maximum=MAX_IMAGE_SIZE,
|
201 |
-
step=64,
|
202 |
-
value=1024,
|
203 |
-
)
|
204 |
-
with gr.Row():
|
205 |
-
guidance_scale = gr.Slider(
|
206 |
-
label="Guidance Scale",
|
207 |
-
minimum=0.1,
|
208 |
-
maximum=6,
|
209 |
-
step=0.1,
|
210 |
-
value=3.0,
|
211 |
-
)
|
212 |
-
num_inference_steps = gr.Slider(
|
213 |
-
label="Number of inference steps",
|
214 |
-
minimum=1,
|
215 |
-
maximum=35,
|
216 |
-
step=1,
|
217 |
-
value=20,
|
218 |
-
)
|
219 |
-
|
220 |
-
# gr.Examples(
|
221 |
-
# examples=examples,
|
222 |
-
# inputs=prompt,
|
223 |
-
# cache_examples=False
|
224 |
-
#)
|
225 |
-
|
226 |
-
use_negative_prompt.change(
|
227 |
-
fn=lambda x: gr.update(visible=x),
|
228 |
-
inputs=use_negative_prompt,
|
229 |
-
outputs=negative_prompt,
|
230 |
-
api_name=False,
|
231 |
-
)
|
232 |
-
|
233 |
-
gr.on(
|
234 |
-
triggers=[
|
235 |
-
prompt.submit,
|
236 |
-
negative_prompt.submit,
|
237 |
-
run_button.click,
|
238 |
-
],
|
239 |
-
fn=generate,
|
240 |
-
inputs=[
|
241 |
-
model_choice,
|
242 |
-
prompt,
|
243 |
-
negative_prompt,
|
244 |
-
use_negative_prompt,
|
245 |
-
seed,
|
246 |
-
width,
|
247 |
-
height,
|
248 |
-
guidance_scale,
|
249 |
-
num_inference_steps,
|
250 |
-
randomize_seed,
|
251 |
-
num_images
|
252 |
-
],
|
253 |
-
outputs=[result, seed],
|
254 |
-
api_name="run",
|
255 |
-
)
|
256 |
-
|
257 |
-
# with gr.Column(scale=3):
|
258 |
-
# gr.Markdown("### Image Gallery")
|
259 |
-
# predefined_gallery = gr.Gallery(label="Image Gallery", columns=4, show_label=False, value=load_predefined_images())
|
260 |
-
|
261 |
-
if __name__ == "__main__":
|
262 |
-
demo.queue(max_size=40).launch(show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
last-commit/doc.txt
DELETED
@@ -1,320 +0,0 @@
|
|
1 |
-
# TEXT 2 IMAGE PLAYGROUND Documentation
|
2 |
-
|
3 |
-
## Overview
|
4 |
-
|
5 |
-
TEXT 2 IMAGE PLAYGROUND is a Gradio-based web application designed to generate images from text prompts using advanced AI models. It offers various model options, customization parameters, and a user-friendly interface for an enhanced user experience.
|
6 |
-
|
7 |
-
## Features
|
8 |
-
|
9 |
-
- **Model Selection**: Choose from multiple AI models to generate images with different styles and qualities.
|
10 |
-
- **Custom Prompts**: Input text prompts to define the content and style of the generated images.
|
11 |
-
- **Negative Prompts**: Use negative prompts to avoid unwanted elements in the images.
|
12 |
-
- **Image Customization**: Adjust parameters like seed, width, height, guidance scale, and number of inference steps.
|
13 |
-
- **Random Seed Generation**: Enable random seed generation for varied outputs.
|
14 |
-
- **Image Gallery**: View a gallery of predefined images for inspiration.
|
15 |
-
|
16 |
-
## Interface
|
17 |
-
|
18 |
-
### Description
|
19 |
-
|
20 |
-
```markdown
|
21 |
-
## TEXT 2 IMAGE PLAYGROUND 🥠
|
22 |
-
```
|
23 |
-
|
24 |
-
### CSS
|
25 |
-
|
26 |
-
```css
|
27 |
-
.gradio-container {
|
28 |
-
max-width: 690px !important;
|
29 |
-
}
|
30 |
-
h1 {
|
31 |
-
text-align: center;
|
32 |
-
}
|
33 |
-
footer {
|
34 |
-
visibility: hidden;
|
35 |
-
}
|
36 |
-
```
|
37 |
-
|
38 |
-
### JavaScript
|
39 |
-
|
40 |
-
```javascript
|
41 |
-
function refresh() {
|
42 |
-
const url = new URL(window.location);
|
43 |
-
if (url.searchParams.get('__theme') !== 'dark') {
|
44 |
-
url.searchParams.set('__theme', 'dark');
|
45 |
-
window.location.href = url.href;
|
46 |
-
}
|
47 |
-
}
|
48 |
-
```
|
49 |
-
|
50 |
-
### Examples
|
51 |
-
|
52 |
-
Predefined text prompts for quick testing:
|
53 |
-
|
54 |
-
- 3d image, cute girl, in the style of Pixar...
|
55 |
-
- Chocolate dripping from a donut against a yellow background...
|
56 |
-
- Illustration of A starry night camp in the mountains...
|
57 |
-
- Man in brown leather jacket posing for camera...
|
58 |
-
- Commercial photography, giant burger...
|
59 |
-
|
60 |
-
## Model Options
|
61 |
-
|
62 |
-
```python
|
63 |
-
MODEL_OPTIONS = {
|
64 |
-
"Realism : V4.0_Lightning🔥": "SG161222/RealVisXL_V4.0_Lightning",
|
65 |
-
"Detailed/SOTA : Mobius🚀": "Corcelio/mobius",
|
66 |
-
"Anime : Cagliostrolab🍺": "cagliostrolab/animagine-xl-3.1"
|
67 |
-
}
|
68 |
-
```
|
69 |
-
|
70 |
-
## Configuration
|
71 |
-
|
72 |
-
Environment variables and configurations:
|
73 |
-
|
74 |
-
```python
|
75 |
-
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
76 |
-
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
77 |
-
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
78 |
-
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1"))
|
79 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
80 |
-
```
|
81 |
-
|
82 |
-
## Model Loading and Preparation
|
83 |
-
|
84 |
-
Function to load and prepare models:
|
85 |
-
|
86 |
-
```python
|
87 |
-
def load_and_prepare_model(model_id):
|
88 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
89 |
-
model_id,
|
90 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
91 |
-
use_safetensors=True,
|
92 |
-
add_watermarker=False,
|
93 |
-
).to(device)
|
94 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
95 |
-
|
96 |
-
if USE_TORCH_COMPILE:
|
97 |
-
pipe.compile()
|
98 |
-
|
99 |
-
if ENABLE_CPU_OFFLOAD:
|
100 |
-
pipe.enable_model_cpu_offload()
|
101 |
-
|
102 |
-
return pipe
|
103 |
-
|
104 |
-
models = {key: load_and_prepare_model(value) for key, value in MODEL_OPTIONS.items()}
|
105 |
-
```
|
106 |
-
|
107 |
-
## Image Generation
|
108 |
-
|
109 |
-
Function to generate images based on user inputs:
|
110 |
-
|
111 |
-
```python
|
112 |
-
@spaces.GPU(duration=60, enable_queue=True)
|
113 |
-
def generate(
|
114 |
-
model_choice: str,
|
115 |
-
prompt: str,
|
116 |
-
negative_prompt: str = "",
|
117 |
-
use_negative_prompt: bool = False,
|
118 |
-
seed: int = 1,
|
119 |
-
width: int = 1024,
|
120 |
-
height: int = 1024,
|
121 |
-
guidance_scale: float = 3,
|
122 |
-
num_inference_steps: int = 25,
|
123 |
-
randomize_seed: bool = False,
|
124 |
-
use_resolution_binning: bool = True,
|
125 |
-
num_images: int = 1,
|
126 |
-
progress=gr.Progress(track_tqdm=True),
|
127 |
-
):
|
128 |
-
global models
|
129 |
-
pipe = models[model_choice]
|
130 |
-
|
131 |
-
seed = int(randomize_seed_fn(seed, randomize_seed))
|
132 |
-
generator = torch.Generator(device=device).manual_seed(seed)
|
133 |
-
|
134 |
-
options = {
|
135 |
-
"prompt": [prompt] * num_images,
|
136 |
-
"negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
|
137 |
-
"width": width,
|
138 |
-
"height": height,
|
139 |
-
"guidance_scale": guidance_scale,
|
140 |
-
"num_inference_steps": num_inference_steps,
|
141 |
-
"generator": generator,
|
142 |
-
"output_type": "pil",
|
143 |
-
}
|
144 |
-
|
145 |
-
if use_resolution_binning:
|
146 |
-
options["use_resolution_binning"] = True
|
147 |
-
|
148 |
-
images = []
|
149 |
-
for i in range(0, num_images, BATCH_SIZE):
|
150 |
-
batch_options = options.copy()
|
151 |
-
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
152 |
-
if "negative_prompt" in batch_options:
|
153 |
-
batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
|
154 |
-
images.extend(pipe(**batch_options).images)
|
155 |
-
|
156 |
-
image_paths = [save_image(img) for img in images]
|
157 |
-
return image_paths, seed
|
158 |
-
```
|
159 |
-
|
160 |
-
## Load Predefined Images
|
161 |
-
|
162 |
-
Function to load predefined images for the gallery:
|
163 |
-
|
164 |
-
```python
|
165 |
-
def load_predefined_images():
|
166 |
-
predefined_images = [
|
167 |
-
"assets/1.png",
|
168 |
-
"assets/2.png",
|
169 |
-
"assets/3.png",
|
170 |
-
"assets/4.png",
|
171 |
-
"assets/5.png",
|
172 |
-
"assets/6.png",
|
173 |
-
"assets/7.png",
|
174 |
-
"assets/8.png",
|
175 |
-
"assets/9.png",
|
176 |
-
"assets/10.png",
|
177 |
-
"assets/11.png",
|
178 |
-
"assets/12.png",
|
179 |
-
]
|
180 |
-
return predefined_images
|
181 |
-
```
|
182 |
-
|
183 |
-
## Gradio Interface
|
184 |
-
|
185 |
-
Creating the Gradio interface:
|
186 |
-
|
187 |
-
```python
|
188 |
-
with gr.Blocks(css=css, theme="bethecloud/storj_theme", js=js_func) as demo:
|
189 |
-
gr.Markdown(DESCRIPTIONx)
|
190 |
-
with gr.Row():
|
191 |
-
prompt = gr.Text(
|
192 |
-
label="Prompt",
|
193 |
-
show_label=False,
|
194 |
-
max_lines=1,
|
195 |
-
placeholder="Enter your prompt",
|
196 |
-
value="Chocolate dripping from a donut against a yellow background, in the style of brocore, hyper-realistic oil --ar 2:3 --q 2 --s 750 --v 5 --ar 2:3 --q 2 --s 750 --v 5",
|
197 |
-
container=False,
|
198 |
-
)
|
199 |
-
run_button = gr.Button("Run🚀", scale=0)
|
200 |
-
result = gr.Gallery(label="Result", columns=1, show_label=False)
|
201 |
-
|
202 |
-
with gr.Row():
|
203 |
-
model_choice = gr.Dropdown(
|
204 |
-
label="Model Selection ☑️",
|
205 |
-
choices=list(MODEL_OPTIONS.keys()),
|
206 |
-
value="Realism : V4.0_Lightning🔥"
|
207 |
-
)
|
208 |
-
|
209 |
-
with gr.Accordion("Advanced options", open=True):
|
210 |
-
num_images = gr.Slider(
|
211 |
-
label="Number of Images",
|
212 |
-
minimum=1,
|
213 |
-
maximum=1,
|
214 |
-
step=1,
|
215 |
-
value=1,
|
216 |
-
)
|
217 |
-
with gr.Row():
|
218 |
-
with gr.Column(scale=1):
|
219 |
-
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
220 |
-
negative_prompt = gr.Text(
|
221 |
-
label="Negative prompt",
|
222 |
-
max_lines=5,
|
223 |
-
lines=4,
|
224 |
-
placeholder="Enter a negative prompt",
|
225 |
-
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",
|
226 |
-
visible=True,
|
227 |
-
)
|
228 |
-
seed = gr.Slider(
|
229 |
-
label="Seed",
|
230 |
-
minimum=0,
|
231 |
-
maximum=MAX_SEED,
|
232 |
-
step=1,
|
233 |
-
value=0,
|
234 |
-
)
|
235 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
236 |
-
with gr.Row():
|
237 |
-
width = gr.Slider(
|
238 |
-
label="Width",
|
239 |
-
minimum=512,
|
240 |
-
maximum=MAX_IMAGE_SIZE,
|
241 |
-
step=64,
|
242 |
-
value=1024,
|
243 |
-
)
|
244 |
-
height = gr.Slider(
|
245 |
-
label="Height",
|
246 |
-
minimum=512,
|
247 |
-
maximum=MAX_IMAGE_SIZE,
|
248 |
-
step=64,
|
249 |
-
value=1024,
|
250 |
-
)
|
251 |
-
with gr.Row():
|
252 |
-
guidance_scale = gr.Slider(
|
253 |
-
label="Guidance Scale",
|
254 |
-
minimum=0.1,
|
255 |
-
maximum=6,
|
256 |
-
step=0.1,
|
257 |
-
value=3.0,
|
258 |
-
)
|
259 |
-
num_inference_steps = gr.Slider(
|
260 |
-
label="Number of inference steps",
|
261 |
-
minimum=1,
|
262 |
-
maximum=35,
|
263 |
-
step=1,
|
264 |
-
value=20,
|
265 |
-
)
|
266 |
-
|
267 |
-
gr.Examples(
|
268 |
-
examples=examples,
|
269 |
-
inputs=prompt,
|
270 |
-
cache_examples=False
|
271 |
-
)
|
272 |
-
|
273 |
-
use_negative_prompt.change(
|
274 |
-
fn=lambda x: gr.update(visible=x),
|
275 |
-
inputs=use_negative_prompt,
|
276 |
-
outputs=negative_prompt,
|
277 |
-
api_name=False,
|
278 |
-
)
|
279 |
-
|
280 |
-
gr.on(
|
281 |
-
triggers=[
|
282 |
-
prompt.submit,
|
283 |
-
negative_prompt.submit,
|
284 |
-
run_button.click,
|
285 |
-
],
|
286 |
-
fn=generate,
|
287 |
-
inputs=[
|
288 |
-
model_choice,
|
289 |
-
prompt,
|
290 |
-
negative_prompt,
|
291 |
-
use_negative_prompt,
|
292 |
-
seed,
|
293 |
-
width,
|
294 |
-
height,
|
295 |
-
guidance_scale,
|
296 |
-
num_inference_steps,
|
297 |
-
randomize_seed,
|
298 |
-
num_images
|
299 |
-
],
|
300 |
-
outputs=[result, seed],
|
301 |
-
api_name="run",
|
302 |
-
)
|
303 |
-
|
304 |
-
with gr.Column(scale=3):
|
305 |
-
gr.Markdown("### Image Gallery")
|
306 |
-
predefined_gallery = gr.Gallery(label="Image Gallery", columns=4, show_label=False, value=load
|
307 |
-
|
308 |
-
_predefined_images())
|
309 |
-
|
310 |
-
if __name__ == "__main__":
|
311 |
-
demo.queue(max_size=40).launch(show_api=False)
|
312 |
-
```
|
313 |
-
|
314 |
-
## Running the Application
|
315 |
-
|
316 |
-
To run the application, simply execute the script. The interface will launch and be accessible via a web browser.
|
317 |
-
|
318 |
-
```sh
|
319 |
-
python app.py
|
320 |
-
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
last-commit/file.txt
DELETED
@@ -1,274 +0,0 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
import os
|
3 |
-
import random
|
4 |
-
import uuid
|
5 |
-
import gradio as gr
|
6 |
-
import numpy as np
|
7 |
-
from PIL import Image
|
8 |
-
import spaces
|
9 |
-
import torch
|
10 |
-
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
11 |
-
|
12 |
-
DESCRIPTIONx = """
|
13 |
-
|
14 |
-
## TEXT-2-IMG SDXL
|
15 |
-
|
16 |
-
"""
|
17 |
-
|
18 |
-
css = '''
|
19 |
-
.gradio-container{max-width: 690px !important}
|
20 |
-
h1{text-align:center}
|
21 |
-
footer {
|
22 |
-
visibility: hidden
|
23 |
-
}
|
24 |
-
'''
|
25 |
-
|
26 |
-
|
27 |
-
js_func = """
|
28 |
-
function refresh() {
|
29 |
-
const url = new URL(window.location);
|
30 |
-
if (url.searchParams.get('__theme') !== 'dark') {
|
31 |
-
url.searchParams.set('__theme', 'dark');
|
32 |
-
window.location.href = url.href;
|
33 |
-
}
|
34 |
-
}
|
35 |
-
"""
|
36 |
-
|
37 |
-
|
38 |
-
examples = [
|
39 |
-
"3d image, cute girl, in the style of Pixar --ar 1:2 --stylize 750, 4K resolution highlights, Sharp focus, octane render, ray tracing, Ultra-High-Definition, 8k, UHD, HDR, (Masterpiece:1.5), (best quality:1.5)",
|
40 |
-
"Chocolate dripping from a donut against a yellow background, in the style of brocore, hyper-realistic oil --ar 2:3 --q 2 --s 750 --v 5 --ar 2:3 --q 2 --s 750 --v 5",
|
41 |
-
"Illustration of A starry night camp in the mountains. Low-angle view, Minimal background, Geometric shapes theme, Pottery, Split-complementary colors, Bicolored light, UHD",
|
42 |
-
"Man in brown leather jacket posing for camera, in the style of sleek and stylized, clockpunk, subtle shades, exacting precision, ferrania p30 --ar 67:101 --v 5",
|
43 |
-
"Commercial photography, giant burger, white lighting, studio light, 8k octane rendering, high resolution photography, insanely detailed, fine details, on white isolated plain, 8k, commercial photography, stock photo, professional color grading, --v 4 --ar 9:16 "
|
44 |
-
]
|
45 |
-
|
46 |
-
MODEL_OPTIONS = {
|
47 |
-
"Hyper Realism : V4.0_Lightning": "SG161222/RealVisXL_V4.0_Lightning",
|
48 |
-
"Deep Realism : RealVisv4_XL": "SG161222/RealVisXL_V4.0",
|
49 |
-
}
|
50 |
-
|
51 |
-
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
52 |
-
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
53 |
-
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
54 |
-
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1"))
|
55 |
-
|
56 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
57 |
-
|
58 |
-
def load_and_prepare_model(model_id):
|
59 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
60 |
-
model_id,
|
61 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
62 |
-
use_safetensors=True,
|
63 |
-
add_watermarker=False,
|
64 |
-
).to(device)
|
65 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
66 |
-
|
67 |
-
if USE_TORCH_COMPILE:
|
68 |
-
pipe.compile()
|
69 |
-
|
70 |
-
if ENABLE_CPU_OFFLOAD:
|
71 |
-
pipe.enable_model_cpu_offload()
|
72 |
-
|
73 |
-
return pipe
|
74 |
-
|
75 |
-
# Preload and compile both models
|
76 |
-
models = {key: load_and_prepare_model(value) for key, value in MODEL_OPTIONS.items()}
|
77 |
-
|
78 |
-
MAX_SEED = np.iinfo(np.int32).max
|
79 |
-
|
80 |
-
def save_image(img):
|
81 |
-
unique_name = str(uuid.uuid4()) + ".png"
|
82 |
-
img.save(unique_name)
|
83 |
-
return unique_name
|
84 |
-
|
85 |
-
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
86 |
-
if randomize_seed:
|
87 |
-
seed = random.randint(0, MAX_SEED)
|
88 |
-
return seed
|
89 |
-
|
90 |
-
@spaces.GPU(duration=60, enable_queue=True)
|
91 |
-
def generate(
|
92 |
-
model_choice: str,
|
93 |
-
prompt: str,
|
94 |
-
negative_prompt: str = "",
|
95 |
-
use_negative_prompt: bool = False,
|
96 |
-
seed: int = 1,
|
97 |
-
width: int = 1024,
|
98 |
-
height: int = 1024,
|
99 |
-
guidance_scale: float = 3,
|
100 |
-
num_inference_steps: int = 25,
|
101 |
-
randomize_seed: bool = False,
|
102 |
-
use_resolution_binning: bool = True,
|
103 |
-
num_images: int = 1,
|
104 |
-
progress=gr.Progress(track_tqdm=True),
|
105 |
-
):
|
106 |
-
global models
|
107 |
-
pipe = models[model_choice]
|
108 |
-
|
109 |
-
seed = int(randomize_seed_fn(seed, randomize_seed))
|
110 |
-
generator = torch.Generator(device=device).manual_seed(seed)
|
111 |
-
|
112 |
-
options = {
|
113 |
-
"prompt": [prompt] * num_images,
|
114 |
-
"negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
|
115 |
-
"width": width,
|
116 |
-
"height": height,
|
117 |
-
"guidance_scale": guidance_scale,
|
118 |
-
"num_inference_steps": num_inference_steps,
|
119 |
-
"generator": generator,
|
120 |
-
"output_type": "pil",
|
121 |
-
}
|
122 |
-
|
123 |
-
if use_resolution_binning:
|
124 |
-
options["use_resolution_binning"] = True
|
125 |
-
|
126 |
-
images = []
|
127 |
-
for i in range(0, num_images, BATCH_SIZE):
|
128 |
-
batch_options = options.copy()
|
129 |
-
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
130 |
-
if "negative_prompt" in batch_options:
|
131 |
-
batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
|
132 |
-
images.extend(pipe(**batch_options).images)
|
133 |
-
|
134 |
-
image_paths = [save_image(img) for img in images]
|
135 |
-
return image_paths, seed
|
136 |
-
|
137 |
-
def load_predefined_images():
|
138 |
-
predefined_images = [
|
139 |
-
"assets/1.png",
|
140 |
-
"assets/2.png",
|
141 |
-
"assets/3.png",
|
142 |
-
"assets/4.png",
|
143 |
-
"assets/5.png",
|
144 |
-
"assets/6.png",
|
145 |
-
"assets/7.png",
|
146 |
-
"assets/8.png",
|
147 |
-
"assets/9.png",
|
148 |
-
"assets/10.png",
|
149 |
-
"assets/11.png",
|
150 |
-
"assets/12.png",
|
151 |
-
]
|
152 |
-
return predefined_images
|
153 |
-
|
154 |
-
with gr.Blocks(css=css, theme="bethecloud/storj_theme", js=js_func) as demo:
|
155 |
-
gr.Markdown(DESCRIPTIONx)
|
156 |
-
with gr.Row():
|
157 |
-
prompt = gr.Text(
|
158 |
-
label="Prompt",
|
159 |
-
show_label=False,
|
160 |
-
max_lines=1,
|
161 |
-
placeholder="Enter your prompt",
|
162 |
-
container=False,
|
163 |
-
)
|
164 |
-
run_button = gr.Button("Run⚡", scale=0)
|
165 |
-
result = gr.Gallery(label="Result", columns=1, show_label=False)
|
166 |
-
|
167 |
-
with gr.Row():
|
168 |
-
model_choice = gr.Dropdown(
|
169 |
-
label="Model Selection",
|
170 |
-
choices=list(MODEL_OPTIONS.keys()),
|
171 |
-
value="Hyper Realism : V4.0_Lightning"
|
172 |
-
)
|
173 |
-
|
174 |
-
with gr.Accordion("Advanced options", open=True):
|
175 |
-
num_images = gr.Slider(
|
176 |
-
label="Number of Images",
|
177 |
-
minimum=1,
|
178 |
-
maximum=1,
|
179 |
-
step=1,
|
180 |
-
value=1,
|
181 |
-
)
|
182 |
-
with gr.Row():
|
183 |
-
with gr.Column(scale=1):
|
184 |
-
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
185 |
-
negative_prompt = gr.Text(
|
186 |
-
label="Negative prompt",
|
187 |
-
max_lines=5,
|
188 |
-
lines=4,
|
189 |
-
placeholder="Enter a negative prompt",
|
190 |
-
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",
|
191 |
-
visible=True,
|
192 |
-
)
|
193 |
-
seed = gr.Slider(
|
194 |
-
label="Seed",
|
195 |
-
minimum=0,
|
196 |
-
maximum=MAX_SEED,
|
197 |
-
step=1,
|
198 |
-
value=0,
|
199 |
-
)
|
200 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
201 |
-
with gr.Row():
|
202 |
-
width = gr.Slider(
|
203 |
-
label="Width",
|
204 |
-
minimum=512,
|
205 |
-
maximum=MAX_IMAGE_SIZE,
|
206 |
-
step=64,
|
207 |
-
value=1024,
|
208 |
-
)
|
209 |
-
height = gr.Slider(
|
210 |
-
label="Height",
|
211 |
-
minimum=512,
|
212 |
-
maximum=MAX_IMAGE_SIZE,
|
213 |
-
step=64,
|
214 |
-
value=1024,
|
215 |
-
)
|
216 |
-
with gr.Row():
|
217 |
-
guidance_scale = gr.Slider(
|
218 |
-
label="Guidance Scale",
|
219 |
-
minimum=0.1,
|
220 |
-
maximum=6,
|
221 |
-
step=0.1,
|
222 |
-
value=3.0,
|
223 |
-
)
|
224 |
-
num_inference_steps = gr.Slider(
|
225 |
-
label="Number of inference steps",
|
226 |
-
minimum=1,
|
227 |
-
maximum=35,
|
228 |
-
step=1,
|
229 |
-
value=20,
|
230 |
-
)
|
231 |
-
|
232 |
-
gr.Examples(
|
233 |
-
examples=examples,
|
234 |
-
inputs=prompt,
|
235 |
-
cache_examples=False
|
236 |
-
)
|
237 |
-
|
238 |
-
use_negative_prompt.change(
|
239 |
-
fn=lambda x: gr.update(visible=x),
|
240 |
-
inputs=use_negative_prompt,
|
241 |
-
outputs=negative_prompt,
|
242 |
-
api_name=False,
|
243 |
-
)
|
244 |
-
|
245 |
-
gr.on(
|
246 |
-
triggers=[
|
247 |
-
prompt.submit,
|
248 |
-
negative_prompt.submit,
|
249 |
-
run_button.click,
|
250 |
-
],
|
251 |
-
fn=generate,
|
252 |
-
inputs=[
|
253 |
-
model_choice,
|
254 |
-
prompt,
|
255 |
-
negative_prompt,
|
256 |
-
use_negative_prompt,
|
257 |
-
seed,
|
258 |
-
width,
|
259 |
-
height,
|
260 |
-
guidance_scale,
|
261 |
-
num_inference_steps,
|
262 |
-
randomize_seed,
|
263 |
-
num_images
|
264 |
-
],
|
265 |
-
outputs=[result, seed],
|
266 |
-
api_name="run",
|
267 |
-
)
|
268 |
-
|
269 |
-
with gr.Column(scale=3):
|
270 |
-
gr.Markdown("### Image Gallery")
|
271 |
-
predefined_gallery = gr.Gallery(label="Image Gallery", columns=4, show_label=False, value=load_predefined_images())
|
272 |
-
|
273 |
-
if __name__ == "__main__":
|
274 |
-
demo.queue(max_size=40).launch(show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|