prithivMLmods commited on
Commit
bd0e42a
·
verified ·
1 Parent(s): 11eee93

Update files/demo.txt

Browse files
Files changed (1) hide show
  1. files/demo.txt +263 -0
files/demo.txt CHANGED
@@ -0,0 +1,263 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gradio as gr
3
+ import numpy as np
4
+ from PIL import Image
5
+ import spaces
6
+ import torch
7
+ from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
8
+ import os
9
+ import uuid
10
+ import random
11
+
12
+ DESCRIPTIONx = """## INSTANT WALLPAPER """
13
+
14
+ css = '''
15
+ .gradio-container{max-width: 575px !important}
16
+ h1{text-align:center}
17
+ footer {
18
+ visibility: hidden
19
+ }
20
+ '''
21
+
22
+ examples = [
23
+ "Illustration of A starry night camp in the mountains. Low-angle view, Minimal background, Geometric shapes theme, Pottery, Split-complementary colors, Bicolored light, UHD",
24
+ "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"
25
+ ]
26
+
27
+ MODEL_ID = os.getenv("MODEL_USED")
28
+ MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
29
+ USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
30
+ ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
31
+ BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1"))
32
+
33
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
34
+ pipe = StableDiffusionXLPipeline.from_pretrained(
35
+ MODEL_ID,
36
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
37
+ use_safetensors=True,
38
+ add_watermarker=False,
39
+ ).to(device)
40
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
41
+
42
+ if USE_TORCH_COMPILE:
43
+ pipe.compile()
44
+
45
+ if ENABLE_CPU_OFFLOAD:
46
+ pipe.enable_model_cpu_offload()
47
+
48
+ MAX_SEED = np.iinfo(np.int32).max
49
+
50
+ def save_image(img):
51
+ unique_name = str(uuid.uuid4()) + ".png"
52
+ img.save(unique_name)
53
+ return unique_name
54
+
55
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
56
+ if randomize_seed:
57
+ seed = random.randint(0, MAX_SEED)
58
+ return seed
59
+
60
+ @spaces.GPU(duration=60, enable_queue=True)
61
+ def generate(
62
+ prompt: str,
63
+ negative_prompt: str = "",
64
+ use_negative_prompt: bool = False,
65
+ seed: int = 1,
66
+ width: int = 1024,
67
+ height: int = 1024,
68
+ guidance_scale: float = 3,
69
+ num_inference_steps: int = 25,
70
+ randomize_seed: bool = False,
71
+ use_resolution_binning: bool = True,
72
+ num_images: int = 1,
73
+ progress=gr.Progress(track_tqdm=True),
74
+ ):
75
+ seed = int(randomize_seed_fn(seed, randomize_seed))
76
+ generator = torch.Generator(device=device).manual_seed(seed)
77
+
78
+ options = {
79
+ "prompt": [prompt] * num_images,
80
+ "negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
81
+ "width": width,
82
+ "height": height,
83
+ "guidance_scale": guidance_scale,
84
+ "num_inference_steps": num_inference_steps,
85
+ "generator": generator,
86
+ "output_type": "pil",
87
+ }
88
+
89
+ if use_resolution_binning:
90
+ options["use_resolution_binning"] = True
91
+
92
+ images = []
93
+ for i in range(0, num_images, BATCH_SIZE):
94
+ batch_options = options.copy()
95
+ batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
96
+ if "negative_prompt" in batch_options:
97
+ batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
98
+ images.extend(pipe(**batch_options).images)
99
+
100
+ image_paths = [save_image(img) for img in images]
101
+ return image_paths, seed
102
+
103
+ def set_wallpaper_size(size):
104
+ if size == "phone":
105
+ return 1080, 1920
106
+ elif size == "desktop":
107
+ return 1920, 1080
108
+ return 1024, 1024
109
+
110
+ # Add a function to load predefined images
111
+ def load_predefined_images():
112
+ predefined_images = [
113
+ "assets/image1.png",
114
+ "assets/image2.png",
115
+ "assets/image3.png",
116
+ "assets/image4.png",
117
+ "assets/image5.png",
118
+ "assets/image6.png",
119
+ "assets/image7.png",
120
+ "assets/image8.png",
121
+ "assets/image9.png",
122
+ ]
123
+ return predefined_images
124
+
125
+ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
126
+ gr.Markdown(DESCRIPTIONx)
127
+ with gr.Group():
128
+ with gr.Row():
129
+ prompt = gr.Text(
130
+ label="Prompt",
131
+ show_label=False,
132
+ max_lines=1,
133
+ placeholder="Enter your prompt",
134
+ container=False,
135
+ )
136
+ run_button = gr.Button("Run", scale=0)
137
+ result = gr.Gallery(label="Result", columns=1, show_label=False)
138
+
139
+
140
+ with gr.Group():
141
+ wallpaper_size = gr.Radio(
142
+ choices=["phone", "desktop", "custom"],
143
+ label="Wallpaper Size",
144
+ value="desktop"
145
+ )
146
+ width = gr.Slider(
147
+ label="Width",
148
+ minimum=512,
149
+ maximum=MAX_IMAGE_SIZE,
150
+ step=64,
151
+ value=1920,
152
+ visible=False,
153
+ )
154
+ height = gr.Slider(
155
+ label="Height",
156
+ minimum=512,
157
+ maximum=MAX_IMAGE_SIZE,
158
+ step=64,
159
+ value=1080,
160
+ visible=False,
161
+ )
162
+
163
+ wallpaper_size.change(
164
+ fn=set_wallpaper_size,
165
+ inputs=wallpaper_size,
166
+ outputs=[width, height],
167
+ api_name="set_wallpaper_size"
168
+ )
169
+
170
+ with gr.Accordion("Advanced options", open=False, visible=False):
171
+ num_images = gr.Slider(
172
+ label="Number of Images",
173
+ minimum=1,
174
+ maximum=4,
175
+ step=1,
176
+ value=1,
177
+ )
178
+ with gr.Row():
179
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
180
+ negative_prompt = gr.Text(
181
+ label="Negative prompt",
182
+ max_lines=5,
183
+ lines=4,
184
+ placeholder="Enter a negative prompt",
185
+ 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",
186
+ visible=True,
187
+ )
188
+ seed = gr.Slider(
189
+ label="Seed",
190
+ minimum=0,
191
+ maximum=MAX_SEED,
192
+ step=1,
193
+ value=0,
194
+ )
195
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
196
+ with gr.Row():
197
+ guidance_scale = gr.Slider(
198
+ label="Guidance Scale",
199
+ minimum=0.1,
200
+ maximum=6,
201
+ step=0.1,
202
+ value=3.0,
203
+ )
204
+ num_inference_steps = gr.Slider(
205
+ label="Number of inference steps",
206
+ minimum=1,
207
+ maximum=25,
208
+ step=1,
209
+ value=20,
210
+ )
211
+
212
+ gr.Examples(
213
+ examples=examples,
214
+ inputs=prompt,
215
+ cache_examples=False
216
+ )
217
+
218
+ use_negative_prompt.change(
219
+ fn=lambda x: gr.update(visible=x),
220
+ inputs=use_negative_prompt,
221
+ outputs=negative_prompt,
222
+ api_name=False,
223
+ )
224
+
225
+ gr.on(
226
+ triggers=[
227
+ prompt.submit,
228
+ negative_prompt.submit,
229
+ run_button.click,
230
+ ],
231
+ fn=generate,
232
+ inputs=[
233
+ prompt,
234
+ negative_prompt,
235
+ use_negative_prompt,
236
+ seed,
237
+ width,
238
+ height,
239
+ guidance_scale,
240
+ num_inference_steps,
241
+ randomize_seed,
242
+ num_images
243
+ ],
244
+ outputs=[result, seed],
245
+ api_name="run",
246
+ )
247
+
248
+
249
+ # Add a predefined gallery section
250
+ gr.Markdown("### Sample Images")
251
+ predefined_gallery = gr.Gallery(label="Predefined Images", columns=3, show_label=False, value=load_predefined_images())
252
+
253
+
254
+ gr.Markdown("**Disclaimer:**")
255
+
256
+ 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>.")
257
+
258
+ gr.Markdown("**Note:**")
259
+
260
+ gr.Markdown("⚠️ users are accountable for the content they generate and are responsible for ensuring it meets appropriate ethical standards.")
261
+
262
+ if __name__ == "__main__":
263
+ demo.queue(max_size=40).launch()