fantos commited on
Commit
b77afd5
ยท
verified ยท
1 Parent(s): a594406

Create app-backup.py

Browse files
Files changed (1) hide show
  1. app-backup.py +338 -0
app-backup.py ADDED
@@ -0,0 +1,338 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import gradio as gr
3
+ import numpy as np
4
+ import spaces
5
+ import torch
6
+ from diffusers import AutoencoderKL
7
+ from mixture_tiling_sdxl import StableDiffusionXLTilingPipeline
8
+
9
+ MAX_SEED = np.iinfo(np.int32).max
10
+ SCHEDULERS = [
11
+ "LMSDiscreteScheduler",
12
+ "DEISMultistepScheduler",
13
+ "HeunDiscreteScheduler",
14
+ "EulerAncestralDiscreteScheduler",
15
+ "EulerDiscreteScheduler",
16
+ "DPMSolverMultistepScheduler",
17
+ "DPMSolverMultistepScheduler-Karras",
18
+ "DPMSolverMultistepScheduler-Karras-SDE",
19
+ "UniPCMultistepScheduler"
20
+ ]
21
+
22
+ # ๋ชจ๋ธ ๋กœ๋”ฉ: VAE ๋ฐ ํƒ€์ผ๋ง ํŒŒ์ดํ”„๋ผ์ธ ์ดˆ๊ธฐํ™”
23
+ vae = AutoencoderKL.from_pretrained(
24
+ "madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
25
+ ).to("cuda")
26
+
27
+ model_id = "stablediffusionapi/yamermix-v8-vae"
28
+ pipe = StableDiffusionXLTilingPipeline.from_pretrained(
29
+ model_id,
30
+ torch_dtype=torch.float16,
31
+ vae=vae,
32
+ use_safetensors=False, # for yammermix
33
+ ).to("cuda")
34
+
35
+ pipe.enable_model_cpu_offload() # VRAM์ด ์ œํ•œ๋œ ๊ฒฝ์šฐ ์‚ฌ์šฉ
36
+ pipe.enable_vae_tiling()
37
+ pipe.enable_vae_slicing()
38
+
39
+ #region functions
40
+ def select_scheduler(scheduler_name):
41
+ scheduler_parts = scheduler_name.split("-")
42
+ scheduler_class_name = scheduler_parts[0]
43
+ add_kwargs = {
44
+ "beta_start": 0.00085,
45
+ "beta_end": 0.012,
46
+ "beta_schedule": "scaled_linear",
47
+ "num_train_timesteps": 1000
48
+ }
49
+ if len(scheduler_parts) > 1:
50
+ add_kwargs["use_karras_sigmas"] = True
51
+ if len(scheduler_parts) > 2:
52
+ add_kwargs["algorithm_type"] = "sde-dpmsolver++"
53
+ import diffusers
54
+ scheduler_cls = getattr(diffusers, scheduler_class_name)
55
+ scheduler = scheduler_cls.from_config(pipe.scheduler.config, **add_kwargs)
56
+ return scheduler
57
+
58
+ @spaces.GPU
59
+ def predict(left_prompt, center_prompt, right_prompt, negative_prompt, left_gs, center_gs, right_gs,
60
+ overlap_pixels, steps, generation_seed, scheduler, tile_height, tile_width, target_height, target_width):
61
+ global pipe
62
+ print(f"Using scheduler: {scheduler}...")
63
+ pipe.scheduler = select_scheduler(scheduler)
64
+ generator = torch.Generator("cuda").manual_seed(generation_seed)
65
+
66
+ target_height = int(target_height)
67
+ target_width = int(target_width)
68
+ tile_height = int(tile_height)
69
+ tile_width = int(tile_width)
70
+
71
+ image = pipe(
72
+ prompt=[[left_prompt, center_prompt, right_prompt]],
73
+ negative_prompt=negative_prompt,
74
+ tile_height=tile_height,
75
+ tile_width=tile_width,
76
+ tile_row_overlap=0,
77
+ tile_col_overlap=overlap_pixels,
78
+ guidance_scale_tiles=[[left_gs, center_gs, right_gs]],
79
+ height=target_height,
80
+ width=target_width,
81
+ generator=generator,
82
+ num_inference_steps=steps,
83
+ )["images"][0]
84
+ return image
85
+
86
+ def calc_tile_size(target_height, target_width, overlap_pixels, max_tile_width_size=1280):
87
+ num_cols = 3
88
+ num_rows = 1
89
+ min_tile_dimension = 8
90
+ reduction_step = 8
91
+ max_tile_height_size = 1024
92
+ best_tile_width = 0
93
+ best_tile_height = 0
94
+ best_adjusted_target_width = 0
95
+ best_adjusted_target_height = 0
96
+ found_valid_solution = False
97
+
98
+ tile_width = max_tile_width_size
99
+ tile_height = max_tile_height_size
100
+ while tile_width >= min_tile_dimension:
101
+ horizontal_borders = num_cols - 1
102
+ total_horizontal_overlap = overlap_pixels * horizontal_borders
103
+ adjusted_target_width = tile_width * num_cols - total_horizontal_overlap
104
+
105
+ vertical_borders = num_rows - 1
106
+ total_vertical_overlap = overlap_pixels * vertical_borders
107
+ adjusted_target_height = tile_height * num_rows - total_vertical_overlap
108
+
109
+ if tile_width <= max_tile_width_size and adjusted_target_width <= target_width:
110
+ if adjusted_target_width > best_adjusted_target_width:
111
+ best_tile_width = tile_width
112
+ best_adjusted_target_width = adjusted_target_width
113
+ found_valid_solution = True
114
+ tile_width -= reduction_step
115
+
116
+ if found_valid_solution:
117
+ tile_width = best_tile_width
118
+ tile_height = max_tile_height_size
119
+ while tile_height >= min_tile_dimension:
120
+ horizontal_borders = num_cols - 1
121
+ total_horizontal_overlap = overlap_pixels * horizontal_borders
122
+ adjusted_target_width = tile_width * num_cols - total_horizontal_overlap
123
+
124
+ vertical_borders = num_rows - 1
125
+ total_vertical_overlap = overlap_pixels * vertical_borders
126
+ adjusted_target_height = tile_height * num_rows - total_vertical_overlap
127
+
128
+ if tile_height <= max_tile_height_size and adjusted_target_height <= target_height:
129
+ if adjusted_target_height > best_adjusted_target_height:
130
+ best_tile_height = tile_height
131
+ best_adjusted_target_height = adjusted_target_height
132
+ tile_height -= reduction_step
133
+
134
+ new_target_height = best_adjusted_target_height
135
+ new_target_width = best_adjusted_target_width
136
+ tile_width = best_tile_width
137
+ tile_height = best_tile_height
138
+
139
+ print("--- TILE SIZE CALCULATED VALUES ---")
140
+ print(f"Requested Overlap Pixels: {overlap_pixels}")
141
+ print(f"Tile Height (max {max_tile_height_size}, divisible by 8): {tile_height}")
142
+ print(f"Tile Width (max {max_tile_width_size}, divisible by 8): {tile_width}")
143
+ print(f"Columns: {num_cols} | Rows: {num_rows}")
144
+ print(f"Original Target: {target_height} x {target_width}")
145
+ print(f"Adjusted Target: {new_target_height} x {new_target_width}\n")
146
+
147
+ return new_target_height, new_target_width, tile_height, tile_width
148
+
149
+ def do_calc_tile(target_height, target_width, overlap_pixels, max_tile_size):
150
+ new_target_height, new_target_width, tile_height, tile_width = calc_tile_size(target_height, target_width, overlap_pixels, max_tile_size)
151
+ return gr.update(value=tile_height), gr.update(value=tile_width), gr.update(value=new_target_height), gr.update(value=new_target_width)
152
+
153
+ def clear_result():
154
+ return gr.update(value=None)
155
+
156
+ def randomize_seed_fn(generation_seed: int, randomize_seed: bool) -> int:
157
+ if randomize_seed:
158
+ generation_seed = random.randint(0, MAX_SEED)
159
+ return generation_seed
160
+ #endregion
161
+
162
+ # CSS ๊ฐœ์„ : ๋ฐฐ๊ฒฝ, ์—ฌ๋ฐฑ, ๊ทธ๋ฆผ์ž ๋ฐ ์˜ˆ์ œ ์˜์—ญ ์ค‘์•™ ๋ฐฐ์น˜
163
+ css = """
164
+ body { background-color: #f0f2f5; }
165
+ .gradio-container {
166
+ background: #ffffff;
167
+ border-radius: 15px;
168
+ padding: 20px;
169
+ box-shadow: 0 4px 10px rgba(0,0,0,0.1);
170
+ }
171
+ .gradio-container h1 { color: #333333; }
172
+ .fillable { width: 95% !important; max-width: unset !important; }
173
+ #examples_container {
174
+ margin: auto;
175
+ width: 90%;
176
+ }
177
+ #examples_row {
178
+ justify-content: center;
179
+ }
180
+ """
181
+
182
+ title = """
183
+ <h1 align="center" style="margin-bottom: 0.2em;">Mixture-of-Diffusers for SDXL Tiling Pipeline ๐Ÿค—</h1>
184
+ <p align="center" style="font-size:1.1em; color:#555;">
185
+ ์ขŒ/์ค‘์•™/์šฐ ๊ฐ ์˜์—ญ์— ๋‹ค๋ฅธ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ ์šฉํ•˜์—ฌ ํƒ€์ผ๋ง ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.<br>
186
+ ์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ํด๋ฆญํ•˜๋ฉด ์ž…๋ ฅ์ฐฝ์— ๊ฐ’์ด ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค.
187
+ </p>
188
+ """
189
+
190
+ with gr.Blocks(css=css, title="SDXL Tiling Pipeline") as app:
191
+ gr.Markdown(title)
192
+
193
+ with gr.Row():
194
+ # ์ขŒ/์ค‘์•™/์šฐ ํ”„๋กฌํ”„ํŠธ ๋ฐ ๊ฒฐ๊ณผ ์˜์—ญ
195
+ with gr.Column(scale=7):
196
+ generate_button = gr.Button("Generate", elem_id="generate_btn")
197
+ with gr.Row():
198
+ with gr.Column(variant="panel"):
199
+ gr.Markdown("### Left Region")
200
+ left_prompt = gr.Textbox(lines=4, placeholder="์˜ˆ: ์šธ์ฐฝํ•œ ์ˆฒ๊ณผ ํ–‡์‚ด์ด ๋น„์ถ”๋Š” ๋‚˜๋ฌด...", label="Left Prompt")
201
+ left_gs = gr.Slider(minimum=0, maximum=15, value=7, step=1, label="Left CFG Scale")
202
+ with gr.Column(variant="panel"):
203
+ gr.Markdown("### Center Region")
204
+ center_prompt = gr.Textbox(lines=4, placeholder="์˜ˆ: ์ž”์ž”ํ•œ ํ˜ธ์ˆ˜์™€ ๋ฐ˜์ง์ด๋Š” ์ˆ˜๋ฉด...", label="Center Prompt")
205
+ center_gs = gr.Slider(minimum=0, maximum=15, value=7, step=1, label="Center CFG Scale")
206
+ with gr.Column(variant="panel"):
207
+ gr.Markdown("### Right Region")
208
+ right_prompt = gr.Textbox(lines=4, placeholder="์˜ˆ: ์›…์žฅํ•œ ์‚ฐ๋งฅ๊ณผ ํ•˜๋Š˜์„ ๊ฐ€๋ฅด๋Š” ๊ตฌ๋ฆ„...", label="Right Prompt")
209
+ right_gs = gr.Slider(minimum=0, maximum=15, value=7, step=1, label="Right CFG Scale")
210
+ with gr.Row():
211
+ negative_prompt = gr.Textbox(
212
+ lines=2,
213
+ label="Negative Prompt",
214
+ placeholder="์˜ˆ: blurry, low resolution, artifacts, poor details",
215
+ value="blurry, low resolution, artifacts, poor details"
216
+ )
217
+ with gr.Row():
218
+ result = gr.Image(label="Generated Image", show_label=True, format="png", interactive=False, scale=1)
219
+
220
+ # ์‚ฌ์ด๋“œ๋ฐ”: ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ ํƒ€์ผ ํฌ๊ธฐ ๊ณ„์‚ฐ
221
+ with gr.Sidebar(label="Parameters", open=True):
222
+ gr.Markdown("### Generation Parameters")
223
+ with gr.Row():
224
+ height = gr.Slider(label="Target Height", value=1024, step=8, minimum=512, maximum=1024)
225
+ width = gr.Slider(label="Target Width", value=1280, step=8, minimum=512, maximum=3840)
226
+ overlap = gr.Slider(minimum=0, maximum=512, value=128, step=8, label="Tile Overlap")
227
+ max_tile_size = gr.Dropdown(label="Max Tile Size", choices=[1024, 1280], value=1280)
228
+ calc_tile = gr.Button("Calculate Tile Size")
229
+ with gr.Row():
230
+ tile_height = gr.Textbox(label="Tile Height", value=1024, interactive=False)
231
+ tile_width = gr.Textbox(label="Tile Width", value=1024, interactive=False)
232
+ with gr.Row():
233
+ new_target_height = gr.Textbox(label="New Image Height", value=1024, interactive=False)
234
+ new_target_width = gr.Textbox(label="New Image Width", value=1280, interactive=False)
235
+ with gr.Row():
236
+ steps = gr.Slider(minimum=1, maximum=50, value=30, step=1, label="Inference Steps")
237
+ generation_seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
238
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=False)
239
+ with gr.Row():
240
+ scheduler = gr.Dropdown(label="Scheduler", choices=SCHEDULERS, value=SCHEDULERS[0])
241
+
242
+ # ์ค‘์•™์— ๋ฐฐ์น˜๋œ ์˜ˆ์ œ ์˜์—ญ
243
+ with gr.Row(elem_id="examples_row"):
244
+ with gr.Column(scale=12, elem_id="examples_container"):
245
+ gr.Markdown("### Example Prompts")
246
+ gr.Examples(
247
+ examples=[
248
+ [
249
+ "Lush green forest with sun rays filtering through the canopy",
250
+ "Crystal clear lake reflecting a vibrant sky",
251
+ "Majestic mountains with snowy peaks in the distance",
252
+ "blurry, low resolution, artifacts, poor details",
253
+ 7, 7, 7,
254
+ 128,
255
+ 30,
256
+ 123456789,
257
+ "UniPCMultistepScheduler",
258
+ 1024, 1280,
259
+ 1024, 1920,
260
+ 1280
261
+ ],
262
+ [
263
+ "Vibrant city street with neon signs and bustling crowds",
264
+ "Sleek modern skyscrapers with digital billboards",
265
+ "High-speed maglev train gliding over a futuristic urban landscape",
266
+ "blurry, poorly rendered, low quality, disfigured",
267
+ 8, 8, 8,
268
+ 100,
269
+ 35,
270
+ 987654321,
271
+ "EulerDiscreteScheduler",
272
+ 1024, 1280,
273
+ 1024, 1920,
274
+ 1280
275
+ ],
276
+ [
277
+ "Vibrant abstract strokes with fluid, swirling patterns in cool tones",
278
+ "Interlocking geometric shapes bursting with color and texture",
279
+ "Dynamic composition of splattered ink with smooth gradients",
280
+ "text, watermark, signature, distorted",
281
+ 6, 6, 6,
282
+ 80,
283
+ 25,
284
+ 192837465,
285
+ "DPMSolverMultistepScheduler-Karras",
286
+ 1024, 1280,
287
+ 1024, 1920,
288
+ 1280
289
+ ],
290
+ [
291
+ "Enchanted forest with glowing bioluminescent plants and mystical fog",
292
+ "Ancient castle with towering spires bathed in moonlight",
293
+ "Majestic dragon soaring above a starry night sky",
294
+ "low quality, artifact, deformed, sketchy",
295
+ 9, 9, 9,
296
+ 150,
297
+ 40,
298
+ 1029384756,
299
+ "DPMSolverMultistepScheduler-Karras-SDE",
300
+ 1024, 1280,
301
+ 1024, 1920,
302
+ 1280
303
+ ]
304
+ ],
305
+ # ์˜ˆ์ œ ํด๋ฆญ ์‹œ ๊ฐ ์ž…๋ ฅ์ฐฝ์— ๊ฐ’์ด ์ฑ„์›Œ์ง€๋„๋ก "inputs" ์ธ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
306
+ inputs=[left_prompt, center_prompt, right_prompt, negative_prompt,
307
+ left_gs, center_gs, right_gs, overlap, steps, generation_seed,
308
+ scheduler, tile_height, tile_width, height, width, max_tile_size],
309
+ cache_examples=False
310
+ )
311
+
312
+ # ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ: ํƒ€์ผ ์‚ฌ์ด์ฆˆ ๊ณ„์‚ฐ ๋ฐ ์ด๋ฏธ์ง€ ์ƒ์„ฑ
313
+ event_calc_tile_size = {
314
+ "fn": do_calc_tile,
315
+ "inputs": [height, width, overlap, max_tile_size],
316
+ "outputs": [tile_height, tile_width, new_target_height, new_target_width]
317
+ }
318
+ calc_tile.click(**event_calc_tile_size)
319
+
320
+ generate_button.click(
321
+ fn=clear_result,
322
+ inputs=None,
323
+ outputs=result,
324
+ ).then(**event_calc_tile_size).then(
325
+ fn=randomize_seed_fn,
326
+ inputs=[generation_seed, randomize_seed],
327
+ outputs=generation_seed,
328
+ queue=False,
329
+ api_name=False,
330
+ ).then(
331
+ fn=predict,
332
+ inputs=[left_prompt, center_prompt, right_prompt, negative_prompt,
333
+ left_gs, center_gs, right_gs, overlap, steps, generation_seed,
334
+ scheduler, tile_height, tile_width, new_target_height, new_target_width],
335
+ outputs=result,
336
+ )
337
+
338
+ app.launch(share=False)