File size: 30,707 Bytes
44f4dde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
import gradio as gr
from gradio_toggle import Toggle
import torch
from huggingface_hub import snapshot_download
from transformers import pipeline

from xora.models.autoencoders.causal_video_autoencoder import CausalVideoAutoencoder
from xora.models.transformers.transformer3d import Transformer3DModel
from xora.models.transformers.symmetric_patchifier import SymmetricPatchifier
from xora.schedulers.rf import RectifiedFlowScheduler
from xora.pipelines.pipeline_xora_video import XoraVideoPipeline
from transformers import T5EncoderModel, T5Tokenizer
from xora.utils.conditioning_method import ConditioningMethod
from pathlib import Path
import safetensors.torch
import json
import numpy as np
import cv2
from PIL import Image
import tempfile
import os
import gc
from openai import OpenAI
import re

# Load system prompts
system_prompt_t2v = """๋‹น์‹ ์€ ๋น„๋””์˜ค ์ƒ์„ฑ์„ ์œ„ํ•œ ํ”„๋กฌํ”„ํŠธ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. 
์ฃผ์–ด์ง„ ํ”„๋กฌํ”„ํŠธ๋ฅผ ๋‹ค์Œ ๊ตฌ์กฐ์— ๋งž๊ฒŒ ๊ฐœ์„ ํ•ด์ฃผ์„ธ์š”:
1. ์ฃผ์š” ๋™์ž‘์„ ๋ช…ํ™•ํ•œ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ์‹œ์ž‘
2. ๊ตฌ์ฒด์ ์ธ ๋™์ž‘๊ณผ ์ œ์Šค์ฒ˜๋ฅผ ์‹œ๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ ์„ค๋ช…
3. ์บ๋ฆญํ„ฐ/๊ฐ์ฒด์˜ ์™ธ๋ชจ๋ฅผ ์ƒ์„ธํžˆ ๋ฌ˜์‚ฌ
4. ๋ฐฐ๊ฒฝ๊ณผ ํ™˜๊ฒฝ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ๊ตฌ์ฒด์ ์œผ๋กœ ํฌํ•จ
5. ์นด๋ฉ”๋ผ ๊ฐ๋„์™€ ์›€์ง์ž„์„ ๋ช…์‹œ
6. ์กฐ๋ช…๊ณผ ์ƒ‰์ƒ์„ ์ž์„ธํžˆ ์„ค๋ช…
7. ๋ณ€ํ™”๋‚˜ ๊ฐ‘์ž‘์Šค๋Ÿฌ์šด ์‚ฌ๊ฑด์„ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํฌํ•จ
๋ชจ๋“  ์„ค๋ช…์€ ํ•˜๋‚˜์˜ ์ž์—ฐ์Šค๋Ÿฌ์šด ๋ฌธ๋‹จ์œผ๋กœ ์ž‘์„ฑํ•˜๊ณ , 
์ดฌ์˜ ๊ฐ๋…์ด ์ดฌ์˜ ๋ชฉ๋ก์„ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๊ตฌ์ฒด์ ์ด๊ณ  ์‹œ๊ฐ์ ์œผ๋กœ ์ž‘์„ฑํ•˜์„ธ์š”.
200๋‹จ์–ด๋ฅผ ๋„˜์ง€ ์•Š๋„๋ก ํ•˜๋˜, ์ตœ๋Œ€ํ•œ ์ƒ์„ธํ•˜๊ฒŒ ์ž‘์„ฑํ•˜์„ธ์š”."""

system_prompt_i2v = """๋‹น์‹ ์€ ์ด๋ฏธ์ง€ ๊ธฐ๋ฐ˜ ๋น„๋””์˜ค ์ƒ์„ฑ์„ ์œ„ํ•œ ํ”„๋กฌํ”„ํŠธ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. 
์ฃผ์–ด์ง„ ํ”„๋กฌํ”„ํŠธ๋ฅผ ๋‹ค์Œ ๊ตฌ์กฐ์— ๋งž๊ฒŒ ๊ฐœ์„ ํ•ด์ฃผ์„ธ์š”:
1. ์ฃผ์š” ๋™์ž‘์„ ๋ช…ํ™•ํ•œ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ์‹œ์ž‘
2. ๊ตฌ์ฒด์ ์ธ ๋™์ž‘๊ณผ ์ œ์Šค์ฒ˜๋ฅผ ์‹œ๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ ์„ค๋ช…
3. ์บ๋ฆญํ„ฐ/๊ฐ์ฒด์˜ ์™ธ๋ชจ๋ฅผ ์ƒ์„ธํžˆ ๋ฌ˜์‚ฌ
4. ๋ฐฐ๊ฒฝ๊ณผ ํ™˜๊ฒฝ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ๊ตฌ์ฒด์ ์œผ๋กœ ํฌํ•จ
5. ์นด๋ฉ”๋ผ ๊ฐ๋„์™€ ์›€์ง์ž„์„ ๋ช…์‹œ
6. ์กฐ๋ช…๊ณผ ์ƒ‰์ƒ์„ ์ž์„ธํžˆ ์„ค๋ช…
7. ๋ณ€ํ™”๋‚˜ ๊ฐ‘์ž‘์Šค๋Ÿฌ์šด ์‚ฌ๊ฑด์„ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํฌํ•จ
๋ชจ๋“  ์„ค๋ช…์€ ํ•˜๋‚˜์˜ ์ž์—ฐ์Šค๋Ÿฌ์šด ๋ฌธ๋‹จ์œผ๋กœ ์ž‘์„ฑํ•˜๊ณ , 
์ดฌ์˜ ๊ฐ๋…์ด ์ดฌ์˜ ๋ชฉ๋ก์„ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๊ตฌ์ฒด์ ์ด๊ณ  ์‹œ๊ฐ์ ์œผ๋กœ ์ž‘์„ฑํ•˜์„ธ์š”.
200๋‹จ์–ด๋ฅผ ๋„˜์ง€ ์•Š๋„๋ก ํ•˜๋˜, ์ตœ๋Œ€ํ•œ ์ƒ์„ธํ•˜๊ฒŒ ์ž‘์„ฑํ•˜์„ธ์š”."""

