prithivMLmods commited on
Commit
ebef84d
1 Parent(s): 190cc51

Create original-file.txt

Browse files
Files changed (1) hide show
  1. files/original-file.txt +300 -0
files/original-file.txt ADDED
@@ -0,0 +1,300 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
11
+ import gradio as gr
12
+ import numpy as np
13
+ 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}
20
+ h1{text-align:center}
21
+ footer {
22
+ visibility: hidden
23
+ }
24
+ '''
25
+
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"))
41
+ USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
42
+ ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
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,
50
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
51
+ use_safetensors=True,
52
+ add_watermarker=False,
53
+ ).to(device)
54
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
55
+
56
+ if USE_TORCH_COMPILE:
57
+ pipe.compile()
58
+
59
+ if ENABLE_CPU_OFFLOAD:
60
+ pipe.enable_model_cpu_offload()
61
+
62
+ return pipe
63
+
64
+ # Preload and compile both models
65
+ models = {key: load_and_prepare_model(value) for key, value in MODEL_OPTIONS.items()}
66
+
67
+ MAX_SEED = np.iinfo(np.int32).max
68
+
69
+ def save_image(img):
70
+ unique_name = str(uuid.uuid4()) + ".png"
71
+ img.save(unique_name)
72
+ return unique_name
73
+
74
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
75
+ if randomize_seed:
76
+ seed = random.randint(0, MAX_SEED)
77
+ return seed
78
+
79
+ @spaces.GPU(duration=60, enable_queue=True)
80
+ def generate(
81
+ model_choice: str,
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,
88
+ guidance_scale: float = 3,
89
+ num_inference_steps: int = 25,
90
+ randomize_seed: bool = False,
91
+ use_resolution_binning: bool = True,
92
+ num_images: int = 1,
93
+ progress=gr.Progress(track_tqdm=True),
94
+ ):
95
+ global models
96
+ pipe = models[model_choice]
97
+
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,
104
+ "width": width,
105
+ "height": height,
106
+ "guidance_scale": guidance_scale,
107
+ "num_inference_steps": num_inference_steps,
108
+ "generator": generator,
109
+ "output_type": "pil",
110
+ }
111
+
112
+ if use_resolution_binning:
113
+ options["use_resolution_binning"] = True
114
+
115
+ images = []
116
+ for i in range(0, num_images, BATCH_SIZE):
117
+ batch_options = options.copy()
118
+ batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
119
+ if "negative_prompt" in batch_options:
120
+ batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
121
+ images.extend(pipe(**batch_options).images)
122
+
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():
158
+ prompt = gr.Text(
159
+ label="Prompt",
160
+ show_label=False,
161
+ max_lines=1,
162
+ placeholder="Enter your prompt",
163
+ container=False,
164
+ )
165
+ run_button = gr.Button("Run", scale=0)
166
+ result = gr.Gallery(label="Result", columns=1, show_label=False)
167
+
168
+ with gr.Row():
169
+ model_choice = gr.Dropdown(
170
+ label="Model Selection🔻",
171
+ choices=list(MODEL_OPTIONS.keys()),
172
+ value="REALVISXL V5.0"
173
+ )
174
+
175
+ with gr.Accordion("Advanced options", open=False, visible=True):
176
+ num_images = gr.Slider(
177
+ label="Number of Images",
178
+ minimum=1,
179
+ maximum=5,
180
+ step=1,
181
+ value=1,
182
+ )
183
+ with gr.Row():
184
+ with gr.Column(scale=1):
185
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
186
+ negative_prompt = gr.Text(
187
+ label="Negative prompt",
188
+ max_lines=5,
189
+ lines=4,
190
+ placeholder="Enter a negative prompt",
191
+ 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",
192
+ visible=True,
193
+ )
194
+ seed = gr.Slider(
195
+ label="Seed",
196
+ minimum=0,
197
+ maximum=MAX_SEED,
198
+ step=1,
199
+ value=0,
200
+ )
201
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
202
+ with gr.Row():
203
+ width = gr.Slider(
204
+ label="Width",
205
+ minimum=512,
206
+ maximum=MAX_IMAGE_SIZE,
207
+ step=64,
208
+ value=1024,
209
+ )
210
+ height = gr.Slider(
211
+ label="Height",
212
+ minimum=512,
213
+ maximum=MAX_IMAGE_SIZE,
214
+ step=64,
215
+ value=1024,
216
+ )
217
+ with gr.Row():
218
+ guidance_scale = gr.Slider(
219
+ label="Guidance Scale",
220
+ minimum=0.1,
221
+ maximum=6,
222
+ step=0.1,
223
+ value=3.0,
224
+ )
225
+ num_inference_steps = gr.Slider(
226
+ label="Number of inference steps",
227
+ minimum=1,
228
+ maximum=60,
229
+ step=1,
230
+ value=32,
231
+ )
232
+
233
+ gr.Examples(
234
+ examples=examples,
235
+ inputs=prompt,
236
+ cache_examples=False
237
+ )
238
+
239
+ use_negative_prompt.change(
240
+ fn=lambda x: gr.update(visible=x),
241
+ inputs=use_negative_prompt,
242
+ outputs=negative_prompt,
243
+ api_name=False,
244
+ )
245
+
246
+ gr.on(
247
+ triggers=[
248
+ prompt.submit,
249
+ negative_prompt.submit,
250
+ run_button.click,
251
+ ],
252
+ fn=generate,
253
+ inputs=[
254
+ model_choice,
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)