K00B404 commited on
Commit
55d9e7b
1 Parent(s): de8c809

Update Adv_app.py

Browse files
Files changed (1) hide show
  1. Adv_app.py +299 -0
Adv_app.py CHANGED
@@ -0,0 +1,299 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ import uuid
4
+
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 StableDiffusionPipeline, EulerAncestralDiscreteScheduler, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
11
+ from diffusers.utils import load_dynamic_module
12
+
13
+ DESCRIPTION = """
14
+ # ImagesXL
15
+ """
16
+
17
+ def save_image(img):
18
+ unique_name = str(uuid.uuid4()) + ".png"
19
+ img.save(unique_name)
20
+ return unique_name
21
+
22
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
23
+ if randomize_seed:
24
+ seed = random.randint(0, MAX_SEED)
25
+ return seed
26
+
27
+ MAX_SEED = np.iinfo(np.int32).max
28
+
29
+ if not torch.cuda.is_available():
30
+ DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may work on CPU.</p>"
31
+
32
+ USE_TORCH_COMPILE = 0
33
+ ENABLE_CPU_OFFLOAD = 0
34
+
35
+ MODEL_CHOICES = {
36
+ "Fluently-XL-v2": "fluently/Fluently-XL-v2",
37
+ "Stable Diffusion v1-5": "runwayml/stable-diffusion-v1-5",
38
+ "Stable Diffusion v2-1": "stabilityai/stable-diffusion-2-1",
39
+ }
40
+
41
+ SCHEDULER_CHOICES = {
42
+ "Euler Ancestral Discrete": EulerAncestralDiscreteScheduler,
43
+ "DDIM": DDIMScheduler,
44
+ "LMS Discrete": LMSDiscreteScheduler,
45
+ "PNDM": PNDMScheduler,
46
+ }
47
+
48
+ UPSCALER_CHOICES = {
49
+ "None": None,
50
+ "Real-ESRGAN": "stabilityai/real-esrgan",
51
+ "Latent Diffusion": "stabilityai/latent-diffusion-upscaler",
52
+ }
53
+
54
+ def generate(
55
+ prompt: str,
56
+ negative_prompt: str = "",
57
+ use_negative_prompt: bool = False,
58
+ seed: int = 0,
59
+ width: int = 512,
60
+ height: int = 512,
61
+ guidance_scale: float = 3,
62
+ num_inference_steps: int = 25,
63
+ scheduler: str = "Euler Ancestral Discrete",
64
+ model_name: str = "Fluently-XL-v2",
65
+ randomize_seed: bool = False,
66
+ num_images_per_prompt: int = 1,
67
+ use_lora: bool = False,
68
+ lora_model_name: str = "",
69
+ use_upscaler: bool = False,
70
+ upscaler_model_name: str = "None",
71
+ progress=gr.Progress(track_tqdm=True),
72
+ ):
73
+ seed = int(randomize_seed_fn(seed, randomize_seed))
74
+
75
+ if not use_negative_prompt:
76
+ negative_prompt = "" # type: ignore
77
+
78
+ if torch.cuda.is_available():
79
+ pipe = StableDiffusionPipeline.from_pretrained(
80
+ MODEL_CHOICES[model_name],
81
+ torch_dtype=torch.float16,
82
+ use_safetensors=True,
83
+ )
84
+ else:
85
+ pipe = StableDiffusionPipeline.from_pretrained(
86
+ MODEL_CHOICES[model_name],
87
+ torch_dtype=torch.float32,
88
+ use_safetensors=True,
89
+ )
90
+
91
+ pipe.scheduler = SCHEDULER_CHOICES[scheduler].from_config(pipe.scheduler.config)
92
+
93
+ if use_lora and lora_model_name:
94
+ pipe = load_dynamic_module("diffusers.loaders", "Lora", "lora")(pipe, lora_model_name, device_map="auto")
95
+
96
+ images = pipe(
97
+ prompt=prompt,
98
+ negative_prompt=negative_prompt,
99
+ width=width,
100
+ height=height,
101
+ guidance_scale=guidance_scale,
102
+ num_inference_steps=num_inference_steps,
103
+ num_images_per_prompt=num_images_per_prompt,
104
+ output_type="pil",
105
+ ).images
106
+
107
+ if use_upscaler and upscaler_model_name:
108
+ upscaler = StableDiffusionPipeline.from_pretrained(
109
+ UPSCALER_CHOICES[upscaler_model_name],
110
+ torch_dtype=torch.float16,
111
+ use_safetensors=True,
112
+ )
113
+ images = [upscaler(image).images[0] for image in images]
114
+
115
+ image_paths = [save_image(img) for img in images]
116
+ print(image_paths)
117
+ return image_paths, seed
118
+
119
+ examples = [
120
+ "neon holography crystal cat",
121
+ "a cat eating a piece of cheese",
122
+ "an astronaut riding a horse in space",
123
+ "a cartoon of a boy playing with a tiger",
124
+ "a cute robot artist painting on an easel, concept art",
125
+ "a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone"
126
+ ]
127
+
128
+ css = '''
129
+ .gradio-container{max-width: 800px !important}
130
+ h1{text-align:center}
131
+ footer {
132
+ visibility: hidden
133
+ }
134
+ '''
135
+ with gr.Blocks(css=css, theme="pseudolab/huggingface-korea-theme") as demo:
136
+ gr.Markdown(DESCRIPTION)
137
+ gr.DuplicateButton(
138
+ value="Duplicate Space for private use",
139
+ elem_id="duplicate-button",
140
+ visible=False,
141
+ )
142
+
143
+ with gr.Group():
144
+ with gr.Row():
145
+ prompt = gr.Text(
146
+ label="Prompt",
147
+ show_label=False,
148
+ max_lines=2,
149
+ placeholder="Enter your prompt",
150
+ container=False,
151
+ )
152
+ run_button = gr.Button("Run", scale=0)
153
+ result = gr.Gallery(label="Result", columns=1, preview=True, show_label=False)
154
+ with gr.Accordion("Advanced options", open=False):
155
+ with gr.Row():
156
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
157
+ negative_prompt = gr.Text(
158
+ label="Negative prompt",
159
+ lines=4,
160
+ max_lines=6,
161
+ 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, (NSFW:1.25)""",
162
+ placeholder="Enter a negative prompt",
163
+ visible=True,
164
+ )
165
+ with gr.Row():
166
+ seed = gr.Slider(
167
+ label="Seed",
168
+ minimum=0,
169
+ maximum=MAX_SEED,
170
+ step=1,
171
+ value=0,
172
+ visible=True
173
+ )
174
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
175
+ with gr.Row(visible=True):
176
+ width = gr.Slider(
177
+ label="Width",
178
+ minimum=512,
179
+ maximum=1024,
180
+ step=8,
181
+ value=512,
182
+ )
183
+ height = gr.Slider(
184
+ label="Height",
185
+ minimum=512,
186
+ maximum=1024,
187
+ step=8,
188
+ value=512,
189
+ )
190
+ with gr.Row():
191
+ guidance_scale = gr.Slider(
192
+ label="Guidance Scale",
193
+ minimum=0.1,
194
+ maximum=20.0,
195
+ step=0.1,
196
+ value=6,
197
+ )
198
+ num_inference_steps = gr.Slider(
199
+ label="Inference Steps",
200
+ minimum=1,
201
+ maximum=100,
202
+ step=1,
203
+ value=25,
204
+ )
205
+ with gr.Row():
206
+ scheduler = gr.Dropdown(
207
+ label="Scheduler",
208
+ choices=list(SCHEDULER_CHOICES.keys()),
209
+ value="Euler Ancestral Discrete",
210
+ )
211
+ model_name = gr.Dropdown(
212
+ label="Model",
213
+ choices=list(MODEL_CHOICES.keys()),
214
+ value="Fluently-XL-v2",
215
+ )
216
+ with gr.Row():
217
+ num_images_per_prompt = gr.Slider(
218
+ label="Images per Prompt",
219
+ minimum=1,
220
+ maximum=8,
221
+ step=1,
222
+ value=1,
223
+ )
224
+ with gr.Row():
225
+ use_lora = gr.Checkbox(label="Use LoRA Model", value=False)
226
+ lora_model_name = gr.Text(
227
+ label="LoRA Model Name",
228
+ placeholder="Enter a LoRA model name (e.g., 'runwayml/stable-diffusion-v1-5-lora')",
229
+ visible=False,
230
+ )
231
+ with gr.Row():
232
+ use_upscaler = gr.Checkbox(label="Use Upscaler", value=False)
233
+ upscaler_model_name = gr.Dropdown(
234
+ label="Upscaler Model",
235
+ choices=list(UPSCALER_CHOICES.keys()),
236
+ value="None",
237
+ visible=False,
238
+ )
239
+
240
+ gr.Examples(
241
+ examples=examples,
242
+ inputs=prompt,
243
+ outputs=[result, seed],
244
+ fn=generate,
245
+ cache_examples=False,
246
+ )
247
+
248
+ use_negative_prompt.change(
249
+ fn=lambda x: gr.update(visible=x),
250
+ inputs=use_negative_prompt,
251
+ outputs=negative_prompt,
252
+ api_name=False,
253
+ )
254
+
255
+ use_lora.change(
256
+ fn=lambda x: gr.update(visible=x),
257
+ inputs=use_lora,
258
+ outputs=lora_model_name,
259
+ api_name=False,
260
+ )
261
+
262
+ use_upscaler.change(
263
+ fn=lambda x: gr.update(visible=x),
264
+ inputs=use_upscaler,
265
+ outputs=upscaler_model_name,
266
+ api_name=False,
267
+ )
268
+
269
+ gr.on(
270
+ triggers=[
271
+ prompt.submit,
272
+ negative_prompt.submit,
273
+ run_button.click,
274
+ ],
275
+ fn=generate,
276
+ inputs=[
277
+ prompt,
278
+ negative_prompt,
279
+ use_negative_prompt,
280
+ seed,
281
+ width,
282
+ height,
283
+ guidance_scale,
284
+ num_inference_steps,
285
+ scheduler,
286
+ model_name,
287
+ randomize_seed,
288
+ num_images_per_prompt,
289
+ use_lora,
290
+ lora_model_name,
291
+ use_upscaler,
292
+ upscaler_model_name,
293
+ ],
294
+ outputs=[result, seed],
295
+ api_name="run",
296
+ )
297
+
298
+ if __name__ == "__main__":
299
+ demo.queue(max_size=20).launch(show_api=False, debug=False)