# Load Hugging Face token if needed
hf_token = os.getenv("HF_TOKEN")
openai_api_key = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key=openai_api_key)

# Initialize translation pipeline
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")

# Korean text detection function
def contains_korean(text):
    korean_pattern = re.compile('[ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ]')
    return bool(korean_pattern.search(text))

def translate_korean_prompt(prompt):
    """
    Translate Korean prompt to English if Korean text is detected
    """
    if contains_korean(prompt):
        translated = translator(prompt)[0]['translation_text']
        print(f"Original Korean prompt: {prompt}")
        print(f"Translated English prompt: {translated}")
        return translated
    return prompt

def enhance_prompt(prompt, type="t2v"):
    system_prompt = system_prompt_t2v if type == "t2v" else system_prompt_i2v
    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": prompt},
    ]

    try:
        response = client.chat.completions.create(
            model="gpt-4-1106-preview",
            messages=messages,
            max_tokens=2000,
        )
        enhanced_prompt = response.choices[0].message.content.strip()
        
        print("\n=== ํ”„๋กฌํ”„ํŠธ ์ฆ๊ฐ• ๊ฒฐ๊ณผ ===")
        print("Original Prompt:")
        print(prompt)
        print("\nEnhanced Prompt:")
        print(enhanced_prompt)
        print("========================\n")
        
        return enhanced_prompt
    except Exception as e:
        print(f"Error during prompt enhancement: {e}")
        return prompt

def update_prompt_t2v(prompt, enhance_toggle):
    return update_prompt(prompt, enhance_toggle, "t2v")

def update_prompt_i2v(prompt, enhance_toggle):
    return update_prompt(prompt, enhance_toggle, "i2v")
    
def update_prompt(prompt, enhance_toggle, type="t2v"):
    if enhance_toggle:
        return enhance_prompt(prompt, type)
    return prompt

# Set model download directory within Hugging Face Spaces
model_path = "asset"
if not os.path.exists(model_path):
    snapshot_download(
        "Lightricks/LTX-Video", local_dir=model_path, repo_type="model", token=hf_token
    )

# Global variables to load components
vae_dir = Path(model_path) / "vae"
unet_dir = Path(model_path) / "unet"
scheduler_dir = Path(model_path) / "scheduler"

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

def load_vae(vae_dir):
    vae_ckpt_path = vae_dir / "vae_diffusion_pytorch_model.safetensors"
    vae_config_path = vae_dir / "config.json"
    with open(vae_config_path, "r") as f:
        vae_config = json.load(f)
    vae = CausalVideoAutoencoder.from_config(vae_config)
    vae_state_dict = safetensors.torch.load_file(vae_ckpt_path)
    vae.load_state_dict(vae_state_dict)
    return vae.to(device=device, dtype=torch.bfloat16)

def load_unet(unet_dir):
    unet_ckpt_path = unet_dir / "unet_diffusion_pytorch_model.safetensors"
    unet_config_path = unet_dir / "config.json"
    transformer_config = Transformer3DModel.load_config(unet_config_path)
    transformer = Transformer3DModel.from_config(transformer_config)
    unet_state_dict = safetensors.torch.load_file(unet_ckpt_path)
    transformer.load_state_dict(unet_state_dict, strict=True)
    return transformer.to(device=device, dtype=torch.bfloat16)

def load_scheduler(scheduler_dir):
    scheduler_config_path = scheduler_dir / "scheduler_config.json"
    scheduler_config = RectifiedFlowScheduler.load_config(scheduler_config_path)
    return RectifiedFlowScheduler.from_config(scheduler_config)

# Helper function for image processing
def center_crop_and_resize(frame, target_height, target_width):
    h, w, _ = frame.shape
    aspect_ratio_target = target_width / target_height
    aspect_ratio_frame = w / h
    if aspect_ratio_frame > aspect_ratio_target:
        new_width = int(h * aspect_ratio_target)
        x_start = (w - new_width) // 2
        frame_cropped = frame[:, x_start : x_start + new_width]
    else:
        new_height = int(w / aspect_ratio_target)
        y_start = (h - new_height) // 2
        frame_cropped = frame[y_start : y_start + new_height, :]
    frame_resized = cv2.resize(frame_cropped, (target_width, target_height))
    return frame_resized

def load_image_to_tensor_with_resize(image_path, target_height=512, target_width=768):
    image = Image.open(image_path).convert("RGB")
    image_np = np.array(image)
    frame_resized = center_crop_and_resize(image_np, target_height, target_width)
    frame_tensor = torch.tensor(frame_resized).permute(2, 0, 1).float()
    frame_tensor = (frame_tensor / 127.5) - 1.0
    return frame_tensor.unsqueeze(0).unsqueeze(2)

# Load models
vae = load_vae(vae_dir)
unet = load_unet(unet_dir)
scheduler = load_scheduler(scheduler_dir)
patchifier = SymmetricPatchifier(patch_size=1)
text_encoder = T5EncoderModel.from_pretrained(
    "PixArt-alpha/PixArt-XL-2-1024-MS", subfolder="text_encoder"
).to(device)
tokenizer = T5Tokenizer.from_pretrained(
    "PixArt-alpha/PixArt-XL-2-1024-MS", subfolder="tokenizer"
)

pipeline = XoraVideoPipeline(
    transformer=unet,
    patchifier=patchifier,
    text_encoder=text_encoder,
    tokenizer=tokenizer,
    scheduler=scheduler,
    vae=vae,
).to(device)

