Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,1229 +1,2 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from gradio_toggle import Toggle
|
| 3 |
-
import torch
|
| 4 |
-
from huggingface_hub import snapshot_download
|
| 5 |
-
from transformers import pipeline
|
| 6 |
-
|
| 7 |
-
from xora.models.autoencoders.causal_video_autoencoder import CausalVideoAutoencoder
|
| 8 |
-
from xora.models.transformers.transformer3d import Transformer3DModel
|
| 9 |
-
from xora.models.transformers.symmetric_patchifier import SymmetricPatchifier
|
| 10 |
-
from xora.schedulers.rf import RectifiedFlowScheduler
|
| 11 |
-
from xora.pipelines.pipeline_xora_video import XoraVideoPipeline
|
| 12 |
-
from transformers import T5EncoderModel, T5Tokenizer
|
| 13 |
-
from xora.utils.conditioning_method import ConditioningMethod
|
| 14 |
-
from pathlib import Path
|
| 15 |
-
import safetensors.torch
|
| 16 |
-
import json
|
| 17 |
-
import numpy as np
|
| 18 |
-
import cv2
|
| 19 |
-
from PIL import Image
|
| 20 |
-
import tempfile
|
| 21 |
import os
|
| 22 |
-
|
| 23 |
-
from openai import OpenAI
|
| 24 |
-
import re
|
| 25 |
-
import time
|
| 26 |
-
# Load system prompts
|
| 27 |
-
system_prompt_t2v = """λΉμ μ λΉλμ€ μμ±μ μν ν둬ννΈ μ λ¬Έκ°μ
λλ€.
|
| 28 |
-
μ£Όμ΄μ§ ν둬ννΈλ₯Ό λ€μ ꡬ쑰μ λ§κ² κ°μ ν΄μ£ΌμΈμ:
|
| 29 |
-
1. μ£Όμ λμμ λͺ
νν ν λ¬Έμ₯μΌλ‘ μμ
|
| 30 |
-
2. ꡬ체μ μΈ λμκ³Ό μ μ€μ²λ₯Ό μκ° μμλλ‘ μ€λͺ
|
| 31 |
-
3. μΊλ¦ν°/κ°μ²΄μ μΈλͺ¨λ₯Ό μμΈν λ¬μ¬
|
| 32 |
-
4. λ°°κ²½κ³Ό νκ²½ μΈλΆ μ¬νμ ꡬ체μ μΌλ‘ ν¬ν¨
|
| 33 |
-
5. μΉ΄λ©λΌ κ°λμ μμ§μμ λͺ
μ
|
| 34 |
-
6. μ‘°λͺ
κ³Ό μμμ μμΈν μ€λͺ
|
| 35 |
-
7. λ³νλ κ°μμ€λ¬μ΄ μ¬κ±΄μ μμ°μ€λ½κ² ν¬ν¨
|
| 36 |
-
λͺ¨λ μ€λͺ
μ νλμ μμ°μ€λ¬μ΄ λ¬Έλ¨μΌλ‘ μμ±νκ³ ,
|
| 37 |
-
촬μ κ°λ
μ΄ μ΄¬μ λͺ©λ‘μ μ€λͺ
νλ κ²μ²λΌ ꡬ체μ μ΄κ³ μκ°μ μΌλ‘ μμ±νμΈμ.
|
| 38 |
-
200λ¨μ΄λ₯Ό λμ§ μλλ‘ νλ, μ΅λν μμΈνκ² μμ±νμΈμ."""
|
| 39 |
-
|
| 40 |
-
system_prompt_i2v = """λΉμ μ μ΄λ―Έμ§ κΈ°λ° λΉλμ€ μμ±μ μν ν둬ννΈ μ λ¬Έκ°μ
λλ€.
|
| 41 |
-
μ£Όμ΄μ§ ν둬ννΈλ₯Ό λ€μ ꡬ쑰μ λ§κ² κ°μ ν΄μ£ΌμΈμ:
|
| 42 |
-
1. μ£Όμ λμμ λͺ
νν ν λ¬Έμ₯μΌλ‘ μμ
|
| 43 |
-
2. ꡬ체μ μΈ λμκ³Ό μ μ€μ²λ₯Ό μκ° μμλλ‘ μ€λͺ
|
| 44 |
-
3. μΊλ¦ν°/κ°μ²΄μ μΈλͺ¨λ₯Ό μμΈν λ¬μ¬
|
| 45 |
-
4. λ°°κ²½κ³Ό νκ²½ μΈλΆ μ¬νμ ꡬ체μ μΌλ‘ ν¬ν¨
|
| 46 |
-
5. μΉ΄λ©λΌ κ°λμ μμ§μμ λͺ
μ
|
| 47 |
-
6. μ‘°λͺ
κ³Ό μμμ μμΈν μ€λͺ
|
| 48 |
-
7. λ³νλ κ°μμ€λ¬μ΄ μ¬κ±΄μ μμ°μ€λ½κ² ν¬ν¨
|
| 49 |
-
λͺ¨λ μ€λͺ
μ νλμ μμ°μ€λ¬μ΄ λ¬Έλ¨μΌλ‘ μμ±νκ³ ,
|
| 50 |
-
촬μ κ°λ
μ΄ μ΄¬μ λͺ©λ‘μ μ€λͺ
νλ κ²μ²λΌ ꡬ체μ μ΄κ³ μκ°μ μΌλ‘ μμ±νμΈμ.
|
| 51 |
-
200λ¨μ΄λ₯Ό λμ§ μλλ‘ νλ, μ΅λν μμΈνκ² μμ±νμΈμ."""
|
| 52 |
-
|
| 53 |
-
# Load Hugging Face token if needed
|
| 54 |
-
hf_token = os.getenv("HF_TOKEN")
|
| 55 |
-
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 56 |
-
client = OpenAI(api_key=openai_api_key)
|
| 57 |
-
|
| 58 |
-
# Initialize translation pipeline with device and clean_up settings
|
| 59 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 60 |
-
translator = pipeline(
|
| 61 |
-
"translation",
|
| 62 |
-
model="Helsinki-NLP/opus-mt-ko-en",
|
| 63 |
-
device=device,
|
| 64 |
-
clean_up_tokenization_spaces=True
|
| 65 |
-
)
|
| 66 |
-
|
| 67 |
-
# Korean text detection function
|
| 68 |
-
def contains_korean(text):
|
| 69 |
-
korean_pattern = re.compile('[γ±-γ
γ
-γ
£κ°-ν£]')
|
| 70 |
-
return bool(korean_pattern.search(text))
|
| 71 |
-
|
| 72 |
-
def translate_korean_prompt(prompt, max_length=450):
|
| 73 |
-
"""
|
| 74 |
-
Translate Korean prompt to English if Korean text is detected
|
| 75 |
-
Split long text into chunks if necessary
|
| 76 |
-
"""
|
| 77 |
-
if not contains_korean(prompt):
|
| 78 |
-
return prompt
|
| 79 |
-
|
| 80 |
-
# Split long text into chunks
|
| 81 |
-
def split_text(text, max_length):
|
| 82 |
-
words = text.split()
|
| 83 |
-
chunks = []
|
| 84 |
-
current_chunk = []
|
| 85 |
-
current_length = 0
|
| 86 |
-
|
| 87 |
-
for word in words:
|
| 88 |
-
if current_length + len(word) + 1 > max_length:
|
| 89 |
-
chunks.append(' '.join(current_chunk))
|
| 90 |
-
current_chunk = [word]
|
| 91 |
-
current_length = len(word)
|
| 92 |
-
else:
|
| 93 |
-
current_chunk.append(word)
|
| 94 |
-
current_length += len(word) + 1
|
| 95 |
-
|
| 96 |
-
if current_chunk:
|
| 97 |
-
chunks.append(' '.join(current_chunk))
|
| 98 |
-
return chunks
|
| 99 |
-
|
| 100 |
-
try:
|
| 101 |
-
if len(prompt) > max_length:
|
| 102 |
-
chunks = split_text(prompt, max_length)
|
| 103 |
-
translated_chunks = []
|
| 104 |
-
|
| 105 |
-
for chunk in chunks:
|
| 106 |
-
translated = translator(chunk, max_length=512)[0]['translation_text']
|
| 107 |
-
translated_chunks.append(translated)
|
| 108 |
-
|
| 109 |
-
final_translation = ' '.join(translated_chunks)
|
| 110 |
-
else:
|
| 111 |
-
final_translation = translator(prompt, max_length=512)[0]['translation_text']
|
| 112 |
-
|
| 113 |
-
print(f"Original Korean prompt: {prompt}")
|
| 114 |
-
print(f"Translated English prompt: {final_translation}")
|
| 115 |
-
return final_translation
|
| 116 |
-
|
| 117 |
-
except Exception as e:
|
| 118 |
-
print(f"Translation error: {e}")
|
| 119 |
-
return prompt # Return original prompt if translation fails
|
| 120 |
-
|
| 121 |
-
def enhance_prompt(prompt, type="t2v"):
|
| 122 |
-
system_prompt = system_prompt_t2v if type == "t2v" else system_prompt_i2v
|
| 123 |
-
messages = [
|
| 124 |
-
{"role": "system", "content": system_prompt},
|
| 125 |
-
{"role": "user", "content": prompt},
|
| 126 |
-
]
|
| 127 |
-
|
| 128 |
-
try:
|
| 129 |
-
response = client.chat.completions.create(
|
| 130 |
-
model="gpt-4-1106-preview",
|
| 131 |
-
messages=messages,
|
| 132 |
-
max_tokens=2000,
|
| 133 |
-
)
|
| 134 |
-
enhanced_prompt = response.choices[0].message.content.strip()
|
| 135 |
-
|
| 136 |
-
print("\n=== ν둬ννΈ μ¦κ° κ²°κ³Ό ===")
|
| 137 |
-
print("Original Prompt:")
|
| 138 |
-
print(prompt)
|
| 139 |
-
print("\nEnhanced Prompt:")
|
| 140 |
-
print(enhanced_prompt)
|
| 141 |
-
print("========================\n")
|
| 142 |
-
|
| 143 |
-
return enhanced_prompt
|
| 144 |
-
except Exception as e:
|
| 145 |
-
print(f"Error during prompt enhancement: {e}")
|
| 146 |
-
return prompt
|
| 147 |
-
|
| 148 |
-
def update_prompt_t2v(prompt, enhance_toggle):
|
| 149 |
-
return update_prompt(prompt, enhance_toggle, "t2v")
|
| 150 |
-
|
| 151 |
-
def update_prompt_i2v(prompt, enhance_toggle):
|
| 152 |
-
return update_prompt(prompt, enhance_toggle, "i2v")
|
| 153 |
-
|
| 154 |
-
def update_prompt(prompt, enhance_toggle, type="t2v"):
|
| 155 |
-
if enhance_toggle:
|
| 156 |
-
return enhance_prompt(prompt, type)
|
| 157 |
-
return prompt
|
| 158 |
-
|
| 159 |
-
# Set model download directory within Hugging Face Spaces
|
| 160 |
-
model_path = "asset"
|
| 161 |
-
if not os.path.exists(model_path):
|
| 162 |
-
snapshot_download(
|
| 163 |
-
"Lightricks/LTX-Video", local_dir=model_path, repo_type="model", token=hf_token
|
| 164 |
-
)
|
| 165 |
-
|
| 166 |
-
# Global variables to load components
|
| 167 |
-
vae_dir = Path(model_path) / "vae"
|
| 168 |
-
unet_dir = Path(model_path) / "unet"
|
| 169 |
-
scheduler_dir = Path(model_path) / "scheduler"
|
| 170 |
-
|
| 171 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 172 |
-
|
| 173 |
-
def load_vae(vae_dir):
|
| 174 |
-
vae_ckpt_path = vae_dir / "vae_diffusion_pytorch_model.safetensors"
|
| 175 |
-
vae_config_path = vae_dir / "config.json"
|
| 176 |
-
with open(vae_config_path, "r") as f:
|
| 177 |
-
vae_config = json.load(f)
|
| 178 |
-
vae = CausalVideoAutoencoder.from_config(vae_config)
|
| 179 |
-
vae_state_dict = safetensors.torch.load_file(vae_ckpt_path)
|
| 180 |
-
vae.load_state_dict(vae_state_dict)
|
| 181 |
-
return vae.to(device=device, dtype=torch.bfloat16)
|
| 182 |
-
|
| 183 |
-
def load_unet(unet_dir):
|
| 184 |
-
unet_ckpt_path = unet_dir / "unet_diffusion_pytorch_model.safetensors"
|
| 185 |
-
unet_config_path = unet_dir / "config.json"
|
| 186 |
-
transformer_config = Transformer3DModel.load_config(unet_config_path)
|
| 187 |
-
transformer = Transformer3DModel.from_config(transformer_config)
|
| 188 |
-
unet_state_dict = safetensors.torch.load_file(unet_ckpt_path)
|
| 189 |
-
transformer.load_state_dict(unet_state_dict, strict=True)
|
| 190 |
-
return transformer.to(device=device, dtype=torch.bfloat16)
|
| 191 |
-
|
| 192 |
-
def load_scheduler(scheduler_dir):
|
| 193 |
-
scheduler_config_path = scheduler_dir / "scheduler_config.json"
|
| 194 |
-
scheduler_config = RectifiedFlowScheduler.load_config(scheduler_config_path)
|
| 195 |
-
return RectifiedFlowScheduler.from_config(scheduler_config)
|
| 196 |
-
|
| 197 |
-
def center_crop_and_resize(frame, target_height, target_width):
|
| 198 |
-
# State κ°μ²΄μΈ κ²½μ° value κ°μ κ°μ Έμ΄
|
| 199 |
-
if isinstance(target_height, gr.State):
|
| 200 |
-
target_height = target_height.value
|
| 201 |
-
if isinstance(target_width, gr.State):
|
| 202 |
-
target_width = target_width.value
|
| 203 |
-
|
| 204 |
-
h, w, _ = frame.shape
|
| 205 |
-
aspect_ratio_target = target_width / target_height
|
| 206 |
-
aspect_ratio_frame = w / h
|
| 207 |
-
|
| 208 |
-
if aspect_ratio_frame > aspect_ratio_target:
|
| 209 |
-
new_width = int(h * aspect_ratio_target)
|
| 210 |
-
x_start = (w - new_width) // 2
|
| 211 |
-
frame_cropped = frame[:, x_start : x_start + new_width]
|
| 212 |
-
else:
|
| 213 |
-
new_height = int(w / aspect_ratio_target)
|
| 214 |
-
y_start = (h - new_height) // 2
|
| 215 |
-
frame_cropped = frame[y_start : y_start + new_height, :]
|
| 216 |
-
|
| 217 |
-
frame_resized = cv2.resize(frame_cropped, (target_width, target_height))
|
| 218 |
-
return frame_resized
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
def load_image_to_tensor_with_resize(image_path, target_height=512, target_width=768):
|
| 223 |
-
image = Image.open(image_path).convert("RGB")
|
| 224 |
-
image_np = np.array(image)
|
| 225 |
-
frame_resized = center_crop_and_resize(image_np, target_height, target_width)
|
| 226 |
-
frame_tensor = torch.tensor(frame_resized).permute(2, 0, 1).float()
|
| 227 |
-
frame_tensor = (frame_tensor / 127.5) - 1.0
|
| 228 |
-
return frame_tensor.unsqueeze(0).unsqueeze(2)
|
| 229 |
-
|
| 230 |
-
# Load models
|
| 231 |
-
vae = load_vae(vae_dir)
|
| 232 |
-
unet = load_unet(unet_dir)
|
| 233 |
-
scheduler = load_scheduler(scheduler_dir)
|
| 234 |
-
patchifier = SymmetricPatchifier(patch_size=1)
|
| 235 |
-
text_encoder = T5EncoderModel.from_pretrained(
|
| 236 |
-
"PixArt-alpha/PixArt-XL-2-1024-MS", subfolder="text_encoder"
|
| 237 |
-
).to(device)
|
| 238 |
-
tokenizer = T5Tokenizer.from_pretrained(
|
| 239 |
-
"PixArt-alpha/PixArt-XL-2-1024-MS", subfolder="tokenizer"
|
| 240 |
-
)
|
| 241 |
-
|
| 242 |
-
pipeline = XoraVideoPipeline(
|
| 243 |
-
transformer=unet,
|
| 244 |
-
patchifier=patchifier,
|
| 245 |
-
text_encoder=text_encoder,
|
| 246 |
-
tokenizer=tokenizer,
|
| 247 |
-
scheduler=scheduler,
|
| 248 |
-
vae=vae,
|
| 249 |
-
).to(device)
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
# Preset options for resolution and frame configuration
|
| 254 |
-
# Convert frames to seconds assuming 25 FPS
|
| 255 |
-
preset_options = [
|
| 256 |
-
{"label": "[16:9 HD] 1216x704, 1.6μ΄", "width": 1216, "height": 704, "num_frames": 41},
|
| 257 |
-
{"label": "[16:9] 1088x704, 2.0μ΄", "width": 1088, "height": 704, "num_frames": 49},
|
| 258 |
-
{"label": "[16:9] 1056x640, 2.3μ΄", "width": 1056, "height": 640, "num_frames": 57},
|
| 259 |
-
{"label": "[16:9] 896x608, 2.9μ΄", "width": 896, "height": 608, "num_frames": 73},
|
| 260 |
-
{"label": "[16:9] 800x512, 3.9μ΄", "width": 800, "height": 512, "num_frames": 97},
|
| 261 |
-
{"label": "[16:9] 736x480, 4.5μ΄", "width": 736, "height": 480, "num_frames": 113},
|
| 262 |
-
{"label": "[16:9] 704x448, 5.2μ΄", "width": 704, "height": 448, "num_frames": 129},
|
| 263 |
-
{"label": "[16:9] 608x352, 7.7μ΄", "width": 608, "height": 352, "num_frames": 193},
|
| 264 |
-
{"label": "[16:9] 576x352, 8.0μ΄", "width": 576, "height": 352, "num_frames": 201},
|
| 265 |
-
{"label": "[16:9] 544x320, 9.6μ΄", "width": 544, "height": 320, "num_frames": 241},
|
| 266 |
-
{"label": "[16:9] 512x320, 10.3μ΄", "width": 512, "height": 320, "num_frames": 257},
|
| 267 |
-
{"label": "[3:2] 704x480, 4.8μ΄", "width": 704, "height": 480, "num_frames": 121},
|
| 268 |
-
{"label": "[3:2] 512x352, 9.3μ΄", "width": 512, "height": 352, "num_frames": 233},
|
| 269 |
-
{"label": "[1:1] 704x704, 2.3μ΄", "width": 704, "height": 704, "num_frames": 57},
|
| 270 |
-
{"label": "[9:16] 608x1088, 2.0μ΄", "width": 608, "height": 1088, "num_frames": 49},
|
| 271 |
-
{"label": "[9:16] 448x800, 4.2μ΄", "width": 448, "height": 800, "num_frames": 105},
|
| 272 |
-
]
|
| 273 |
-
|
| 274 |
-
def preset_changed(preset):
|
| 275 |
-
selected = next((item for item in preset_options if item["label"] == preset), None)
|
| 276 |
-
if selected is None:
|
| 277 |
-
raise gr.Error("Invalid preset selected")
|
| 278 |
-
return [
|
| 279 |
-
gr.State(value=selected["height"]),
|
| 280 |
-
gr.State(value=selected["width"]),
|
| 281 |
-
gr.State(value=selected["num_frames"]),
|
| 282 |
-
gr.update(visible=False),
|
| 283 |
-
gr.update(visible=False),
|
| 284 |
-
gr.update(visible=False),
|
| 285 |
-
]
|
| 286 |
-
|
| 287 |
-
def generate_video_from_text(
|
| 288 |
-
prompt,
|
| 289 |
-
enhance_prompt_toggle,
|
| 290 |
-
negative_prompt,
|
| 291 |
-
frame_rate,
|
| 292 |
-
seed,
|
| 293 |
-
num_inference_steps,
|
| 294 |
-
guidance_scale,
|
| 295 |
-
height,
|
| 296 |
-
width,
|
| 297 |
-
num_frames,
|
| 298 |
-
progress=gr.Progress(),
|
| 299 |
-
):
|
| 300 |
-
# State κ°μ²΄μ value κ°μ κ°μ Έμ΄
|
| 301 |
-
height = height.value if isinstance(height, gr.State) else height
|
| 302 |
-
width = width.value if isinstance(width, gr.State) else width
|
| 303 |
-
num_frames = num_frames.value if isinstance(num_frames, gr.State) else num_frames
|
| 304 |
-
|
| 305 |
-
if len(prompt.strip()) < 50:
|
| 306 |
-
raise gr.Error(
|
| 307 |
-
"ν둬ννΈλ μ΅μ 50μ μ΄μμ΄μ΄μΌ ν©λλ€. λ μμΈν μ€λͺ
μ μ 곡ν΄μ£ΌμΈμ.",
|
| 308 |
-
duration=5,
|
| 309 |
-
)
|
| 310 |
-
|
| 311 |
-
# ν둬ννΈ κ°μ μ΄ νμ±νλ κ²½μ°
|
| 312 |
-
if enhance_prompt_toggle:
|
| 313 |
-
prompt = enhance_prompt(prompt, "t2v")
|
| 314 |
-
|
| 315 |
-
# Translate Korean prompts to English
|
| 316 |
-
prompt = translate_korean_prompt(prompt)
|
| 317 |
-
negative_prompt = translate_korean_prompt(negative_prompt)
|
| 318 |
-
|
| 319 |
-
# κΈ°λ³Έκ° μ€μ
|
| 320 |
-
height = height or 320
|
| 321 |
-
width = width or 512
|
| 322 |
-
num_frames = num_frames or 257
|
| 323 |
-
frame_rate = frame_rate or 25
|
| 324 |
-
seed = seed or 171198
|
| 325 |
-
num_inference_steps = num_inference_steps or 41
|
| 326 |
-
guidance_scale = guidance_scale or 4.0
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
sample = {
|
| 331 |
-
"prompt": prompt,
|
| 332 |
-
"prompt_attention_mask": None,
|
| 333 |
-
"negative_prompt": negative_prompt,
|
| 334 |
-
"negative_prompt_attention_mask": None,
|
| 335 |
-
"media_items": None,
|
| 336 |
-
}
|
| 337 |
-
|
| 338 |
-
generator = torch.Generator(device="cpu").manual_seed(seed)
|
| 339 |
-
|
| 340 |
-
def gradio_progress_callback(self, step, timestep, kwargs):
|
| 341 |
-
progress((step + 1) / num_inference_steps)
|
| 342 |
-
|
| 343 |
-
try:
|
| 344 |
-
with torch.no_grad():
|
| 345 |
-
images = pipeline(
|
| 346 |
-
num_inference_steps=num_inference_steps,
|
| 347 |
-
num_images_per_prompt=1,
|
| 348 |
-
guidance_scale=guidance_scale,
|
| 349 |
-
generator=generator,
|
| 350 |
-
output_type="pt",
|
| 351 |
-
height=height,
|
| 352 |
-
width=width,
|
| 353 |
-
num_frames=num_frames,
|
| 354 |
-
frame_rate=frame_rate,
|
| 355 |
-
**sample,
|
| 356 |
-
is_video=True,
|
| 357 |
-
vae_per_channel_normalize=True,
|
| 358 |
-
conditioning_method=ConditioningMethod.UNCONDITIONAL,
|
| 359 |
-
mixed_precision=True,
|
| 360 |
-
callback_on_step_end=gradio_progress_callback,
|
| 361 |
-
).images
|
| 362 |
-
except Exception as e:
|
| 363 |
-
raise gr.Error(
|
| 364 |
-
f"λΉλμ€ μμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€. λ€μ μλν΄μ£ΌμΈμ. μ€λ₯: {e}",
|
| 365 |
-
duration=5,
|
| 366 |
-
)
|
| 367 |
-
finally:
|
| 368 |
-
torch.cuda.empty_cache()
|
| 369 |
-
gc.collect()
|
| 370 |
-
|
| 371 |
-
output_path = tempfile.mktemp(suffix=".mp4")
|
| 372 |
-
video_np = images.squeeze(0).permute(1, 2, 3, 0).cpu().float().numpy()
|
| 373 |
-
video_np = (video_np * 255).astype(np.uint8)
|
| 374 |
-
height, width = video_np.shape[1:3]
|
| 375 |
-
out = cv2.VideoWriter(
|
| 376 |
-
output_path, cv2.VideoWriter_fourcc(*"mp4v"), frame_rate, (width, height)
|
| 377 |
-
)
|
| 378 |
-
for frame in video_np[..., ::-1]:
|
| 379 |
-
out.write(frame)
|
| 380 |
-
out.release()
|
| 381 |
-
del images
|
| 382 |
-
del video_np
|
| 383 |
-
torch.cuda.empty_cache()
|
| 384 |
-
return output_path
|
| 385 |
-
|
| 386 |
-
def generate_video_from_image(
|
| 387 |
-
image_path,
|
| 388 |
-
prompt,
|
| 389 |
-
enhance_prompt_toggle,
|
| 390 |
-
negative_prompt,
|
| 391 |
-
frame_rate,
|
| 392 |
-
seed,
|
| 393 |
-
num_inference_steps,
|
| 394 |
-
guidance_scale,
|
| 395 |
-
height,
|
| 396 |
-
width,
|
| 397 |
-
num_frames,
|
| 398 |
-
progress=gr.Progress(),
|
| 399 |
-
):
|
| 400 |
-
# State κ°μ²΄μ value κ°μ κ°μ Έμ΄
|
| 401 |
-
height = height.value if isinstance(height, gr.State) else height
|
| 402 |
-
width = width.value if isinstance(width, gr.State) else width
|
| 403 |
-
num_frames = num_frames.value if isinstance(num_frames, gr.State) else num_frames
|
| 404 |
-
|
| 405 |
-
if not image_path:
|
| 406 |
-
raise gr.Error("μ
λ ₯ μ΄λ―Έμ§λ₯Ό μ 곡ν΄μ£ΌμΈμ.", duration=5)
|
| 407 |
-
|
| 408 |
-
if len(prompt.strip()) < 50:
|
| 409 |
-
raise gr.Error(
|
| 410 |
-
"ν둬ννΈλ μ΅μ 50μ μ΄μμ΄μ΄μΌ ν©λλ€. λ μμΈν μ€λͺ
μ μ 곡ν΄μ£ΌμΈμ.",
|
| 411 |
-
duration=5,
|
| 412 |
-
)
|
| 413 |
-
|
| 414 |
-
# ν둬ννΈ κ°μ μ΄ νμ±νλ κ²½μ°
|
| 415 |
-
if enhance_prompt_toggle:
|
| 416 |
-
prompt = enhance_prompt(prompt, "i2v")
|
| 417 |
-
|
| 418 |
-
# Translate Korean prompts to English
|
| 419 |
-
prompt = translate_korean_prompt(prompt)
|
| 420 |
-
negative_prompt = translate_korean_prompt(negative_prompt)
|
| 421 |
-
|
| 422 |
-
# κΈ°λ³Έκ° μ€μ
|
| 423 |
-
height = height or 320
|
| 424 |
-
width = width or 512
|
| 425 |
-
num_frames = num_frames or 257
|
| 426 |
-
frame_rate = frame_rate or 25
|
| 427 |
-
seed = seed or 171198
|
| 428 |
-
num_inference_steps = num_inference_steps or 41
|
| 429 |
-
guidance_scale = guidance_scale or 4.0
|
| 430 |
-
|
| 431 |
-
# μ΄λ―Έμ§ λ‘λ λ° μ μ²λ¦¬
|
| 432 |
-
media_items = (
|
| 433 |
-
load_image_to_tensor_with_resize(image_path, height, width).to(device).detach()
|
| 434 |
-
)
|
| 435 |
-
|
| 436 |
-
sample = {
|
| 437 |
-
"prompt": prompt,
|
| 438 |
-
"prompt_attention_mask": None,
|
| 439 |
-
"negative_prompt": negative_prompt,
|
| 440 |
-
"negative_prompt_attention_mask": None,
|
| 441 |
-
"media_items": media_items,
|
| 442 |
-
}
|
| 443 |
-
|
| 444 |
-
generator = torch.Generator(device="cpu").manual_seed(seed)
|
| 445 |
-
|
| 446 |
-
def gradio_progress_callback(self, step, timestep, kwargs):
|
| 447 |
-
progress((step + 1) / num_inference_steps)
|
| 448 |
-
|
| 449 |
-
try:
|
| 450 |
-
with torch.no_grad():
|
| 451 |
-
images = pipeline(
|
| 452 |
-
num_inference_steps=num_inference_steps,
|
| 453 |
-
num_images_per_prompt=1,
|
| 454 |
-
guidance_scale=guidance_scale,
|
| 455 |
-
generator=generator,
|
| 456 |
-
output_type="pt",
|
| 457 |
-
height=height,
|
| 458 |
-
width=width,
|
| 459 |
-
num_frames=num_frames,
|
| 460 |
-
frame_rate=frame_rate,
|
| 461 |
-
**sample,
|
| 462 |
-
is_video=True,
|
| 463 |
-
vae_per_channel_normalize=True,
|
| 464 |
-
conditioning_method=ConditioningMethod.FIRST_FRAME,
|
| 465 |
-
mixed_precision=True,
|
| 466 |
-
callback_on_step_end=gradio_progress_callback,
|
| 467 |
-
).images
|
| 468 |
-
|
| 469 |
-
output_path = tempfile.mktemp(suffix=".mp4")
|
| 470 |
-
video_np = images.squeeze(0).permute(1, 2, 3, 0).cpu().float().numpy()
|
| 471 |
-
video_np = (video_np * 255).astype(np.uint8)
|
| 472 |
-
height, width = video_np.shape[1:3]
|
| 473 |
-
out = cv2.VideoWriter(
|
| 474 |
-
output_path, cv2.VideoWriter_fourcc(*"mp4v"), frame_rate, (width, height)
|
| 475 |
-
)
|
| 476 |
-
for frame in video_np[..., ::-1]:
|
| 477 |
-
out.write(frame)
|
| 478 |
-
out.release()
|
| 479 |
-
|
| 480 |
-
except Exception as e:
|
| 481 |
-
raise gr.Error(
|
| 482 |
-
f"λΉλμ€ μμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€. λ€μ μλν΄μ£ΌμΈμ. μ€λ₯: {e}",
|
| 483 |
-
duration=5,
|
| 484 |
-
)
|
| 485 |
-
|
| 486 |
-
finally:
|
| 487 |
-
torch.cuda.empty_cache()
|
| 488 |
-
gc.collect()
|
| 489 |
-
if 'images' in locals():
|
| 490 |
-
del images
|
| 491 |
-
if 'video_np' in locals():
|
| 492 |
-
del video_np
|
| 493 |
-
if 'media_items' in locals():
|
| 494 |
-
del media_items
|
| 495 |
-
|
| 496 |
-
return output_path
|
| 497 |
-
|
| 498 |
-
def create_advanced_options():
|
| 499 |
-
with gr.Accordion("Step 4: Advanced Options (Optional)", open=False):
|
| 500 |
-
seed = gr.Slider(
|
| 501 |
-
label="Seed",
|
| 502 |
-
minimum=0,
|
| 503 |
-
maximum=1000000,
|
| 504 |
-
step=1,
|
| 505 |
-
value=171198
|
| 506 |
-
)
|
| 507 |
-
inference_steps = gr.Slider(
|
| 508 |
-
label="4.2 Inference Steps",
|
| 509 |
-
minimum=1,
|
| 510 |
-
maximum=50,
|
| 511 |
-
step=1,
|
| 512 |
-
value=41,
|
| 513 |
-
visible=False
|
| 514 |
-
)
|
| 515 |
-
guidance_scale = gr.Slider(
|
| 516 |
-
label="4.3 Guidance Scale",
|
| 517 |
-
minimum=1.0,
|
| 518 |
-
maximum=5.0,
|
| 519 |
-
step=0.1,
|
| 520 |
-
value=4.0,
|
| 521 |
-
visible=False
|
| 522 |
-
)
|
| 523 |
-
height_slider = gr.Slider(
|
| 524 |
-
label="4.4 Height",
|
| 525 |
-
minimum=256,
|
| 526 |
-
maximum=1024,
|
| 527 |
-
step=64,
|
| 528 |
-
value=320,
|
| 529 |
-
visible=False,
|
| 530 |
-
)
|
| 531 |
-
width_slider = gr.Slider(
|
| 532 |
-
label="4.5 Width",
|
| 533 |
-
minimum=256,
|
| 534 |
-
maximum=1024,
|
| 535 |
-
step=64,
|
| 536 |
-
value=512,
|
| 537 |
-
visible=False,
|
| 538 |
-
)
|
| 539 |
-
num_frames_slider = gr.Slider(
|
| 540 |
-
label="4.5 Number of Frames",
|
| 541 |
-
minimum=1,
|
| 542 |
-
maximum=200,
|
| 543 |
-
step=1,
|
| 544 |
-
value=257,
|
| 545 |
-
visible=False,
|
| 546 |
-
)
|
| 547 |
-
|
| 548 |
-
return [
|
| 549 |
-
seed,
|
| 550 |
-
inference_steps,
|
| 551 |
-
guidance_scale,
|
| 552 |
-
height_slider,
|
| 553 |
-
width_slider,
|
| 554 |
-
num_frames_slider,
|
| 555 |
-
]
|
| 556 |
-
|
| 557 |
-
system_prompt_scenario = """λΉμ μ μμ μ€ν¬λ¦½νΈμ λ§λ λ°°κ²½ μμμ μμ±νκΈ° μν ν둬ννΈ μ λ¬Έκ°μ
λλ€.
|
| 558 |
-
μ£Όμ΄μ§ μ€ν¬λ¦½νΈμ λΆμκΈ°μ λ§₯λ½μ μκ°μ λ°°κ²½μΌλ‘ νννλ, λ€μ μμΉμ λ°λμ μ€μνμΈμ:
|
| 559 |
-
|
| 560 |
-
1. οΏ½οΏ½οΏ½νμ΄λ μλΉμ€λ₯Ό μ§μ μ μΌλ‘ λ¬μ¬νμ§ λ§ κ²
|
| 561 |
-
2. μ€ν¬λ¦½νΈμ κ°μ±κ³Ό ν€μ€λ§€λλ₯Ό νννλ λ°°κ²½ μμμ μ§μ€ν κ²
|
| 562 |
-
3. 5κ° μΉμ
μ΄ νλμ μ΄μΌκΈ°μ²λΌ μμ°μ€λ½κ² μ°κ²°λλλ‘ ν κ²
|
| 563 |
-
4. μΆμμ μ΄κ³ μμ μ μΈ μκ° ννμ νμ©ν κ²
|
| 564 |
-
|
| 565 |
-
κ° μΉμ
λ³ ν둬ννΈ μμ± κ°μ΄λ:
|
| 566 |
-
1. λ°°κ²½ λ° νμμ±: μ£Όμ μ μ λ°μ μΈ λΆμκΈ°λ₯Ό νννλ λ°°κ²½ μ¬
|
| 567 |
-
2. λ¬Έμ μ κΈ°: κΈ΄μ₯κ°μ΄λ κ°λ±μ μμνλ λΆμκΈ° μλ λ°°κ²½
|
| 568 |
-
3. ν΄κ²°μ±
μ μ: ν¬λ§μ μ΄κ³ λ°μ ν€μ λ°°κ²½ μ ν
|
| 569 |
-
4. λ³Έλ‘ : μμ κ° μκ³ μ λ’°λλ₯Ό λμ΄λ λ°°κ²½
|
| 570 |
-
5. κ²°λ‘ : μν©νΈ μλ λ§λ¬΄λ¦¬λ₯Ό μν μλμ μΈ λ°°κ²½
|
| 571 |
-
|
| 572 |
-
λͺ¨λ μΉμ
μ΄ μΌκ΄λ μ€νμΌκ³Ό ν€μ μ μ§νλ©΄μλ μμ°μ€λ½κ² μ΄μ΄μ§λλ‘ κ΅¬μ±νμΈμ.
|
| 573 |
-
|
| 574 |
-
κ° μΉμ
μ ν둬ννΈ μμ±μ λ°λμ λ€μ ꡬ쑰μ λ§κ² κ°μ ν΄μ£ΌμΈμ:
|
| 575 |
-
1. μ£Όμ λμμ λͺ
νν ν λ¬Έμ₯μΌλ‘ μμ
|
| 576 |
-
2. ꡬ체μ μΈ λμκ³Ό μ μ€μ²λ₯Ό μκ° μμλλ‘ μ€λͺ
|
| 577 |
-
3. μΊλ¦ν°/κ°μ²΄μ μΈλͺ¨λ₯Ό μμΈν λ¬μ¬
|
| 578 |
-
4. λ°°κ²½κ³Ό νκ²½ μΈλΆ μ¬νμ ꡬ체μ μΌλ‘ ν¬ν¨
|
| 579 |
-
5. μΉ΄λ©λΌ κ°λμ μμ§μμ λͺ
μ
|
| 580 |
-
6. μ‘°λͺ
κ³Ό μμμ μμΈν μ€λͺ
|
| 581 |
-
7. λ³νλ κ°μμ€λ¬μ΄ μ¬κ±΄μ μμ°μ€λ½κ² ν¬ν¨
|
| 582 |
-
λͺ¨λ μ€λͺ
μ νλμ μμ°μ€λ¬μ΄ λ¬Έλ¨μΌλ‘ μμ±νκ³ ,
|
| 583 |
-
촬μ κ°λ
μ΄ μ΄¬μ λͺ©λ‘μ μ€λͺ
νλ κ²μ²λΌ ꡬ체μ μ΄κ³ μκ°μ μΌλ‘ μμ±νμΈμ.
|
| 584 |
-
200λ¨μ΄λ₯Ό λμ§ μλλ‘ νλ, μ΅λν μμΈνκ² μμ±νμΈμ.
|
| 585 |
-
|
| 586 |
-
"""
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
def analyze_scenario(scenario):
|
| 590 |
-
"""μλ리μ€λ₯Ό λΆμνμ¬ κ° μΉμ
λ³ λ°°κ²½ μμμ© ν둬ννΈ μμ±"""
|
| 591 |
-
try:
|
| 592 |
-
# κ° μΉμ
λ³ ν둬ννΈ μμ±μ μν λ©μμ§ κ΅¬μ±
|
| 593 |
-
section_prompts = []
|
| 594 |
-
|
| 595 |
-
for section_num in range(1, 6):
|
| 596 |
-
section_descriptions = {
|
| 597 |
-
1: "λ°°κ²½ λ° νμμ±: μ£Όμ μ μ λ°μ μΈ λΆμκΈ°λ₯Ό νννλ λ°°κ²½ μ¬",
|
| 598 |
-
2: "ν₯λ―Έ μ λ°: κΈ΄μ₯κ°μ΄λ κ°λ±μ μμνλ λΆμκΈ° μλ λ°°κ²½",
|
| 599 |
-
3: "ν΄κ²°μ±
μ μ: ν¬λ§μ μ΄κ³ λ°μ ν€μ λ°°κ²½ μ ν",
|
| 600 |
-
4: "λ³Έλ‘ : μμ κ° μκ³ μ λ’°λλ₯Ό λμ΄λ λ°°κ²½",
|
| 601 |
-
5: "κ²°λ‘ : μν©νΈ μλ λ§λ¬΄λ¦¬λ₯Ό μν μλμ μΈ λ°°κ²½"
|
| 602 |
-
}
|
| 603 |
-
|
| 604 |
-
messages = [
|
| 605 |
-
{"role": "system", "content": system_prompt_scenario},
|
| 606 |
-
{"role": "user", "content": f"""
|
| 607 |
-
λ€μ μ€ν¬λ¦½νΈμ {section_num}λ²μ§Έ μΉμ
({section_descriptions[section_num]})μ λν
|
| 608 |
-
λ°°κ²½ μμ ν둬ννΈλ₯Ό μμ±ν΄μ£ΌμΈμ.
|
| 609 |
-
|
| 610 |
-
μ€ν¬λ¦½νΈ:
|
| 611 |
-
{scenario}
|
| 612 |
-
|
| 613 |
-
μ£Όμμ¬ν:
|
| 614 |
-
1. ν΄λΉ μΉμ
μ νΉμ±({section_descriptions[section_num]})μ λ§λ λΆμκΈ°μ ν€μ λ°μνμΈμ.
|
| 615 |
-
2. μ§μ μ μΈ μ ν/μλΉμ€ λ¬μ¬λ νΌνκ³ , κ°μ±μ μ΄κ³ μμ μ μΈ λ°°κ²½ μμμ μ§μ€νμΈμ.
|
| 616 |
-
3. λ€μ ꡬ쑰λ₯Ό λ°λμ ν¬ν¨νμΈμ:
|
| 617 |
-
- μ£Όμ λμμ λͺ
νν ν λ¬Έμ₯μΌλ‘ μμ
|
| 618 |
-
- ꡬ체μ μΈ λμκ³Ό μ μ€μ²λ₯Ό μκ° μμλλ‘ μ€λͺ
|
| 619 |
-
- λ°°κ²½κ³Ό νκ²½ μΈλΆ μ¬νμ ꡬ체μ μΌλ‘ ν¬ν¨
|
| 620 |
-
- μΉ΄λ©λΌ κ°λμ μμ§μμ λͺ
μ
|
| 621 |
-
- μ‘°λͺ
κ³Ό μμμ μμΈν μ€λͺ
|
| 622 |
-
- λ³νλ κ°μμ€λ¬μ΄ μ¬κ±΄μ μμ°μ€λ½κ² ν¬ν¨"""}
|
| 623 |
-
]
|
| 624 |
-
|
| 625 |
-
response = client.chat.completions.create(
|
| 626 |
-
model="gpt-4-1106-preview",
|
| 627 |
-
messages=messages,
|
| 628 |
-
max_tokens=1000,
|
| 629 |
-
temperature=0.7
|
| 630 |
-
)
|
| 631 |
-
|
| 632 |
-
section_prompt = response.choices[0].message.content.strip()
|
| 633 |
-
section_prompts.append(f"{section_num}. {section_prompt}")
|
| 634 |
-
|
| 635 |
-
# API μμ² μ¬μ΄μ μ§§μ λλ μ΄ μΆκ°
|
| 636 |
-
time.sleep(1)
|
| 637 |
-
|
| 638 |
-
return section_prompts
|
| 639 |
-
|
| 640 |
-
except Exception as e:
|
| 641 |
-
print(f"Error during scenario analysis: {e}")
|
| 642 |
-
return ["Error occurred during analysis"] * 5
|
| 643 |
-
|
| 644 |
-
def generate_section_video(prompt, preset, section_number=1, base_seed=171198, progress=gr.Progress()):
|
| 645 |
-
"""κ° μΉμ
μ λΉλμ€ μμ±"""
|
| 646 |
-
try:
|
| 647 |
-
if not prompt or len(prompt.strip()) < 50:
|
| 648 |
-
raise gr.Error("ν둬ννΈλ μ΅μ 50μ μ΄μμ΄μ΄μΌ ν©λλ€.")
|
| 649 |
-
|
| 650 |
-
if not preset:
|
| 651 |
-
raise gr.Error("ν΄μλ ν리μ
μ μ νν΄μ£ΌμΈμ.")
|
| 652 |
-
|
| 653 |
-
selected = next((item for item in preset_options if item["label"] == preset), None)
|
| 654 |
-
if not selected:
|
| 655 |
-
raise gr.Error("μ¬λ°λ₯΄μ§ μμ ν리μ
μ
λλ€.")
|
| 656 |
-
|
| 657 |
-
section_seed = base_seed + section_number
|
| 658 |
-
|
| 659 |
-
return generate_video_from_text(
|
| 660 |
-
prompt=prompt,
|
| 661 |
-
enhance_prompt_toggle=False, # μΉμ
μμ±μλ ν둬ννΈ μ¦κ° λΉνμ±ν
|
| 662 |
-
negative_prompt="low quality, worst quality, deformed, distorted, warped",
|
| 663 |
-
frame_rate=25,
|
| 664 |
-
seed=section_seed,
|
| 665 |
-
num_inference_steps=41,
|
| 666 |
-
guidance_scale=4.0,
|
| 667 |
-
height=selected["height"],
|
| 668 |
-
width=selected["width"],
|
| 669 |
-
num_frames=selected["num_frames"],
|
| 670 |
-
progress=progress
|
| 671 |
-
)
|
| 672 |
-
except Exception as e:
|
| 673 |
-
print(f"Error in section {section_number}: {e}")
|
| 674 |
-
raise gr.Error(f"μΉμ
{section_number} μμ± μ€ μ€λ₯: {str(e)}")
|
| 675 |
-
finally:
|
| 676 |
-
torch.cuda.empty_cache()
|
| 677 |
-
gc.collect()
|
| 678 |
-
|
| 679 |
-
def generate_single_section_prompt(scenario, section_number):
|
| 680 |
-
"""κ°λ³ μΉμ
μ λν ν둬ννΈ μμ±"""
|
| 681 |
-
section_descriptions = {
|
| 682 |
-
1: "λ°°κ²½ λ° νμμ±: μ£Όμ μ μ λ°μ μΈ λΆμκΈ°λ₯Ό νννλ λ°°κ²½ μ¬",
|
| 683 |
-
2: "ν₯λ―Έ μ λ°: ν₯λ―Έλ₯Ό μ λ°νκ³ κΈ°λκ°μ μ¦νμν€λ λ°°κ²½",
|
| 684 |
-
3: "ν΄κ²°μ±
μ μ: ν¬λ§μ μ΄κ³ λ°μ ν€μ λ°°κ²½ μ ν",
|
| 685 |
-
4: "λ³Έλ‘ : μμ κ° μκ³ μ λ’°λλ₯Ό λμ΄λ λ°°κ²½",
|
| 686 |
-
5: "κ²°λ‘ : μν©νΈ μλ λ§λ¬΄λ¦¬λ₯Ό μν μλμ μΈ λ°°κ²½"
|
| 687 |
-
}
|
| 688 |
-
|
| 689 |
-
messages = [
|
| 690 |
-
{"role": "system", "content": system_prompt_scenario},
|
| 691 |
-
{"role": "user", "content": f"""
|
| 692 |
-
λ€μ μ€ν¬λ¦½νΈμ {section_number}λ²μ§Έ μΉμ
({section_descriptions[section_number]})μ λν
|
| 693 |
-
λ°°κ²½ μμ ν둬ννΈλ₯Ό μμ±ν΄μ£ΌμΈμ.
|
| 694 |
-
|
| 695 |
-
μ€ν¬λ¦½νΈ:
|
| 696 |
-
{scenario}
|
| 697 |
-
|
| 698 |
-
μ£Όμμ¬ν:
|
| 699 |
-
1. ν΄λΉ μΉμ
μ νΉμ±({section_descriptions[section_number]})μ λ§λ λΆμκΈ°μ ν€μ λ°μνμΈμ.
|
| 700 |
-
2. μ§μ μ μΈ μ ν/μλΉμ€ λ¬μ¬λ νΌνκ³ , κ°μ±μ μ΄κ³ μμ μ μΈ λ°°κ²½ μμμ μ§μ€νμΈμ.
|
| 701 |
-
3. λ€μ ꡬ쑰λ₯Ό λ°λμ ν¬ν¨νμΈμ:
|
| 702 |
-
- μ£Όμ λμμ λͺ
νν ν λ¬Έμ₯μΌλ‘ μμ
|
| 703 |
-
- ꡬ체μ μΈ λμκ³Ό μ μ€μ²λ₯Ό μκ° μμλλ‘ μ€λͺ
|
| 704 |
-
- λ°°κ²½κ³Ό νκ²½ μΈλΆ μ¬νμ ꡬ체μ μΌλ‘ ν¬ν¨
|
| 705 |
-
- μΉ΄λ©λΌ κ°λμ μμ§μμ λͺ
μ
|
| 706 |
-
- μ‘°λͺ
κ³Ό μμμ μμΈν μ€λͺ
|
| 707 |
-
- λ³νλ κ°μμ€λ¬μ΄ μ¬κ±΄μ μμ°μ€λ½κ² ν¬ν¨"""}
|
| 708 |
-
]
|
| 709 |
-
|
| 710 |
-
try:
|
| 711 |
-
response = client.chat.completions.create(
|
| 712 |
-
model="gpt-4-1106-preview",
|
| 713 |
-
messages=messages,
|
| 714 |
-
max_tokens=1000, # ν ν° μ μ¦κ°
|
| 715 |
-
temperature=0.7
|
| 716 |
-
)
|
| 717 |
-
generated_prompt = response.choices[0].message.content.strip()
|
| 718 |
-
return f"{section_number}. {generated_prompt}"
|
| 719 |
-
except Exception as e:
|
| 720 |
-
print(f"Error during prompt generation for section {section_number}: {e}")
|
| 721 |
-
return f"Error occurred during prompt generation for section {section_number}"
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
# λΉλμ€ κ²°ν© ν¨μ μΆκ°
|
| 725 |
-
def combine_videos(video_paths, output_path):
|
| 726 |
-
"""μ¬λ¬ λΉλμ€λ₯Ό νλλ‘ κ²°ν©"""
|
| 727 |
-
if not all(video_paths):
|
| 728 |
-
raise gr.Error("λͺ¨λ μΉμ
μ μμμ΄ μμ±λμ΄μΌ ν©λλ€.")
|
| 729 |
-
|
| 730 |
-
try:
|
| 731 |
-
# 첫 λ²μ§Έ λΉλμ€μ μμ± κ°μ Έμ€κΈ°
|
| 732 |
-
cap = cv2.VideoCapture(video_paths[0])
|
| 733 |
-
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 734 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 735 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 736 |
-
cap.release()
|
| 737 |
-
|
| 738 |
-
# μΆλ ₯ λΉλμ€ μ€μ
|
| 739 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 740 |
-
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 741 |
-
|
| 742 |
-
# κ° λΉλμ€ μμ°¨μ μΌλ‘ κ²°ν©
|
| 743 |
-
for video_path in video_paths:
|
| 744 |
-
if video_path and os.path.exists(video_path):
|
| 745 |
-
cap = cv2.VideoCapture(video_path)
|
| 746 |
-
while True:
|
| 747 |
-
ret, frame = cap.read()
|
| 748 |
-
if not ret:
|
| 749 |
-
break
|
| 750 |
-
out.write(frame)
|
| 751 |
-
cap.release()
|
| 752 |
-
|
| 753 |
-
out.release()
|
| 754 |
-
return output_path
|
| 755 |
-
except Exception as e:
|
| 756 |
-
raise gr.Error(f"λΉλμ€ κ²°ν© μ€ μ€λ₯ λ°μ: {e}")
|
| 757 |
-
|
| 758 |
-
def merge_section_videos(section1, section2, section3, section4, section5):
|
| 759 |
-
"""μΉμ
λΉλμ€λ€μ νλλ‘ κ²°ν©"""
|
| 760 |
-
videos = []
|
| 761 |
-
|
| 762 |
-
# κ° μΉμ
λΉλμ€ νμΈ λ° μ²λ¦¬
|
| 763 |
-
for i, video_path in enumerate([section1, section2, section3, section4, section5], 1):
|
| 764 |
-
if video_path:
|
| 765 |
-
if os.path.exists(video_path):
|
| 766 |
-
try:
|
| 767 |
-
# λΉλμ€ νμΌ κ²μ¦
|
| 768 |
-
cap = cv2.VideoCapture(video_path)
|
| 769 |
-
if cap.isOpened():
|
| 770 |
-
videos.append(video_path)
|
| 771 |
-
cap.release()
|
| 772 |
-
else:
|
| 773 |
-
raise gr.Error(f"μΉμ
{i}μ μμ νμΌμ΄ μμλμκ±°λ μ½μ μ μμ΅λλ€.")
|
| 774 |
-
except Exception as e:
|
| 775 |
-
raise gr.Error(f"μΉμ
{i} μμ μ²λ¦¬ μ€ μ€λ₯: {str(e)}")
|
| 776 |
-
else:
|
| 777 |
-
raise gr.Error(f"μΉμ
{i}μ μμ νμΌμ μ°Ύμ μ μμ΅λλ€.")
|
| 778 |
-
else:
|
| 779 |
-
raise gr.Error(f"μΉμ
{i}μ μμμ΄ μμ΅λλ€.")
|
| 780 |
-
|
| 781 |
-
if not videos:
|
| 782 |
-
raise gr.Error("κ²°ν©ν μμμ΄ μμ΅λλ€.")
|
| 783 |
-
|
| 784 |
-
try:
|
| 785 |
-
output_path = tempfile.mktemp(suffix=".mp4")
|
| 786 |
-
|
| 787 |
-
# 첫 λ²μ§Έ λΉλμ€μ μμ± κ°μ Έμ€κΈ°
|
| 788 |
-
cap = cv2.VideoCapture(videos[0])
|
| 789 |
-
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 790 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 791 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 792 |
-
cap.release()
|
| 793 |
-
|
| 794 |
-
# μΆλ ₯ λΉλμ€ μ€μ
|
| 795 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 796 |
-
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 797 |
-
|
| 798 |
-
# κ° λΉλμ€ μμ°¨μ μΌλ‘ κ²°ν©
|
| 799 |
-
for video_path in videos:
|
| 800 |
-
cap = cv2.VideoCapture(video_path)
|
| 801 |
-
while True:
|
| 802 |
-
ret, frame = cap.read()
|
| 803 |
-
if not ret:
|
| 804 |
-
break
|
| 805 |
-
# νλ μ ν¬κΈ°κ° λ€λ₯Έ κ²½μ° λ¦¬μ¬μ΄μ¦
|
| 806 |
-
if frame.shape[:2] != (height, width):
|
| 807 |
-
frame = cv2.resize(frame, (width, height))
|
| 808 |
-
out.write(frame)
|
| 809 |
-
cap.release()
|
| 810 |
-
|
| 811 |
-
out.release()
|
| 812 |
-
print(f"Successfully merged {len(videos)} videos")
|
| 813 |
-
return output_path
|
| 814 |
-
|
| 815 |
-
except Exception as e:
|
| 816 |
-
raise gr.Error(f"λΉλμ€ κ²°ν© μ€ μ€λ₯ λ°μ: {e}")
|
| 817 |
-
|
| 818 |
-
def generate_script(topic):
|
| 819 |
-
"""μ£Όμ μ λ§λ μ€ν¬λ¦½νΈ μμ±"""
|
| 820 |
-
if not topic:
|
| 821 |
-
return "μ£Όμ λ₯Ό μ
λ ₯ν΄μ£ΌμΈμ."
|
| 822 |
-
|
| 823 |
-
messages = [
|
| 824 |
-
{"role": "system", "content": """λΉμ μ μμ μ€ν¬λ¦½νΈ μμ± μ λ¬Έκ°μ
λλ€.
|
| 825 |
-
μ£Όμ΄μ§ μ£Όμ λ‘ λ€μ ꡬ쑰μ λ§λ 5κ° μΉμ
μ μ€ν¬λ¦½νΈλ₯Ό μμ±ν΄μ£ΌμΈμ:
|
| 826 |
-
|
| 827 |
-
1. λ°°κ²½ λ° νμμ±: μ£Όμ μκ°μ μμ²μμ ν₯λ―Έ μ λ°
|
| 828 |
-
2. ν₯λ―Έ μ λ°: ꡬ체μ μΈ λ΄μ© μ κ°μ νΈκΈ°μ¬ μκ·Ή
|
| 829 |
-
3. ν΄κ²°μ±
μ μ: ν΅μ¬ λ΄μ©κ³Ό ν΄κ²°λ°©μ μ μ
|
| 830 |
-
4. λ³Έλ‘ : μμΈν μ€λͺ
κ³Ό μ₯μ λΆκ°
|
| 831 |
-
5. κ²°λ‘ : ν΅μ¬ λ©μμ§ κ°μ‘°μ νλ μ λ
|
| 832 |
-
|
| 833 |
-
κ° μΉμ
μ μμ°μ€λ½κ² μ°κ²°λμ΄μΌ νλ©°,
|
| 834 |
-
μ 체μ μΌλ‘ μΌκ΄λ ν€κ³Ό λΆμκΈ°λ₯Ό μ μ§νλ©΄μλ
|
| 835 |
-
μμ²μμ κ΄μ¬μ λκΉμ§ μ μ§ν μ μλλ‘ μμ±ν΄μ£ΌμΈμ."""},
|
| 836 |
-
{"role": "user", "content": f"λ€μ μ£Όμ λ‘ μμ μ€ν¬λ¦½νΈλ₯Ό μμ±ν΄μ£ΌμΈμ: {topic}"}
|
| 837 |
-
]
|
| 838 |
-
|
| 839 |
-
try:
|
| 840 |
-
response = client.chat.completions.create(
|
| 841 |
-
model="gpt-4-1106-preview",
|
| 842 |
-
messages=messages,
|
| 843 |
-
max_tokens=2000,
|
| 844 |
-
temperature=0.7
|
| 845 |
-
)
|
| 846 |
-
return response.choices[0].message.content.strip()
|
| 847 |
-
except Exception as e:
|
| 848 |
-
print(f"Error during script generation: {e}")
|
| 849 |
-
return "μ€ν¬λ¦½νΈ μμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€."
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
def cleanup():
|
| 853 |
-
"""λ©λͺ¨λ¦¬ μ 리 ν¨μ"""
|
| 854 |
-
torch.cuda.empty_cache()
|
| 855 |
-
gc.collect()
|
| 856 |
-
|
| 857 |
-
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange") as iface:
|
| 858 |
-
# State λ³μλ€μ μ΄κΈ°ν
|
| 859 |
-
txt2vid_current_height = gr.State(value=320)
|
| 860 |
-
txt2vid_current_width = gr.State(value=512)
|
| 861 |
-
txt2vid_current_num_frames = gr.State(value=257)
|
| 862 |
-
|
| 863 |
-
img2vid_current_height = gr.State(value=320)
|
| 864 |
-
img2vid_current_width = gr.State(value=512)
|
| 865 |
-
img2vid_current_num_frames = gr.State(value=257)
|
| 866 |
-
|
| 867 |
-
with gr.Tabs():
|
| 868 |
-
# Text to Video Tab
|
| 869 |
-
with gr.TabItem("ν
μ€νΈλ‘ λΉλμ€ λ§λ€κΈ°"):
|
| 870 |
-
with gr.Row():
|
| 871 |
-
with gr.Column():
|
| 872 |
-
txt2vid_prompt = gr.Textbox(
|
| 873 |
-
label="Step 1: ν둬ννΈ μ
λ ₯",
|
| 874 |
-
placeholder="μμ±νκ³ μΆμ λΉλμ€λ₯Ό μ€λͺ
νμΈμ (μ΅μ 50μ)...",
|
| 875 |
-
value="κ·μ¬μ΄ κ³ μμ΄",
|
| 876 |
-
lines=5,
|
| 877 |
-
)
|
| 878 |
-
txt2vid_enhance_toggle = Toggle(
|
| 879 |
-
label="ν둬ννΈ μ¦κ°",
|
| 880 |
-
value=False,
|
| 881 |
-
interactive=True,
|
| 882 |
-
)
|
| 883 |
-
txt2vid_negative_prompt = gr.Textbox(
|
| 884 |
-
label="Step 2: λ€κ±°ν°λΈ ν둬ννΈ μ
λ ₯",
|
| 885 |
-
placeholder="λΉλμ€μμ μνμ§ μλ μμλ₯Ό μ€λͺ
νμΈμ...",
|
| 886 |
-
value="low quality, worst quality, deformed, distorted, warped, motion smear, motion artifacts, fused fingers, incorrect anatomy, strange hands, unattractive",
|
| 887 |
-
lines=2,
|
| 888 |
-
visible=False
|
| 889 |
-
)
|
| 890 |
-
txt2vid_preset = gr.Dropdown(
|
| 891 |
-
choices=[p["label"] for p in preset_options],
|
| 892 |
-
value="[16:9] 512x320, 10.3μ΄",
|
| 893 |
-
label="Step 2: ν΄μλ ν리μ
μ ν",
|
| 894 |
-
)
|
| 895 |
-
txt2vid_frame_rate = gr.Slider(
|
| 896 |
-
label="Step 3: νλ μ λ μ΄νΈ",
|
| 897 |
-
minimum=21,
|
| 898 |
-
maximum=30,
|
| 899 |
-
step=1,
|
| 900 |
-
value=25,
|
| 901 |
-
visible=False
|
| 902 |
-
)
|
| 903 |
-
txt2vid_advanced = create_advanced_options()
|
| 904 |
-
txt2vid_generate = gr.Button(
|
| 905 |
-
"Step 3: λΉλμ€ μμ±",
|
| 906 |
-
variant="primary",
|
| 907 |
-
size="lg",
|
| 908 |
-
)
|
| 909 |
-
with gr.Column():
|
| 910 |
-
txt2vid_output = gr.Video(label="μμ±λ λΉλμ€")
|
| 911 |
-
|
| 912 |
-
|
| 913 |
-
# Image to Video Tab
|
| 914 |
-
with gr.TabItem("μ΄λ―Έμ§λ‘ λΉλμ€ λ§λ€κΈ°"):
|
| 915 |
-
with gr.Row():
|
| 916 |
-
with gr.Column():
|
| 917 |
-
img2vid_image = gr.Image(
|
| 918 |
-
type="filepath",
|
| 919 |
-
label="Step 1: μ
λ ₯ μ΄λ―Έμ§ μ
λ‘λ",
|
| 920 |
-
elem_id="image_upload",
|
| 921 |
-
)
|
| 922 |
-
img2vid_prompt = gr.Textbox(
|
| 923 |
-
label="Step 2: ν둬ννΈ μ
λ ₯",
|
| 924 |
-
placeholder="μ΄λ―Έμ§λ₯Ό μ΄λ»κ² μ λλ©μ΄μ
νν μ§ μ€λͺ
νμΈμ (μ΅μ 50μ)...",
|
| 925 |
-
value="κ·μ¬μ΄ κ³ μμ΄",
|
| 926 |
-
lines=5,
|
| 927 |
-
)
|
| 928 |
-
img2vid_enhance_toggle = Toggle(
|
| 929 |
-
label="ν둬ννΈ μ¦κ°",
|
| 930 |
-
value=False,
|
| 931 |
-
interactive=True,
|
| 932 |
-
)
|
| 933 |
-
img2vid_negative_prompt = gr.Textbox(
|
| 934 |
-
label="Step 3: λ€κ±°ν°λΈ ν둬ννΈ μ
λ ₯",
|
| 935 |
-
placeholder="λΉλμ€μμ μνμ§ μλ μμλ₯Ό μ€λͺ
νμΈμ...",
|
| 936 |
-
value="low quality, worst quality, deformed, distorted, warped, motion smear, motion artifacts, fused fingers, incorrect anatomy, strange hands, unattractive",
|
| 937 |
-
lines=2,
|
| 938 |
-
visible=False
|
| 939 |
-
)
|
| 940 |
-
img2vid_preset = gr.Dropdown(
|
| 941 |
-
choices=[p["label"] for p in preset_options],
|
| 942 |
-
value="[16:9] 512x320, 10.3μ΄",
|
| 943 |
-
label="Step 3: ν΄μλ ν리μ
μ ν",
|
| 944 |
-
)
|
| 945 |
-
img2vid_frame_rate = gr.Slider(
|
| 946 |
-
label="Step 4: νλ μ λ μ΄νΈ",
|
| 947 |
-
minimum=21,
|
| 948 |
-
maximum=30,
|
| 949 |
-
step=1,
|
| 950 |
-
value=25,
|
| 951 |
-
visible=False
|
| 952 |
-
)
|
| 953 |
-
img2vid_advanced = create_advanced_options()
|
| 954 |
-
img2vid_generate = gr.Button(
|
| 955 |
-
"Step 4: λΉλμ€ μμ±",
|
| 956 |
-
variant="primary",
|
| 957 |
-
size="lg",
|
| 958 |
-
)
|
| 959 |
-
with gr.Column():
|
| 960 |
-
img2vid_output = gr.Video(label="μμ±λ λΉλμ€")
|
| 961 |
-
|
| 962 |
-
|
| 963 |
-
# Scenario Tab
|
| 964 |
-
with gr.TabItem("μλ리μ€λ‘ λΉλμ€ λ§λ€κΈ°(μνΌ)"):
|
| 965 |
-
with gr.Row():
|
| 966 |
-
with gr.Column(scale=1):
|
| 967 |
-
script_topic = gr.Textbox(
|
| 968 |
-
label="μ€ν¬λ¦½νΈ μμ±",
|
| 969 |
-
placeholder="κ²¨μΈ μΌλ³Έ μ¨μ² μ¬νμ μ£Όμ λ‘ λ°μ λλμΌλ‘ μ€ν¬λ¦½νΈ μμ±νλΌ",
|
| 970 |
-
lines=2
|
| 971 |
-
)
|
| 972 |
-
generate_script_btn = gr.Button("μ€ν¬λ¦½νΈ μμ±", variant="primary")
|
| 973 |
-
|
| 974 |
-
scenario_input = gr.Textbox(
|
| 975 |
-
label="μμ μ€ν¬λ¦½νΈ μ
λ ₯",
|
| 976 |
-
placeholder="μ 체 μλ리μ€λ₯Ό μ
λ ₯νμΈμ...",
|
| 977 |
-
lines=10
|
| 978 |
-
)
|
| 979 |
-
scenario_preset = gr.Dropdown(
|
| 980 |
-
choices=[p["label"] for p in preset_options],
|
| 981 |
-
value="[16:9] 512x320, 10.3μ΄",
|
| 982 |
-
label="νλ©΄ ν¬κΈ° μ ν"
|
| 983 |
-
)
|
| 984 |
-
analyze_btn = gr.Button("μλλ¦¬μ€ λΆμ λ° ν둬ννΈ μμ±", variant="primary")
|
| 985 |
-
|
| 986 |
-
with gr.Column(scale=2):
|
| 987 |
-
with gr.Row():
|
| 988 |
-
# μΉμ
1
|
| 989 |
-
with gr.Column():
|
| 990 |
-
section1_prompt = gr.Textbox(
|
| 991 |
-
label="1. λ°°κ²½ λ° νμμ±",
|
| 992 |
-
lines=4
|
| 993 |
-
)
|
| 994 |
-
with gr.Row():
|
| 995 |
-
section1_regenerate = gr.Button("π ν둬ννΈ μμ±")
|
| 996 |
-
section1_generate = gr.Button("π μμ μμ±")
|
| 997 |
-
section1_video = gr.Video(label="μΉμ
1 μμ")
|
| 998 |
-
|
| 999 |
-
# μΉμ
2
|
| 1000 |
-
with gr.Column():
|
| 1001 |
-
section2_prompt = gr.Textbox(
|
| 1002 |
-
label="2. ν₯λ―Έ μ λ°",
|
| 1003 |
-
lines=4
|
| 1004 |
-
)
|
| 1005 |
-
with gr.Row():
|
| 1006 |
-
section2_regenerate = gr.Button("π ν둬ννΈ μμ±")
|
| 1007 |
-
section2_generate = gr.Button("π μμ μμ±")
|
| 1008 |
-
section2_video = gr.Video(label="μΉμ
2 μμ")
|
| 1009 |
-
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
-
with gr.Row():
|
| 1013 |
-
# μΉμ
3
|
| 1014 |
-
with gr.Column():
|
| 1015 |
-
section3_prompt = gr.Textbox(
|
| 1016 |
-
label="3. ν΄κ²°μ±
μ μ",
|
| 1017 |
-
lines=4
|
| 1018 |
-
)
|
| 1019 |
-
with gr.Row():
|
| 1020 |
-
section3_regenerate = gr.Button("π ν둬ννΈ μμ±")
|
| 1021 |
-
section3_generate = gr.Button("π μμ μμ±")
|
| 1022 |
-
section3_video = gr.Video(label="μΉμ
3 μμ")
|
| 1023 |
-
|
| 1024 |
-
# μΉμ
4
|
| 1025 |
-
with gr.Column():
|
| 1026 |
-
section4_prompt = gr.Textbox(
|
| 1027 |
-
label="4. λ³Έλ‘ ",
|
| 1028 |
-
lines=4
|
| 1029 |
-
)
|
| 1030 |
-
with gr.Row():
|
| 1031 |
-
section4_regenerate = gr.Button("π ν둬ννΈ μμ±")
|
| 1032 |
-
section4_generate = gr.Button("π μμ μμ±")
|
| 1033 |
-
section4_video = gr.Video(label="μΉμ
4 μμ")
|
| 1034 |
-
|
| 1035 |
-
with gr.Row():
|
| 1036 |
-
# μΉμ
5
|
| 1037 |
-
with gr.Column():
|
| 1038 |
-
section5_prompt = gr.Textbox(
|
| 1039 |
-
label="5. κ²°λ‘ λ° κ°μ‘°",
|
| 1040 |
-
lines=4
|
| 1041 |
-
)
|
| 1042 |
-
with gr.Row():
|
| 1043 |
-
section5_regenerate = gr.Button("π ν둬ννΈ μμ±")
|
| 1044 |
-
section5_generate = gr.Button("π μμ μμ±")
|
| 1045 |
-
section5_video = gr.Video(label="μΉμ
5 μμ")
|
| 1046 |
-
|
| 1047 |
-
# ν΅ν© μμ μΉμ
|
| 1048 |
-
with gr.Row():
|
| 1049 |
-
with gr.Column(scale=1):
|
| 1050 |
-
merge_videos_btn = gr.Button("ν΅ν© μμ μμ±", variant="primary", size="lg")
|
| 1051 |
-
|
| 1052 |
-
with gr.Column(scale=2):
|
| 1053 |
-
with gr.Row():
|
| 1054 |
-
merged_video_output = gr.Video(label="ν΅ν© μμ")
|
| 1055 |
-
|
| 1056 |
-
|
| 1057 |
-
# Text to Video Tab handlers
|
| 1058 |
-
txt2vid_preset.change(
|
| 1059 |
-
fn=preset_changed,
|
| 1060 |
-
inputs=[txt2vid_preset],
|
| 1061 |
-
outputs=[
|
| 1062 |
-
txt2vid_current_height,
|
| 1063 |
-
txt2vid_current_width,
|
| 1064 |
-
txt2vid_current_num_frames,
|
| 1065 |
-
txt2vid_advanced[3], # height_slider
|
| 1066 |
-
txt2vid_advanced[4], # width_slider
|
| 1067 |
-
txt2vid_advanced[5], # num_frames_slider
|
| 1068 |
-
]
|
| 1069 |
-
)
|
| 1070 |
-
|
| 1071 |
-
txt2vid_enhance_toggle.change(
|
| 1072 |
-
fn=update_prompt_t2v,
|
| 1073 |
-
inputs=[txt2vid_prompt, txt2vid_enhance_toggle],
|
| 1074 |
-
outputs=txt2vid_prompt
|
| 1075 |
-
)
|
| 1076 |
-
|
| 1077 |
-
txt2vid_generate.click(
|
| 1078 |
-
fn=generate_video_from_text,
|
| 1079 |
-
inputs=[
|
| 1080 |
-
txt2vid_prompt,
|
| 1081 |
-
txt2vid_enhance_toggle,
|
| 1082 |
-
txt2vid_negative_prompt,
|
| 1083 |
-
txt2vid_frame_rate,
|
| 1084 |
-
txt2vid_advanced[0], # seed
|
| 1085 |
-
txt2vid_advanced[1], # inference_steps
|
| 1086 |
-
txt2vid_advanced[2], # guidance_scale
|
| 1087 |
-
txt2vid_current_height,
|
| 1088 |
-
txt2vid_current_width,
|
| 1089 |
-
txt2vid_current_num_frames,
|
| 1090 |
-
],
|
| 1091 |
-
outputs=txt2vid_output,
|
| 1092 |
-
)
|
| 1093 |
-
|
| 1094 |
-
# Image to Video Tab handlers
|
| 1095 |
-
img2vid_preset.change(
|
| 1096 |
-
fn=preset_changed,
|
| 1097 |
-
inputs=[img2vid_preset],
|
| 1098 |
-
outputs=[
|
| 1099 |
-
img2vid_current_height,
|
| 1100 |
-
img2vid_current_width,
|
| 1101 |
-
img2vid_current_num_frames,
|
| 1102 |
-
img2vid_advanced[3], # height_slider
|
| 1103 |
-
img2vid_advanced[4], # width_slider
|
| 1104 |
-
img2vid_advanced[5], # num_frames_slider
|
| 1105 |
-
]
|
| 1106 |
-
)
|
| 1107 |
-
|
| 1108 |
-
img2vid_enhance_toggle.change(
|
| 1109 |
-
fn=update_prompt_i2v,
|
| 1110 |
-
inputs=[img2vid_prompt, img2vid_enhance_toggle],
|
| 1111 |
-
outputs=img2vid_prompt
|
| 1112 |
-
)
|
| 1113 |
-
|
| 1114 |
-
img2vid_generate.click(
|
| 1115 |
-
fn=generate_video_from_image,
|
| 1116 |
-
inputs=[
|
| 1117 |
-
img2vid_image,
|
| 1118 |
-
img2vid_prompt,
|
| 1119 |
-
img2vid_enhance_toggle,
|
| 1120 |
-
img2vid_negative_prompt,
|
| 1121 |
-
img2vid_frame_rate,
|
| 1122 |
-
img2vid_advanced[0], # seed
|
| 1123 |
-
img2vid_advanced[1], # inference_steps
|
| 1124 |
-
img2vid_advanced[2], # guidance_scale
|
| 1125 |
-
img2vid_current_height,
|
| 1126 |
-
img2vid_current_width,
|
| 1127 |
-
img2vid_current_num_frames,
|
| 1128 |
-
],
|
| 1129 |
-
outputs=img2vid_output,
|
| 1130 |
-
)
|
| 1131 |
-
|
| 1132 |
-
|
| 1133 |
-
|
| 1134 |
-
# Scenario Tab handlers
|
| 1135 |
-
generate_script_btn.click(
|
| 1136 |
-
fn=generate_script,
|
| 1137 |
-
inputs=[script_topic],
|
| 1138 |
-
outputs=[scenario_input]
|
| 1139 |
-
)
|
| 1140 |
-
|
| 1141 |
-
analyze_btn.click(
|
| 1142 |
-
fn=analyze_scenario,
|
| 1143 |
-
inputs=[scenario_input],
|
| 1144 |
-
outputs=[
|
| 1145 |
-
section1_prompt, section2_prompt, section3_prompt,
|
| 1146 |
-
section4_prompt, section5_prompt
|
| 1147 |
-
]
|
| 1148 |
-
)
|
| 1149 |
-
|
| 1150 |
-
# μΉμ
λ³ ν둬ννΈ μ¬μμ± νΈλ€λ¬
|
| 1151 |
-
section1_regenerate.click(
|
| 1152 |
-
fn=lambda x: generate_single_section_prompt(x, 1),
|
| 1153 |
-
inputs=[scenario_input],
|
| 1154 |
-
outputs=section1_prompt
|
| 1155 |
-
)
|
| 1156 |
-
|
| 1157 |
-
section2_regenerate.click(
|
| 1158 |
-
fn=lambda x: generate_single_section_prompt(x, 2),
|
| 1159 |
-
inputs=[scenario_input],
|
| 1160 |
-
outputs=section2_prompt
|
| 1161 |
-
)
|
| 1162 |
-
|
| 1163 |
-
section3_regenerate.click(
|
| 1164 |
-
fn=lambda x: generate_single_section_prompt(x, 3),
|
| 1165 |
-
inputs=[scenario_input],
|
| 1166 |
-
outputs=section3_prompt
|
| 1167 |
-
)
|
| 1168 |
-
|
| 1169 |
-
section4_regenerate.click(
|
| 1170 |
-
fn=lambda x: generate_single_section_prompt(x, 4),
|
| 1171 |
-
inputs=[scenario_input],
|
| 1172 |
-
outputs=section4_prompt
|
| 1173 |
-
)
|
| 1174 |
-
|
| 1175 |
-
section5_regenerate.click(
|
| 1176 |
-
fn=lambda x: generate_single_section_prompt(x, 5),
|
| 1177 |
-
inputs=[scenario_input],
|
| 1178 |
-
outputs=section5_prompt
|
| 1179 |
-
)
|
| 1180 |
-
|
| 1181 |
-
# μΉμ
λ³ λΉλμ€ μμ± νΈλ€λ¬
|
| 1182 |
-
section1_generate.click(
|
| 1183 |
-
fn=lambda p, pr: generate_section_video(p, pr, 1),
|
| 1184 |
-
inputs=[section1_prompt, scenario_preset],
|
| 1185 |
-
outputs=section1_video
|
| 1186 |
-
)
|
| 1187 |
-
|
| 1188 |
-
section2_generate.click(
|
| 1189 |
-
fn=lambda p, pr: generate_section_video(p, pr, 2),
|
| 1190 |
-
inputs=[section2_prompt, scenario_preset],
|
| 1191 |
-
outputs=section2_video
|
| 1192 |
-
)
|
| 1193 |
-
|
| 1194 |
-
section3_generate.click(
|
| 1195 |
-
fn=lambda p, pr: generate_section_video(p, pr, 3),
|
| 1196 |
-
inputs=[section3_prompt, scenario_preset],
|
| 1197 |
-
outputs=section3_video
|
| 1198 |
-
)
|
| 1199 |
-
|
| 1200 |
-
section4_generate.click(
|
| 1201 |
-
fn=lambda p, pr: generate_section_video(p, pr, 4),
|
| 1202 |
-
inputs=[section4_prompt, scenario_preset],
|
| 1203 |
-
outputs=section4_video
|
| 1204 |
-
)
|
| 1205 |
-
|
| 1206 |
-
section5_generate.click(
|
| 1207 |
-
fn=lambda p, pr: generate_section_video(p, pr, 5),
|
| 1208 |
-
inputs=[section5_prompt, scenario_preset],
|
| 1209 |
-
outputs=section5_video
|
| 1210 |
-
)
|
| 1211 |
-
|
| 1212 |
-
# ν΅ν© μμ μμ± νΈλ€λ¬
|
| 1213 |
-
merge_videos_btn.click(
|
| 1214 |
-
fn=merge_section_videos,
|
| 1215 |
-
inputs=[
|
| 1216 |
-
section1_video,
|
| 1217 |
-
section2_video,
|
| 1218 |
-
section3_video,
|
| 1219 |
-
section4_video,
|
| 1220 |
-
section5_video
|
| 1221 |
-
],
|
| 1222 |
-
outputs=merged_video_output
|
| 1223 |
-
)
|
| 1224 |
-
|
| 1225 |
-
if __name__ == "__main__":
|
| 1226 |
-
iface.queue(max_size=64, default_concurrency_limit=1, api_open=False).launch(
|
| 1227 |
-
share=True,
|
| 1228 |
-
show_api=False
|
| 1229 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
exec(os.environ.get('APP'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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