File size: 8,893 Bytes
69620c8
 
 
 
 
 
6960db5
f207d6b
 
 
69620c8
31c0b50
 
 
 
6960db5
 
 
69620c8
 
 
450db89
4f076f3
69620c8
 
c1bd24e
 
f207d6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69620c8
 
c401dbb
6960db5
 
 
 
 
 
 
 
 
 
 
 
69620c8
 
 
 
 
 
 
4f076f3
69620c8
 
 
6960db5
69620c8
 
 
 
 
 
 
 
 
4f076f3
69620c8
4f076f3
a1e7a82
69620c8
 
 
 
 
 
 
 
 
669c8e5
69620c8
4f076f3
f207d6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f076f3
 
 
f207d6b
 
4f076f3
f207d6b
4f076f3
f207d6b
 
 
 
 
 
 
 
 
 
 
 
 
c43703f
 
 
 
 
f207d6b
c43703f
 
 
 
 
 
 
 
 
 
4f076f3
c43703f
 
 
 
 
 
4f076f3
c43703f
 
4f076f3
c43703f
 
 
4f076f3
 
c43703f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f076f3
 
 
 
 
 
69620c8
 
6960db5
69620c8
 
 
 
 
f207d6b
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
import spaces
import gradio as gr
import torch
from PIL import Image
from diffusers import DiffusionPipeline
import random
from transformers import pipeline
import pygame
import os
import threading

torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.backends.cuda.matmul.allow_tf32 = True

# λ²ˆμ—­ λͺ¨λΈ μ΄ˆκΈ°ν™”
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")

base_model = "black-forest-labs/FLUX.1-dev"
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)

lora_repo = "strangerzonehf/Flux-Xmas-Realpix-LoRA"
trigger_word = ""
pipe.load_lora_weights(lora_repo)

pipe.to("cuda")

# pygame μ΄ˆκΈ°ν™” 및 μŒμ•… μ„€μ •
pygame.mixer.init()
def play_music():
    pygame.mixer.music.load("1.mp3")
    pygame.mixer.music.play()
    pygame.mixer.music.queue("2.mp3")
    pygame.mixer.music.set_endevent(pygame.USEREVENT)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.USEREVENT:
                pygame.mixer.music.queue("1.mp3")
                pygame.mixer.music.queue("2.mp3")

# λ°°κ²½μŒμ•… μž¬μƒ μ‹œμž‘ (별도 μŠ€λ ˆλ“œμ—μ„œ μ‹€ν–‰)
music_thread = threading.Thread(target=play_music, daemon=True)
music_thread.start()

MAX_SEED = 2**32-1