# Preset options for resolution and frame configuration
preset_options = [
    {"label": "1216x704, 41 frames", "width": 1216, "height": 704, "num_frames": 41},
    {"label": "1088x704, 49 frames", "width": 1088, "height": 704, "num_frames": 49},
    {"label": "1056x640, 57 frames", "width": 1056, "height": 640, "num_frames": 57},
    {"label": "992x608, 65 frames", "width": 992, "height": 608, "num_frames": 65},
    {"label": "896x608, 73 frames", "width": 896, "height": 608, "num_frames": 73},
    {"label": "896x544, 81 frames", "width": 896, "height": 544, "num_frames": 81},
    {"label": "832x544, 89 frames", "width": 832, "height": 544, "num_frames": 89},
    {"label": "800x512, 97 frames", "width": 800, "height": 512, "num_frames": 97},
    {"label": "768x512, 97 frames", "width": 768, "height": 512, "num_frames": 97},
    {"label": "800x480, 105 frames", "width": 800, "height": 480, "num_frames": 105},
    {"label": "736x480, 113 frames", "width": 736, "height": 480, "num_frames": 113},
    {"label": "704x480, 121 frames", "width": 704, "height": 480, "num_frames": 121},
    {"label": "704x448, 129 frames", "width": 704, "height": 448, "num_frames": 129},
    {"label": "672x448, 137 frames", "width": 672, "height": 448, "num_frames": 137},
    {"label": "640x416, 153 frames", "width": 640, "height": 416, "num_frames": 153},
    {"label": "672x384, 161 frames", "width": 672, "height": 384, "num_frames": 161},
    {"label": "640x384, 169 frames", "width": 640, "height": 384, "num_frames": 169},
    {"label": "608x384, 177 frames", "width": 608, "height": 384, "num_frames": 177},
    {"label": "576x384, 185 frames", "width": 576, "height": 384, "num_frames": 185},
    {"label": "608x352, 193 frames", "width": 608, "height": 352, "num_frames": 193},
    {"label": "576x352, 201 frames", "width": 576, "height": 352, "num_frames": 201},
    {"label": "544x352, 209 frames", "width": 544, "height": 352, "num_frames": 209},
    {"label": "512x352, 225 frames", "width": 512, "height": 352, "num_frames": 225},
    {"label": "512x352, 233 frames", "width": 512, "height": 352, "num_frames": 233},
    {"label": "544x320, 241 frames", "width": 544, "height": 320, "num_frames": 241},
    {"label": "512x320, 249 frames", "width": 512, "height": 320, "num_frames": 249},
    {"label": "512x320, 257 frames", "width": 512, "height": 320, "num_frames": 257},
]

def preset_changed(preset):
    if preset != "Custom":
        selected = next(item for item in preset_options if item["label"] == preset)
        return (
            selected["height"],
            selected["width"],
            selected["num_frames"],
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
        )
    else:
        return (
            None,
            None,
            None,
            gr.update(visible=True),
            gr.update(visible=True),
            gr.update(visible=True),
        )

def generate_video_from_text(
    prompt="",
    enhance_prompt_toggle=False,
    negative_prompt="",
    frame_rate=25,
    seed=171198,
    num_inference_steps=30,
    guidance_scale=3,
    height=512,
    width=768,
    num_frames=121,
    progress=gr.Progress(),
):
    if len(prompt.strip()) < 50:
        raise gr.Error(
            "ํ”„๋กฌํ”„ํŠธ๋Š” ์ตœ์†Œ 50์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋” ์ž์„ธํ•œ ์„ค๋ช…์„ ์ œ๊ณตํ•ด์ฃผ์„ธ์š”.",
            duration=5,
        )

    # Translate Korean prompts to English
    prompt = translate_korean_prompt(prompt)
    negative_prompt = translate_korean_prompt(negative_prompt)

    sample = {
        "prompt": prompt,
        "prompt_attention_mask": None,
        "negative_prompt": negative_prompt,
        "negative_prompt_attention_mask": None,
        "media_items": None,
    }

    generator = torch.Generator(device="cpu").manual_seed(seed)

    def gradio_progress_callback(self, step, timestep, kwargs):
        progress((step + 1) / num_inference_steps)

    try:
        with torch.no_grad():
            images = pipeline(
                num_inference_steps=num_inference_steps,
                num_images_per_prompt=1,
                guidance_scale=guidance_scale,
                generator=generator,
                output_type="pt",
                height=height,
                width=width,
                num_frames=num_frames,
                frame_rate=frame_rate,
                **sample,
                is_video=True,
                vae_per_channel_normalize=True,
                conditioning_method=ConditioningMethod.UNCONDITIONAL,
                mixed_precision=True,
                callback_on_step_end=gradio_progress_callback,
            ).images
    except Exception as e:
        raise gr.Error(
            f"๋น„๋””์˜ค ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”. ์˜ค๋ฅ˜: {e}",
            duration=5,
        )
    finally:
        torch.cuda.empty_cache()
        gc.collect()

    output_path = tempfile.mktemp(suffix=".mp4")
    print(images.shape)
    video_np = images.squeeze(0).permute(1, 2, 3, 0).cpu().float().numpy()
    video_np = (video_np * 255).astype(np.uint8)
    height, width = video_np.shape[1:3]
    out = cv2.VideoWriter(
        output_path, cv2.VideoWriter_fourcc(*"mp4v"), frame_rate, (width, height)
    )
    for frame in video_np[..., ::-1]:
        out.write(frame)
    out.release()
    del images
    del video_np
    torch.cuda.empty_cache()
    return output_path

