Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
@@ -1,305 +0,0 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
#patch 0.01 ()
|
3 |
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4 |
-
# of this software and associated documentation files (the "Software"), to deal
|
5 |
-
# in the Software without restriction, including without limitation the rights
|
6 |
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7 |
-
# copies of the Software, and to permit persons to whom the Software is
|
8 |
-
# furnished to do so, subject to the following conditions:
|
9 |
-
#
|
10 |
-
# ...
|
11 |
-
|
12 |
-
import json
|
13 |
-
import gradio as gr
|
14 |
-
import numpy as np
|
15 |
-
from PIL import Image
|
16 |
-
#import spaces
|
17 |
-
import torch
|
18 |
-
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
19 |
-
import os
|
20 |
-
import uuid
|
21 |
-
import random
|
22 |
-
|
23 |
-
# Description for the Gradio interface
|
24 |
-
DESCRIPTION = """
|
25 |
-
|
26 |
-
## WALLPAPER XL 🌅
|
27 |
-
|
28 |
-
"""
|
29 |
-
|
30 |
-
|
31 |
-
# CSS for styling the Gradio interface
|
32 |
-
css = '''
|
33 |
-
.gradio-container{max-width: 575px !important}
|
34 |
-
h1{text-align:center}
|
35 |
-
footer {
|
36 |
-
visibility: hidden
|
37 |
-
}
|
38 |
-
'''
|
39 |
-
|
40 |
-
# Example prompts for the user to try
|
41 |
-
examples = [
|
42 |
-
"Illustration of A starry night camp in the mountains. Low-angle view, Minimal background, Geometric shapes theme, Pottery, Split-complementary colors, Bicolored light, UHD",
|
43 |
-
"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"
|
44 |
-
]
|
45 |
-
|
46 |
-
# Environment variables and defaults for configuration
|
47 |
-
|
48 |
-
if not torch.cuda.is_available():
|
49 |
-
DESCRIPTION += "\n<p>⚠️Running on CPU, This may not work on CPU. If it runs for an extended time or if you encounter errors, try running it on a GPU by duplicating the space using @spaces.GPU(). +import spaces.📍</p>"
|
50 |
-
|
51 |
-
MODEL_ID = os.getenv("MODEL_USED") #Use SDXL Model as "MODEL_REPO" --------->>> ”VALUE”.
|
52 |
-
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
53 |
-
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
54 |
-
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
55 |
-
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1"))
|
56 |
-
|
57 |
-
# Setting the device to GPU if available, otherwise CPU
|
58 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
59 |
-
|
60 |
-
# Loading the Stable Diffusion model
|
61 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
62 |
-
MODEL_ID,
|
63 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
64 |
-
use_safetensors=True,
|
65 |
-
add_watermarker=False,
|
66 |
-
).to(device)
|
67 |
-
|
68 |
-
# Configuring the scheduler for the model
|
69 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
70 |
-
|
71 |
-
# Compiling the model for performance improvement if enabled
|
72 |
-
if USE_TORCH_COMPILE:
|
73 |
-
pipe.compile()
|
74 |
-
|
75 |
-
# Enabling CPU offload to save GPU memory if enabled
|
76 |
-
if ENABLE_CPU_OFFLOAD:
|
77 |
-
pipe.enable_model_cpu_offload()
|
78 |
-
|
79 |
-
# Maximum seed value for randomization
|
80 |
-
MAX_SEED = np.iinfo(np.int32).max
|
81 |
-
|
82 |
-
# Function to save the generated image
|
83 |
-
def save_image(img):
|
84 |
-
unique_name = str(uuid.uuid4()) + ".png"
|
85 |
-
img.save(unique_name)
|
86 |
-
return unique_name
|
87 |
-
|
88 |
-
# Function to randomize the seed if needed
|
89 |
-
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
90 |
-
if randomize_seed:
|
91 |
-
seed = random.randint(0, MAX_SEED)
|
92 |
-
return seed
|
93 |
-
|
94 |
-
# Defining the main generation function with GPU acceleration
|
95 |
-
#@spaces.GPU(duration=60, enable_queue=True)
|
96 |
-
def generate(
|
97 |
-
prompt: str,
|
98 |
-
negative_prompt: str = "",
|
99 |
-
use_negative_prompt: bool = False,
|
100 |
-
seed: int = 1,
|
101 |
-
width: int = 1024,
|
102 |
-
height: int = 1024,
|
103 |
-
guidance_scale: float = 3,
|
104 |
-
num_inference_steps: int = 25,
|
105 |
-
randomize_seed: bool = False,
|
106 |
-
use_resolution_binning: bool = True,
|
107 |
-
num_images: int = 1,
|
108 |
-
progress=gr.Progress(track_tqdm=True),
|
109 |
-
):
|
110 |
-
# Randomizing the seed if required
|
111 |
-
seed = int(randomize_seed_fn(seed, randomize_seed))
|
112 |
-
generator = torch.Generator(device=device).manual_seed(seed)
|
113 |
-
|
114 |
-
# Setting up the options for the image generation
|
115 |
-
options = {
|
116 |
-
"prompt": [prompt] * num_images,
|
117 |
-
"negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
|
118 |
-
"width": width,
|
119 |
-
"height": height,
|
120 |
-
"guidance_scale": guidance_scale,
|
121 |
-
"num_inference_steps": num_inference_steps,
|
122 |
-
"generator": generator,
|
123 |
-
"output_type": "pil",
|
124 |
-
}
|
125 |
-
|
126 |
-
if use_resolution_binning:
|
127 |
-
options["use_resolution_binning"] = True
|
128 |
-
|
129 |
-
# Generating images in batches
|
130 |
-
images = []
|
131 |
-
for i in range(0, num_images, BATCH_SIZE):
|
132 |
-
batch_options = options.copy()
|
133 |
-
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
134 |
-
if "negative_prompt" in batch_options:
|
135 |
-
batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
|
136 |
-
images.extend(pipe(**batch_options).images)
|
137 |
-
|
138 |
-
# Saving the generated images
|
139 |
-
image_paths = [save_image(img) for img in images]
|
140 |
-
return image_paths, seed
|
141 |
-
|
142 |
-
# Function to set the wallpaper size based on the selected option
|
143 |
-
|
144 |
-
def set_wallpaper_size(size):
|
145 |
-
if size == "phone":
|
146 |
-
return 1080, 1920
|
147 |
-
elif size == "desktop":
|
148 |
-
return 1920, 1080
|
149 |
-
return 1024, 1024
|
150 |
-
|
151 |
-
# Function to load predefined images for display
|
152 |
-
|
153 |
-
def load_predefined_images():
|
154 |
-
predefined_images = [
|
155 |
-
"assets/1.png",
|
156 |
-
"assets/2.png",
|
157 |
-
"assets/3.png",
|
158 |
-
"assets/4.png",
|
159 |
-
"assets/5.png",
|
160 |
-
"assets/6.png",
|
161 |
-
"assets/7.png",
|
162 |
-
"assets/8.png",
|
163 |
-
"assets/9.png",
|
164 |
-
]
|
165 |
-
return predefined_images
|
166 |
-
|
167 |
-
# Defining the Gradio interface with blocks
|
168 |
-
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
169 |
-
gr.Markdown(DESCRIPTION)
|
170 |
-
with gr.Group():
|
171 |
-
with gr.Row():
|
172 |
-
prompt = gr.Text(
|
173 |
-
label="Prompt",
|
174 |
-
show_label=False,
|
175 |
-
max_lines=1,
|
176 |
-
placeholder="Enter your prompt",
|
177 |
-
container=False,
|
178 |
-
)
|
179 |
-
run_button = gr.Button("Run", scale=0)
|
180 |
-
result = gr.Gallery(label="Result", columns=1, show_label=False)
|
181 |
-
|
182 |
-
with gr.Group():
|
183 |
-
wallpaper_size = gr.Radio(
|
184 |
-
choices=["phone", "desktop", "custom"],
|
185 |
-
label="Wallpaper Size",
|
186 |
-
value="desktop"
|
187 |
-
)
|
188 |
-
width = gr.Slider(
|
189 |
-
label="Width",
|
190 |
-
minimum=512,
|
191 |
-
maximum=MAX_IMAGE_SIZE,
|
192 |
-
step=64,
|
193 |
-
value=1920,
|
194 |
-
visible=False,
|
195 |
-
)
|
196 |
-
height = gr.Slider(
|
197 |
-
label="Height",
|
198 |
-
minimum=512,
|
199 |
-
maximum=MAX_IMAGE_SIZE,
|
200 |
-
step=64,
|
201 |
-
value=1080,
|
202 |
-
visible=False,
|
203 |
-
)
|
204 |
-
|
205 |
-
# Changing the wallpaper size based on user selection
|
206 |
-
wallpaper_size.change(
|
207 |
-
fn=set_wallpaper_size,
|
208 |
-
inputs=wallpaper_size,
|
209 |
-
outputs=[width, height],
|
210 |
-
api_name="set_wallpaper_size"
|
211 |
-
)
|
212 |
-
|
213 |
-
# Advanced options for image generation
|
214 |
-
with gr.Accordion("Advanced options", open=False, visible=False):
|
215 |
-
num_images = gr.Slider(
|
216 |
-
label="Number of Images",
|
217 |
-
minimum=1,
|
218 |
-
maximum=4,
|
219 |
-
step=1,
|
220 |
-
value=1,
|
221 |
-
)
|
222 |
-
with gr.Row():
|
223 |
-
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
224 |
-
negative_prompt = gr.Text(
|
225 |
-
label="Negative prompt",
|
226 |
-
max_lines=5,
|
227 |
-
lines=4,
|
228 |
-
placeholder="Enter a negative prompt",
|
229 |
-
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",
|
230 |
-
visible=True,
|
231 |
-
)
|
232 |
-
seed = gr.Slider(
|
233 |
-
label="Seed",
|
234 |
-
minimum=0,
|
235 |
-
maximum=MAX_SEED,
|
236 |
-
step=1,
|
237 |
-
value=0,
|
238 |
-
)
|
239 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
240 |
-
with gr.Row():
|
241 |
-
guidance_scale = gr.Slider(
|
242 |
-
label="Guidance Scale",
|
243 |
-
minimum=0.1,
|
244 |
-
maximum=6,
|
245 |
-
step=0.1,
|
246 |
-
value=3.0,
|
247 |
-
)
|
248 |
-
num_inference_steps = gr.Slider(
|
249 |
-
label="Number of inference steps",
|
250 |
-
minimum=1,
|
251 |
-
maximum=25,
|
252 |
-
step=1,
|
253 |
-
value=20,
|
254 |
-
)
|
255 |
-
|
256 |
-
# Adding examples for the user to try
|
257 |
-
gr.Examples(
|
258 |
-
examples=examples,
|
259 |
-
inputs=prompt,
|
260 |
-
cache_examples=False
|
261 |
-
)
|
262 |
-
|
263 |
-
# Changing the visibility of the negative prompt based on user selection
|
264 |
-
use_negative_prompt.change(
|
265 |
-
fn=lambda x: gr.update(visible=x),
|
266 |
-
inputs=use_negative_prompt,
|
267 |
-
outputs=negative_prompt,
|
268 |
-
api_name=False,
|
269 |
-
)
|
270 |
-
|
271 |
-
# Setting up the triggers and linking them to the generate function
|
272 |
-
gr.on(
|
273 |
-
triggers=[
|
274 |
-
prompt.submit,
|
275 |
-
negative_prompt.submit,
|
276 |
-
run_button.click,
|
277 |
-
],
|
278 |
-
fn=generate,
|
279 |
-
inputs=[
|
280 |
-
prompt,
|
281 |
-
negative_prompt,
|
282 |
-
use_negative_prompt,
|
283 |
-
seed,
|
284 |
-
width,
|
285 |
-
height,
|
286 |
-
guidance_scale,
|
287 |
-
num_inference_steps,
|
288 |
-
randomize_seed,
|
289 |
-
num_images
|
290 |
-
],
|
291 |
-
outputs=[result, seed],
|
292 |
-
api_name="run",
|
293 |
-
)
|
294 |
-
# Adding a predefined gallery section
|
295 |
-
gr.Markdown("### Generated Wallpapers")
|
296 |
-
predefined_gallery = gr.Gallery(label="Generated Images", columns=3, show_label=False, value=load_predefined_images())
|
297 |
-
# Adding a disclaimer
|
298 |
-
gr.Markdown("**Disclaimer/Note:**")
|
299 |
-
gr.Markdown("🌅This is the demo space for generating wallpapers using detailed prompts. This space works best for desktop-sized images (1920x1080). Reasonable quality images can be generated for mobile sizes (1080x1920), and custom images (1024x1024) can also be generated with better quality. Mobile settings may become disfigured. Try the sample prompts for generating higher quality images.<a href='https://huggingface.co/spaces/prithivMLmods/INSTANT-WALLPAPER/blob/main/sample_prompts.txt' target='_blank'>Try prompts</a>.")
|
300 |
-
gr.Markdown("🌅Usage: Works better for Illustration, Hyper-Realistic Inputs, Others may contain artifacts and perform poorly in some cases.")
|
301 |
-
gr.Markdown("⚠️users are accountable for the content they generate and are responsible for ensuring it meets appropriate ethical standards.")
|
302 |
-
|
303 |
-
# Launching the Gradio interface
|
304 |
-
if __name__ == "__main__":
|
305 |
-
demo.queue(max_size=40).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|