ginigen-sora / app-backup1.py
openfree's picture
Update app-backup1.py
f2f0666 verified
raw
history blame
43.4 kB
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 with device and clean_up settings
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
translator = pipeline(
"translation",
model="Helsinki-NLP/opus-mt-ko-en",
device=device,
clean_up_tokenization_spaces=True
)
# Korean text detection function
def contains_korean(text):
korean_pattern = re.compile('[ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ]')
return bool(korean_pattern.search(text))
def translate_korean_prompt(prompt, max_length=450):
"""
Translate Korean prompt to English if Korean text is detected
Split long text into chunks if necessary
"""
if not contains_korean(prompt):
return prompt
# Split long text into chunks
def split_text(text, max_length):
words = text.split()
chunks = []
current_chunk = []
current_length = 0
for word in words:
if current_length + len(word) + 1 > max_length:
chunks.append(' '.join(current_chunk))
current_chunk = [word]
current_length = len(word)
else:
current_chunk.append(word)
current_length += len(word) + 1
if current_chunk:
chunks.append(' '.join(current_chunk))
return chunks
try:
if len(prompt) > max_length:
chunks = split_text(prompt, max_length)
translated_chunks = []
for chunk in chunks:
translated = translator(chunk, max_length=512)[0]['translation_text']
translated_chunks.append(translated)
final_translation = ' '.join(translated_chunks)
else:
final_translation = translator(prompt, max_length=512)[0]['translation_text']
print(f"Original Korean prompt: {prompt}")
print(f"Translated English prompt: {final_translation}")
return final_translation
except Exception as e:
print(f"Translation error: {e}")
return prompt # Return original prompt if translation fails
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)
# State ๋ณ€์ˆ˜๋“ค์˜ ์ดˆ๊ธฐํ™” ์ˆ˜์ •
txt2vid_current_height = gr.State(value=320)
txt2vid_current_width = gr.State(value=512)
txt2vid_current_num_frames = gr.State(value=257)
img2vid_current_height = gr.State(value=320)
img2vid_current_width = gr.State(value=512)
img2vid_current_num_frames = gr.State(value=257)
# Preset options for resolution and frame configuration
# Convert frames to seconds assuming 25 FPS
preset_options = [
{"label": "[16:9 HD] 1216x704, 1.6์ดˆ", "width": 1216, "height": 704, "num_frames": 41},
{"label": "[16:9] 1088x704, 2.0์ดˆ", "width": 1088, "height": 704, "num_frames": 49},
{"label": "[16:9] 1056x640, 2.3์ดˆ", "width": 1056, "height": 640, "num_frames": 57},
{"label": "[16:9] 992x608, 2.6์ดˆ", "width": 992, "height": 608, "num_frames": 65},
{"label": "[16:9] 896x608, 2.9์ดˆ", "width": 896, "height": 608, "num_frames": 73},
{"label": "[16:9] 896x544, 3.2์ดˆ", "width": 896, "height": 544, "num_frames": 81},
{"label": "[16:9] 832x544, 3.6์ดˆ", "width": 832, "height": 544, "num_frames": 89},
{"label": "[16:9] 800x512, 3.9์ดˆ", "width": 800, "height": 512, "num_frames": 97},
{"label": "[16:9] 768x512, 3.9์ดˆ", "width": 768, "height": 512, "num_frames": 97},
{"label": "[16:9] 800x480, 4.2์ดˆ", "width": 800, "height": 480, "num_frames": 105},
{"label": "[16:9] 736x480, 4.5์ดˆ", "width": 736, "height": 480, "num_frames": 113},
{"label": "[3:2] 704x480, 4.8์ดˆ", "width": 704, "height": 480, "num_frames": 121},
{"label": "[16:9] 704x448, 5.2์ดˆ", "width": 704, "height": 448, "num_frames": 129},
{"label": "[16:9] 672x448, 5.5์ดˆ", "width": 672, "height": 448, "num_frames": 137},
{"label": "[16:9] 640x416, 6.1์ดˆ", "width": 640, "height": 416, "num_frames": 153},
{"label": "[16:9] 672x384, 6.4์ดˆ", "width": 672, "height": 384, "num_frames": 161},
{"label": "[16:9] 640x384, 6.8์ดˆ", "width": 640, "height": 384, "num_frames": 169},
{"label": "[16:9] 608x384, 7.1์ดˆ", "width": 608, "height": 384, "num_frames": 177},
{"label": "[16:9] 576x384, 7.4์ดˆ", "width": 576, "height": 384, "num_frames": 185},
{"label": "[16:9] 608x352, 7.7์ดˆ", "width": 608, "height": 352, "num_frames": 193},
{"label": "[16:9] 576x352, 8.0์ดˆ", "width": 576, "height": 352, "num_frames": 201},
{"label": "[16:9] 544x352, 8.4์ดˆ", "width": 544, "height": 352, "num_frames": 209},
{"label": "[3:2] 512x352, 9.3์ดˆ", "width": 512, "height": 352, "num_frames": 233},
{"label": "[16:9] 544x320, 9.6์ดˆ", "width": 544, "height": 320, "num_frames": 241},
{"label": "[16:9] 512x320, 10.3์ดˆ", "width": 512, "height": 320, "num_frames": 257},
]
def preset_changed(preset):
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),
]
def generate_video_from_text(
prompt="",
enhance_prompt_toggle=False,
negative_prompt="low quality, worst quality, deformed, distorted, warped, motion smear, motion artifacts, fused fingers, incorrect anatomy, strange hands, unattractive",
frame_rate=25,
seed=171198,
num_inference_steps=41,
guidance_scale=4,
height=320,
width=512,
num_frames=257,
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="low quality, worst quality, deformed, distorted, warped, motion smear, motion artifacts, fused fingers, incorrect anatomy, strange hands, unattractive",
frame_rate=25,
seed=171198,
num_inference_steps=41,
guidance_scale=4,
height=320,
width=512,
num_frames=257,
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="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=41,
visible=False
)
guidance_scale = gr.Slider(
label="4.3 Guidance Scale",
minimum=1.0,
maximum=5.0,
step=0.1,
value=4.0,
visible=False
)
height_slider = gr.Slider(
label="4.4 Height",
minimum=256,
maximum=1024,
step=64,
value=320,
visible=False,
)
width_slider = gr.Slider(
label="4.5 Width",
minimum=256,
maximum=1024,
step=64,
value=512,
visible=False,
)
num_frames_slider = gr.Slider(
label="4.5 Number of Frames",
minimum=1,
maximum=200,
step=1,
value=257,
visible=False,
)
return [
seed,
inference_steps,
guidance_scale,
height_slider,
width_slider,
num_frames_slider,
]
system_prompt_scenario = """๋‹น์‹ ์€ ์˜์ƒ ์Šคํฌ๋ฆฝํŠธ์— ๋งž๋Š” ๋ฐฐ๊ฒฝ ์˜์ƒ์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋กฌํ”„ํŠธ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค.
์ฃผ์–ด์ง„ ์Šคํฌ๋ฆฝํŠธ์˜ ๋ถ„์œ„๊ธฐ์™€ ๋งฅ๋ฝ์„ ์‹œ๊ฐ์  ๋ฐฐ๊ฒฝ์œผ๋กœ ํ‘œํ˜„ํ•˜๋˜, ๋‹ค์Œ ์›์น™์„ ๋ฐ˜๋“œ์‹œ ์ค€์ˆ˜ํ•˜์„ธ์š”:
1. ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ์ง์ ‘์ ์œผ๋กœ ๋ฌ˜์‚ฌํ•˜์ง€ ๋ง ๊ฒƒ
2. ์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์„ฑ๊ณผ ํ†ค์•ค๋งค๋„ˆ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฐ๊ฒฝ ์˜์ƒ์— ์ง‘์ค‘ํ•  ๊ฒƒ
3. 5๊ฐœ ์„น์…˜์ด ํ•˜๋‚˜์˜ ์ด์•ผ๊ธฐ์ฒ˜๋Ÿผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์—ฐ๊ฒฐ๋˜๋„๋ก ํ•  ๊ฒƒ
4. ์ถ”์ƒ์ ์ด๊ณ  ์€์œ ์ ์ธ ์‹œ๊ฐ ํ‘œํ˜„์„ ํ™œ์šฉํ•  ๊ฒƒ
๊ฐ ์„น์…˜๋ณ„ ํ”„๋กฌํ”„ํŠธ ์ž‘์„ฑ ๊ฐ€์ด๋“œ:
1. ๋ฐฐ๊ฒฝ ๋ฐ ํ•„์š”์„ฑ: ์ฃผ์ œ์˜ ์ „๋ฐ˜์ ์ธ ๋ถ„์œ„๊ธฐ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฐ๊ฒฝ ์”ฌ
2. ๋ฌธ์ œ ์ œ๊ธฐ: ๊ธด์žฅ๊ฐ์ด๋‚˜ ๊ฐˆ๋“ฑ์„ ์•”์‹œํ•˜๋Š” ๋ถ„์œ„๊ธฐ ์žˆ๋Š” ๋ฐฐ๊ฒฝ
3. ํ•ด๊ฒฐ์ฑ… ์ œ์‹œ: ํฌ๋ง์ ์ด๊ณ  ๋ฐ์€ ํ†ค์˜ ๋ฐฐ๊ฒฝ ์ „ํ™˜
4. ๋ณธ๋ก : ์•ˆ์ •๊ฐ ์žˆ๊ณ  ์‹ ๋ขฐ๋„๋ฅผ ๋†’์ด๋Š” ๋ฐฐ๊ฒฝ
5. ๊ฒฐ๋ก : ์ž„ํŒฉํŠธ ์žˆ๋Š” ๋งˆ๋ฌด๋ฆฌ๋ฅผ ์œ„ํ•œ ์—ญ๋™์ ์ธ ๋ฐฐ๊ฒฝ
๋ชจ๋“  ์„น์…˜์ด ์ผ๊ด€๋œ ์Šคํƒ€์ผ๊ณผ ํ†ค์„ ์œ ์ง€ํ•˜๋ฉด์„œ๋„ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ด์–ด์ง€๋„๋ก ๊ตฌ์„ฑํ•˜์„ธ์š”.
๊ฐ ์„น์…˜์˜ ํ”„๋กฌํ”„ํŠธ ์ž‘์„ฑ์‹œ ๋ฐ˜๋“œ์‹œ ๋‹ค์Œ ๊ตฌ์กฐ์— ๋งž๊ฒŒ ๊ฐœ์„ ํ•ด์ฃผ์„ธ์š”:
1. ์ฃผ์š” ๋™์ž‘์„ ๋ช…ํ™•ํ•œ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ์‹œ์ž‘
2. ๊ตฌ์ฒด์ ์ธ ๋™์ž‘๊ณผ ์ œ์Šค์ฒ˜๋ฅผ ์‹œ๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ ์„ค๋ช…
3. ์บ๋ฆญํ„ฐ/๊ฐ์ฒด์˜ ์™ธ๋ชจ๋ฅผ ์ƒ์„ธํžˆ ๋ฌ˜์‚ฌ
4. ๋ฐฐ๊ฒฝ๊ณผ ํ™˜๊ฒฝ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ๊ตฌ์ฒด์ ์œผ๋กœ ํฌํ•จ
5. ์นด๋ฉ”๋ผ ๊ฐ๋„์™€ ์›€์ง์ž„์„ ๋ช…์‹œ
6. ์กฐ๋ช…๊ณผ ์ƒ‰์ƒ์„ ์ž์„ธํžˆ ์„ค๋ช…
7. ๋ณ€ํ™”๋‚˜ ๊ฐ‘์ž‘์Šค๋Ÿฌ์šด ์‚ฌ๊ฑด์„ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํฌํ•จ
๋ชจ๋“  ์„ค๋ช…์€ ํ•˜๋‚˜์˜ ์ž์—ฐ์Šค๋Ÿฌ์šด ๋ฌธ๋‹จ์œผ๋กœ ์ž‘์„ฑํ•˜๊ณ ,
์ดฌ์˜ ๊ฐ๋…์ด ์ดฌ์˜ ๋ชฉ๋ก์„ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๊ตฌ์ฒด์ ์ด๊ณ  ์‹œ๊ฐ์ ์œผ๋กœ ์ž‘์„ฑํ•˜์„ธ์š”.
200๋‹จ์–ด๋ฅผ ๋„˜์ง€ ์•Š๋„๋ก ํ•˜๋˜, ์ตœ๋Œ€ํ•œ ์ƒ์„ธํ•˜๊ฒŒ ์ž‘์„ฑํ•˜์„ธ์š”.
"""
def analyze_scenario(scenario):
"""์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๋ฐฐ๊ฒฝ ์˜์ƒ์šฉ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ"""
messages = [
{"role": "system", "content": system_prompt_scenario},
{"role": "user", "content": f"""
๋‹ค์Œ ์Šคํฌ๋ฆฝํŠธ์˜ ๋ถ„์œ„๊ธฐ์™€ ๊ฐ์„ฑ์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฐ๊ฒฝ ์˜์ƒ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”:
์ฃผ์–ด์ง„ ์Šคํฌ๋ฆฝํŠธ์˜ ๋ถ„์œ„๊ธฐ์™€ ๋งฅ๋ฝ์„ ์‹œ๊ฐ์  ๋ฐฐ๊ฒฝ์œผ๋กœ ํ‘œํ˜„ํ•˜๋˜, ๋‹ค์Œ ์›์น™์„ ๋ฐ˜๋“œ์‹œ ์ค€์ˆ˜ํ•˜์„ธ์š”:
1. ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ์ง์ ‘์ ์œผ๋กœ ๋ฌ˜์‚ฌํ•˜์ง€ ๋ง ๊ฒƒ
2. ์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์„ฑ๊ณผ ํ†ค์•ค๋งค๋„ˆ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฐ๊ฒฝ ์˜์ƒ์— ์ง‘์ค‘ํ•  ๊ฒƒ
3. 5๊ฐœ ์„น์…˜์ด ํ•˜๋‚˜์˜ ์ด์•ผ๊ธฐ์ฒ˜๋Ÿผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์—ฐ๊ฒฐ๋˜๋„๋ก ํ•  ๊ฒƒ
4. ์ถ”์ƒ์ ์ด๊ณ  ์€์œ ์ ์ธ ์‹œ๊ฐ ํ‘œํ˜„์„ ํ™œ์šฉํ•  ๊ฒƒ
๊ฐ ์„น์…˜๋ณ„ ํ”„๋กฌํ”„ํŠธ ์ž‘์„ฑ ๊ฐ€์ด๋“œ:
1. ๋ฐฐ๊ฒฝ ๋ฐ ํ•„์š”์„ฑ: ์ฃผ์ œ์˜ ์ „๋ฐ˜์ ์ธ ๋ถ„์œ„๊ธฐ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฐ๊ฒฝ ์”ฌ
2. ํฅ๋ฏธ ์œ ๋ฐœ: ๊ธด์žฅ๊ฐ์ด๋‚˜ ๊ฐˆ๋“ฑ์„ ์•”์‹œํ•˜๋Š” ๋ถ„์œ„๊ธฐ ์žˆ๋Š” ๋ฐฐ๊ฒฝ
3. ํ•ด๊ฒฐ์ฑ… ์ œ์‹œ: ํฌ๋ง์ ์ด๊ณ  ๋ฐ์€ ํ†ค์˜ ๋ฐฐ๊ฒฝ ์ „ํ™˜
4. ๋ณธ๋ก : ์•ˆ์ •๊ฐ ์žˆ๊ณ  ์‹ ๋ขฐ๋„๋ฅผ ๋†’์ด๋Š” ๋ฐฐ๊ฒฝ
5. ๊ฒฐ๋ก : ์ž„ํŒฉํŠธ ์žˆ๋Š” ๋งˆ๋ฌด๋ฆฌ๋ฅผ ์œ„ํ•œ ์—ญ๋™์ ์ธ ๋ฐฐ๊ฒฝ
๋ชจ๋“  ์„น์…˜์ด ์ผ๊ด€๋œ ์Šคํƒ€์ผ๊ณผ ํ†ค์„ ์œ ์ง€ํ•˜๋ฉด์„œ๋„ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ด์–ด์ง€๋„๋ก ๊ตฌ์„ฑํ•˜์„ธ์š”.
๊ฐ ์„น์…˜์˜ ํ”„๋กฌํ”„ํŠธ ์ž‘์„ฑ์‹œ ๋ฐ˜๋“œ์‹œ ๋‹ค์Œ ๊ตฌ์กฐ์— ๋งž๊ฒŒ ๊ฐœ์„ ํ•ด์ฃผ์„ธ์š”:
1. ์ฃผ์š” ๋™์ž‘์„ ๋ช…ํ™•ํ•œ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ์‹œ์ž‘
2. ๊ตฌ์ฒด์ ์ธ ๋™์ž‘๊ณผ ์ œ์Šค์ฒ˜๋ฅผ ์‹œ๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ ์„ค๋ช…
3. ์บ๋ฆญํ„ฐ/๊ฐ์ฒด์˜ ์™ธ๋ชจ๋ฅผ ์ƒ์„ธํžˆ ๋ฌ˜์‚ฌ
4. ๋ฐฐ๊ฒฝ๊ณผ ํ™˜๊ฒฝ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ๊ตฌ์ฒด์ ์œผ๋กœ ํฌํ•จ
5. ์นด๋ฉ”๋ผ ๊ฐ๋„์™€ ์›€์ง์ž„์„ ๋ช…์‹œ
6. ์กฐ๋ช…๊ณผ ์ƒ‰์ƒ์„ ์ž์„ธํžˆ ์„ค๋ช…
7. ๋ณ€ํ™”๋‚˜ ๊ฐ‘์ž‘์Šค๋Ÿฌ์šด ์‚ฌ๊ฑด์„ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํฌํ•จ
๋ชจ๋“  ์„ค๋ช…์€ ํ•˜๋‚˜์˜ ์ž์—ฐ์Šค๋Ÿฌ์šด ๋ฌธ๋‹จ์œผ๋กœ ์ž‘์„ฑํ•˜๊ณ ,
์ดฌ์˜ ๊ฐ๋…์ด ์ดฌ์˜ ๋ชฉ๋ก์„ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๊ตฌ์ฒด์ ์ด๊ณ  ์‹œ๊ฐ์ ์œผ๋กœ ์ž‘์„ฑํ•˜์„ธ์š”.
200๋‹จ์–ด๋ฅผ ๋„˜์ง€ ์•Š๋„๋ก ํ•˜๋˜, ์ตœ๋Œ€ํ•œ ์ƒ์„ธํ•˜๊ฒŒ ์ž‘์„ฑํ•˜์„ธ์š”.
{scenario}
๊ฐ ์„น์…˜๋ณ„๋กœ ์ง์ ‘์ ์ธ ์ œํ’ˆ ๋ฌ˜์‚ฌ๋Š” ํ”ผํ•˜๊ณ , ์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์„ฑ์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฐ๊ฒฝ ์˜์ƒ์— ์ง‘์ค‘ํ•ด์ฃผ์„ธ์š”."""},
]
response = client.chat.completions.create(
model="gpt-4-1106-preview",
messages=messages,
max_tokens=2000,
)
content = response.choices[0].message.content.strip()
# ์„น์…˜๋ณ„๋กœ ๋ถ„๋ฆฌ
sections = []
current_section = ""
for line in content.split('\n'):
if line.strip().startswith(('1.', '2.', '3.', '4.', '5.')):
if current_section:
sections.append(current_section.strip())
current_section = line
else:
current_section += "\n" + line
if current_section:
sections.append(current_section.strip())
# ๋ถ€์กฑํ•œ ์„น์…˜ ์ฑ„์šฐ๊ธฐ
while len(sections) < 5:
sections.append("์ถ”๊ฐ€ ์„น์…˜์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.")
return sections[:5]
except Exception as e:
print(f"Error during scenario analysis: {e}")
return ["Error occurred during analysis"] * 5
def generate_section_video(prompt, preset, section_number=1, base_seed=171198, progress=gr.Progress()):
"""๊ฐ ์„น์…˜์˜ ๋น„๋””์˜ค ์ƒ์„ฑ - ์—๋Ÿฌ ์ฒ˜๋ฆฌ ์ถ”๊ฐ€"""
try:
if not prompt or len(prompt.strip()) < 50:
raise gr.Error("ํ”„๋กฌํ”„ํŠธ๋Š” ์ตœ์†Œ 50์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.")
selected = next(item for item in preset_options if item["label"] == preset)
section_seed = base_seed + section_number
return generate_video_from_text(
prompt=prompt,
height=selected["height"],
width=selected["width"],
num_frames=selected["num_frames"],
seed=section_seed,
progress=progress
)
except Exception as e:
print(f"Error in section {section_number}: {e}")
raise gr.Error(f"์„น์…˜ {section_number} ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜: {str(e)}")
# ๊ฐœ๋ณ„ ์„น์…˜ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ํ•จ์ˆ˜ ์ถ”๊ฐ€
def generate_single_section_prompt(scenario, section_number):
"""๊ฐœ๋ณ„ ์„น์…˜์— ๋Œ€ํ•œ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ"""
section_descriptions = {
1: "๋ฐฐ๊ฒฝ ๋ฐ ํ•„์š”์„ฑ: ์ฃผ์ œ์˜ ์ „๋ฐ˜์ ์ธ ๋ถ„์œ„๊ธฐ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฐ๊ฒฝ ์”ฌ",
2: "ํฅ๋ฏธ ์œ ๋ฐœ: ํฅ๋ฏธ๋ฅผ ์œ ๋ฐœํ•˜๊ณ  ๊ธฐ๋Œ€๊ฐ์„ ์ฆํญ์‹œํ‚ค๋Š” ๋ฐฐ๊ฒฝ",
3: "ํ•ด๊ฒฐ์ฑ… ์ œ์‹œ: ํฌ๋ง์ ์ด๊ณ  ๋ฐ์€ ํ†ค์˜ ๋ฐฐ๊ฒฝ ์ „ํ™˜",
4: "๋ณธ๋ก : ์•ˆ์ •๊ฐ ์žˆ๊ณ  ์‹ ๋ขฐ๋„๋ฅผ ๋†’์ด๋Š” ๋ฐฐ๊ฒฝ",
5: "๊ฒฐ๋ก : ์ž„ํŒฉํŠธ ์žˆ๋Š” ๋งˆ๋ฌด๋ฆฌ๋ฅผ ์œ„ํ•œ ์—ญ๋™์ ์ธ ๋ฐฐ๊ฒฝ"
}
messages = [
{"role": "system", "content": system_prompt_scenario},
{"role": "user", "content": f"""
๋‹ค์Œ ์Šคํฌ๋ฆฝํŠธ์˜ {section_number}๋ฒˆ์งธ ์„น์…˜({section_descriptions[section_number]})์— ๋Œ€ํ•œ
๋ฐฐ๊ฒฝ ์˜์ƒ ํ”„๋กฌํ”„ํŠธ๋งŒ์„ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”:
{scenario}
์ง์ ‘์ ์ธ ์ œํ’ˆ ๋ฌ˜์‚ฌ๋Š” ํ”ผํ•˜๊ณ , ์Šคํฌ๋ฆฝํŠธ์˜ ์ฃผ์ œ์™€ ๊ฐ์„ฑ์„ ํ‘œํ˜„ํ•˜๋Š” ํ•ต์‹ฌ ํ‚ค์›Œ๋“œ๋ฅผ ๋ฐ˜์˜ํ•œ ๋ฐฐ๊ฒฝ ์˜์ƒ์— ์ง‘์ค‘ํ•ด์ฃผ์„ธ์š”."""}
]
try:
response = client.chat.completions.create(
model="gpt-4-1106-preview",
messages=messages,
max_tokens=500,
)
return response.choices[0].message.content.strip()
except Exception as e:
print(f"Error during prompt generation: {e}")
return "Error occurred during prompt generation"
# ๋น„๋””์˜ค ๊ฒฐํ•ฉ ํ•จ์ˆ˜ ์ถ”๊ฐ€
def combine_videos(video_paths, output_path):
"""์—ฌ๋Ÿฌ ๋น„๋””์˜ค๋ฅผ ํ•˜๋‚˜๋กœ ๊ฒฐํ•ฉ"""
if not all(video_paths):
raise gr.Error("๋ชจ๋“  ์„น์…˜์˜ ์˜์ƒ์ด ์ƒ์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.")
try:
# ์ฒซ ๋ฒˆ์งธ ๋น„๋””์˜ค์˜ ์†์„ฑ ๊ฐ€์ ธ์˜ค๊ธฐ
cap = cv2.VideoCapture(video_paths[0])
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
cap.release()
# ์ถœ๋ ฅ ๋น„๋””์˜ค ์„ค์ •
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
# ๊ฐ ๋น„๋””์˜ค ์ˆœ์ฐจ์ ์œผ๋กœ ๊ฒฐํ•ฉ
for video_path in video_paths:
if video_path and os.path.exists(video_path):
cap = cv2.VideoCapture(video_path)
while True:
ret, frame = cap.read()
if not ret:
break
out.write(frame)
cap.release()
out.release()
return output_path
except Exception as e:
raise gr.Error(f"๋น„๋””์˜ค ๊ฒฐํ•ฉ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}")
def merge_section_videos(section1, section2, section3, section4, section5):
"""์„น์…˜ ๋น„๋””์˜ค๋“ค์„ ํ•˜๋‚˜๋กœ ๊ฒฐํ•ฉ"""
videos = []
# ๊ฐ ์„น์…˜ ๋น„๋””์˜ค ํ™•์ธ ๋ฐ ์ฒ˜๋ฆฌ
for i, video in enumerate([section1, section2, section3, section4, section5], 1):
if video and os.path.exists(video):
videos.append(video)
else:
raise gr.Error(f"์„น์…˜ {i}์˜ ์˜์ƒ์ด ์—†๊ฑฐ๋‚˜ ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
if not videos:
raise gr.Error("๊ฒฐํ•ฉํ•  ์˜์ƒ์ด ์—†์Šต๋‹ˆ๋‹ค.")
try:
output_path = tempfile.mktemp(suffix=".mp4")
# ์ฒซ ๋ฒˆ์งธ ๋น„๋””์˜ค์˜ ์†์„ฑ ๊ฐ€์ ธ์˜ค๊ธฐ
cap = cv2.VideoCapture(videos[0])
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
cap.release()
# ์ถœ๋ ฅ ๋น„๋””์˜ค ์„ค์ •
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
# ๊ฐ ๋น„๋””์˜ค ์ˆœ์ฐจ์ ์œผ๋กœ ๊ฒฐํ•ฉ
for video_path in videos:
cap = cv2.VideoCapture(video_path)
while True:
ret, frame = cap.read()
if not ret:
break
# ํ•„์š”ํ•œ ๊ฒฝ์šฐ ํ”„๋ ˆ์ž„ ํฌ๊ธฐ ์กฐ์ •
if frame.shape[:2] != (height, width):
frame = cv2.resize(frame, (width, height))
out.write(frame)
cap.release()
out.release()
return output_path
except Exception as e:
raise gr.Error(f"๋น„๋””์˜ค ๊ฒฐํ•ฉ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}")
# 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,
visible=False
)
txt2vid_preset = gr.Dropdown(
choices=[p["label"] for p in preset_options],
value="[16:9] 512x320, 10.3์ดˆ",
label="Step 2: ํ•ด์ƒ๋„ ํ”„๋ฆฌ์…‹ ์„ ํƒ",
)
txt2vid_frame_rate = gr.Slider(
label="Step 3: ํ”„๋ ˆ์ž„ ๋ ˆ์ดํŠธ",
minimum=21,
maximum=30,
step=1,
value=25,
visible=False
)
txt2vid_advanced = create_advanced_options()
txt2vid_generate = gr.Button(
"Step 3: ๋น„๋””์˜ค ์ƒ์„ฑ",
variant="primary",
size="lg",
)
with gr.Column():
txt2vid_output = gr.Video(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,
visible=False
)
img2vid_preset = gr.Dropdown(
choices=[p["label"] for p in preset_options],
value="[16:9] 512x320, 10.3์ดˆ",
label="Step 3: ํ•ด์ƒ๋„ ํ”„๋ฆฌ์…‹ ์„ ํƒ",
)
img2vid_frame_rate = gr.Slider(
label="Step 4: ํ”„๋ ˆ์ž„ ๋ ˆ์ดํŠธ",
minimum=21,
maximum=30,
step=1,
value=25,
visible=False
)
img2vid_advanced = create_advanced_options()
img2vid_generate = gr.Button(
"Step 4: ๋น„๋””์˜ค ์ƒ์„ฑ",
variant="primary",
size="lg",
)
with gr.Column():
img2vid_output = gr.Video(label="์ƒ์„ฑ๋œ ๋น„๋””์˜ค")
# Scenario to Video Tab (Modified)
with gr.TabItem("์‹œ๋‚˜๋ฆฌ์˜ค๋กœ ๋น„๋””์˜ค ๋งŒ๋“ค๊ธฐ(์ˆํผ)"):
with gr.Row():
with gr.Column(scale=1):
scenario_input = gr.Textbox(
label="์˜์ƒ ์Šคํฌ๋ฆฝํŠธ ์ž…๋ ฅ",
placeholder="์ „์ฒด ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”...",
lines=10
)
scenario_preset = gr.Dropdown(
choices=[p["label"] for p in preset_options],
value="[16:9] 512x320, 10.3์ดˆ",
label="ํ™”๋ฉด ํฌ๊ธฐ ์„ ํƒ"
)
analyze_btn = gr.Button("์‹œ๋‚˜๋ฆฌ์˜ค ๋ถ„์„ ๋ฐ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ", variant="primary")
with gr.Column(scale=2):
with gr.Row():
# ์„น์…˜ 1
with gr.Column():
section1_prompt = gr.Textbox(
label="1. ๋ฐฐ๊ฒฝ ๋ฐ ํ•„์š”์„ฑ",
lines=4
)
with gr.Row():
section1_regenerate = gr.Button("๐Ÿ”„ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ")
section1_generate = gr.Button("๐Ÿ”„ ์˜์ƒ ์ƒ์„ฑ")
section1_video = gr.Video(label="์„น์…˜ 1 ์˜์ƒ")
# ์„น์…˜ 2
with gr.Column():
section2_prompt = gr.Textbox(
label="2. ํฅ๋ฏธ ์œ ๋ฐœ",
lines=4
)
with gr.Row():
section2_regenerate = gr.Button("๐Ÿ”„ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ")
section2_generate = gr.Button("๐Ÿ”„ ์˜์ƒ ์ƒ์„ฑ")
section2_video = gr.Video(label="์„น์…˜ 2 ์˜์ƒ")
with gr.Row():
# ์„น์…˜ 3
with gr.Column():
section3_prompt = gr.Textbox(
label="3. ํ•ด๊ฒฐ์ฑ… ์ œ์‹œ",
lines=4
)
with gr.Row():
section3_regenerate = gr.Button("๐Ÿ”„ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ")
section3_generate = gr.Button("๐Ÿ”„ ์˜์ƒ ์ƒ์„ฑ")
section3_video = gr.Video(label="์„น์…˜ 3 ์˜์ƒ")
# ์„น์…˜ 4
with gr.Column():
section4_prompt = gr.Textbox(
label="4. ๋ณธ๋ก ",
lines=4
)
with gr.Row():
section4_regenerate = gr.Button("๐Ÿ”„ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ")
section4_generate = gr.Button("๐Ÿ”„ ์˜์ƒ ์ƒ์„ฑ")
section4_video = gr.Video(label="์„น์…˜ 4 ์˜์ƒ")
with gr.Row():
# ์„น์…˜ 5
with gr.Column():
section5_prompt = gr.Textbox(
label="5. ๊ฒฐ๋ก  ๋ฐ ๊ฐ•์กฐ",
lines=4
)
with gr.Row():
section5_regenerate = gr.Button("๐Ÿ”„ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ")
section5_generate = gr.Button("๐Ÿ”„ ์˜์ƒ ์ƒ์„ฑ")
section5_video = gr.Video(label="์„น์…˜ 5 ์˜์ƒ")
# ํ†ตํ•ฉ ์˜์ƒ ์„น์…˜ ์ถ”๊ฐ€
with gr.Row():
with gr.Column(scale=1):
# ๊ธฐ์กด์˜ scenario_input๊ณผ analyze_btn ์œ ์ง€
merge_videos_btn = gr.Button("ํ†ตํ•ฉ ์˜์ƒ ์ƒ์„ฑ", variant="primary", size="lg")
with gr.Column(scale=2):
# ๊ธฐ์กด์˜ ์„น์…˜ 1-5 ์œ ์ง€
# ํ†ตํ•ฉ ์˜์ƒ ์ถœ๋ ฅ ์„น์…˜ ์ถ”๊ฐ€
with gr.Row():
merged_video_output = gr.Video(label="ํ†ตํ•ฉ ์˜์ƒ")
# Event handlers
txt2vid_preset.change(
fn=preset_changed,
inputs=[txt2vid_preset],
outputs=[
txt2vid_current_height,
txt2vid_current_width,
txt2vid_current_num_frames,
*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[:3],
txt2vid_current_height,
txt2vid_current_width,
txt2vid_current_num_frames,
],
outputs=txt2vid_output,
concurrency_limit=1,
concurrency_id="generate_video",
queue=True,
)
img2vid_preset.change(
fn=preset_changed,
inputs=[img2vid_preset],
outputs=[
img2vid_current_height,
img2vid_current_width,
img2vid_current_num_frames,
*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[:3],
img2vid_current_height,
img2vid_current_width,
img2vid_current_num_frames,
],
outputs=img2vid_output,
concurrency_limit=1,
concurrency_id="generate_video",
queue=True,
)
# Scenario tab event handlers
analyze_btn.click(
fn=analyze_scenario,
inputs=[scenario_input],
outputs=[
section1_prompt, section2_prompt, section3_prompt,
section4_prompt, section5_prompt
]
)
# ๊ฐ ์„น์…˜์˜ ํ”„๋กฌํ”„ํŠธ ์žฌ์ƒ์„ฑ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์ถ”๊ฐ€
section1_regenerate.click(
fn=lambda x: generate_single_section_prompt(x, 1),
inputs=[scenario_input],
outputs=section1_prompt
)
section2_regenerate.click(
fn=lambda x: generate_single_section_prompt(x, 2),
inputs=[scenario_input],
outputs=section2_prompt
)
section3_regenerate.click(
fn=lambda x: generate_single_section_prompt(x, 3),
inputs=[scenario_input],
outputs=section3_prompt
)
section4_regenerate.click(
fn=lambda x: generate_single_section_prompt(x, 4),
inputs=[scenario_input],
outputs=section4_prompt
)
section5_regenerate.click(
fn=lambda x: generate_single_section_prompt(x, 5),
inputs=[scenario_input],
outputs=section5_prompt
)
# ์„น์…˜ ์ƒ์„ฑ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ
section1_generate.click(
fn=generate_section_video,
inputs=[section1_prompt, scenario_preset],
outputs=section1_video,
api_name=f"generate_section1"
)
section2_generate.click(
fn=lambda p, pr: generate_section_video(p, pr, 2),
inputs=[section2_prompt, scenario_preset],
outputs=section2_video,
api_name=f"generate_section2"
)
section3_generate.click(
fn=lambda p, pr: generate_section_video(p, pr, 3),
inputs=[section3_prompt, scenario_preset],
outputs=section3_video,
api_name=f"generate_section3"
)
section4_generate.click(
fn=lambda p, pr: generate_section_video(p, pr, 4),
inputs=[section4_prompt, scenario_preset],
outputs=section4_video,
api_name=f"generate_section4"
)
section5_generate.click(
fn=lambda p, pr: generate_section_video(p, pr, 5),
inputs=[section5_prompt, scenario_preset],
outputs=section5_video,
api_name=f"generate_section5"
)
# ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์ถ”๊ฐ€
merge_videos_btn.click(
fn=merge_section_videos,
inputs=[
section1_video,
section2_video,
section3_video,
section4_video,
section5_video
],
outputs=merged_video_output
)
if __name__ == "__main__":
iface.queue(max_size=64, default_concurrency_limit=1, api_open=False).launch(
share=True, show_api=False
)