def generate_video_from_image(
    image_path,
    prompt="",
    enhance_prompt_toggle=False,
    negative_prompt="",
    frame_rate=25,
    seed=171198,
    num_inference_steps=30,
    guidance_scale=3,
    height=512,
    width=768,
    num_frames=121,
    progress=gr.Progress(),
):
    print("Height: ", height)
    print("Width: ", width)
    print("Num Frames: ", num_frames)

    if len(prompt.strip()) < 50:
        raise gr.Error(
            "ํ”„๋กฌํ”„ํŠธ๋Š” ์ตœ์†Œ 50์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋” ์ž์„ธํ•œ ์„ค๋ช…์„ ์ œ๊ณตํ•ด์ฃผ์„ธ์š”.",
            duration=5,
        )

    if not image_path:
        raise gr.Error("์ž…๋ ฅ ์ด๋ฏธ์ง€๋ฅผ ์ œ๊ณตํ•ด์ฃผ์„ธ์š”.", duration=5)

    # Translate Korean prompts to English
    prompt = translate_korean_prompt(prompt)
    negative_prompt = translate_korean_prompt(negative_prompt)

    media_items = (
        load_image_to_tensor_with_resize(image_path, height, width).to(device).detach()
    )

    sample = {
        "prompt": prompt,
        "prompt_attention_mask": None,
        "negative_prompt": negative_prompt,
        "negative_prompt_attention_mask": None,
        "media_items": media_items,
    }

    generator = torch.Generator(device="cpu").manual_seed(seed)

    def gradio_progress_callback(self, step, timestep, kwargs):
        progress((step + 1) / num_inference_steps)

    try:
        with torch.no_grad():
            images = pipeline(
                num_inference_steps=num_inference_steps,
                num_images_per_prompt=1,
                guidance_scale=guidance_scale,
                generator=generator,
                output_type="pt",
                height=height,
                width=width,
                num_frames=num_frames,
                frame_rate=frame_rate,
                **sample,
                is_video=True,
                vae_per_channel_normalize=True,
                conditioning_method=ConditioningMethod.FIRST_FRAME,
                mixed_precision=True,
                callback_on_step_end=gradio_progress_callback,
            ).images

        output_path = tempfile.mktemp(suffix=".mp4")
        video_np = images.squeeze(0).permute(1, 2, 3, 0).cpu().float().numpy()
        video_np = (video_np * 255).astype(np.uint8)
        height, width = video_np.shape[1:3]
        out = cv2.VideoWriter(
            output_path, cv2.VideoWriter_fourcc(*"mp4v"), frame_rate, (width, height)
        )
        for frame in video_np[..., ::-1]:
            out.write(frame)
        out.release()
    except Exception as e:
        raise gr.Error(
            f"๋น„๋””์˜ค ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”. ์˜ค๋ฅ˜: {e}",
            duration=5,
        )

    finally:
        torch.cuda.empty_cache()
        gc.collect()

    return output_path

def create_advanced_options():
    with gr.Accordion("Step 4: Advanced Options (Optional)", open=False):
        seed = gr.Slider(
            label="4.1 Seed", minimum=0, maximum=1000000, step=1, value=171198
        )
        inference_steps = gr.Slider(
            label="4.2 Inference Steps", minimum=1, maximum=50, step=1, value=30
        )
        guidance_scale = gr.Slider(
            label="4.3 Guidance Scale", minimum=1.0, maximum=5.0, step=0.1, value=3.0
        )

        height_slider = gr.Slider(
            label="4.4 Height",
            minimum=256,
            maximum=1024,
            step=64,
            value=512,
            visible=False,
        )
        width_slider = gr.Slider(
            label="4.5 Width",
            minimum=256,
            maximum=1024,
            step=64,
            value=768,
            visible=False,
        )
        num_frames_slider = gr.Slider(
            label="4.5 Number of Frames",
            minimum=1,
            maximum=200,
            step=1,
            value=121,
            visible=False,
        )

        return [
            seed,
            inference_steps,
            guidance_scale,
            height_slider,
            width_slider,
            num_frames_slider,
        ]

