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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|