File size: 21,644 Bytes
769de07
 
e4ed2d7
769de07
 
 
 
 
 
 
 
e4ed2d7
769de07
e4ed2d7
 
769de07
 
e4ed2d7
 
769de07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4ed2d7
 
 
769de07
 
 
 
e4ed2d7
 
 
 
 
 
 
 
 
 
 
 
 
 
769de07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4ed2d7
 
 
769de07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4ed2d7
769de07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4ed2d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
769de07
 
 
 
e4ed2d7
769de07
e4ed2d7
769de07
 
e4ed2d7
769de07
e4ed2d7
 
 
769de07
e4ed2d7
 
 
 
 
769de07
e4ed2d7
 
 
 
 
 
769de07
e4ed2d7
 
769de07
e4ed2d7
769de07
e4ed2d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
769de07
 
 
 
 
 
 
 
 
 
e4ed2d7
769de07
 
 
 
e4ed2d7
769de07
e4ed2d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
769de07
e4ed2d7
769de07
 
e4ed2d7
 
769de07
e4ed2d7
769de07
 
 
 
 
 
 
e4ed2d7
769de07
 
 
e4ed2d7
 
 
 
 
 
 
 
769de07
 
e4ed2d7
 
769de07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4ed2d7
769de07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import os
import gc
import re
import gradio as gr
import numpy as np
import torch
import json
import spaces
import config
import utils
import logging
import time
from datetime import datetime
from typing import List, Dict, Tuple, Optional
from PIL import Image, PngImagePlugin
from diffusers.models import AutoencoderKL
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
from transformers import pipeline as translation_pipeline

from config import (
    MODEL,
    MIN_IMAGE_SIZE,
    MAX_IMAGE_SIZE,
    USE_TORCH_COMPILE,
    ENABLE_CPU_OFFLOAD,
    OUTPUT_DIR,
    DEFAULT_NEGATIVE_PROMPT,
    DEFAULT_ASPECT_RATIO,
    examples,
    sampler_list,
    aspect_ratios,
    style_list,
)

# Enhanced logging configuration
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)

# Constants
IS_COLAB = utils.is_google_colab() or os.getenv("IS_COLAB") == "1"
HF_TOKEN = os.getenv("HF_TOKEN")
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES") == "1" 

# PyTorch settings for better performance and determinism
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.backends.cuda.matmul.allow_tf32 = True

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
logger.info(f"Using device: {device}")

# λ²ˆμ—­ νŒŒμ΄ν”„λΌμΈ μ΄ˆκΈ°ν™” (ν•œκΈ€ β†’ μ˜μ–΄)
translator = translation_pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")

class GenerationError(Exception):
    """Custom exception for generation errors"""
    pass

def translate_if_korean(prompt: str) -> str:
    """ν”„λ‘¬ν”„νŠΈμ— ν•œκΈ€μ΄ ν¬ν•¨λ˜μ–΄ 있으면 μ˜μ–΄λ‘œ λ²ˆμ—­"""
    if re.search(r'[γ„±-γ…Žγ…-γ…£κ°€-힣]', prompt):
        logger.info("Korean detected in prompt. Translating to English...")
        try:
            translation = translator(prompt)[0]['translation_text']
            logger.info(f"Translation result: {translation}")
            return translation
        except Exception as e:
            logger.error(f"Translation error: {e}")
            # λ²ˆμ—­ μ‹€νŒ¨ μ‹œ 원본 ν”„λ‘¬ν”„νŠΈ μ‚¬μš©
            return prompt
    return prompt

def validate_prompt(prompt: str) -> str:
    """Validate and clean up the input prompt."""
    if not isinstance(prompt, str):
        raise GenerationError("Prompt must be a string")
    try:
        # Ensure proper UTF-8 encoding/decoding
        prompt = prompt.encode('utf-8').decode('utf-8')
        # Add space between ! and ,
        prompt = prompt.replace("!,", "! ,")
    except UnicodeError:
        raise GenerationError("Invalid characters in prompt")
    
    # Only check if the prompt is completely empty or only whitespace
    if not prompt or prompt.isspace():
        raise GenerationError("Prompt cannot be empty")
    
    # λ²ˆμ—­ 적용: ν•œκΈ€μ΄ κ°μ§€λ˜λ©΄ μ˜μ–΄λ‘œ λ³€ν™˜
    prompt = translate_if_korean(prompt)
    return prompt.strip()