# Gradio Interface Definition
with gr.Blocks(theme=gr.themes.Soft()) as iface:
    with gr.Tabs():
        # Text to Video Tab
        with gr.TabItem("ํ…์ŠคํŠธ๋กœ ๋น„๋””์˜ค ๋งŒ๋“ค๊ธฐ"):
            with gr.Row():
                with gr.Column():
                    txt2vid_prompt = gr.Textbox(
                        label="Step 1: ํ”„๋กฌํ”„ํŠธ ์ž…๋ ฅ",
                        placeholder="์ƒ์„ฑํ•˜๊ณ  ์‹ถ์€ ๋น„๋””์˜ค๋ฅผ ์„ค๋ช…ํ•˜์„ธ์š” (์ตœ์†Œ 50์ž)...",
                        value="๊ฐˆ์ƒ‰ ๊ธด ๋จธ๋ฆฌ๋ฅผ ๊ฐ€์ง„ ์—ฌ์„ฑ์ด ๊ธˆ๋ฐœ์˜ ๊ธด ๋จธ๋ฆฌ๋ฅผ ๊ฐ€์ง„ ๋‹ค๋ฅธ ์—ฌ์„ฑ์„ ํ–ฅํ•ด ๋ฏธ์†Œ์ง“์Šต๋‹ˆ๋‹ค. ๊ฐˆ์ƒ‰ ๋จธ๋ฆฌ์˜ ์—ฌ์„ฑ์€ ๊ฒ€์€์ƒ‰ ์ž์ผ“์„ ์ž…๊ณ  ์žˆ์œผ๋ฉฐ ์˜ค๋ฅธ์ชฝ ๋บจ์— ์ž‘์€ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์นด๋ฉ”๋ผ ๊ฐ๋„๋Š” ๊ฐˆ์ƒ‰ ๋จธ๋ฆฌ ์—ฌ์„ฑ์˜ ์–ผ๊ตด์— ํด๋กœ์ฆˆ์—…๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์กฐ๋ช…์€ ์ž์—ฐ์Šค๋Ÿฝ๊ณ  ๋”ฐ๋œปํ•˜๋ฉฐ, ์„์–‘์—์„œ ์˜ค๋Š” ๋“ฏํ•œ ๋ถ€๋“œ๋Ÿฌ์šด ๋น›์ด ์žฅ๋ฉด์„ ๋น„์ถฅ๋‹ˆ๋‹ค. ์žฅ๋ฉด์€ ์‹ค์ œ ์˜์ƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค.",
                        lines=5,
                    )
                    txt2vid_enhance_toggle = Toggle(
                        label="ํ”„๋กฌํ”„ํŠธ ๊ฐœ์„ ",
                        value=False,
                        interactive=True,
                    )

                    txt2vid_negative_prompt = gr.Textbox(
                        label="Step 2: ๋„ค๊ฑฐํ‹ฐ๋ธŒ ํ”„๋กฌํ”„ํŠธ ์ž…๋ ฅ",
                        placeholder="๋น„๋””์˜ค์—์„œ ์›ํ•˜์ง€ ์•Š๋Š” ์š”์†Œ๋ฅผ ์„ค๋ช…ํ•˜์„ธ์š”...",
                        value="low quality, worst quality, deformed, distorted, warped, motion smear, motion artifacts, fused fingers, incorrect anatomy, strange hands, unattractive",
                        lines=2,
                    )

                    txt2vid_preset = gr.Dropdown(
                        choices=[p["label"] for p in preset_options],
                        value="768x512, 97 frames",
                        label="Step 3.1: ํ•ด์ƒ๋„ ํ”„๋ฆฌ์…‹ ์„ ํƒ",
                    )

                    txt2vid_frame_rate = gr.Slider(
                        label="Step 3.2: ํ”„๋ ˆ์ž„ ๋ ˆ์ดํŠธ",
                        minimum=21,
                        maximum=30,
                        step=1,
                        value=25,
                    )

                    txt2vid_advanced = create_advanced_options()
                    txt2vid_generate = gr.Button(
                        "Step 5: ๋น„๋””์˜ค ์ƒ์„ฑ",
                        variant="primary",
                        size="lg",
                    )

                with gr.Column():
                    txt2vid_output = gr.Video(label="์ƒ์„ฑ๋œ ๋น„๋””์˜ค")

            with gr.Row():
                gr.Examples(
                    examples=[
                        [
                            "์ „ํ†ต์ ์ธ ๋ชฝ๊ณจ ๋“œ๋ ˆ์Šค๋ฅผ ์ž…์€ ์ Š์€ ์—ฌ์„ฑ์ด ์–‡์€ ํฐ์ƒ‰ ์ปคํŠผ์„ ํ†ตํ•ด ํ˜ธ๊ธฐ์‹ฌ๊ณผ ๊ธด์žฅ์ด ์„ž์ธ ํ‘œ์ •์œผ๋กœ ๋“ค์—ฌ๋‹ค๋ณด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ์„ฑ์€ ํฐ ๊ตฌ์Šฌ๋กœ ์žฅ์‹๋œ ๋‘ ๊ฐœ์˜ ๋•‹์€ ๋จธ๋ฆฌ๋กœ ์Šคํƒ€์ผ๋ง๋œ ๊ธด ๊ฒ€์€ ๋จธ๋ฆฌ๋ฅผ ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ๋ˆˆ์€ ๋†€๋žŒ์„ ๋„๋ฉฐ ํฌ๊ฒŒ ๋– ์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋…€์˜ ๋“œ๋ ˆ์Šค๋Š” ํ™”๋ คํ•œ ๊ธˆ์ƒ‰ ์ž์ˆ˜๊ฐ€ ์ƒˆ๊ฒจ์ง„ ์„ ๋ช…ํ•œ ํŒŒ๋ž€์ƒ‰์ด๋ฉฐ, ๋น„์Šทํ•œ ๋””์ž์ธ์˜ ๋จธ๋ฆฌ๋ ๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ๊ฒฝ์€ ์‹ ๋น„๋กœ์›€๊ณผ ํ˜ธ๊ธฐ์‹ฌ์„ ์ž์•„๋‚ด๋Š” ๋‹จ์ˆœํ•œ ํฐ์ƒ‰ ์ปคํŠผ์ž…๋‹ˆ๋‹ค.",
                            "low quality, worst quality, deformed, distorted, warped, motion smear, motion artifacts, fused fingers, incorrect anatomy, strange hands, unattractive",
                            "assets/t2v_2.mp4",
                        ],
                        [
                            "๋…ธ๋ž€์ƒ‰ ์žฌํ‚ท์„ ์ž…์€ ๊ธˆ๋ฐœ ๋จธ๋ฆฌ์˜ ์ Š์€ ๋‚จ์ž๊ฐ€ ์ˆฒ์— ์„œ์„œ ์ฃผ์œ„๋ฅผ ๋‘˜๋Ÿฌ๋ด…๋‹ˆ๋‹ค. ๊ทธ๋Š” ๋ฐ์€ ํ”ผ๋ถ€๋ฅผ ๊ฐ€์กŒ๊ณ  ๋จธ๋ฆฌ๋Š” ๊ฐ€์šด๋ฐ ๊ฐ€๋ฅด๋งˆ๋กœ ์Šคํƒ€์ผ๋ง๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Š” ์™ผ์ชฝ์„ ๋ณด๊ณ  ๋‚œ ํ›„ ์˜ค๋ฅธ์ชฝ์„ ๋ณด๋ฉฐ, ๊ฐ ๋ฐฉํ–ฅ์„ ์ž ์‹œ ์‘์‹œํ•ฉ๋‹ˆ๋‹ค. ์นด๋ฉ”๋ผ๋Š” ๋‚ฎ์€ ๊ฐ๋„์—์„œ ๋‚จ์ž๋ฅผ ์˜ฌ๋ ค๋‹ค๋ณด๋ฉฐ ๊ณ ์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ๊ฒฝ์€ ์•ฝ๊ฐ„ ํ๋ฆฟํ•˜๋ฉฐ, ๋…น์ƒ‰ ๋‚˜๋ฌด๋“ค๊ณผ ๋‚จ์ž์˜ ๋’ค์—์„œ ๋ฐ๊ฒŒ ๋น„์น˜๋Š” ํƒœ์–‘์ด ๋ณด์ž…๋‹ˆ๋‹ค. ์กฐ๋ช…์€ ์ž์—ฐ์Šค๋Ÿฝ๊ณ  ๋”ฐ๋œปํ•˜๋ฉฐ, ํƒœ์–‘ ๋น›์ด ๋‚จ์ž์˜ ์–ผ๊ตด์„ ๊ฐ€๋กœ์ง€๋ฅด๋Š” ๋ Œ์ฆˆ ํ”Œ๋ ˆ์–ด๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์žฅ๋ฉด์€ ์‹ค์ œ ์˜์ƒ์ฒ˜๋Ÿผ ์ดฌ์˜๋˜์—ˆ์Šต๋‹ˆ๋‹ค.",
                            "low quality, worst quality, deformed, distorted, warped, motion smear, motion artifacts, fused fingers, incorrect anatomy, strange hands, unattractive",
                            "assets/t2v_1.mp4",
                        ],
                        [
                            "ํ•œ ์‚ฌ์ดํด๋ฆฌ์ŠคํŠธ๊ฐ€ ๊ตฝ์ด์ง„ ์‚ฐ๊ธธ์„ ๋”ฐ๋ผ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค. ๊ณต๊ธฐ์—ญํ•™์ ์ธ ์žฅ๋น„๋ฅผ ์ž…์€ ๊ทธ๋Š” ๊ฐ•ํ•˜๊ฒŒ ํŽ˜๋‹ฌ์„ ๋ฐŸ๊ณ  ์žˆ์œผ๋ฉฐ, ์ด๋งˆ์—๋Š” ๋•€๋ฐฉ์šธ์ด ๋ฐ˜์ง์ž…๋‹ˆ๋‹ค. ์นด๋ฉ”๋ผ๋Š” ๊ทธ์˜ ๊ฒฐ์—ฐํ•œ ํ‘œ์ •๊ณผ ์ˆจ ๋ง‰ํžˆ๋Š” ํ’๊ฒฝ์„ ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉฐ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์†Œ๋‚˜๋ฌด๋“ค์ด ์Šค์ณ ์ง€๋‚˜๊ฐ€๊ณ , ํ•˜๋Š˜์€ ์„ ๋ช…ํ•œ ํŒŒ๋ž€์ƒ‰์ž…๋‹ˆ๋‹ค. ์ด ์žฅ๋ฉด์€ ํ™œ๊ธฐ์ฐจ๊ณ  ๊ฒฝ์Ÿ์ ์ธ ๋ถ„์œ„๊ธฐ๋ฅผ ์ž์•„๋ƒ…๋‹ˆ๋‹ค.",
                            "low quality, worst quality, deformed, distorted, warped, motion smear, motion artifacts, fused fingers, incorrect anatomy, strange hands, unattractive",
                            "assets/t2v_0.mp4",
                        ],
                    ],
                    inputs=[txt2vid_prompt, txt2vid_negative_prompt, txt2vid_output],
                    label="ํ…์ŠคํŠธ-๋น„๋””์˜ค ์ƒ์„ฑ ์˜ˆ์‹œ",
                )

        # Image to Video Tab
        with gr.TabItem("์ด๋ฏธ์ง€๋กœ ๋น„๋””์˜ค ๋งŒ๋“ค๊ธฐ"):
            with gr.Row():
                with gr.Column():
                    img2vid_image = gr.Image(
                        type="filepath",
                        label="Step 1: ์ž…๋ ฅ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ",
                        elem_id="image_upload",
                    )
                    img2vid_prompt = gr.Textbox(
                        label="Step 2: ํ”„๋กฌํ”„ํŠธ ์ž…๋ ฅ",
                        placeholder="์ด๋ฏธ์ง€๋ฅผ ์–ด๋–ป๊ฒŒ ์• ๋‹ˆ๋ฉ”์ด์…˜ํ™”ํ• ์ง€ ์„ค๋ช…ํ•˜์„ธ์š” (์ตœ์†Œ 50์ž)...",
                        value="๊ฐˆ์ƒ‰ ๊ธด ๋จธ๋ฆฌ๋ฅผ ๊ฐ€์ง„ ์—ฌ์„ฑ์ด ๊ธˆ๋ฐœ์˜ ๊ธด ๋จธ๋ฆฌ๋ฅผ ๊ฐ€์ง„ ๋‹ค๋ฅธ ์—ฌ์„ฑ์„ ํ–ฅํ•ด ๋ฏธ์†Œ์ง“์Šต๋‹ˆ๋‹ค. ๊ฐˆ์ƒ‰ ๋จธ๋ฆฌ์˜ ์—ฌ์„ฑ์€ ๊ฒ€์€์ƒ‰ ์ž์ผ“์„ ์ž…๊ณ  ์žˆ์œผ๋ฉฐ ์˜ค๋ฅธ์ชฝ ๋บจ์— ์ž‘์€ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์นด๋ฉ”๋ผ ๊ฐ๋„๋Š” ๊ฐˆ์ƒ‰ ๋จธ๋ฆฌ ์—ฌ์„ฑ์˜ ์–ผ๊ตด์— ํด๋กœ์ฆˆ์—…๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์กฐ๋ช…์€ ์ž์—ฐ์Šค๋Ÿฝ๊ณ  ๋”ฐ๋œปํ•˜๋ฉฐ, ์„์–‘์—์„œ ์˜ค๋Š” ๋“ฏํ•œ ๋ถ€๋“œ๋Ÿฌ์šด ๋น›์ด ์žฅ๋ฉด์„ ๋น„์ถฅ๋‹ˆ๋‹ค. ์žฅ๋ฉด์€ ์‹ค์ œ ์˜์ƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค.",
                        lines=5,
                    )
                    img2vid_enhance_toggle = Toggle(
                        label="ํ”„๋กฌํ”„ํŠธ ๊ฐœ์„ ",
                        value=False,
                        interactive=True,
                    )
                    img2vid_negative_prompt = gr.Textbox(
                        label="Step 3: ๋„ค๊ฑฐํ‹ฐ๋ธŒ ํ”„๋กฌํ”„ํŠธ ์ž…๋ ฅ",
                        placeholder="๋น„๋””์˜ค์—์„œ ์›ํ•˜์ง€ ์•Š๋Š” ์š”์†Œ๋ฅผ ์„ค๋ช…ํ•˜์„ธ์š”...",
                        value="low quality, worst quality, deformed, distorted, warped, motion smear, motion artifacts, fused fingers, incorrect anatomy, strange hands, unattractive",
                        lines=2,
                    )

                    img2vid_preset = gr.Dropdown(
                        choices=[p["label"] for p in preset_options],
                        value="768x512, 97 frames",
                        label="Step 3.1: ํ•ด์ƒ๋„ ํ”„๋ฆฌ์…‹ ์„ ํƒ",
                    )

                    img2vid_frame_rate = gr.Slider(
                        label="Step 3.2: ํ”„๋ ˆ์ž„ ๋ ˆ์ดํŠธ",
                        minimum=21,
                        maximum=30,
                        step=1,
                        value=25,
                    )

                    img2vid_advanced = create_advanced_options()
                    img2vid_generate = gr.Button(
                        "Step 6: ๋น„๋””์˜ค ์ƒ์„ฑ", variant="primary", size="lg"
                    )

                with gr.Column():
                    img2vid_output = gr.Video(label="์ƒ์„ฑ๋œ ๋น„๋””์˜ค")

            with gr.Row():
                gr.Examples(
                    examples=[
                        [
                            "assets/i2v_i2.png",
                            "์—ฌ์„ฑ์ด ํฐ์ƒ‰ ์ „๊ธฐ ๋ฒ„๋„ˆ ์œ„์—์„œ ๋“๋Š” ๋ฌผ์ด ๋‹ด๊ธด ๋ƒ„๋น„๋ฅผ ์ “๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณด๋ผ์ƒ‰ ๋งค๋‹ˆํ์–ด๋ฅผ ๋ฐ”๋ฅธ ๊ทธ๋…€์˜ ์†์ด ํ•˜์–€ ๋ƒ„๋น„ ์•ˆ์—์„œ ๋‚˜๋ฌด ์ˆŸ๊ฐ€๋ฝ์„ ์›ํ˜•์œผ๋กœ ์›€์ง์ž…๋‹ˆ๋‹ค. ๋ƒ„๋น„๋Š” ๊ฒ€์€์ƒ‰ ๋ฒ„ํŠผ๊ณผ ๋””์ง€ํ„ธ ๋””์Šคํ”Œ๋ ˆ์ด๊ฐ€ ์žˆ๋Š” ํฐ์ƒ‰ ์ „๊ธฐ ๋ฒ„๋„ˆ ์œ„์— ๋†“์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฒ„๋„ˆ๋Š” ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ ๋ชจ์„œ๋ฆฌ์— ๋นจ๊ฐ„์ƒ‰๊ณผ ํฐ์ƒ‰ ์ฒดํฌ๋ฌด๋Šฌ ์ฒœ์ด ๋ถ€๋ถ„์ ์œผ๋กœ ๋ณด์ด๋Š” ํฐ์ƒ‰ ์กฐ๋ฆฌ๋Œ€ ์œ„์— ๋†“์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค. ์นด๋ฉ”๋ผ ๊ฐ๋„๋Š” ์ •ํ™•ํžˆ ์œ„์—์„œ ๋‚ด๋ ค๋‹ค๋ณด๋Š” ๊ฐ๋„์ด๋ฉฐ ์žฅ๋ฉด ๋‚ด๋‚ด ๊ณ ์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์กฐ๋ช…์€ ๋ฐ๊ณ  ๊ณ ๋ฅธ ์ค‘์„ฑ์ ์ธ ํฐ์ƒ‰ ๋น›์œผ๋กœ ์žฅ๋ฉด์„ ๋น„์ถฅ๋‹ˆ๋‹ค. ์žฅ๋ฉด์€ ์‹ค์ œ ์˜์ƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค.",
                            "low quality, worst quality, deformed, distorted, warped, motion smear, motion artifacts, fused fingers, incorrect anatomy, strange hands, unattractive",
                            "assets/i2v_2.mp4",
                        ],
                        [
                            "assets/i2v_i0.png",
                            "๊ธด ํ๋ฅด๋Š” ๋“œ๋ ˆ์Šค๋ฅผ ์ž…์€ ์—ฌ์„ฑ์ด ๋“คํŒ์— ์„œ์„œ ๋“ฑ์„ ์นด๋ฉ”๋ผ๋ฅผ ํ–ฅํ•œ ์ฑ„ ์ง€ํ‰์„ ์„ ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋…€์˜ ๋จธ๋ฆฌ์นด๋ฝ์€ ๊ธธ๊ณ  ๋ฐ์œผ๋ฉฐ ๋“ฑ ์•„๋ž˜๋กœ ํ˜๋Ÿฌ๋‚ด๋ฆฝ๋‹ˆ๋‹ค. ๊ทธ๋…€๋Š” ํฐ ์ฐธ๋‚˜๋ฌด์˜ ๋„“๊ฒŒ ํผ์ง„ ๊ฐ€์ง€ ์•„๋ž˜์— ์„œ ์žˆ์Šต๋‹ˆ๋‹ค. ์™ผ์ชฝ์œผ๋กœ๋Š” ๋ง๋ผ๋ถ™์€ ์ž”๋”” ์œ„์— ํด๋ž˜์‹ํ•œ ๋ฏธ๊ตญ ์ž๋™์ฐจ๊ฐ€ ์ฃผ์ฐจ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉ€๋ฆฌ์„œ๋Š” ํ•œ ๋Œ€์˜ ๋ถ€์„œ์ง„ ์ž๋™์ฐจ๊ฐ€ ์˜†์œผ๋กœ ๋ˆ„์›Œ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ํ•˜๋Š˜์€ ์–ด๋‘์šด ํ•˜๋Š˜์„ ๋ฐฐ๊ฒฝ์œผ๋กœ ๋ฐ์€ ํฐ ๊ตฌ๋ฆ„์ด ๊ทน์ ์ธ ์บ”๋ฒ„์Šค๋ฅผ ์ด๋ฃจ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ์ด๋ฏธ์ง€๋Š” ํ‘๋ฐฑ์œผ๋กœ, ๋น›๊ณผ ๊ทธ๋ฆผ์ž์˜ ๋Œ€๋น„๋ฅผ ๊ฐ•์กฐํ•ฉ๋‹ˆ๋‹ค. ์—ฌ์„ฑ์ด ์ฒœ์ฒœํžˆ ์ž๋™์ฐจ๋ฅผ ํ–ฅํ•ด ๊ฑธ์–ด๊ฐ€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.",
                            "low quality, worst quality, deformed, distorted, warped, motion smear, motion artifacts, fused fingers, incorrect anatomy, strange hands, unattractive",
                            "assets/i2v_0.mp4",
                        ],
                        [
                            "assets/i2v_i1.png",
                            "ํ•œ ์Œ์˜ ์†์ด ๋„์ž๊ธฐ ๋ฌผ๋ ˆ ์œ„์—์„œ ์ ํ†  ์กฐ๊ฐ์„ ๋ชจ์–‘ ์žก์•„ ์ ์ฐจ์ ์œผ๋กœ ์›๋ฟ” ๋ชจ์–‘์„ ๋งŒ๋“ค์–ด๊ฐ€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ”„๋ ˆ์ž„ ๋ฐ–์˜ ์‚ฌ๋žŒ์˜ ์†์ด ์ ํ† ๋กœ ๋ฎ์—ฌ ์žˆ์œผ๋ฉฐ, ํšŒ์ „ํ•˜๋Š” ๋„์ž๊ธฐ ๋ฌผ๋ ˆ ์ค‘์•™์— ์ ํ†  ๋ฉ์–ด๋ฆฌ๋ฅผ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ๋ˆ„๋ฅด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์†์€ ์›ํ˜•์œผ๋กœ ์›€์ง์ด๋ฉฐ, ์ ํ†  ์œ„์ชฝ์— ์ ์ฐจ์ ์œผ๋กœ ์›๋ฟ” ๋ชจ์–‘์„ ๋งŒ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ์นด๋ฉ”๋ผ๋Š” ๋„์ž๊ธฐ ๋ฌผ๋ ˆ ๋ฐ”๋กœ ์œ„์— ์œ„์น˜ํ•˜์—ฌ ์ ํ† ๊ฐ€ ๋ชจ์–‘ ์žกํ˜€๊ฐ€๋Š” ๊ฒƒ์„ ์กฐ๊ฐ๋„๋กœ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์กฐ๋ช…์€ ๋ฐ๊ณ  ๊ณ ๋ฅด๋ฉฐ, ์ ํ† ์™€ ๊ทธ๊ฒƒ์„ ๋‹ค๋ฃจ๋Š” ์†์„ ๋ฐ๊ฒŒ ๋น„์ถฅ๋‹ˆ๋‹ค. ์žฅ๋ฉด์€ ์‹ค์ œ ์˜์ƒ์ฒ˜๋Ÿผ ์ดฌ์˜๋˜์—ˆ์Šต๋‹ˆ๋‹ค.",
                            "low quality, worst quality, deformed, distorted, warped, motion smear, motion artifacts, fused fingers, incorrect anatomy, strange hands, unattractive",
                            "assets/i2v_1.mp4",
                        ],
                    ],
                    inputs=[
                        img2vid_image,
                        img2vid_prompt,
                        img2vid_negative_prompt,
                        img2vid_output,
                    ],
                    label="์ด๋ฏธ์ง€-๋น„๋””์˜ค ์ƒ์„ฑ ์˜ˆ์‹œ",
                )

    # Event handlers