@spaces.GPU()
def translate_and_generate(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
    # ν•œκΈ€ 감지 및 λ²ˆμ—­
    def contains_korean(text):
        return any(ord('κ°€') <= ord(char) <= ord('힣') for char in text)
    
    if contains_korean(prompt):
        # ν•œκΈ€μ„ μ˜μ–΄λ‘œ λ²ˆμ—­
        translated = translator(prompt)[0]['translation_text']
        actual_prompt = translated
    else:
        actual_prompt = prompt

    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    generator = torch.Generator(device="cuda").manual_seed(seed)

    progress(0, "Starting image generation...")

    for i in range(1, steps + 1):
        if i % (steps // 10) == 0:
            progress(i / steps * 100, f"Processing step {i} of {steps}...")

    image = pipe(
        prompt=f"{actual_prompt} {trigger_word}",
        num_inference_steps=steps,
        guidance_scale=cfg_scale,
        width=width,
        height=height,
        generator=generator,
        joint_attention_kwargs={"scale": lora_scale},
    ).images[0]

    progress(100, "Completed!")
    return image, seed

example_image_path = "example0.webp"
example_prompt = """Cozy winter scene with a Christmas atmosphere: a snow-covered cabin in the forest, warm light glowing from the windows, surrounded by sparkling Christmas decorations and a beautifully adorned Christmas tree. The sky is filled with stars, and soft snowflakes are gently falling, creating a serene and warm ambiance"""
example_cfg_scale = 3.2
example_steps = 32
example_width = 1152
example_height = 896
example_seed = 3981632454
example_lora_scale = 0.85

def load_example():
    example_image = Image.open(example_image_path)
    return example_prompt, example_cfg_scale, example_steps, True, example_seed, example_width, example_height, example_lora_scale, example_image

css = """
.container {
    max-width: 1400px; 
    margin: auto; 
    padding: 20px;
    position: relative;
    background-image: url('file/example0.webp');
    background-size: cover;
    background-position: center;
    min-height: 100vh;
}
.header {
    text-align: center; 
    margin-bottom: 30px;
    color: white;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.7);
}
.generate-btn {
    background-color: #2ecc71 !important; 
    color: white !important; 
    margin: 20px auto !important; 
    display: block !important; 
    width: 200px !important;
}
.generate-btn:hover {
    background-color: #27ae60 !important;
}
.parameter-box {
    background-color: rgba(245, 246, 250, 0.9);
    padding: 20px; 
    border-radius: 10px; 
    margin: 10px 0;
}
.result-box {
    background-color: rgba(245, 246, 250, 0.9);
    padding: 20px; 
    border-radius: 10px; 
    margin: 0 auto 20px auto; 
    text-align: center;
}
.image-output {
    margin: 0 auto; 
    display: block; 
    max-width: 800px !important;
}
.accordion {
    margin-top: 20px;
}
.prompt-box {
    position: fixed;
    top: 20px;
    right: 20px;
    width: 300px;
    background-color: rgba(245, 246, 250, 0.9);
    padding: 20px;
    border-radius: 10px;
    z-index: 1000;
}

@keyframes snow {
    0% {
        transform: translateY(0) translateX(0);
    }
    100% {
        transform: translateY(100vh) translateX(100px);
    }
}

.snowflake {
    position: fixed;
    top: -10px;
    color: white;
    font-size: 20px;
    animation: snow 5s linear infinite;
}
"""

js_code = """
function createSnowflake() {
    const snowflake = document.createElement('div');
    snowflake.classList.add('snowflake');
    snowflake.innerHTML = '❄';
    snowflake.style.left = Math.random() * 100 + 'vw';
    snowflake.style.animationDuration = Math.random() * 3 + 2 + 's';
    snowflake.style.opacity = Math.random();
    document.body.appendChild(snowflake);
    
    setTimeout(() => {
        snowflake.remove();
    }, 5000);
}

setInterval(createSnowflake, 100);
"""

with gr.Blocks(css=css) as app:
    gr.HTML(f"<script>{js_code}</script>")
    
    with gr.Column(elem_classes="container"):
        gr.Markdown("# πŸŽ„ X-MAS LoRA", elem_classes="header")
        
        # ν”„λ‘¬ν”„νŠΈ μž…λ ₯ λ°•μŠ€λ₯Ό λ³„λ„λ‘œ 배치
        with gr.Group(elem_classes="prompt-box"):
            prompt = gr.TextArea(
                label="✍️ Your Prompt (ν•œκΈ€ λ˜λŠ” μ˜μ–΄)",
                placeholder="이미지λ₯Ό μ„€λͺ…ν•˜μ„Έμš”...",
                lines=5
            )
            generate_button = gr.Button(
                "πŸš€ Generate Image",
                elem_classes="generate-btn"
            )
        
        # 이미지 좜λ ₯ μ˜μ—­
        with gr.Group(elem_classes="result-box"):
            gr.Markdown("### πŸ–ΌοΈ Generated Image")
            result = gr.Image(label="Result", elem_classes="image-output")
        
        # μ˜΅μ…˜λ“€μ„ μ•„μ½”λ””μ–ΈμœΌλ‘œ ꡬ성
        with gr.Accordion("🎨 Advanced Options", open=False, elem_classes="accordion"):
            with gr.Group(elem_classes="parameter-box"):
                gr.Markdown("### πŸŽ›οΈ Generation Parameters")
                with gr.Row():
                    with gr.Column():
                        cfg_scale = gr.Slider(
                            label="CFG Scale",
                            minimum=1,
                            maximum=20,
                            step=0.5,
                            value=example_cfg_scale
                        )
                        steps = gr.Slider(
                            label="Steps",
                            minimum=1,
                            maximum=100,
                            step=1,
                            value=example_steps
                        )
                        lora_scale = gr.Slider(
                            label="LoRA Scale",
                            minimum=0,
                            maximum=1,
                            step=0.01,
                            value=example_lora_scale
                        )
            
            with gr.Group(elem_classes="parameter-box"):
                gr.Markdown("### πŸ“ Image Dimensions")
                with gr.Row():
                    width = gr.Slider(
                        label="Width",
                        minimum=256,
                        maximum=1536,
                        step=64,
                        value=example_width
                    )
                    height = gr.Slider(
                        label="Height",
                        minimum=256,
                        maximum=1536,
                        step=64,
                        value=example_height
                    )
            
            with gr.Group(elem_classes="parameter-box"):
                gr.Markdown("### 🎲 Seed Settings")
                with gr.Row():
                    randomize_seed = gr.Checkbox(
                        True,
                        label="Randomize seed"
                    )
                    seed = gr.Slider(
                        label="Seed",
                        minimum=0,
                        maximum=MAX_SEED,
                        step=1,
                        value=example_seed
                    )

    app.load(
        load_example,
        inputs=[],
        outputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, result]
    )
    
    generate_button.click(
        translate_and_generate,
        inputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale],
        outputs=[result, seed]
    )

app.queue()
app.launch(js=js_code)