def validate_dimensions(width: int, height: int) -> None:
    """Validate image dimensions."""
    if not MIN_IMAGE_SIZE <= width <= MAX_IMAGE_SIZE:
        raise GenerationError(f"Width must be between {MIN_IMAGE_SIZE} and {MAX_IMAGE_SIZE}")
        
    if not MIN_IMAGE_SIZE <= height <= MAX_IMAGE_SIZE:
        raise GenerationError(f"Height must be between {MIN_IMAGE_SIZE} and {MAX_IMAGE_SIZE}")

@spaces.GPU
def generate(
    prompt: str,
    negative_prompt: str = DEFAULT_NEGATIVE_PROMPT,
    seed: int = 0,
    custom_width: int = 1024,
    custom_height: int = 1024,
    guidance_scale: float = 6.0,
    num_inference_steps: int = 25,
    sampler: str = "Euler a",
    aspect_ratio_selector: str = DEFAULT_ASPECT_RATIO,
    style_selector: str = "(None)",
    use_upscaler: bool = False,
    upscaler_strength: float = 0.55,
    upscale_by: float = 1.5,
    add_quality_tags: bool = True,
    progress: gr.Progress = gr.Progress(track_tqdm=True),
) -> Tuple[List[str], Dict]:
    """Generate images based on the given parameters."""
    start_time = time.time()
    upscaler_pipe = None
    backup_scheduler = None
    
    try:
        # Memory management
        torch.cuda.empty_cache()
        gc.collect()

        # Input validation
        prompt = validate_prompt(prompt)
        if negative_prompt:
            negative_prompt = negative_prompt.encode('utf-8').decode('utf-8')
        
        validate_dimensions(custom_width, custom_height)
        
        # Set up generation
        generator = utils.seed_everything(seed)
        width, height = utils.aspect_ratio_handler(
            aspect_ratio_selector,
            custom_width,
            custom_height,
        )

        # Process prompts
        if add_quality_tags:
            prompt = f"masterpiece, high score, great score, absurdres, {prompt}"

        prompt, negative_prompt = utils.preprocess_prompt(
            styles, style_selector, prompt, negative_prompt
        )    

        width, height = utils.preprocess_image_dimensions(width, height)

        # Set up pipeline
        backup_scheduler = pipe.scheduler
        pipe.scheduler = utils.get_scheduler(pipe.scheduler.config, sampler)

        if use_upscaler:
            upscaler_pipe = StableDiffusionXLImg2ImgPipeline(**pipe.components)
            
        # Prepare metadata
        metadata = {
            "prompt": prompt,
            "negative_prompt": negative_prompt,
            "resolution": f"{width} x {height}",
            "guidance_scale": guidance_scale,
            "num_inference_steps": num_inference_steps,
            "style_preset": style_selector,
            "seed": seed,
            "sampler": sampler,
            "Model": "Animagine XL 4.0",
            "Model hash": "e3c47aedb0",
        }

        if use_upscaler:
            new_width = int(width * upscale_by)
            new_height = int(height * upscale_by)
            metadata["use_upscaler"] = {
                "upscale_method": "nearest-exact",
                "upscaler_strength": upscaler_strength,
                "upscale_by": upscale_by,
                "new_resolution": f"{new_width} x {new_height}",
            }
        else:
            metadata["use_upscaler"] = None
        
        logger.info(f"Starting generation with parameters: {json.dumps(metadata, indent=4)}")

        # Generate images
        if use_upscaler:
            latents = pipe(
                prompt=prompt,
                negative_prompt=negative_prompt,
                width=width,
                height=height,
                guidance_scale=guidance_scale,
                num_inference_steps=num_inference_steps,
                generator=generator,
                output_type="latent",
            ).images
            upscaled_latents = utils.upscale(latents, "nearest-exact", upscale_by)
            images = upscaler_pipe(
                prompt=prompt,
                negative_prompt=negative_prompt,
                image=upscaled_latents,
                guidance_scale=guidance_scale,
                num_inference_steps=num_inference_steps,
                strength=upscaler_strength,
                generator=generator,
                output_type="pil",
            ).images
        else:
            images = pipe(
                prompt=prompt,
                negative_prompt=negative_prompt,
                width=width,
                height=height,
                guidance_scale=guidance_scale,
                num_inference_steps=num_inference_steps,
                generator=generator,
                output_type="pil",
            ).images

        # Save images
        if images:
            total = len(images)
            image_paths = []
            for idx, image in enumerate(images, 1):
                progress(idx/total, desc="Saving images...")
                path = utils.save_image(image, metadata, OUTPUT_DIR, IS_COLAB)
                image_paths.append(path)
                logger.info(f"Image {idx}/{total} saved as {path}")

        generation_time = time.time() - start_time
        logger.info(f"Generation completed successfully in {generation_time:.2f} seconds")
        metadata["generation_time"] = f"{generation_time:.2f}s"
        
        return image_paths, metadata

    except GenerationError as e:
        logger.warning(f"Generation validation error: {str(e)}")
        raise gr.Error(str(e))
    except Exception as e:
        logger.exception("Unexpected error during generation")
        raise gr.Error(f"Generation failed: {str(e)}")
    finally:
        # Cleanup
        torch.cuda.empty_cache()
        gc.collect()
        
        if upscaler_pipe is not None:
            del upscaler_pipe
        
        if backup_scheduler is not None and pipe is not None:
            pipe.scheduler = backup_scheduler
            
        utils.free_memory()