# Event handlers
    txt2vid_preset.change(
        fn=preset_changed, 
        inputs=[txt2vid_preset], 
        outputs=txt2vid_advanced[3:]
    )

    txt2vid_enhance_toggle.change(
        fn=update_prompt_t2v,
        inputs=[txt2vid_prompt, txt2vid_enhance_toggle],
        outputs=txt2vid_prompt
    )

    txt2vid_generate.click(
        fn=generate_video_from_text,
        inputs=[
            txt2vid_prompt,
            txt2vid_enhance_toggle,
            txt2vid_negative_prompt,
            txt2vid_frame_rate,
            *txt2vid_advanced,
        ],
        outputs=txt2vid_output,
        concurrency_limit=1,
        concurrency_id="generate_video",
        queue=True,
    )

    img2vid_preset.change(
        fn=preset_changed, 
        inputs=[img2vid_preset], 
        outputs=img2vid_advanced[3:]
    )

    img2vid_enhance_toggle.change(
        fn=update_prompt_i2v,
        inputs=[img2vid_prompt, img2vid_enhance_toggle],
        outputs=img2vid_prompt
    )

    img2vid_generate.click(
        fn=generate_video_from_image,
        inputs=[
            img2vid_image,
            img2vid_prompt,
            img2vid_enhance_toggle,
            img2vid_negative_prompt,
            img2vid_frame_rate,
            *img2vid_advanced,
        ],
        outputs=img2vid_output,
        concurrency_limit=1,
        concurrency_id="generate_video",
        queue=True,
    )

if __name__ == "__main__":
    iface.queue(max_size=64, default_concurrency_limit=1, api_open=False).launch(
        share=True, show_api=False
    )