# Model initialization
if torch.cuda.is_available():
    try:
        logger.info("Loading VAE and pipeline...")
        vae = AutoencoderKL.from_pretrained(
            "madebyollin/sdxl-vae-fp16-fix",
            torch_dtype=torch.float16,
        )
        pipe = utils.load_pipeline(MODEL, device, vae=vae)
        logger.info("Pipeline loaded successfully on GPU!")
    except Exception as e:
        logger.error(f"Error loading VAE, falling back to default: {e}")
        pipe = utils.load_pipeline(MODEL, device)
else:
    logger.warning("CUDA not available, running on CPU")
    pipe = None

# Process styles
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}

# μ‚¬μš©μž μΈν„°νŽ˜μ΄μŠ€ (UI) κ°œμ„ : CSS μŠ€νƒ€μΌ 및 λ ˆμ΄μ•„μ›ƒ μˆ˜μ •
custom_css = """
/* λ°°κ²½ 및 κΈ€μž 색상 λ³€κ²½ */
body {
    background-color: #f7f9fc;
    color: #333;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* 헀더 μŠ€νƒ€μΌ */
.header {
    text-align: center;
    padding: 20px;
}
.header .title {
    font-size: 3em;
    font-weight: bold;
    color: #2c3e50;
}
.header .subtitle {
    font-size: 1.2em;
    color: #7f8c8d;
}
a {
    text-decoration: none;
    color: #3498db;
}

/* Discord λ²„νŠΌ μŠ€νƒ€μΌ */
.discord-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px 20px;
    background: #7289da;
    color: white;
    border-radius: 8px;
    font-weight: bold;
    margin-top: 20px;
}
.discord-btn:hover {
    background: #5b6eae;
}
.discord-icon {
    width: 24px;
    height: 24px;
    margin-right: 8px;
}

/* Gradio 가러리 μŠ€νƒ€μΌ κ°œμ„  */
.gradio-gallery {
    border: none;
    box-shadow: none;
}
"""

with gr.Blocks(css=custom_css, theme="default") as demo:
    # 상단 헀더
    gr.HTML(
        """
        <div class="header">
            <div class="title">ANIM4GINE</div>
            <div class="subtitle">A sleek and intuitive demo for <a href="https://huggingface.co/CagliostroLab/Animagine-XL-4.0" target="_blank">Animagine XL 4.0</a></div>
        </div>
        """
    )
    
    # μ’ŒμΈ‘μ— μž…λ ₯ 및 μ„€μ •, μš°μΈ‘μ— κ²°κ³Ό 좜λ ₯ 배치
    with gr.Row():
        with gr.Column(scale=3):
            with gr.Tabs():
                with gr.TabItem("Generate"):
                    with gr.Group():
                        prompt = gr.Textbox(
                            label="Prompt",
                            lines=4,
                            placeholder="Describe what you want to generate...",
                            info="Enter your image generation prompt here. ν•œκΈ€ μž…λ ₯ μ‹œ μžλ™μœΌλ‘œ μ˜μ–΄λ‘œ λ²ˆμ—­λ©λ‹ˆλ‹€.",
                        )
                        negative_prompt = gr.Textbox(
                            label="Negative Prompt",
                            lines=4,
                            placeholder="Describe what you want to avoid",
                            value=DEFAULT_NEGATIVE_PROMPT,
                            info="Specify elements you don't want in the image.",
                        )
                        add_quality_tags = gr.Checkbox(
                            label="Quality Tags",
                            value=True,
                            info="Automatically add quality-enhancing tags to your prompt.",
                        )
                    with gr.Accordion(label="More Settings", open=False):
                        with gr.Column():
                            aspect_ratio_selector = gr.Radio(
                                label="Aspect Ratio",
                                choices=aspect_ratios,
                                value=DEFAULT_ASPECT_RATIO,
                                container=True,
                                info="Choose the dimensions of your image.",
                            )
                            with gr.Row(visible=False) as custom_resolution:
                                custom_width = gr.Slider(
                                    label="Width",
                                    minimum=MIN_IMAGE_SIZE,
                                    maximum=MAX_IMAGE_SIZE,
                                    step=8,
                                    value=1024,
                                    info=f"Image width (between {MIN_IMAGE_SIZE} and {MAX_IMAGE_SIZE})",
                                )
                                custom_height = gr.Slider(
                                    label="Height",
                                    minimum=MIN_IMAGE_SIZE,
                                    maximum=MAX_IMAGE_SIZE,
                                    step=8,
                                    value=1024,
                                    info=f"Image height (between {MIN_IMAGE_SIZE} and {MAX_IMAGE_SIZE})",
                                )
                    with gr.Accordion(label="Advanced Parameters", open=False):
                        with gr.Row():
                            style_selector = gr.Dropdown(
                                label="Style Preset",
                                choices=list(styles.keys()),
                                value="(None)",
                                info="Apply a predefined style to your generation.",
                            )
                            sampler = gr.Dropdown(
                                label="Sampler",
                                choices=sampler_list,
                                value="Euler a",
                                info="Different samplers can produce varying results.",
                            )
                        with gr.Row():
                            seed = gr.Slider(
                                label="Seed",
                                minimum=0,
                                maximum=utils.MAX_SEED,
                                step=1,
                                value=0,
                                info="Set a specific seed for reproducible results.",
                            )
                            randomize_seed = gr.Checkbox(
                                label="Randomize seed",
                                value=True,
                                info="Generate a new random seed for each image.",
                            )
                        with gr.Row():
                            guidance_scale = gr.Slider(
                                label="Guidance scale",
                                minimum=1,
                                maximum=12,
                                step=0.1,
                                value=6.0,
                                info="Higher values make the image more closely match your prompt.",
                            )
                            num_inference_steps = gr.Slider(
                                label="Inference steps",
                                minimum=1,
                                maximum=50,
                                step=1,
                                value=25,
                                info="More steps generally yield higher quality images.",
                            )
                        with gr.Row():
                            use_upscaler = gr.Checkbox(
                                label="Use Upscaler",
                                value=False,
                                info="Enable high-resolution upscaling.",
                            )
                            upscaler_strength = gr.Slider(
                                label="Upscaler Strength",
                                minimum=0,
                                maximum=1,
                                step=0.05,
                                value=0.55,
                                visible=False,
                                info="Control how strongly the upscaler affects the image.",
                            )
                            upscale_by = gr.Slider(
                                label="Upscale By",
                                minimum=1,
                                maximum=1.5,
                                step=0.1,
                                value=1.5,
                                visible=False,
                                info="Multiplier for the final image resolution.",
                            )
                with gr.TabItem("Examples"):
                    gr.Markdown(
                        """
                        ### Example Prompts
                        - **Scenic Landscape:** A breathtaking view of a mountain landscape during sunrise.
                        - **Cyberpunk City:** A futuristic cyberpunk city with neon lights and towering skyscrapers.
                        - **Fantasy Character:** A majestic wizard with a long beard and glowing magical staff.
                        """
                    )
                    gr.Examples(
                        examples=examples,
                        inputs=prompt,
                        outputs=[],
                        cache_examples=CACHE_EXAMPLES,
                    )
            run_button = gr.Button("Generate", variant="primary", elem_id="generate-button")
        
        with gr.Column(scale=4):
            result = gr.Gallery(
                label="Generated Images",
                columns=2,
                height="600px",
                show_label=True,
                elem_classes="gradio-gallery",
            )
            with gr.Accordion(label="Generation Parameters", open=False):
                gr_metadata = gr.JSON(
                    label="Image Metadata",
                    show_label=True,
                )
    
    # Discord λ²„νŠΌμ„ ν•˜λ‹¨ 쀑앙에 배치
    with gr.Row():
        gr.HTML(
            """
            <div style="width:100%; display:flex; justify-content:center;">
                <a href="https://discord.com/invite/cqh9tZgbGc" target="_blank" class="discord-btn">
                    <svg class="discord-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 127.14 96.36">
                        <path fill="currentColor" d="M107.7,8.07A105.15,105.15,0,0,0,81.47,0a72.06,72.06,0,0,0-3.36,6.83A97.68,97.68,0,0,0,49,6.83,72.37,72.37,0,0,0,45.64,0,105.89,105.89,0,0,0,19.39,8.09C2.79,32.65-1.71,56.6.54,80.21h0A105.73,105.73,0,0,0,32.71,96.36,77.7,77.7,0,0,0,39.6,85.25a68.42,68.42,0,0,1-10.85-5.18c.91-.66,1.8-1.34,2.66-2a75.57,75.57,0,0,0,64.32,0c.87.71,1.76,1.39,2.66,2a68.68,68.68,0,0,1-10.87,5.19,77,77,0,0,0,6.89,11.1A105.25,105.25,0,0,0,126.6,80.22h0C129.24,52.84,122.09,29.11,107.7,8.07ZM42.45,65.69C36.18,65.69,31,60,31,53s5-12.74,11.43-12.74S54,46,53.89,53,48.84,65.69,42.45,65.69Zm42.24,0C78.41,65.69,73.25,60,73.25,53s5-12.74,11.44-12.74S96.23,46,96.12,53,91.08,65.69,84.69,65.69Z"/>
                    </svg>
                    <span class="discord-text">Join our Discord Server</span>
                </a>
            </div>
            """
        )
    
    # UI 동적 μ—…λ°μ΄νŠΈ: μ—…μŠ€μΌ€μΌλŸ¬ μ˜΅μ…˜ 및 μ»€μŠ€ν…€ 해상도 μŠ¬λΌμ΄λ” ν‘œμ‹œ μ œμ–΄
    use_upscaler.change(
        fn=lambda x: [gr.update(visible=x), gr.update(visible=x)],
        inputs=use_upscaler,
        outputs=[upscaler_strength, upscale_by],
        queue=False,
        api_name=False,
    )
    aspect_ratio_selector.change(
        fn=lambda x: gr.update(visible=x == "Custom"),
        inputs=aspect_ratio_selector,
        outputs=custom_resolution,
        queue=False,
        api_name=False,
    )

    # 트리거 μ„€μ •: ν”„λ‘¬ν”„νŠΈ μž…λ ₯, λ²„νŠΌ 클릭 μ‹œ μ‹œλ“œ λžœλ€ν™” ν›„ 이미지 생성 μ‹€ν–‰
    gr.on(
        triggers=[
            prompt.submit,
            negative_prompt.submit,
            run_button.click,
        ],
        fn=utils.randomize_seed_fn,
        inputs=[seed, randomize_seed],
        outputs=seed,
        queue=False,
        api_name=False,
    ).then(
        fn=lambda: gr.update(interactive=False, value="Generating..."),
        outputs=run_button,
    ).then(
        fn=generate,
        inputs=[
            prompt,
            negative_prompt,
            seed,
            custom_width,
            custom_height,
            guidance_scale,
            num_inference_steps,
            sampler,
            aspect_ratio_selector,
            style_selector,
            use_upscaler,
            upscaler_strength,
            upscale_by,
            add_quality_tags,
        ],
        outputs=[result, gr_metadata],
    ).then(
        fn=lambda: gr.update(interactive=True, value="Generate"),
        outputs=run_button,
    )

if __name__ == "__main__":
    demo.queue(max_size=20).launch(debug=IS_COLAB, share=IS_COLAB)