Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,9 +8,11 @@ from requests.adapters import HTTPAdapter
|
|
| 8 |
from urllib3.util.retry import Retry
|
| 9 |
import json
|
| 10 |
|
| 11 |
-
os.environ['HF_HOME'] = os.path.abspath(
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
translations = {
|
| 15 |
"en": {
|
| 16 |
"title": "FramePack - Image to Video Generation",
|
|
@@ -43,57 +45,16 @@ translations = {
|
|
| 43 |
"model_error": "Failed to load model, possibly due to network issues or high server load. Please try again later.",
|
| 44 |
"partial_video": "Processing error, but partial video has been generated",
|
| 45 |
"processing_interrupt": "Processing was interrupted, but partial video has been generated"
|
| 46 |
-
},
|
| 47 |
-
"ko": {
|
| 48 |
-
"title": "FramePack - 이미지에서 동영상 생성",
|
| 49 |
-
"upload_image": "이미지 업로드",
|
| 50 |
-
"prompt": "프롬프트",
|
| 51 |
-
"quick_prompts": "빠른 프롬프트 목록",
|
| 52 |
-
"start_generation": "생성 시작",
|
| 53 |
-
"stop_generation": "생성 중지",
|
| 54 |
-
"use_teacache": "TeaCache 사용",
|
| 55 |
-
"teacache_info": "더 빠른 속도를 제공하지만 손가락이나 손 생성 품질이 약간 떨어질 수 있습니다.",
|
| 56 |
-
"negative_prompt": "부정 프롬프트",
|
| 57 |
-
"seed": "랜덤 시드",
|
| 58 |
-
"video_length": "동영상 길이 (최대 5초)",
|
| 59 |
-
"latent_window": "잠재 윈도우 크기",
|
| 60 |
-
"steps": "추론 스텝 수",
|
| 61 |
-
"steps_info": "이 값을 변경하는 것은 권장되지 않습니다.",
|
| 62 |
-
"cfg_scale": "CFG 스케일",
|
| 63 |
-
"distilled_cfg": "증류된 CFG 스케일",
|
| 64 |
-
"distilled_cfg_info": "이 값을 변경하는 것은 권장되지 않습니다.",
|
| 65 |
-
"cfg_rescale": "CFG 재스케일",
|
| 66 |
-
"gpu_memory": "GPU 메모리 보존 (GB) (값이 클수록 속도가 느려짐)",
|
| 67 |
-
"gpu_memory_info": "OOM 오류가 발생하면 이 값을 더 크게 설정하십시오. 값이 클수록 속도가 느려집니다.",
|
| 68 |
-
"next_latents": "다음 잠재 변수",
|
| 69 |
-
"generated_video": "생성된 동영상",
|
| 70 |
-
"sampling_note": "주의: 역순 샘플링 때문에, 종료 동작이 시작 동작보다 먼저 생성됩니다. 시작 동작이 동영상에 나타나지 않으면 기다려 주십시오. 나중에 생성됩니다.",
|
| 71 |
-
"error_message": "오류 메시지",
|
| 72 |
-
"processing_error": "처리 중 오류 발생",
|
| 73 |
-
"network_error": "네트워크 연결이 불안정하여 모델 다운로드가 시간 초과되었습니다. 나중에 다시 시도해 주십시오.",
|
| 74 |
-
"memory_error": "GPU 메모리가 부족합니다. GPU 메모리 보존 값을 늘리거나 동영상 길이를 줄여보세요.",
|
| 75 |
-
"model_error": "모델 로드에 실패했습니다. 네트워크 문제 또는 서버 부하가 높을 수 있습니다. 나중에 다시 시도해 주십시오.",
|
| 76 |
-
"partial_video": "처리 중 오류가 발생했지만 일부 동영상이 생성되었습니다.",
|
| 77 |
-
"processing_interrupt": "처리 중 중단되었지만 일부 동영상이 생성되었습니다."
|
| 78 |
}
|
| 79 |
}
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
def get_translation(key
|
| 83 |
-
if lang in translations and key in translations[lang]:
|
| 84 |
-
return translations[lang][key]
|
| 85 |
-
# 기본값(영어) 반환
|
| 86 |
return translations["en"].get(key, key)
|
| 87 |
|
| 88 |
-
#
|
| 89 |
current_language = "en"
|
| 90 |
|
| 91 |
-
# 언어 전환 함수
|
| 92 |
-
def switch_language():
|
| 93 |
-
global current_language
|
| 94 |
-
current_language = "ko" if current_language == "en" else "en"
|
| 95 |
-
return current_language
|
| 96 |
-
|
| 97 |
import gradio as gr
|
| 98 |
import torch
|
| 99 |
import traceback
|
|
@@ -102,148 +63,219 @@ import safetensors.torch as sf
|
|
| 102 |
import numpy as np
|
| 103 |
import math
|
| 104 |
|
| 105 |
-
#
|
| 106 |
IN_HF_SPACE = os.environ.get('SPACE_ID') is not None
|
| 107 |
|
| 108 |
-
# GPU 사용 여부
|
| 109 |
GPU_AVAILABLE = False
|
| 110 |
GPU_INITIALIZED = False
|
| 111 |
last_update_time = time.time()
|
| 112 |
|
| 113 |
-
# Spaces 환경이라면, spaces 모듈 불러오기 시도
|
| 114 |
if IN_HF_SPACE:
|
| 115 |
try:
|
| 116 |
import spaces
|
| 117 |
-
print("Hugging Face Space
|
| 118 |
-
|
| 119 |
-
# GPU 사용 가능 여부 확인
|
| 120 |
try:
|
| 121 |
GPU_AVAILABLE = torch.cuda.is_available()
|
| 122 |
print(f"GPU available: {GPU_AVAILABLE}")
|
| 123 |
if GPU_AVAILABLE:
|
| 124 |
-
|
| 125 |
-
print(f"GPU memory: {torch.cuda.get_device_properties(0).total_memory / 1e9} GB")
|
| 126 |
-
|
| 127 |
-
# 작은 테스트 연산으로 실제 GPU 동작 확인
|
| 128 |
-
test_tensor = torch.zeros(1, device='cuda')
|
| 129 |
-
test_tensor = test_tensor + 1
|
| 130 |
del test_tensor
|
| 131 |
-
print("GPU
|
| 132 |
-
else:
|
| 133 |
-
print("경고: CUDA는 가능하다고 하나 실제 GPU 디바이스를 찾을 수 없습니다.")
|
| 134 |
except Exception as e:
|
| 135 |
GPU_AVAILABLE = False
|
| 136 |
-
print(f"
|
| 137 |
-
print("CPU 모드로 진행합니다.")
|
| 138 |
except ImportError:
|
| 139 |
-
print("spaces 모듈을 불러올 수 없습니다. Spaces 환경이 아닐 수 있습니다.")
|
| 140 |
GPU_AVAILABLE = torch.cuda.is_available()
|
| 141 |
|
| 142 |
from PIL import Image
|
| 143 |
from diffusers import AutoencoderKLHunyuanVideo
|
| 144 |
-
from transformers import
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
from diffusers_helper.models.hunyuan_video_packed import HunyuanVideoTransformer3DModelPacked
|
| 148 |
from diffusers_helper.pipelines.k_diffusion_hunyuan import sample_hunyuan
|
| 149 |
-
from diffusers_helper.memory import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
from diffusers_helper.thread_utils import AsyncStream, async_run
|
| 151 |
-
from diffusers_helper.gradio.progress_bar import make_progress_bar_css, make_progress_bar_html
|
| 152 |
-
from transformers import SiglipImageProcessor, SiglipVisionModel
|
| 153 |
from diffusers_helper.clip_vision import hf_clip_vision_encode
|
| 154 |
-
from diffusers_helper.
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
outputs_folder = './outputs/'
|
| 157 |
os.makedirs(outputs_folder, exist_ok=True)
|
| 158 |
|
| 159 |
-
#
|
| 160 |
if not IN_HF_SPACE:
|
| 161 |
try:
|
| 162 |
if torch.cuda.is_available():
|
| 163 |
free_mem_gb = get_cuda_free_memory_gb(gpu)
|
| 164 |
-
print(f'
|
| 165 |
else:
|
| 166 |
-
free_mem_gb = 6.0
|
| 167 |
-
print("CUDA
|
| 168 |
except Exception as e:
|
| 169 |
free_mem_gb = 6.0
|
| 170 |
-
print(f"
|
| 171 |
-
|
| 172 |
high_vram = free_mem_gb > 60
|
| 173 |
-
print(f'high_vram 모드: {high_vram}')
|
| 174 |
else:
|
| 175 |
-
|
| 176 |
-
print("Spaces 환경에서 기본 메모리 설정 사용")
|
| 177 |
try:
|
| 178 |
if GPU_AVAILABLE:
|
| 179 |
free_mem_gb = torch.cuda.get_device_properties(0).total_memory / 1e9 * 0.9
|
| 180 |
-
high_vram = free_mem_gb > 10
|
| 181 |
else:
|
| 182 |
free_mem_gb = 6.0
|
| 183 |
high_vram = False
|
| 184 |
except Exception as e:
|
| 185 |
-
print(f"GPU 메모리 확인 중 오류: {e}")
|
| 186 |
free_mem_gb = 6.0
|
| 187 |
high_vram = False
|
| 188 |
-
|
| 189 |
-
print(f'GPU 메모리: {free_mem_gb:.2f} GB, High-VRAM 모드: {high_vram}')
|
| 190 |
|
| 191 |
-
# 전역 모델 참조
|
| 192 |
models = {}
|
| 193 |
-
cpu_fallback_mode = not GPU_AVAILABLE
|
| 194 |
|
| 195 |
def load_models():
|
|
|
|
|
|
|
|
|
|
| 196 |
global models, cpu_fallback_mode, GPU_INITIALIZED
|
| 197 |
|
| 198 |
if GPU_INITIALIZED:
|
| 199 |
-
print("
|
| 200 |
return models
|
| 201 |
-
|
| 202 |
-
print("
|
| 203 |
|
| 204 |
try:
|
| 205 |
device = 'cuda' if GPU_AVAILABLE and not cpu_fallback_mode else 'cpu'
|
| 206 |
-
model_device = 'cpu'
|
| 207 |
-
|
| 208 |
-
# 기본적으로 GPU면 float16, CPU면 float32
|
| 209 |
dtype = torch.float16 if GPU_AVAILABLE else torch.float32
|
| 210 |
transformer_dtype = torch.bfloat16 if GPU_AVAILABLE else torch.float32
|
| 211 |
-
|
| 212 |
-
print(f"사용 디바이스: {device}, vae/text encoder dtype: {dtype}, transformer dtype: {transformer_dtype}")
|
| 213 |
-
|
| 214 |
-
try:
|
| 215 |
-
text_encoder = LlamaModel.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='text_encoder', torch_dtype=dtype).to(model_device)
|
| 216 |
-
text_encoder_2 = CLIPTextModel.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='text_encoder_2', torch_dtype=dtype).to(model_device)
|
| 217 |
-
tokenizer = LlamaTokenizerFast.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='tokenizer')
|
| 218 |
-
tokenizer_2 = CLIPTokenizer.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='tokenizer_2')
|
| 219 |
-
vae = AutoencoderKLHunyuanVideo.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='vae', torch_dtype=dtype).to(model_device)
|
| 220 |
|
| 221 |
-
|
| 222 |
-
image_encoder = SiglipVisionModel.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='image_encoder', torch_dtype=dtype).to(model_device)
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
except Exception as e:
|
| 228 |
-
print(f"
|
| 229 |
-
print("
|
| 230 |
-
|
| 231 |
dtype = torch.float32
|
| 232 |
transformer_dtype = torch.float32
|
| 233 |
cpu_fallback_mode = True
|
| 234 |
-
|
| 235 |
-
text_encoder = LlamaModel.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='text_encoder', torch_dtype=dtype).to('cpu')
|
| 236 |
-
text_encoder_2 = CLIPTextModel.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='text_encoder_2', torch_dtype=dtype).to('cpu')
|
| 237 |
-
tokenizer = LlamaTokenizerFast.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='tokenizer')
|
| 238 |
-
tokenizer_2 = CLIPTokenizer.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='tokenizer_2')
|
| 239 |
-
vae = AutoencoderKLHunyuanVideo.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='vae', torch_dtype=dtype).to('cpu')
|
| 240 |
|
| 241 |
-
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
-
transformer = HunyuanVideoTransformer3DModelPacked.from_pretrained(
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
vae.eval()
|
| 249 |
text_encoder.eval()
|
|
@@ -256,7 +288,7 @@ def load_models():
|
|
| 256 |
vae.enable_tiling()
|
| 257 |
|
| 258 |
transformer.high_quality_fp32_output_for_inference = True
|
| 259 |
-
print(
|
| 260 |
|
| 261 |
if not cpu_fallback_mode:
|
| 262 |
transformer.to(dtype=transformer_dtype)
|
|
@@ -274,7 +306,6 @@ def load_models():
|
|
| 274 |
if torch.cuda.is_available() and not cpu_fallback_mode:
|
| 275 |
try:
|
| 276 |
if not high_vram:
|
| 277 |
-
# 메모리 최적화
|
| 278 |
DynamicSwapInstaller.install_model(transformer, device=device)
|
| 279 |
DynamicSwapInstaller.install_model(text_encoder, device=device)
|
| 280 |
else:
|
|
@@ -283,10 +314,9 @@ def load_models():
|
|
| 283 |
image_encoder.to(device)
|
| 284 |
vae.to(device)
|
| 285 |
transformer.to(device)
|
| 286 |
-
print(f"
|
| 287 |
except Exception as e:
|
| 288 |
-
print(f"
|
| 289 |
-
print("CPU 모드로 전환")
|
| 290 |
cpu_fallback_mode = True
|
| 291 |
|
| 292 |
models_local = {
|
|
@@ -299,116 +329,156 @@ def load_models():
|
|
| 299 |
'image_encoder': image_encoder,
|
| 300 |
'transformer': transformer
|
| 301 |
}
|
| 302 |
-
|
| 303 |
GPU_INITIALIZED = True
|
| 304 |
models.update(models_local)
|
| 305 |
-
print(f"
|
| 306 |
return models
|
| 307 |
except Exception as e:
|
| 308 |
-
print(f"
|
| 309 |
traceback.print_exc()
|
| 310 |
-
|
| 311 |
-
error_info = {
|
| 312 |
-
"error": str(e),
|
| 313 |
-
"traceback": traceback.format_exc(),
|
| 314 |
-
"cuda_available": torch.cuda.is_available(),
|
| 315 |
-
"device": "cpu" if cpu_fallback_mode else "cuda",
|
| 316 |
-
}
|
| 317 |
-
|
| 318 |
-
try:
|
| 319 |
-
with open(os.path.join(outputs_folder, "error_log.txt"), "w") as f:
|
| 320 |
-
f.write(str(error_info))
|
| 321 |
-
except:
|
| 322 |
-
pass
|
| 323 |
-
|
| 324 |
cpu_fallback_mode = True
|
| 325 |
return {}
|
| 326 |
|
|
|
|
| 327 |
if IN_HF_SPACE and 'spaces' in globals() and GPU_AVAILABLE:
|
| 328 |
try:
|
| 329 |
@spaces.GPU
|
| 330 |
def initialize_models():
|
| 331 |
-
"""@spaces.GPU 환경에서 모델을 초기화"""
|
| 332 |
global GPU_INITIALIZED
|
| 333 |
try:
|
| 334 |
result = load_models()
|
| 335 |
GPU_INITIALIZED = True
|
| 336 |
return result
|
| 337 |
except Exception as e:
|
| 338 |
-
print(f"@spaces.GPU
|
| 339 |
-
traceback.print_exc()
|
| 340 |
global cpu_fallback_mode
|
| 341 |
cpu_fallback_mode = True
|
| 342 |
return load_models()
|
| 343 |
except Exception as e:
|
| 344 |
-
print(f"spaces.GPU
|
| 345 |
def initialize_models():
|
| 346 |
return load_models()
|
|
|
|
|
|
|
|
|
|
| 347 |
|
| 348 |
def get_models():
|
| 349 |
-
"""
|
| 350 |
-
|
| 351 |
-
|
|
|
|
| 352 |
model_loading_key = "__model_loading__"
|
| 353 |
-
|
| 354 |
if not models:
|
| 355 |
if model_loading_key in globals():
|
| 356 |
-
print("
|
| 357 |
import time
|
| 358 |
start_wait = time.time()
|
| 359 |
-
while not models and model_loading_key in globals():
|
| 360 |
time.sleep(0.5)
|
| 361 |
if time.time() - start_wait > 60:
|
| 362 |
-
print("
|
| 363 |
break
|
| 364 |
-
|
| 365 |
if models:
|
| 366 |
return models
|
| 367 |
-
|
| 368 |
try:
|
| 369 |
globals()[model_loading_key] = True
|
| 370 |
-
|
| 371 |
if IN_HF_SPACE and 'spaces' in globals() and GPU_AVAILABLE and not cpu_fallback_mode:
|
| 372 |
try:
|
| 373 |
-
print("
|
| 374 |
models_local = initialize_models()
|
| 375 |
models.update(models_local)
|
| 376 |
except Exception as e:
|
| 377 |
-
print(f"
|
| 378 |
models_local = load_models()
|
| 379 |
models.update(models_local)
|
| 380 |
else:
|
| 381 |
-
print("모델 직접 로딩 시도")
|
| 382 |
models_local = load_models()
|
| 383 |
models.update(models_local)
|
| 384 |
except Exception as e:
|
| 385 |
-
print(f"
|
| 386 |
-
traceback.print_exc()
|
| 387 |
models.clear()
|
| 388 |
finally:
|
| 389 |
if model_loading_key in globals():
|
| 390 |
del globals()[model_loading_key]
|
| 391 |
-
|
| 392 |
return models
|
| 393 |
|
| 394 |
stream = AsyncStream()
|
| 395 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
@torch.no_grad()
|
| 397 |
-
def worker(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
global last_update_time
|
| 399 |
last_update_time = time.time()
|
| 400 |
-
|
| 401 |
total_second_length = min(total_second_length, 5.0)
|
| 402 |
-
|
| 403 |
try:
|
| 404 |
models_local = get_models()
|
| 405 |
if not models_local:
|
| 406 |
-
error_msg = "
|
| 407 |
print(error_msg)
|
| 408 |
stream.output_queue.push(('error', error_msg))
|
| 409 |
stream.output_queue.push(('end', None))
|
| 410 |
return
|
| 411 |
-
|
| 412 |
text_encoder = models_local['text_encoder']
|
| 413 |
text_encoder_2 = models_local['text_encoder_2']
|
| 414 |
tokenizer = models_local['tokenizer']
|
|
@@ -418,22 +488,22 @@ def worker(input_image, prompt, n_prompt, seed, total_second_length, latent_wind
|
|
| 418 |
image_encoder = models_local['image_encoder']
|
| 419 |
transformer = models_local['transformer']
|
| 420 |
except Exception as e:
|
| 421 |
-
|
| 422 |
-
print(
|
| 423 |
traceback.print_exc()
|
| 424 |
-
stream.output_queue.push(('error',
|
| 425 |
stream.output_queue.push(('end', None))
|
| 426 |
return
|
| 427 |
-
|
| 428 |
-
device = 'cuda' if GPU_AVAILABLE and not cpu_fallback_mode else 'cpu'
|
| 429 |
-
print(f"
|
| 430 |
|
| 431 |
if cpu_fallback_mode:
|
| 432 |
-
print("CPU
|
| 433 |
latent_window_size = min(latent_window_size, 5)
|
| 434 |
steps = min(steps, 15)
|
| 435 |
total_second_length = min(total_second_length, 2.0)
|
| 436 |
-
|
| 437 |
total_latent_sections = (total_second_length * 30) / (latent_window_size * 4)
|
| 438 |
total_latent_sections = int(max(round(total_latent_sections), 1))
|
| 439 |
|
|
@@ -443,6 +513,8 @@ def worker(input_image, prompt, n_prompt, seed, total_second_length, latent_wind
|
|
| 443 |
history_latents = None
|
| 444 |
total_generated_latent_frames = 0
|
| 445 |
|
|
|
|
|
|
|
| 446 |
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Starting ...'))))
|
| 447 |
|
| 448 |
try:
|
|
@@ -452,95 +524,102 @@ def worker(input_image, prompt, n_prompt, seed, total_second_length, latent_wind
|
|
| 452 |
text_encoder, text_encoder_2, image_encoder, vae, transformer
|
| 453 |
)
|
| 454 |
except Exception as e:
|
| 455 |
-
print(f"
|
| 456 |
-
|
| 457 |
-
#
|
| 458 |
last_update_time = time.time()
|
| 459 |
-
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Text encoding
|
| 460 |
|
| 461 |
try:
|
| 462 |
if not high_vram and not cpu_fallback_mode:
|
| 463 |
fake_diffusers_current_device(text_encoder, device)
|
| 464 |
load_model_as_complete(text_encoder_2, target_device=device)
|
| 465 |
|
| 466 |
-
llama_vec, clip_l_pooler = encode_prompt_conds(
|
|
|
|
|
|
|
| 467 |
|
| 468 |
if cfg == 1:
|
| 469 |
-
llama_vec_n, clip_l_pooler_n =
|
|
|
|
|
|
|
|
|
|
| 470 |
else:
|
| 471 |
-
llama_vec_n, clip_l_pooler_n = encode_prompt_conds(
|
|
|
|
|
|
|
| 472 |
|
| 473 |
llama_vec, llama_attention_mask = crop_or_pad_yield_mask(llama_vec, length=512)
|
| 474 |
llama_vec_n, llama_attention_mask_n = crop_or_pad_yield_mask(llama_vec_n, length=512)
|
| 475 |
except Exception as e:
|
| 476 |
-
|
| 477 |
-
print(
|
| 478 |
traceback.print_exc()
|
| 479 |
-
stream.output_queue.push(('error',
|
| 480 |
stream.output_queue.push(('end', None))
|
| 481 |
return
|
| 482 |
|
| 483 |
-
#
|
| 484 |
last_update_time = time.time()
|
| 485 |
-
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Image processing
|
| 486 |
|
| 487 |
try:
|
| 488 |
H, W, C = input_image.shape
|
| 489 |
height, width = find_nearest_bucket(H, W, resolution=640)
|
| 490 |
-
|
| 491 |
if cpu_fallback_mode:
|
| 492 |
height = min(height, 320)
|
| 493 |
width = min(width, 320)
|
| 494 |
-
|
| 495 |
input_image_np = resize_and_center_crop(input_image, target_width=width, target_height=height)
|
| 496 |
Image.fromarray(input_image_np).save(os.path.join(outputs_folder, f'{job_id}.png'))
|
| 497 |
|
| 498 |
input_image_pt = torch.from_numpy(input_image_np).float() / 127.5 - 1
|
| 499 |
input_image_pt = input_image_pt.permute(2, 0, 1)[None, :, None]
|
| 500 |
except Exception as e:
|
| 501 |
-
|
| 502 |
-
print(
|
| 503 |
traceback.print_exc()
|
| 504 |
-
stream.output_queue.push(('error',
|
| 505 |
stream.output_queue.push(('end', None))
|
| 506 |
return
|
| 507 |
|
| 508 |
-
# VAE
|
| 509 |
last_update_time = time.time()
|
| 510 |
-
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'VAE encoding
|
| 511 |
|
| 512 |
try:
|
| 513 |
if not high_vram and not cpu_fallback_mode:
|
| 514 |
load_model_as_complete(vae, target_device=device)
|
| 515 |
-
|
| 516 |
start_latent = vae_encode(input_image_pt, vae)
|
| 517 |
except Exception as e:
|
| 518 |
-
|
| 519 |
-
print(
|
| 520 |
traceback.print_exc()
|
| 521 |
-
stream.output_queue.push(('error',
|
| 522 |
stream.output_queue.push(('end', None))
|
| 523 |
return
|
| 524 |
|
| 525 |
-
# CLIP Vision
|
| 526 |
last_update_time = time.time()
|
| 527 |
-
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'CLIP Vision
|
| 528 |
|
| 529 |
try:
|
| 530 |
if not high_vram and not cpu_fallback_mode:
|
| 531 |
load_model_as_complete(image_encoder, target_device=device)
|
| 532 |
-
|
| 533 |
-
|
|
|
|
| 534 |
image_encoder_last_hidden_state = image_encoder_output.last_hidden_state
|
| 535 |
except Exception as e:
|
| 536 |
-
|
| 537 |
-
print(
|
| 538 |
traceback.print_exc()
|
| 539 |
-
stream.output_queue.push(('error',
|
| 540 |
stream.output_queue.push(('end', None))
|
| 541 |
return
|
| 542 |
|
| 543 |
-
# dtype
|
| 544 |
try:
|
| 545 |
llama_vec = llama_vec.to(transformer.dtype)
|
| 546 |
llama_vec_n = llama_vec_n.to(transformer.dtype)
|
|
@@ -548,67 +627,81 @@ def worker(input_image, prompt, n_prompt, seed, total_second_length, latent_wind
|
|
| 548 |
clip_l_pooler_n = clip_l_pooler_n.to(transformer.dtype)
|
| 549 |
image_encoder_last_hidden_state = image_encoder_last_hidden_state.to(transformer.dtype)
|
| 550 |
except Exception as e:
|
| 551 |
-
|
| 552 |
-
print(
|
| 553 |
traceback.print_exc()
|
| 554 |
-
stream.output_queue.push(('error',
|
| 555 |
stream.output_queue.push(('end', None))
|
| 556 |
return
|
| 557 |
|
| 558 |
-
#
|
| 559 |
last_update_time = time.time()
|
| 560 |
-
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Start sampling
|
| 561 |
|
| 562 |
rnd = torch.Generator("cpu").manual_seed(seed)
|
| 563 |
num_frames = latent_window_size * 4 - 3
|
| 564 |
|
| 565 |
try:
|
| 566 |
-
history_latents = torch.zeros(
|
|
|
|
|
|
|
|
|
|
| 567 |
history_pixels = None
|
| 568 |
total_generated_latent_frames = 0
|
| 569 |
except Exception as e:
|
| 570 |
-
|
| 571 |
-
print(
|
| 572 |
traceback.print_exc()
|
| 573 |
-
stream.output_queue.push(('error',
|
| 574 |
stream.output_queue.push(('end', None))
|
| 575 |
return
|
| 576 |
|
| 577 |
-
latent_paddings = reversed(range(total_latent_sections))
|
| 578 |
if total_latent_sections > 4:
|
|
|
|
| 579 |
latent_paddings = [3] + [2]*(total_latent_sections - 3) + [1, 0]
|
| 580 |
|
| 581 |
for latent_padding in latent_paddings:
|
| 582 |
last_update_time = time.time()
|
| 583 |
-
is_last_section = latent_padding == 0
|
| 584 |
latent_padding_size = latent_padding * latent_window_size
|
| 585 |
|
| 586 |
if stream.input_queue.top() == 'end':
|
| 587 |
-
#
|
| 588 |
if history_pixels is not None and total_generated_latent_frames > 0:
|
| 589 |
try:
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
|
|
|
|
|
|
| 593 |
except Exception as e:
|
| 594 |
-
print(f"
|
| 595 |
-
|
| 596 |
stream.output_queue.push(('end', None))
|
| 597 |
return
|
| 598 |
|
| 599 |
-
print(f
|
| 600 |
|
| 601 |
try:
|
| 602 |
-
indices = torch.arange(
|
| 603 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 604 |
clean_latent_indices = torch.cat([clean_latent_indices_pre, clean_latent_indices_post], dim=1)
|
| 605 |
|
| 606 |
clean_latents_pre = start_latent.to(history_latents)
|
| 607 |
-
clean_latents_post, clean_latents_2x, clean_latents_4x = history_latents[:, :, :1 + 2 + 16
|
| 608 |
clean_latents = torch.cat([clean_latents_pre, clean_latents_post], dim=2)
|
| 609 |
except Exception as e:
|
| 610 |
-
|
| 611 |
-
print(
|
| 612 |
traceback.print_exc()
|
| 613 |
if last_output_filename:
|
| 614 |
stream.output_queue.push(('file', last_output_filename))
|
|
@@ -617,15 +710,17 @@ def worker(input_image, prompt, n_prompt, seed, total_second_length, latent_wind
|
|
| 617 |
if not high_vram and not cpu_fallback_mode:
|
| 618 |
try:
|
| 619 |
unload_complete_models()
|
| 620 |
-
move_model_to_device_with_memory_preservation(
|
|
|
|
|
|
|
| 621 |
except Exception as e:
|
| 622 |
-
print(f"transformer GPU
|
| 623 |
|
| 624 |
if use_teacache and not cpu_fallback_mode:
|
| 625 |
try:
|
| 626 |
transformer.initialize_teacache(enable_teacache=True, num_steps=steps)
|
| 627 |
except Exception as e:
|
| 628 |
-
print(f"
|
| 629 |
transformer.initialize_teacache(enable_teacache=False)
|
| 630 |
else:
|
| 631 |
transformer.initialize_teacache(enable_teacache=False)
|
|
@@ -633,33 +728,31 @@ def worker(input_image, prompt, n_prompt, seed, total_second_length, latent_wind
|
|
| 633 |
def callback(d):
|
| 634 |
global last_update_time
|
| 635 |
last_update_time = time.time()
|
| 636 |
-
|
| 637 |
try:
|
| 638 |
if stream.input_queue.top() == 'end':
|
| 639 |
stream.output_queue.push(('end', None))
|
| 640 |
-
raise KeyboardInterrupt('
|
| 641 |
-
|
| 642 |
preview = d['denoised']
|
| 643 |
preview = vae_decode_fake(preview)
|
| 644 |
-
|
| 645 |
-
preview = (preview * 255.0).detach().cpu().numpy().clip(0, 255).astype(np.uint8)
|
| 646 |
preview = einops.rearrange(preview, 'b c t h w -> (b h) (t w) c')
|
| 647 |
|
| 648 |
-
|
| 649 |
-
percentage = int(100.0 *
|
| 650 |
-
hint = f'Sampling {
|
| 651 |
-
desc = f'Total
|
| 652 |
-
|
|
|
|
| 653 |
except KeyboardInterrupt:
|
| 654 |
raise
|
| 655 |
except Exception as e:
|
| 656 |
-
print(f"
|
| 657 |
return
|
| 658 |
|
| 659 |
try:
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
try:
|
| 664 |
generated_latents = sample_hunyuan(
|
| 665 |
transformer=transformer,
|
|
@@ -688,144 +781,134 @@ def worker(input_image, prompt, n_prompt, seed, total_second_length, latent_wind
|
|
| 688 |
clean_latent_2x_indices=clean_latent_2x_indices,
|
| 689 |
clean_latents_4x=clean_latents_4x,
|
| 690 |
clean_latent_4x_indices=clean_latent_4x_indices,
|
| 691 |
-
callback=callback
|
| 692 |
)
|
| 693 |
-
|
| 694 |
-
print(f"샘플링 완료. 소요 시간: {time.time() - sampling_start_time:.2f} 초")
|
| 695 |
except KeyboardInterrupt as e:
|
| 696 |
-
print(f"
|
| 697 |
if last_output_filename:
|
| 698 |
stream.output_queue.push(('file', last_output_filename))
|
| 699 |
-
|
| 700 |
else:
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
stream.output_queue.push(('error', error_msg))
|
| 704 |
stream.output_queue.push(('end', None))
|
| 705 |
return
|
| 706 |
except Exception as e:
|
| 707 |
-
print(f"
|
| 708 |
traceback.print_exc()
|
| 709 |
if last_output_filename:
|
| 710 |
stream.output_queue.push(('file', last_output_filename))
|
| 711 |
-
|
| 712 |
-
stream.output_queue.push(('error',
|
| 713 |
else:
|
| 714 |
-
|
| 715 |
-
stream.output_queue.push(('error',
|
| 716 |
stream.output_queue.push(('end', None))
|
| 717 |
return
|
| 718 |
|
| 719 |
try:
|
| 720 |
if is_last_section:
|
| 721 |
generated_latents = torch.cat([start_latent.to(generated_latents), generated_latents], dim=2)
|
| 722 |
-
|
| 723 |
total_generated_latent_frames += int(generated_latents.shape[2])
|
| 724 |
history_latents = torch.cat([generated_latents.to(history_latents), history_latents], dim=2)
|
| 725 |
except Exception as e:
|
| 726 |
-
|
| 727 |
-
print(
|
| 728 |
traceback.print_exc()
|
| 729 |
if last_output_filename:
|
| 730 |
stream.output_queue.push(('file', last_output_filename))
|
| 731 |
-
stream.output_queue.push(('error',
|
| 732 |
stream.output_queue.push(('end', None))
|
| 733 |
return
|
| 734 |
|
| 735 |
if not high_vram and not cpu_fallback_mode:
|
| 736 |
try:
|
| 737 |
-
offload_model_from_device_for_memory_preservation(
|
|
|
|
|
|
|
| 738 |
load_model_as_complete(vae, target_device=device)
|
| 739 |
except Exception as e:
|
| 740 |
-
print(f"
|
| 741 |
|
| 742 |
try:
|
| 743 |
-
real_history_latents = history_latents[:, :, :total_generated_latent_frames
|
| 744 |
except Exception as e:
|
| 745 |
-
|
| 746 |
-
print(
|
| 747 |
if last_output_filename:
|
| 748 |
stream.output_queue.push(('file', last_output_filename))
|
| 749 |
continue
|
| 750 |
|
| 751 |
try:
|
| 752 |
-
|
| 753 |
-
print(f"VAE 디코딩 시작, 잠재 변수 크기: {real_history_latents.shape}")
|
| 754 |
-
|
| 755 |
if history_pixels is None:
|
| 756 |
history_pixels = vae_decode(real_history_latents, vae).cpu()
|
| 757 |
else:
|
| 758 |
-
|
|
|
|
|
|
|
|
|
|
| 759 |
overlapped_frames = latent_window_size * 4 - 3
|
| 760 |
-
|
| 761 |
current_pixels = vae_decode(real_history_latents[:, :, :section_latent_frames], vae).cpu()
|
| 762 |
history_pixels = soft_append_bcthw(current_pixels, history_pixels, overlapped_frames)
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
try:
|
| 768 |
-
unload_complete_models()
|
| 769 |
-
except Exception as e:
|
| 770 |
-
print(f"모델 언로드 중 오류: {e}")
|
| 771 |
-
|
| 772 |
-
output_filename = os.path.join(outputs_folder, f'{job_id}_{total_generated_latent_frames}.mp4')
|
| 773 |
-
|
| 774 |
-
save_start_time = time.time()
|
| 775 |
save_bcthw_as_mp4(history_pixels, output_filename, fps=30)
|
| 776 |
-
print(f"비디오 저장 완료, 소요 시간: {time.time() - save_start_time:.2f} 초")
|
| 777 |
-
|
| 778 |
-
print(f'디코딩 완료. 현재 latent 크기: {real_history_latents.shape}, pixel 크기: {history_pixels.shape}')
|
| 779 |
-
|
| 780 |
last_output_filename = output_filename
|
| 781 |
stream.output_queue.push(('file', output_filename))
|
| 782 |
except Exception as e:
|
| 783 |
-
print(f"
|
| 784 |
traceback.print_exc()
|
| 785 |
if last_output_filename:
|
| 786 |
stream.output_queue.push(('file', last_output_filename))
|
| 787 |
-
|
| 788 |
-
stream.output_queue.push(('error',
|
| 789 |
continue
|
| 790 |
|
| 791 |
if is_last_section:
|
| 792 |
break
|
| 793 |
except Exception as e:
|
| 794 |
-
print(f"
|
| 795 |
traceback.print_exc()
|
| 796 |
-
|
| 797 |
-
if isinstance(e, KeyboardInterrupt):
|
| 798 |
-
print("KeyboardInterrupt 발생")
|
| 799 |
-
|
| 800 |
if not high_vram and not cpu_fallback_mode:
|
| 801 |
try:
|
| 802 |
unload_complete_models(
|
| 803 |
text_encoder, text_encoder_2, image_encoder, vae, transformer
|
| 804 |
)
|
| 805 |
-
except Exception as
|
| 806 |
-
print(f"
|
| 807 |
-
|
| 808 |
if last_output_filename:
|
| 809 |
stream.output_queue.push(('file', last_output_filename))
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
stream.output_queue.push(('error', error_msg))
|
| 813 |
|
| 814 |
-
print("
|
| 815 |
stream.output_queue.push(('end', None))
|
| 816 |
-
return
|
| 817 |
|
|
|
|
| 818 |
if IN_HF_SPACE and 'spaces' in globals():
|
| 819 |
@spaces.GPU
|
| 820 |
-
def process_with_gpu(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 821 |
global stream
|
| 822 |
-
assert input_image is not None,
|
| 823 |
-
|
| 824 |
-
yield None, None, '', '', gr.update(interactive=False), gr.update(interactive=True)
|
| 825 |
|
|
|
|
|
|
|
| 826 |
try:
|
| 827 |
stream = AsyncStream()
|
| 828 |
-
async_run(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 829 |
|
| 830 |
output_filename = None
|
| 831 |
prev_output_filename = None
|
|
@@ -834,61 +917,73 @@ if IN_HF_SPACE and 'spaces' in globals():
|
|
| 834 |
while True:
|
| 835 |
try:
|
| 836 |
flag, data = stream.output_queue.next()
|
| 837 |
-
|
| 838 |
if flag == 'file':
|
| 839 |
output_filename = data
|
| 840 |
prev_output_filename = output_filename
|
| 841 |
yield output_filename, gr.update(), gr.update(), '', gr.update(interactive=False), gr.update(interactive=True)
|
| 842 |
-
|
| 843 |
-
if flag == 'progress':
|
| 844 |
preview, desc, html = data
|
| 845 |
yield gr.update(), gr.update(visible=True, value=preview), desc, html, gr.update(interactive=False), gr.update(interactive=True)
|
| 846 |
-
|
| 847 |
-
if flag == 'error':
|
| 848 |
error_message = data
|
| 849 |
-
print(f"
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
if output_filename is None and prev_output_filename is not None:
|
| 853 |
output_filename = prev_output_filename
|
| 854 |
-
|
| 855 |
if error_message:
|
| 856 |
-
|
| 857 |
-
yield
|
|
|
|
|
|
|
|
|
|
| 858 |
else:
|
| 859 |
-
yield
|
|
|
|
|
|
|
|
|
|
| 860 |
break
|
| 861 |
except Exception as e:
|
| 862 |
-
print(f"
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
print(f"처리가 {current_time - last_update_time:.1f}초 동안 정지됨. 타임��웃으로 간주.")
|
| 866 |
if prev_output_filename:
|
| 867 |
-
|
| 868 |
-
yield
|
|
|
|
|
|
|
|
|
|
| 869 |
else:
|
| 870 |
-
|
| 871 |
-
yield
|
|
|
|
|
|
|
|
|
|
| 872 |
break
|
| 873 |
except Exception as e:
|
| 874 |
-
print(f"
|
| 875 |
traceback.print_exc()
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
yield None, gr.update(visible=False), gr.update(), error_html, gr.update(interactive=True), gr.update(interactive=False)
|
| 880 |
-
|
| 881 |
process = process_with_gpu
|
| 882 |
else:
|
| 883 |
-
def process(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 884 |
global stream
|
| 885 |
-
assert input_image is not None,
|
| 886 |
-
|
| 887 |
-
yield None, None, '', '', gr.update(interactive=False), gr.update(interactive=True)
|
| 888 |
|
|
|
|
| 889 |
try:
|
| 890 |
stream = AsyncStream()
|
| 891 |
-
async_run(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 892 |
|
| 893 |
output_filename = None
|
| 894 |
prev_output_filename = None
|
|
@@ -897,561 +992,314 @@ else:
|
|
| 897 |
while True:
|
| 898 |
try:
|
| 899 |
flag, data = stream.output_queue.next()
|
| 900 |
-
|
| 901 |
if flag == 'file':
|
| 902 |
output_filename = data
|
| 903 |
prev_output_filename = output_filename
|
| 904 |
yield output_filename, gr.update(), gr.update(), '', gr.update(interactive=False), gr.update(interactive=True)
|
| 905 |
-
|
| 906 |
-
if flag == 'progress':
|
| 907 |
preview, desc, html = data
|
| 908 |
yield gr.update(), gr.update(visible=True, value=preview), desc, html, gr.update(interactive=False), gr.update(interactive=True)
|
| 909 |
-
|
| 910 |
-
if flag == 'error':
|
| 911 |
error_message = data
|
| 912 |
-
print(f"
|
| 913 |
-
|
| 914 |
-
|
| 915 |
-
if output_filename is None and prev_output_filename is not None:
|
| 916 |
output_filename = prev_output_filename
|
| 917 |
-
|
| 918 |
if error_message:
|
| 919 |
-
|
| 920 |
-
yield
|
|
|
|
|
|
|
|
|
|
| 921 |
else:
|
| 922 |
-
yield
|
|
|
|
|
|
|
|
|
|
| 923 |
break
|
| 924 |
except Exception as e:
|
| 925 |
-
print(f"
|
| 926 |
-
|
| 927 |
-
|
| 928 |
-
print(f"{current_time - last_update_time:.1f}초 동안 진행이 없어 타임아웃으로 간주합니다.")
|
| 929 |
if prev_output_filename:
|
| 930 |
-
|
| 931 |
-
yield
|
|
|
|
|
|
|
|
|
|
| 932 |
else:
|
| 933 |
-
|
| 934 |
-
yield
|
|
|
|
|
|
|
|
|
|
| 935 |
break
|
| 936 |
except Exception as e:
|
| 937 |
-
print(f"
|
| 938 |
traceback.print_exc()
|
| 939 |
-
|
| 940 |
-
|
| 941 |
-
error_html = create_error_html(error_msg)
|
| 942 |
-
yield None, gr.update(visible=False), gr.update(), error_html, gr.update(interactive=True), gr.update(interactive=False)
|
| 943 |
|
| 944 |
def end_process():
|
| 945 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 946 |
if 'stream' in globals() and stream is not None:
|
| 947 |
try:
|
| 948 |
-
|
| 949 |
-
print(f"
|
| 950 |
except Exception as e:
|
| 951 |
-
print(f"
|
| 952 |
try:
|
| 953 |
stream.input_queue.push('end')
|
| 954 |
-
print("end
|
| 955 |
-
try:
|
| 956 |
-
current_top_after = stream.input_queue.top()
|
| 957 |
-
print(f"신호 전송 후 입력 큐 top: {current_top_after}")
|
| 958 |
-
except Exception as e:
|
| 959 |
-
print(f"신호 전송 후 큐 상태 확인 오류: {e}")
|
| 960 |
except Exception as e:
|
| 961 |
-
print(f"
|
| 962 |
else:
|
| 963 |
-
print("
|
| 964 |
return None
|
| 965 |
|
|
|
|
| 966 |
quick_prompts = [
|
| 967 |
-
|
| 968 |
-
|
| 969 |
]
|
| 970 |
-
quick_prompts = [[x] for x in quick_prompts]
|
| 971 |
|
|
|
|
| 972 |
def make_custom_css():
|
| 973 |
-
|
| 974 |
-
|
| 975 |
-
|
| 976 |
-
|
| 977 |
-
|
|
|
|
|
|
|
| 978 |
#app-container {
|
| 979 |
-
max-width:
|
| 980 |
margin: 0 auto;
|
|
|
|
|
|
|
| 981 |
}
|
| 982 |
-
|
| 983 |
-
|
| 984 |
-
|
| 985 |
-
|
| 986 |
-
right: 10px;
|
| 987 |
-
z-index: 1000;
|
| 988 |
-
background-color: rgba(0, 0, 0, 0.7);
|
| 989 |
-
color: white;
|
| 990 |
-
border: none;
|
| 991 |
-
border-radius: 4px;
|
| 992 |
-
padding: 5px 10px;
|
| 993 |
-
cursor: pointer;
|
| 994 |
-
font-size: 14px;
|
| 995 |
}
|
| 996 |
-
|
| 997 |
-
|
| 998 |
-
|
| 999 |
-
|
| 1000 |
-
|
|
|
|
| 1001 |
}
|
| 1002 |
-
|
| 1003 |
-
.start-btn, .stop-btn {
|
| 1004 |
min-height: 45px;
|
| 1005 |
font-size: 1rem;
|
|
|
|
| 1006 |
}
|
| 1007 |
-
|
| 1008 |
-
|
| 1009 |
-
|
| 1010 |
-
font-size: 1.5rem;
|
| 1011 |
-
margin-bottom: 0.5rem;
|
| 1012 |
-
}
|
| 1013 |
-
|
| 1014 |
-
.mobile-full-width {
|
| 1015 |
-
flex-direction: column !important;
|
| 1016 |
-
}
|
| 1017 |
-
|
| 1018 |
-
.mobile-full-width > .gr-block {
|
| 1019 |
-
min-width: 100% !important;
|
| 1020 |
-
flex-grow: 1;
|
| 1021 |
-
}
|
| 1022 |
-
|
| 1023 |
-
.video-container {
|
| 1024 |
-
height: auto !important;
|
| 1025 |
-
}
|
| 1026 |
-
|
| 1027 |
-
.button-container button {
|
| 1028 |
-
min-height: 50px;
|
| 1029 |
-
font-size: 1rem;
|
| 1030 |
-
touch-action: manipulation;
|
| 1031 |
-
}
|
| 1032 |
-
|
| 1033 |
-
.slider-container input[type="range"] {
|
| 1034 |
-
height: 30px;
|
| 1035 |
-
}
|
| 1036 |
}
|
| 1037 |
-
|
| 1038 |
-
|
| 1039 |
-
|
| 1040 |
-
width: 48% !important;
|
| 1041 |
-
}
|
| 1042 |
}
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
.dark-mode-text {
|
| 1046 |
-
color: #f0f0f0;
|
| 1047 |
-
}
|
| 1048 |
-
.dark-mode-bg {
|
| 1049 |
-
background-color: #2a2a2a;
|
| 1050 |
-
}
|
| 1051 |
-
}
|
| 1052 |
-
|
| 1053 |
-
button, input, select, textarea {
|
| 1054 |
-
font-size: 16px;
|
| 1055 |
-
}
|
| 1056 |
-
|
| 1057 |
-
button, .interactive-element {
|
| 1058 |
-
min-height: 44px;
|
| 1059 |
-
min-width: 44px;
|
| 1060 |
}
|
| 1061 |
-
|
| 1062 |
-
|
| 1063 |
-
|
| 1064 |
-
|
| 1065 |
}
|
| 1066 |
-
|
| 1067 |
.progress-container {
|
| 1068 |
-
margin-top:
|
| 1069 |
-
margin-bottom:
|
| 1070 |
}
|
| 1071 |
-
|
| 1072 |
-
#error-message {
|
| 1073 |
-
color: #ff4444;
|
| 1074 |
-
font-weight: bold;
|
| 1075 |
-
padding: 10px;
|
| 1076 |
-
border-radius: 4px;
|
| 1077 |
-
margin-top: 10px;
|
| 1078 |
-
}
|
| 1079 |
-
|
| 1080 |
.error-message {
|
| 1081 |
-
background-color:
|
|
|
|
|
|
|
| 1082 |
padding: 10px;
|
| 1083 |
border-radius: 4px;
|
| 1084 |
margin-top: 10px;
|
| 1085 |
-
border: 1px solid #ffcccc;
|
| 1086 |
}
|
| 1087 |
-
|
| 1088 |
-
.error-msg-en, .error-msg-ko {
|
| 1089 |
-
font-weight: bold;
|
| 1090 |
-
}
|
| 1091 |
-
|
| 1092 |
.error-icon {
|
| 1093 |
-
color: #
|
| 1094 |
-
font-size: 18px;
|
| 1095 |
margin-right: 8px;
|
| 1096 |
}
|
| 1097 |
-
|
| 1098 |
-
|
| 1099 |
-
|
| 1100 |
-
|
| 1101 |
-
|
| 1102 |
-
margin:
|
| 1103 |
}
|
| 1104 |
-
|
| 1105 |
-
|
| 1106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1107 |
}
|
| 1108 |
"""
|
| 1109 |
-
|
| 1110 |
-
return progress_bar_css + responsive_css
|
| 1111 |
|
| 1112 |
css = make_custom_css()
|
|
|
|
|
|
|
| 1113 |
block = gr.Blocks(css=css).queue()
|
| 1114 |
with block:
|
| 1115 |
-
|
| 1116 |
-
|
| 1117 |
-
|
| 1118 |
-
</div>
|
| 1119 |
-
<script>
|
| 1120 |
-
window.currentLang = "en";
|
| 1121 |
-
function toggleLanguage() {
|
| 1122 |
-
window.currentLang = (window.currentLang === "en") ? "ko" : "en";
|
| 1123 |
-
|
| 1124 |
-
const elements = document.querySelectorAll('[data-i18n]');
|
| 1125 |
-
elements.forEach(el => {
|
| 1126 |
-
const key = el.getAttribute('data-i18n');
|
| 1127 |
-
const translations = {
|
| 1128 |
-
"en": {
|
| 1129 |
-
"title": "FramePack - Image to Video Generation",
|
| 1130 |
-
"upload_image": "Upload Image",
|
| 1131 |
-
"prompt": "Prompt",
|
| 1132 |
-
"quick_prompts": "Quick Prompts",
|
| 1133 |
-
"start_generation": "Generate",
|
| 1134 |
-
"stop_generation": "Stop",
|
| 1135 |
-
"use_teacache": "Use TeaCache",
|
| 1136 |
-
"teacache_info": "Faster speed, but may result in slightly worse finger and hand generation.",
|
| 1137 |
-
"negative_prompt": "Negative Prompt",
|
| 1138 |
-
"seed": "Seed",
|
| 1139 |
-
"video_length": "Video Length (max 5 seconds)",
|
| 1140 |
-
"latent_window": "Latent Window Size",
|
| 1141 |
-
"steps": "Inference Steps",
|
| 1142 |
-
"steps_info": "Changing this value is not recommended.",
|
| 1143 |
-
"cfg_scale": "CFG Scale",
|
| 1144 |
-
"distilled_cfg": "Distilled CFG Scale",
|
| 1145 |
-
"distilled_cfg_info": "Changing this value is not recommended.",
|
| 1146 |
-
"cfg_rescale": "CFG Rescale",
|
| 1147 |
-
"gpu_memory": "GPU Memory Preservation (GB) (larger means slower)",
|
| 1148 |
-
"gpu_memory_info": "Set this to a larger value if you encounter OOM errors. Larger values cause slower speed.",
|
| 1149 |
-
"next_latents": "Next Latents",
|
| 1150 |
-
"generated_video": "Generated Video",
|
| 1151 |
-
"sampling_note": "Note: Due to reversed sampling, ending actions will be generated before starting actions. If the starting action is not in the video, please wait, it will be generated later.",
|
| 1152 |
-
"error_message": "Error"
|
| 1153 |
-
},
|
| 1154 |
-
"ko": {
|
| 1155 |
-
"title": "FramePack - 이미지에서 동영상 생성",
|
| 1156 |
-
"upload_image": "이미지 업로드",
|
| 1157 |
-
"prompt": "프롬프트",
|
| 1158 |
-
"quick_prompts": "빠른 프롬프트 목록",
|
| 1159 |
-
"start_generation": "생성 시작",
|
| 1160 |
-
"stop_generation": "생성 중지",
|
| 1161 |
-
"use_teacache": "TeaCache 사용",
|
| 1162 |
-
"teacache_info": "더 빠른 속도를 제공하지만 손가락이나 손 생성 품질이 약간 떨어질 수 있습니다.",
|
| 1163 |
-
"negative_prompt": "부정 프롬프트",
|
| 1164 |
-
"seed": "랜덤 시드",
|
| 1165 |
-
"video_length": "동영상 길이 (최대 5초)",
|
| 1166 |
-
"latent_window": "잠재 윈도우 크기",
|
| 1167 |
-
"steps": "추론 스텝 수",
|
| 1168 |
-
"steps_info": "이 값을 변경하는 것은 권장되지 않습니다.",
|
| 1169 |
-
"cfg_scale": "CFG 스케일",
|
| 1170 |
-
"distilled_cfg": "증류된 CFG 스케일",
|
| 1171 |
-
"distilled_cfg_info": "이 값을 변경하는 것은 권장되지 않습니다.",
|
| 1172 |
-
"cfg_rescale": "CFG 재스케일",
|
| 1173 |
-
"gpu_memory": "GPU 메모리 보존 (GB) (값이 클수록 속도가 느려짐)",
|
| 1174 |
-
"gpu_memory_info": "OOM 오류가 발생하면 이 값을 더 크게 설정하십시오. 값이 클수록 속도가 느려집니다.",
|
| 1175 |
-
"next_latents": "다음 잠재 변수",
|
| 1176 |
-
"generated_video": "생성된 동영상",
|
| 1177 |
-
"sampling_note": "주의: 역순 샘플링 때문에, 종료 동작이 시작 동작보다 먼저 생성됩니다. 시작 동작이 나타나지 않으면 기다려 주십시오.",
|
| 1178 |
-
"error_message": "오류 메시지"
|
| 1179 |
-
}
|
| 1180 |
-
};
|
| 1181 |
-
|
| 1182 |
-
if (translations[window.currentLang] && translations[window.currentLang][key]) {
|
| 1183 |
-
if (el.tagName === 'BUTTON') {
|
| 1184 |
-
el.textContent = translations[window.currentLang][key];
|
| 1185 |
-
} else if (el.tagName === 'LABEL') {
|
| 1186 |
-
el.textContent = translations[window.currentLang][key];
|
| 1187 |
-
} else {
|
| 1188 |
-
el.innerHTML = translations[window.currentLang][key];
|
| 1189 |
-
}
|
| 1190 |
-
}
|
| 1191 |
-
});
|
| 1192 |
-
|
| 1193 |
-
// bilingual-label 처리
|
| 1194 |
-
document.querySelectorAll('.bilingual-label').forEach(el => {
|
| 1195 |
-
const enText = el.getAttribute('data-en');
|
| 1196 |
-
const koText = el.getAttribute('data-ko');
|
| 1197 |
-
el.textContent = (window.currentLang === 'en') ? enText : koText;
|
| 1198 |
-
});
|
| 1199 |
-
|
| 1200 |
-
// data-lang 처리
|
| 1201 |
-
document.querySelectorAll('[data-lang]').forEach(el => {
|
| 1202 |
-
el.style.display = (el.getAttribute('data-lang') === window.currentLang) ? 'block' : 'none';
|
| 1203 |
-
});
|
| 1204 |
-
}
|
| 1205 |
-
|
| 1206 |
-
document.addEventListener('DOMContentLoaded', function() {
|
| 1207 |
-
setTimeout(() => {
|
| 1208 |
-
// 매핑
|
| 1209 |
-
const labelMap = {
|
| 1210 |
-
"Upload Image": "upload_image",
|
| 1211 |
-
"이미지 업로드": "upload_image",
|
| 1212 |
-
"Prompt": "prompt",
|
| 1213 |
-
"프롬프트": "prompt",
|
| 1214 |
-
"Quick Prompts": "quick_prompts",
|
| 1215 |
-
"빠른 프롬프트 목록": "quick_prompts",
|
| 1216 |
-
"Generate": "start_generation",
|
| 1217 |
-
"생성 시작": "start_generation",
|
| 1218 |
-
"Stop": "stop_generation",
|
| 1219 |
-
"생성 중지": "stop_generation"
|
| 1220 |
-
};
|
| 1221 |
-
|
| 1222 |
-
document.querySelectorAll('label, span, button').forEach(el => {
|
| 1223 |
-
const text = el.textContent.trim();
|
| 1224 |
-
if (labelMap[text]) {
|
| 1225 |
-
el.setAttribute('data-i18n', labelMap[text]);
|
| 1226 |
-
}
|
| 1227 |
-
});
|
| 1228 |
-
|
| 1229 |
-
const titleEl = document.querySelector('h1');
|
| 1230 |
-
if (titleEl) titleEl.setAttribute('data-i18n', 'title');
|
| 1231 |
-
|
| 1232 |
-
toggleLanguage();
|
| 1233 |
-
}, 1000);
|
| 1234 |
-
});
|
| 1235 |
-
</script>
|
| 1236 |
-
""")
|
| 1237 |
-
|
| 1238 |
-
gr.HTML("<h1 data-i18n='title'>FramePack - Image to Video Generation</h1>")
|
| 1239 |
-
|
| 1240 |
with gr.Row(elem_classes="mobile-full-width"):
|
| 1241 |
-
with gr.Column(scale=1, elem_classes="
|
| 1242 |
input_image = gr.Image(
|
| 1243 |
-
|
| 1244 |
-
|
| 1245 |
-
|
| 1246 |
elem_id="input-image",
|
| 1247 |
height=320
|
| 1248 |
)
|
| 1249 |
-
|
| 1250 |
-
|
| 1251 |
-
label="Prompt",
|
| 1252 |
-
value='',
|
| 1253 |
-
elem_id="prompt-input"
|
| 1254 |
-
)
|
| 1255 |
-
|
| 1256 |
example_quick_prompts = gr.Dataset(
|
| 1257 |
-
samples=quick_prompts,
|
| 1258 |
-
label=
|
| 1259 |
-
samples_per_page=1000,
|
| 1260 |
components=[prompt]
|
| 1261 |
)
|
| 1262 |
example_quick_prompts.click(
|
| 1263 |
-
lambda x: x[0],
|
| 1264 |
-
inputs=[example_quick_prompts],
|
| 1265 |
-
outputs=prompt,
|
| 1266 |
-
show_progress=False,
|
| 1267 |
queue=False
|
| 1268 |
)
|
| 1269 |
-
|
| 1270 |
with gr.Row(elem_classes="button-container"):
|
| 1271 |
start_button = gr.Button(
|
| 1272 |
-
value="Generate",
|
| 1273 |
-
elem_classes="start-btn",
|
| 1274 |
elem_id="start-button",
|
| 1275 |
variant="primary"
|
| 1276 |
)
|
| 1277 |
-
|
| 1278 |
end_button = gr.Button(
|
| 1279 |
-
value="Stop",
|
| 1280 |
-
elem_classes="stop-btn",
|
| 1281 |
elem_id="stop-button",
|
| 1282 |
interactive=False
|
| 1283 |
)
|
| 1284 |
-
|
| 1285 |
-
with gr.Group():
|
| 1286 |
-
use_teacache = gr.Checkbox(
|
| 1287 |
-
label='Use TeaCache',
|
| 1288 |
-
value=True,
|
| 1289 |
-
info='Faster speed, but may result in slightly worse finger and hand generation.'
|
| 1290 |
-
)
|
| 1291 |
-
|
| 1292 |
-
n_prompt = gr.Textbox(label="Negative Prompt", value="", visible=False)
|
| 1293 |
-
|
| 1294 |
-
seed = gr.Number(
|
| 1295 |
-
label="Seed",
|
| 1296 |
-
value=31337,
|
| 1297 |
-
precision=0
|
| 1298 |
-
)
|
| 1299 |
-
|
| 1300 |
-
with gr.Group(elem_classes="slider-container"):
|
| 1301 |
-
total_second_length = gr.Slider(
|
| 1302 |
-
label="Video Length (max 5 seconds)",
|
| 1303 |
-
minimum=1,
|
| 1304 |
-
maximum=5,
|
| 1305 |
-
value=5,
|
| 1306 |
-
step=0.1
|
| 1307 |
-
)
|
| 1308 |
-
|
| 1309 |
-
latent_window_size = gr.Slider(
|
| 1310 |
-
label="Latent Window Size",
|
| 1311 |
-
minimum=1,
|
| 1312 |
-
maximum=33,
|
| 1313 |
-
value=9,
|
| 1314 |
-
step=1,
|
| 1315 |
-
visible=False
|
| 1316 |
-
)
|
| 1317 |
-
|
| 1318 |
-
steps = gr.Slider(
|
| 1319 |
-
label="Inference Steps",
|
| 1320 |
-
minimum=1,
|
| 1321 |
-
maximum=100,
|
| 1322 |
-
value=25,
|
| 1323 |
-
step=1,
|
| 1324 |
-
info='Changing this value is not recommended.'
|
| 1325 |
-
)
|
| 1326 |
-
|
| 1327 |
-
cfg = gr.Slider(
|
| 1328 |
-
label="CFG Scale",
|
| 1329 |
-
minimum=1.0,
|
| 1330 |
-
maximum=32.0,
|
| 1331 |
-
value=1.0,
|
| 1332 |
-
step=0.01,
|
| 1333 |
-
visible=False
|
| 1334 |
-
)
|
| 1335 |
-
|
| 1336 |
-
gs = gr.Slider(
|
| 1337 |
-
label="Distilled CFG Scale",
|
| 1338 |
-
minimum=1.0,
|
| 1339 |
-
maximum=32.0,
|
| 1340 |
-
value=10.0,
|
| 1341 |
-
step=0.01,
|
| 1342 |
-
info='Changing this value is not recommended.'
|
| 1343 |
-
)
|
| 1344 |
-
|
| 1345 |
-
rs = gr.Slider(
|
| 1346 |
-
label="CFG Rescale",
|
| 1347 |
-
minimum=0.0,
|
| 1348 |
-
maximum=1.0,
|
| 1349 |
-
value=0.0,
|
| 1350 |
-
step=0.01,
|
| 1351 |
-
visible=False
|
| 1352 |
-
)
|
| 1353 |
-
|
| 1354 |
-
gpu_memory_preservation = gr.Slider(
|
| 1355 |
-
label="GPU Memory (GB)",
|
| 1356 |
-
minimum=6,
|
| 1357 |
-
maximum=128,
|
| 1358 |
-
value=6,
|
| 1359 |
-
step=0.1,
|
| 1360 |
-
info="Set this to a larger value if you encounter OOM errors. Larger values cause slower speed."
|
| 1361 |
-
)
|
| 1362 |
-
|
| 1363 |
-
with gr.Column(scale=1, elem_classes="mobile-full-width"):
|
| 1364 |
-
preview_image = gr.Image(
|
| 1365 |
-
label="Preview",
|
| 1366 |
-
height=200,
|
| 1367 |
-
visible=False,
|
| 1368 |
-
elem_classes="preview-container"
|
| 1369 |
-
)
|
| 1370 |
|
| 1371 |
result_video = gr.Video(
|
| 1372 |
-
label="Generated Video",
|
| 1373 |
-
autoplay=True,
|
| 1374 |
-
show_share_button=True,
|
| 1375 |
-
height=512,
|
| 1376 |
loop=True,
|
|
|
|
| 1377 |
elem_classes="video-container",
|
| 1378 |
elem_id="result-video"
|
| 1379 |
)
|
| 1380 |
-
|
| 1381 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1382 |
|
| 1383 |
with gr.Group(elem_classes="progress-container"):
|
| 1384 |
-
progress_desc = gr.Markdown(''
|
| 1385 |
-
progress_bar = gr.HTML(''
|
| 1386 |
|
| 1387 |
error_message = gr.HTML('', elem_id='error-message', visible=True)
|
| 1388 |
-
|
| 1389 |
-
ips = [input_image, prompt, n_prompt, seed, total_second_length, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache]
|
| 1390 |
|
| 1391 |
-
|
| 1392 |
-
|
| 1393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1394 |
end_button.click(fn=end_process)
|
| 1395 |
|
| 1396 |
block.launch()
|
| 1397 |
-
|
| 1398 |
-
def create_error_html(error_msg, is_timeout=False):
|
| 1399 |
-
en_msg = ""
|
| 1400 |
-
ko_msg = ""
|
| 1401 |
-
|
| 1402 |
-
if is_timeout:
|
| 1403 |
-
if "부분" in error_msg or "partial" in error_msg:
|
| 1404 |
-
en_msg = "Processing timed out, but partial video has been generated."
|
| 1405 |
-
ko_msg = "처리 시간이 초과되었지만 일부 동영상이 생성되었습니다."
|
| 1406 |
-
else:
|
| 1407 |
-
en_msg = f"Processing timed out: {error_msg}"
|
| 1408 |
-
ko_msg = f"처리 시간 초과: {error_msg}"
|
| 1409 |
-
elif "모델 로드" in error_msg:
|
| 1410 |
-
en_msg = "Failed to load models. Possibly heavy traffic or GPU problem."
|
| 1411 |
-
ko_msg = "모델 로드에 실패했습니다. 과도한 트래픽 또는 GPU 문제일 수 있습니다."
|
| 1412 |
-
elif "GPU" in error_msg or "CUDA" in error_msg or "memory" in error_msg or "메모리" in error_msg:
|
| 1413 |
-
en_msg = "GPU memory insufficient or error. Increase GPU memory preservation or reduce video length."
|
| 1414 |
-
ko_msg = "GPU 메모리가 부족하거나 오류가 발생했습니다. GPU 메모리 보존 값을 늘리거나 동영상 길이를 줄여보세요."
|
| 1415 |
-
elif "샘플링 중 오류" in error_msg or "sampling process" in error_msg:
|
| 1416 |
-
if "부분" in error_msg or "partial" in error_msg:
|
| 1417 |
-
en_msg = "Error during sampling, but partial video has been generated."
|
| 1418 |
-
ko_msg = "샘플링 중 오류가 발생했지만 일부 동영상이 생성되었습니다."
|
| 1419 |
-
else:
|
| 1420 |
-
en_msg = "Error during sampling. Unable to generate video."
|
| 1421 |
-
ko_msg = "샘플링 중 오류가 발생했습니다. 비디오 생성에 실패했습니다."
|
| 1422 |
-
elif "네트워크" in error_msg or "Network" in error_msg or "ConnectionError" in error_msg or "ReadTimeoutError" in error_msg:
|
| 1423 |
-
en_msg = "Network is unstable, model download timed out. Please try again later."
|
| 1424 |
-
ko_msg = "네트워크가 불안정하여 모델 다운로드가 시간 초과되었습니다. 잠시 후 다시 시도해 주세요."
|
| 1425 |
-
elif "VAE" in error_msg or "디코딩" in error_msg or "decode" in error_msg:
|
| 1426 |
-
en_msg = "Error during video decoding or saving process. Try a different seed."
|
| 1427 |
-
ko_msg = "비디오 디코딩/저장 중 오류가 발생했습니다. 다른 시드를 시도해보세요."
|
| 1428 |
-
else:
|
| 1429 |
-
en_msg = f"Processing error: {error_msg}"
|
| 1430 |
-
ko_msg = f"처리 중 오류가 발생했습니다: {error_msg}"
|
| 1431 |
-
|
| 1432 |
-
return f"""
|
| 1433 |
-
<div class="error-message" id="custom-error-container">
|
| 1434 |
-
<div class="error-msg-en" data-lang="en">
|
| 1435 |
-
<span class="error-icon">⚠️</span> {en_msg}
|
| 1436 |
-
</div>
|
| 1437 |
-
<div class="error-msg-ko" data-lang="ko">
|
| 1438 |
-
<span class="error-icon">⚠️</span> {ko_msg}
|
| 1439 |
-
</div>
|
| 1440 |
-
</div>
|
| 1441 |
-
<script>
|
| 1442 |
-
(function() {{
|
| 1443 |
-
const errorContainer = document.getElementById('custom-error-container');
|
| 1444 |
-
if (errorContainer) {{
|
| 1445 |
-
const currentLang = window.currentLang || 'en';
|
| 1446 |
-
const errMsgs = errorContainer.querySelectorAll('[data-lang]');
|
| 1447 |
-
errMsgs.forEach(msg => {{
|
| 1448 |
-
msg.style.display = (msg.getAttribute('data-lang') === currentLang) ? 'block' : 'none';
|
| 1449 |
-
}});
|
| 1450 |
-
const defaultErrorElements = document.querySelectorAll('.error');
|
| 1451 |
-
defaultErrorElements.forEach(el => {{
|
| 1452 |
-
el.style.display = 'none';
|
| 1453 |
-
}});
|
| 1454 |
-
}}
|
| 1455 |
-
}})();
|
| 1456 |
-
</script>
|
| 1457 |
-
"""
|
|
|
|
| 8 |
from urllib3.util.retry import Retry
|
| 9 |
import json
|
| 10 |
|
| 11 |
+
os.environ['HF_HOME'] = os.path.abspath(
|
| 12 |
+
os.path.realpath(os.path.join(os.path.dirname(__file__), './hf_download'))
|
| 13 |
+
)
|
| 14 |
|
| 15 |
+
# 단일 언어(영어)만 사용하기 위한 번역 딕셔너리
|
| 16 |
translations = {
|
| 17 |
"en": {
|
| 18 |
"title": "FramePack - Image to Video Generation",
|
|
|
|
| 45 |
"model_error": "Failed to load model, possibly due to network issues or high server load. Please try again later.",
|
| 46 |
"partial_video": "Processing error, but partial video has been generated",
|
| 47 |
"processing_interrupt": "Processing was interrupted, but partial video has been generated"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
}
|
| 49 |
}
|
| 50 |
|
| 51 |
+
# 영어만 사용할 것이므로 아래 함수는 사실상 항상 영어를 반환합니다.
|
| 52 |
+
def get_translation(key):
|
|
|
|
|
|
|
|
|
|
| 53 |
return translations["en"].get(key, key)
|
| 54 |
|
| 55 |
+
# 언어는 영어로 고정
|
| 56 |
current_language = "en"
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
import gradio as gr
|
| 59 |
import torch
|
| 60 |
import traceback
|
|
|
|
| 63 |
import numpy as np
|
| 64 |
import math
|
| 65 |
|
| 66 |
+
# Hugging Face Space 환경 체크
|
| 67 |
IN_HF_SPACE = os.environ.get('SPACE_ID') is not None
|
| 68 |
|
| 69 |
+
# GPU 사용 여부 전역 관리
|
| 70 |
GPU_AVAILABLE = False
|
| 71 |
GPU_INITIALIZED = False
|
| 72 |
last_update_time = time.time()
|
| 73 |
|
|
|
|
| 74 |
if IN_HF_SPACE:
|
| 75 |
try:
|
| 76 |
import spaces
|
| 77 |
+
print("Running in Hugging Face Space environment.")
|
|
|
|
|
|
|
| 78 |
try:
|
| 79 |
GPU_AVAILABLE = torch.cuda.is_available()
|
| 80 |
print(f"GPU available: {GPU_AVAILABLE}")
|
| 81 |
if GPU_AVAILABLE:
|
| 82 |
+
test_tensor = torch.zeros(1, device='cuda') + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
del test_tensor
|
| 84 |
+
print("GPU small test pass")
|
|
|
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
GPU_AVAILABLE = False
|
| 87 |
+
print(f"Error checking GPU: {e}")
|
|
|
|
| 88 |
except ImportError:
|
|
|
|
| 89 |
GPU_AVAILABLE = torch.cuda.is_available()
|
| 90 |
|
| 91 |
from PIL import Image
|
| 92 |
from diffusers import AutoencoderKLHunyuanVideo
|
| 93 |
+
from transformers import (
|
| 94 |
+
LlamaModel,
|
| 95 |
+
CLIPTextModel,
|
| 96 |
+
LlamaTokenizerFast,
|
| 97 |
+
CLIPTokenizer,
|
| 98 |
+
SiglipImageProcessor,
|
| 99 |
+
SiglipVisionModel
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
from diffusers_helper.hunyuan import (
|
| 103 |
+
encode_prompt_conds,
|
| 104 |
+
vae_decode,
|
| 105 |
+
vae_encode,
|
| 106 |
+
vae_decode_fake
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
from diffusers_helper.utils import (
|
| 110 |
+
save_bcthw_as_mp4,
|
| 111 |
+
crop_or_pad_yield_mask,
|
| 112 |
+
soft_append_bcthw,
|
| 113 |
+
resize_and_center_crop,
|
| 114 |
+
generate_timestamp
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
from diffusers_helper.bucket_tools import find_nearest_bucket
|
| 118 |
from diffusers_helper.models.hunyuan_video_packed import HunyuanVideoTransformer3DModelPacked
|
| 119 |
from diffusers_helper.pipelines.k_diffusion_hunyuan import sample_hunyuan
|
| 120 |
+
from diffusers_helper.memory import (
|
| 121 |
+
cpu,
|
| 122 |
+
gpu,
|
| 123 |
+
get_cuda_free_memory_gb,
|
| 124 |
+
move_model_to_device_with_memory_preservation,
|
| 125 |
+
offload_model_from_device_for_memory_preservation,
|
| 126 |
+
fake_diffusers_current_device,
|
| 127 |
+
DynamicSwapInstaller,
|
| 128 |
+
unload_complete_models,
|
| 129 |
+
load_model_as_complete
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
from diffusers_helper.thread_utils import AsyncStream, async_run
|
|
|
|
|
|
|
| 133 |
from diffusers_helper.clip_vision import hf_clip_vision_encode
|
| 134 |
+
from diffusers_helper.gradio.progress_bar import (
|
| 135 |
+
make_progress_bar_css,
|
| 136 |
+
make_progress_bar_html
|
| 137 |
+
)
|
| 138 |
|
| 139 |
outputs_folder = './outputs/'
|
| 140 |
os.makedirs(outputs_folder, exist_ok=True)
|
| 141 |
|
| 142 |
+
# GPU 메모리 확인
|
| 143 |
if not IN_HF_SPACE:
|
| 144 |
try:
|
| 145 |
if torch.cuda.is_available():
|
| 146 |
free_mem_gb = get_cuda_free_memory_gb(gpu)
|
| 147 |
+
print(f'Free VRAM: {free_mem_gb} GB')
|
| 148 |
else:
|
| 149 |
+
free_mem_gb = 6.0
|
| 150 |
+
print("CUDA not available, default memory setting used.")
|
| 151 |
except Exception as e:
|
| 152 |
free_mem_gb = 6.0
|
| 153 |
+
print(f"Error getting GPU mem: {e}, using default=6GB")
|
|
|
|
| 154 |
high_vram = free_mem_gb > 60
|
|
|
|
| 155 |
else:
|
| 156 |
+
print("Using default memory setting in Spaces environment.")
|
|
|
|
| 157 |
try:
|
| 158 |
if GPU_AVAILABLE:
|
| 159 |
free_mem_gb = torch.cuda.get_device_properties(0).total_memory / 1e9 * 0.9
|
| 160 |
+
high_vram = (free_mem_gb > 10)
|
| 161 |
else:
|
| 162 |
free_mem_gb = 6.0
|
| 163 |
high_vram = False
|
| 164 |
except Exception as e:
|
|
|
|
| 165 |
free_mem_gb = 6.0
|
| 166 |
high_vram = False
|
| 167 |
+
print(f'GPU memory: {free_mem_gb:.2f} GB, High-VRAM mode: {high_vram}')
|
|
|
|
| 168 |
|
|
|
|
| 169 |
models = {}
|
| 170 |
+
cpu_fallback_mode = not GPU_AVAILABLE
|
| 171 |
|
| 172 |
def load_models():
|
| 173 |
+
"""
|
| 174 |
+
Load or initialize the global models
|
| 175 |
+
"""
|
| 176 |
global models, cpu_fallback_mode, GPU_INITIALIZED
|
| 177 |
|
| 178 |
if GPU_INITIALIZED:
|
| 179 |
+
print("Models are already loaded, skipping re-initialization.")
|
| 180 |
return models
|
| 181 |
+
|
| 182 |
+
print("Start loading models...")
|
| 183 |
|
| 184 |
try:
|
| 185 |
device = 'cuda' if GPU_AVAILABLE and not cpu_fallback_mode else 'cpu'
|
| 186 |
+
model_device = 'cpu'
|
| 187 |
+
|
|
|
|
| 188 |
dtype = torch.float16 if GPU_AVAILABLE else torch.float32
|
| 189 |
transformer_dtype = torch.bfloat16 if GPU_AVAILABLE else torch.float32
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
+
print(f"Device: {device}, VAE/Encoders dtype={dtype}, Transformer dtype={transformer_dtype}")
|
|
|
|
| 192 |
|
| 193 |
+
try:
|
| 194 |
+
text_encoder = LlamaModel.from_pretrained(
|
| 195 |
+
"hunyuanvideo-community/HunyuanVideo",
|
| 196 |
+
subfolder='text_encoder',
|
| 197 |
+
torch_dtype=dtype
|
| 198 |
+
).to(model_device)
|
| 199 |
+
text_encoder_2 = CLIPTextModel.from_pretrained(
|
| 200 |
+
"hunyuanvideo-community/HunyuanVideo",
|
| 201 |
+
subfolder='text_encoder_2',
|
| 202 |
+
torch_dtype=dtype
|
| 203 |
+
).to(model_device)
|
| 204 |
+
tokenizer = LlamaTokenizerFast.from_pretrained(
|
| 205 |
+
"hunyuanvideo-community/HunyuanVideo",
|
| 206 |
+
subfolder='tokenizer'
|
| 207 |
+
)
|
| 208 |
+
tokenizer_2 = CLIPTokenizer.from_pretrained(
|
| 209 |
+
"hunyuanvideo-community/HunyuanVideo",
|
| 210 |
+
subfolder='tokenizer_2'
|
| 211 |
+
)
|
| 212 |
+
vae = AutoencoderKLHunyuanVideo.from_pretrained(
|
| 213 |
+
"hunyuanvideo-community/HunyuanVideo",
|
| 214 |
+
subfolder='vae',
|
| 215 |
+
torch_dtype=dtype
|
| 216 |
+
).to(model_device)
|
| 217 |
+
|
| 218 |
+
feature_extractor = SiglipImageProcessor.from_pretrained(
|
| 219 |
+
"lllyasviel/flux_redux_bfl", subfolder='feature_extractor'
|
| 220 |
+
)
|
| 221 |
+
image_encoder = SiglipVisionModel.from_pretrained(
|
| 222 |
+
"lllyasviel/flux_redux_bfl",
|
| 223 |
+
subfolder='image_encoder',
|
| 224 |
+
torch_dtype=dtype
|
| 225 |
+
).to(model_device)
|
| 226 |
+
|
| 227 |
+
transformer = HunyuanVideoTransformer3DModelPacked.from_pretrained(
|
| 228 |
+
"lllyasviel/FramePackI2V_HY",
|
| 229 |
+
torch_dtype=transformer_dtype
|
| 230 |
+
).to(model_device)
|
| 231 |
+
|
| 232 |
+
print("All models loaded successfully.")
|
| 233 |
except Exception as e:
|
| 234 |
+
print(f"Error loading models: {e}")
|
| 235 |
+
print("Retry with float32 on CPU...")
|
|
|
|
| 236 |
dtype = torch.float32
|
| 237 |
transformer_dtype = torch.float32
|
| 238 |
cpu_fallback_mode = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
+
text_encoder = LlamaModel.from_pretrained(
|
| 241 |
+
"hunyuanvideo-community/HunyuanVideo",
|
| 242 |
+
subfolder='text_encoder',
|
| 243 |
+
torch_dtype=dtype
|
| 244 |
+
).to('cpu')
|
| 245 |
+
text_encoder_2 = CLIPTextModel.from_pretrained(
|
| 246 |
+
"hunyuanvideo-community/HunyuanVideo",
|
| 247 |
+
subfolder='text_encoder_2',
|
| 248 |
+
torch_dtype=dtype
|
| 249 |
+
).to('cpu')
|
| 250 |
+
tokenizer = LlamaTokenizerFast.from_pretrained(
|
| 251 |
+
"hunyuanvideo-community/HunyuanVideo",
|
| 252 |
+
subfolder='tokenizer'
|
| 253 |
+
)
|
| 254 |
+
tokenizer_2 = CLIPTokenizer.from_pretrained(
|
| 255 |
+
"hunyuanvideo-community/HunyuanVideo",
|
| 256 |
+
subfolder='tokenizer_2'
|
| 257 |
+
)
|
| 258 |
+
vae = AutoencoderKLHunyuanVideo.from_pretrained(
|
| 259 |
+
"hunyuanvideo-community/HunyuanVideo",
|
| 260 |
+
subfolder='vae',
|
| 261 |
+
torch_dtype=dtype
|
| 262 |
+
).to('cpu')
|
| 263 |
+
|
| 264 |
+
feature_extractor = SiglipImageProcessor.from_pretrained(
|
| 265 |
+
"lllyasviel/flux_redux_bfl", subfolder='feature_extractor'
|
| 266 |
+
)
|
| 267 |
+
image_encoder = SiglipVisionModel.from_pretrained(
|
| 268 |
+
"lllyasviel/flux_redux_bfl",
|
| 269 |
+
subfolder='image_encoder',
|
| 270 |
+
torch_dtype=dtype
|
| 271 |
+
).to('cpu')
|
| 272 |
|
| 273 |
+
transformer = HunyuanVideoTransformer3DModelPacked.from_pretrained(
|
| 274 |
+
"lllyasviel/FramePackI2V_HY",
|
| 275 |
+
torch_dtype=transformer_dtype
|
| 276 |
+
).to('cpu')
|
| 277 |
+
|
| 278 |
+
print("Loaded in CPU-only fallback mode.")
|
| 279 |
|
| 280 |
vae.eval()
|
| 281 |
text_encoder.eval()
|
|
|
|
| 288 |
vae.enable_tiling()
|
| 289 |
|
| 290 |
transformer.high_quality_fp32_output_for_inference = True
|
| 291 |
+
print("transformer.high_quality_fp32_output_for_inference = True")
|
| 292 |
|
| 293 |
if not cpu_fallback_mode:
|
| 294 |
transformer.to(dtype=transformer_dtype)
|
|
|
|
| 306 |
if torch.cuda.is_available() and not cpu_fallback_mode:
|
| 307 |
try:
|
| 308 |
if not high_vram:
|
|
|
|
| 309 |
DynamicSwapInstaller.install_model(transformer, device=device)
|
| 310 |
DynamicSwapInstaller.install_model(text_encoder, device=device)
|
| 311 |
else:
|
|
|
|
| 314 |
image_encoder.to(device)
|
| 315 |
vae.to(device)
|
| 316 |
transformer.to(device)
|
| 317 |
+
print(f"Moved models to {device}")
|
| 318 |
except Exception as e:
|
| 319 |
+
print(f"Error moving models to {device}: {e}, fallback to CPU")
|
|
|
|
| 320 |
cpu_fallback_mode = True
|
| 321 |
|
| 322 |
models_local = {
|
|
|
|
| 329 |
'image_encoder': image_encoder,
|
| 330 |
'transformer': transformer
|
| 331 |
}
|
| 332 |
+
|
| 333 |
GPU_INITIALIZED = True
|
| 334 |
models.update(models_local)
|
| 335 |
+
print(f"Model load complete. Running mode: {'CPU' if cpu_fallback_mode else 'GPU'}")
|
| 336 |
return models
|
| 337 |
except Exception as e:
|
| 338 |
+
print(f"Unexpected error in load_models(): {e}")
|
| 339 |
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
cpu_fallback_mode = True
|
| 341 |
return {}
|
| 342 |
|
| 343 |
+
# GPU 데코레이터 사용 여부 (Spaces 전용)
|
| 344 |
if IN_HF_SPACE and 'spaces' in globals() and GPU_AVAILABLE:
|
| 345 |
try:
|
| 346 |
@spaces.GPU
|
| 347 |
def initialize_models():
|
|
|
|
| 348 |
global GPU_INITIALIZED
|
| 349 |
try:
|
| 350 |
result = load_models()
|
| 351 |
GPU_INITIALIZED = True
|
| 352 |
return result
|
| 353 |
except Exception as e:
|
| 354 |
+
print(f"Error in @spaces.GPU model init: {e}")
|
|
|
|
| 355 |
global cpu_fallback_mode
|
| 356 |
cpu_fallback_mode = True
|
| 357 |
return load_models()
|
| 358 |
except Exception as e:
|
| 359 |
+
print(f"Error creating spaces.GPU decorator: {e}")
|
| 360 |
def initialize_models():
|
| 361 |
return load_models()
|
| 362 |
+
else:
|
| 363 |
+
def initialize_models():
|
| 364 |
+
return load_models()
|
| 365 |
|
| 366 |
def get_models():
|
| 367 |
+
"""
|
| 368 |
+
Retrieve or load models if not loaded yet.
|
| 369 |
+
"""
|
| 370 |
+
global models
|
| 371 |
model_loading_key = "__model_loading__"
|
| 372 |
+
|
| 373 |
if not models:
|
| 374 |
if model_loading_key in globals():
|
| 375 |
+
print("Models are loading, please wait...")
|
| 376 |
import time
|
| 377 |
start_wait = time.time()
|
| 378 |
+
while (not models) and (model_loading_key in globals()):
|
| 379 |
time.sleep(0.5)
|
| 380 |
if time.time() - start_wait > 60:
|
| 381 |
+
print("Timed out waiting for model load.")
|
| 382 |
break
|
|
|
|
| 383 |
if models:
|
| 384 |
return models
|
|
|
|
| 385 |
try:
|
| 386 |
globals()[model_loading_key] = True
|
|
|
|
| 387 |
if IN_HF_SPACE and 'spaces' in globals() and GPU_AVAILABLE and not cpu_fallback_mode:
|
| 388 |
try:
|
| 389 |
+
print("Loading models via @spaces.GPU decorator.")
|
| 390 |
models_local = initialize_models()
|
| 391 |
models.update(models_local)
|
| 392 |
except Exception as e:
|
| 393 |
+
print(f"Error with GPU decorator: {e}, direct load fallback.")
|
| 394 |
models_local = load_models()
|
| 395 |
models.update(models_local)
|
| 396 |
else:
|
|
|
|
| 397 |
models_local = load_models()
|
| 398 |
models.update(models_local)
|
| 399 |
except Exception as e:
|
| 400 |
+
print(f"Unexpected error while loading models: {e}")
|
|
|
|
| 401 |
models.clear()
|
| 402 |
finally:
|
| 403 |
if model_loading_key in globals():
|
| 404 |
del globals()[model_loading_key]
|
|
|
|
| 405 |
return models
|
| 406 |
|
| 407 |
stream = AsyncStream()
|
| 408 |
|
| 409 |
+
# 오류 메시지 HTML 생성 함수(영어만)
|
| 410 |
+
def create_error_html(error_msg, is_timeout=False):
|
| 411 |
+
"""
|
| 412 |
+
Create a user-friendly error message in English only
|
| 413 |
+
"""
|
| 414 |
+
if is_timeout:
|
| 415 |
+
if "partial" in error_msg:
|
| 416 |
+
en_msg = "Processing timed out, but partial video has been generated."
|
| 417 |
+
else:
|
| 418 |
+
en_msg = f"Processing timed out: {error_msg}"
|
| 419 |
+
elif "model load" in error_msg.lower():
|
| 420 |
+
en_msg = "Failed to load models. Possibly heavy traffic or GPU issues."
|
| 421 |
+
elif "gpu" in error_msg.lower() or "cuda" in error_msg.lower() or "memory" in error_msg.lower():
|
| 422 |
+
en_msg = "GPU memory insufficient or error. Please try increasing GPU memory or reduce video length."
|
| 423 |
+
elif "sampling" in error_msg.lower():
|
| 424 |
+
if "partial" in error_msg.lower():
|
| 425 |
+
en_msg = "Error during sampling process, but partial video has been generated."
|
| 426 |
+
else:
|
| 427 |
+
en_msg = "Error during sampling process. Unable to generate video."
|
| 428 |
+
elif "timeout" in error_msg.lower():
|
| 429 |
+
en_msg = "Network or model download timed out. Please try again later."
|
| 430 |
+
else:
|
| 431 |
+
en_msg = f"Processing error: {error_msg}"
|
| 432 |
+
|
| 433 |
+
return f"""
|
| 434 |
+
<div class="error-message" id="custom-error-container">
|
| 435 |
+
<div>
|
| 436 |
+
<span class="error-icon">⚠️</span> {en_msg}
|
| 437 |
+
</div>
|
| 438 |
+
</div>
|
| 439 |
+
<script>
|
| 440 |
+
// Hide default Gradio error UI
|
| 441 |
+
(function() {{
|
| 442 |
+
const defaultErrorElements = document.querySelectorAll('.error');
|
| 443 |
+
defaultErrorElements.forEach(el => {{
|
| 444 |
+
el.style.display = 'none';
|
| 445 |
+
}});
|
| 446 |
+
}})();
|
| 447 |
+
</script>
|
| 448 |
+
"""
|
| 449 |
+
|
| 450 |
@torch.no_grad()
|
| 451 |
+
def worker(
|
| 452 |
+
input_image,
|
| 453 |
+
prompt,
|
| 454 |
+
n_prompt,
|
| 455 |
+
seed,
|
| 456 |
+
total_second_length,
|
| 457 |
+
latent_window_size,
|
| 458 |
+
steps,
|
| 459 |
+
cfg,
|
| 460 |
+
gs,
|
| 461 |
+
rs,
|
| 462 |
+
gpu_memory_preservation,
|
| 463 |
+
use_teacache
|
| 464 |
+
):
|
| 465 |
+
"""
|
| 466 |
+
Actual generation logic in background thread.
|
| 467 |
+
"""
|
| 468 |
global last_update_time
|
| 469 |
last_update_time = time.time()
|
| 470 |
+
|
| 471 |
total_second_length = min(total_second_length, 5.0)
|
| 472 |
+
|
| 473 |
try:
|
| 474 |
models_local = get_models()
|
| 475 |
if not models_local:
|
| 476 |
+
error_msg = "Model load failed. Check logs for details."
|
| 477 |
print(error_msg)
|
| 478 |
stream.output_queue.push(('error', error_msg))
|
| 479 |
stream.output_queue.push(('end', None))
|
| 480 |
return
|
| 481 |
+
|
| 482 |
text_encoder = models_local['text_encoder']
|
| 483 |
text_encoder_2 = models_local['text_encoder_2']
|
| 484 |
tokenizer = models_local['tokenizer']
|
|
|
|
| 488 |
image_encoder = models_local['image_encoder']
|
| 489 |
transformer = models_local['transformer']
|
| 490 |
except Exception as e:
|
| 491 |
+
err = f"Error retrieving models: {e}"
|
| 492 |
+
print(err)
|
| 493 |
traceback.print_exc()
|
| 494 |
+
stream.output_queue.push(('error', err))
|
| 495 |
stream.output_queue.push(('end', None))
|
| 496 |
return
|
| 497 |
+
|
| 498 |
+
device = 'cuda' if (GPU_AVAILABLE and not cpu_fallback_mode) else 'cpu'
|
| 499 |
+
print(f"Inference device: {device}")
|
| 500 |
|
| 501 |
if cpu_fallback_mode:
|
| 502 |
+
print("CPU fallback mode: reducing some parameters for performance.")
|
| 503 |
latent_window_size = min(latent_window_size, 5)
|
| 504 |
steps = min(steps, 15)
|
| 505 |
total_second_length = min(total_second_length, 2.0)
|
| 506 |
+
|
| 507 |
total_latent_sections = (total_second_length * 30) / (latent_window_size * 4)
|
| 508 |
total_latent_sections = int(max(round(total_latent_sections), 1))
|
| 509 |
|
|
|
|
| 513 |
history_latents = None
|
| 514 |
total_generated_latent_frames = 0
|
| 515 |
|
| 516 |
+
from diffusers_helper.memory import unload_complete_models
|
| 517 |
+
|
| 518 |
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Starting ...'))))
|
| 519 |
|
| 520 |
try:
|
|
|
|
| 524 |
text_encoder, text_encoder_2, image_encoder, vae, transformer
|
| 525 |
)
|
| 526 |
except Exception as e:
|
| 527 |
+
print(f"Error unloading models: {e}")
|
| 528 |
+
|
| 529 |
+
# Text Encode
|
| 530 |
last_update_time = time.time()
|
| 531 |
+
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Text encoding...'))))
|
| 532 |
|
| 533 |
try:
|
| 534 |
if not high_vram and not cpu_fallback_mode:
|
| 535 |
fake_diffusers_current_device(text_encoder, device)
|
| 536 |
load_model_as_complete(text_encoder_2, target_device=device)
|
| 537 |
|
| 538 |
+
llama_vec, clip_l_pooler = encode_prompt_conds(
|
| 539 |
+
prompt, text_encoder, text_encoder_2, tokenizer, tokenizer_2
|
| 540 |
+
)
|
| 541 |
|
| 542 |
if cfg == 1:
|
| 543 |
+
llama_vec_n, clip_l_pooler_n = (
|
| 544 |
+
torch.zeros_like(llama_vec),
|
| 545 |
+
torch.zeros_like(clip_l_pooler),
|
| 546 |
+
)
|
| 547 |
else:
|
| 548 |
+
llama_vec_n, clip_l_pooler_n = encode_prompt_conds(
|
| 549 |
+
n_prompt, text_encoder, text_encoder_2, tokenizer, tokenizer_2
|
| 550 |
+
)
|
| 551 |
|
| 552 |
llama_vec, llama_attention_mask = crop_or_pad_yield_mask(llama_vec, length=512)
|
| 553 |
llama_vec_n, llama_attention_mask_n = crop_or_pad_yield_mask(llama_vec_n, length=512)
|
| 554 |
except Exception as e:
|
| 555 |
+
err = f"Text encoding error: {e}"
|
| 556 |
+
print(err)
|
| 557 |
traceback.print_exc()
|
| 558 |
+
stream.output_queue.push(('error', err))
|
| 559 |
stream.output_queue.push(('end', None))
|
| 560 |
return
|
| 561 |
|
| 562 |
+
# Image processing
|
| 563 |
last_update_time = time.time()
|
| 564 |
+
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Image processing...'))))
|
| 565 |
|
| 566 |
try:
|
| 567 |
H, W, C = input_image.shape
|
| 568 |
height, width = find_nearest_bucket(H, W, resolution=640)
|
| 569 |
+
|
| 570 |
if cpu_fallback_mode:
|
| 571 |
height = min(height, 320)
|
| 572 |
width = min(width, 320)
|
| 573 |
+
|
| 574 |
input_image_np = resize_and_center_crop(input_image, target_width=width, target_height=height)
|
| 575 |
Image.fromarray(input_image_np).save(os.path.join(outputs_folder, f'{job_id}.png'))
|
| 576 |
|
| 577 |
input_image_pt = torch.from_numpy(input_image_np).float() / 127.5 - 1
|
| 578 |
input_image_pt = input_image_pt.permute(2, 0, 1)[None, :, None]
|
| 579 |
except Exception as e:
|
| 580 |
+
err = f"Image preprocess error: {e}"
|
| 581 |
+
print(err)
|
| 582 |
traceback.print_exc()
|
| 583 |
+
stream.output_queue.push(('error', err))
|
| 584 |
stream.output_queue.push(('end', None))
|
| 585 |
return
|
| 586 |
|
| 587 |
+
# VAE encoding
|
| 588 |
last_update_time = time.time()
|
| 589 |
+
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'VAE encoding...'))))
|
| 590 |
|
| 591 |
try:
|
| 592 |
if not high_vram and not cpu_fallback_mode:
|
| 593 |
load_model_as_complete(vae, target_device=device)
|
|
|
|
| 594 |
start_latent = vae_encode(input_image_pt, vae)
|
| 595 |
except Exception as e:
|
| 596 |
+
err = f"VAE encode error: {e}"
|
| 597 |
+
print(err)
|
| 598 |
traceback.print_exc()
|
| 599 |
+
stream.output_queue.push(('error', err))
|
| 600 |
stream.output_queue.push(('end', None))
|
| 601 |
return
|
| 602 |
|
| 603 |
+
# CLIP Vision
|
| 604 |
last_update_time = time.time()
|
| 605 |
+
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'CLIP Vision encode...'))))
|
| 606 |
|
| 607 |
try:
|
| 608 |
if not high_vram and not cpu_fallback_mode:
|
| 609 |
load_model_as_complete(image_encoder, target_device=device)
|
| 610 |
+
image_encoder_output = hf_clip_vision_encode(
|
| 611 |
+
input_image_np, feature_extractor, image_encoder
|
| 612 |
+
)
|
| 613 |
image_encoder_last_hidden_state = image_encoder_output.last_hidden_state
|
| 614 |
except Exception as e:
|
| 615 |
+
err = f"CLIP Vision encode error: {e}"
|
| 616 |
+
print(err)
|
| 617 |
traceback.print_exc()
|
| 618 |
+
stream.output_queue.push(('error', err))
|
| 619 |
stream.output_queue.push(('end', None))
|
| 620 |
return
|
| 621 |
|
| 622 |
+
# Convert dtype
|
| 623 |
try:
|
| 624 |
llama_vec = llama_vec.to(transformer.dtype)
|
| 625 |
llama_vec_n = llama_vec_n.to(transformer.dtype)
|
|
|
|
| 627 |
clip_l_pooler_n = clip_l_pooler_n.to(transformer.dtype)
|
| 628 |
image_encoder_last_hidden_state = image_encoder_last_hidden_state.to(transformer.dtype)
|
| 629 |
except Exception as e:
|
| 630 |
+
err = f"Data type conversion error: {e}"
|
| 631 |
+
print(err)
|
| 632 |
traceback.print_exc()
|
| 633 |
+
stream.output_queue.push(('error', err))
|
| 634 |
stream.output_queue.push(('end', None))
|
| 635 |
return
|
| 636 |
|
| 637 |
+
# Sampling
|
| 638 |
last_update_time = time.time()
|
| 639 |
+
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Start sampling...'))))
|
| 640 |
|
| 641 |
rnd = torch.Generator("cpu").manual_seed(seed)
|
| 642 |
num_frames = latent_window_size * 4 - 3
|
| 643 |
|
| 644 |
try:
|
| 645 |
+
history_latents = torch.zeros(
|
| 646 |
+
size=(1, 16, 1 + 2 + 16, height // 8, width // 8),
|
| 647 |
+
dtype=torch.float32
|
| 648 |
+
).cpu()
|
| 649 |
history_pixels = None
|
| 650 |
total_generated_latent_frames = 0
|
| 651 |
except Exception as e:
|
| 652 |
+
err = f"Init history state error: {e}"
|
| 653 |
+
print(err)
|
| 654 |
traceback.print_exc()
|
| 655 |
+
stream.output_queue.push(('error', err))
|
| 656 |
stream.output_queue.push(('end', None))
|
| 657 |
return
|
| 658 |
|
| 659 |
+
latent_paddings = list(reversed(range(total_latent_sections)))
|
| 660 |
if total_latent_sections > 4:
|
| 661 |
+
# Some heuristic to flatten out large steps
|
| 662 |
latent_paddings = [3] + [2]*(total_latent_sections - 3) + [1, 0]
|
| 663 |
|
| 664 |
for latent_padding in latent_paddings:
|
| 665 |
last_update_time = time.time()
|
| 666 |
+
is_last_section = (latent_padding == 0)
|
| 667 |
latent_padding_size = latent_padding * latent_window_size
|
| 668 |
|
| 669 |
if stream.input_queue.top() == 'end':
|
| 670 |
+
# If user requests end, save partial video if possible
|
| 671 |
if history_pixels is not None and total_generated_latent_frames > 0:
|
| 672 |
try:
|
| 673 |
+
outname = os.path.join(
|
| 674 |
+
outputs_folder, f'{job_id}_final_{total_generated_latent_frames}.mp4'
|
| 675 |
+
)
|
| 676 |
+
save_bcthw_as_mp4(history_pixels, outname, fps=30)
|
| 677 |
+
stream.output_queue.push(('file', outname))
|
| 678 |
except Exception as e:
|
| 679 |
+
print(f"Error saving final partial video: {e}")
|
|
|
|
| 680 |
stream.output_queue.push(('end', None))
|
| 681 |
return
|
| 682 |
|
| 683 |
+
print(f"latent_padding_size={latent_padding_size}, last_section={is_last_section}")
|
| 684 |
|
| 685 |
try:
|
| 686 |
+
indices = torch.arange(
|
| 687 |
+
0, sum([1, latent_padding_size, latent_window_size, 1, 2, 16])
|
| 688 |
+
).unsqueeze(0)
|
| 689 |
+
(
|
| 690 |
+
clean_latent_indices_pre,
|
| 691 |
+
blank_indices,
|
| 692 |
+
latent_indices,
|
| 693 |
+
clean_latent_indices_post,
|
| 694 |
+
clean_latent_2x_indices,
|
| 695 |
+
clean_latent_4x_indices
|
| 696 |
+
) = indices.split([1, latent_padding_size, latent_window_size, 1, 2, 16], dim=1)
|
| 697 |
clean_latent_indices = torch.cat([clean_latent_indices_pre, clean_latent_indices_post], dim=1)
|
| 698 |
|
| 699 |
clean_latents_pre = start_latent.to(history_latents)
|
| 700 |
+
clean_latents_post, clean_latents_2x, clean_latents_4x = history_latents[:, :, :1 + 2 + 16].split([1, 2, 16], dim=2)
|
| 701 |
clean_latents = torch.cat([clean_latents_pre, clean_latents_post], dim=2)
|
| 702 |
except Exception as e:
|
| 703 |
+
err = f"Sampling data prep error: {e}"
|
| 704 |
+
print(err)
|
| 705 |
traceback.print_exc()
|
| 706 |
if last_output_filename:
|
| 707 |
stream.output_queue.push(('file', last_output_filename))
|
|
|
|
| 710 |
if not high_vram and not cpu_fallback_mode:
|
| 711 |
try:
|
| 712 |
unload_complete_models()
|
| 713 |
+
move_model_to_device_with_memory_preservation(
|
| 714 |
+
transformer, target_device=device, preserved_memory_gb=gpu_memory_preservation
|
| 715 |
+
)
|
| 716 |
except Exception as e:
|
| 717 |
+
print(f"Error moving transformer to GPU: {e}")
|
| 718 |
|
| 719 |
if use_teacache and not cpu_fallback_mode:
|
| 720 |
try:
|
| 721 |
transformer.initialize_teacache(enable_teacache=True, num_steps=steps)
|
| 722 |
except Exception as e:
|
| 723 |
+
print(f"Error init teacache: {e}")
|
| 724 |
transformer.initialize_teacache(enable_teacache=False)
|
| 725 |
else:
|
| 726 |
transformer.initialize_teacache(enable_teacache=False)
|
|
|
|
| 728 |
def callback(d):
|
| 729 |
global last_update_time
|
| 730 |
last_update_time = time.time()
|
|
|
|
| 731 |
try:
|
| 732 |
if stream.input_queue.top() == 'end':
|
| 733 |
stream.output_queue.push(('end', None))
|
| 734 |
+
raise KeyboardInterrupt('User requested stop.')
|
|
|
|
| 735 |
preview = d['denoised']
|
| 736 |
preview = vae_decode_fake(preview)
|
| 737 |
+
preview = (preview * 255.0).cpu().numpy().clip(0,255).astype(np.uint8)
|
|
|
|
| 738 |
preview = einops.rearrange(preview, 'b c t h w -> (b h) (t w) c')
|
| 739 |
|
| 740 |
+
curr_step = d['i'] + 1
|
| 741 |
+
percentage = int(100.0 * curr_step / steps)
|
| 742 |
+
hint = f'Sampling {curr_step}/{steps}'
|
| 743 |
+
desc = f'Total frames so far: {int(max(0, total_generated_latent_frames * 4 - 3))}'
|
| 744 |
+
barhtml = make_progress_bar_html(percentage, hint)
|
| 745 |
+
stream.output_queue.push(('progress', (preview, desc, barhtml)))
|
| 746 |
except KeyboardInterrupt:
|
| 747 |
raise
|
| 748 |
except Exception as e:
|
| 749 |
+
print(f"Callback error: {e}")
|
| 750 |
return
|
| 751 |
|
| 752 |
try:
|
| 753 |
+
print(f"Sampling with device={device}, dtype={transformer.dtype}, teacache={use_teacache}")
|
| 754 |
+
from diffusers_helper.pipelines.k_diffusion_hunyuan import sample_hunyuan
|
| 755 |
+
|
| 756 |
try:
|
| 757 |
generated_latents = sample_hunyuan(
|
| 758 |
transformer=transformer,
|
|
|
|
| 781 |
clean_latent_2x_indices=clean_latent_2x_indices,
|
| 782 |
clean_latents_4x=clean_latents_4x,
|
| 783 |
clean_latent_4x_indices=clean_latent_4x_indices,
|
| 784 |
+
callback=callback
|
| 785 |
)
|
|
|
|
|
|
|
| 786 |
except KeyboardInterrupt as e:
|
| 787 |
+
print(f"User interrupt: {e}")
|
| 788 |
if last_output_filename:
|
| 789 |
stream.output_queue.push(('file', last_output_filename))
|
| 790 |
+
err = "User stopped generation, partial video returned."
|
| 791 |
else:
|
| 792 |
+
err = "User stopped generation, no video produced."
|
| 793 |
+
stream.output_queue.push(('error', err))
|
|
|
|
| 794 |
stream.output_queue.push(('end', None))
|
| 795 |
return
|
| 796 |
except Exception as e:
|
| 797 |
+
print(f"Sampling error: {e}")
|
| 798 |
traceback.print_exc()
|
| 799 |
if last_output_filename:
|
| 800 |
stream.output_queue.push(('file', last_output_filename))
|
| 801 |
+
err = f"Error during sampling, partial video returned: {e}"
|
| 802 |
+
stream.output_queue.push(('error', err))
|
| 803 |
else:
|
| 804 |
+
err = f"Error during sampling, no video produced: {e}"
|
| 805 |
+
stream.output_queue.push(('error', err))
|
| 806 |
stream.output_queue.push(('end', None))
|
| 807 |
return
|
| 808 |
|
| 809 |
try:
|
| 810 |
if is_last_section:
|
| 811 |
generated_latents = torch.cat([start_latent.to(generated_latents), generated_latents], dim=2)
|
|
|
|
| 812 |
total_generated_latent_frames += int(generated_latents.shape[2])
|
| 813 |
history_latents = torch.cat([generated_latents.to(history_latents), history_latents], dim=2)
|
| 814 |
except Exception as e:
|
| 815 |
+
err = f"Post-latent processing error: {e}"
|
| 816 |
+
print(err)
|
| 817 |
traceback.print_exc()
|
| 818 |
if last_output_filename:
|
| 819 |
stream.output_queue.push(('file', last_output_filename))
|
| 820 |
+
stream.output_queue.push(('error', err))
|
| 821 |
stream.output_queue.push(('end', None))
|
| 822 |
return
|
| 823 |
|
| 824 |
if not high_vram and not cpu_fallback_mode:
|
| 825 |
try:
|
| 826 |
+
offload_model_from_device_for_memory_preservation(
|
| 827 |
+
transformer, target_device=device, preserved_memory_gb=8
|
| 828 |
+
)
|
| 829 |
load_model_as_complete(vae, target_device=device)
|
| 830 |
except Exception as e:
|
| 831 |
+
print(f"Model memory manage error: {e}")
|
| 832 |
|
| 833 |
try:
|
| 834 |
+
real_history_latents = history_latents[:, :, :total_generated_latent_frames]
|
| 835 |
except Exception as e:
|
| 836 |
+
err = f"History latents slice error: {e}"
|
| 837 |
+
print(err)
|
| 838 |
if last_output_filename:
|
| 839 |
stream.output_queue.push(('file', last_output_filename))
|
| 840 |
continue
|
| 841 |
|
| 842 |
try:
|
| 843 |
+
# VAE decode
|
|
|
|
|
|
|
| 844 |
if history_pixels is None:
|
| 845 |
history_pixels = vae_decode(real_history_latents, vae).cpu()
|
| 846 |
else:
|
| 847 |
+
# Overlap logic
|
| 848 |
+
section_latent_frames = (
|
| 849 |
+
(latent_window_size * 2 + 1) if is_last_section else (latent_window_size * 2)
|
| 850 |
+
)
|
| 851 |
overlapped_frames = latent_window_size * 4 - 3
|
|
|
|
| 852 |
current_pixels = vae_decode(real_history_latents[:, :, :section_latent_frames], vae).cpu()
|
| 853 |
history_pixels = soft_append_bcthw(current_pixels, history_pixels, overlapped_frames)
|
| 854 |
+
|
| 855 |
+
output_filename = os.path.join(
|
| 856 |
+
outputs_folder, f'{job_id}_{total_generated_latent_frames}.mp4'
|
| 857 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 858 |
save_bcthw_as_mp4(history_pixels, output_filename, fps=30)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 859 |
last_output_filename = output_filename
|
| 860 |
stream.output_queue.push(('file', output_filename))
|
| 861 |
except Exception as e:
|
| 862 |
+
print(f"Video decode/save error: {e}")
|
| 863 |
traceback.print_exc()
|
| 864 |
if last_output_filename:
|
| 865 |
stream.output_queue.push(('file', last_output_filename))
|
| 866 |
+
err = f"Video decode/save error: {e}"
|
| 867 |
+
stream.output_queue.push(('error', err))
|
| 868 |
continue
|
| 869 |
|
| 870 |
if is_last_section:
|
| 871 |
break
|
| 872 |
except Exception as e:
|
| 873 |
+
print(f"Outer error: {e}, type={type(e)}")
|
| 874 |
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 875 |
if not high_vram and not cpu_fallback_mode:
|
| 876 |
try:
|
| 877 |
unload_complete_models(
|
| 878 |
text_encoder, text_encoder_2, image_encoder, vae, transformer
|
| 879 |
)
|
| 880 |
+
except Exception as ue:
|
| 881 |
+
print(f"Unload error: {ue}")
|
| 882 |
+
|
| 883 |
if last_output_filename:
|
| 884 |
stream.output_queue.push(('file', last_output_filename))
|
| 885 |
+
err = f"Error in worker: {e}"
|
| 886 |
+
stream.output_queue.push(('error', err))
|
|
|
|
| 887 |
|
| 888 |
+
print("Worker finished, pushing 'end'.")
|
| 889 |
stream.output_queue.push(('end', None))
|
|
|
|
| 890 |
|
| 891 |
+
# 최종 처리 함수 (Spaces GPU decorator or normal)
|
| 892 |
if IN_HF_SPACE and 'spaces' in globals():
|
| 893 |
@spaces.GPU
|
| 894 |
+
def process_with_gpu(
|
| 895 |
+
input_image, prompt, n_prompt, seed,
|
| 896 |
+
total_second_length, latent_window_size, steps,
|
| 897 |
+
cfg, gs, rs, gpu_memory_preservation, use_teacache
|
| 898 |
+
):
|
| 899 |
global stream
|
| 900 |
+
assert input_image is not None, "No input image given."
|
|
|
|
|
|
|
| 901 |
|
| 902 |
+
# Initialize UI state
|
| 903 |
+
yield None, None, "", "", gr.update(interactive=False), gr.update(interactive=True)
|
| 904 |
try:
|
| 905 |
stream = AsyncStream()
|
| 906 |
+
async_run(
|
| 907 |
+
worker,
|
| 908 |
+
input_image, prompt, n_prompt, seed,
|
| 909 |
+
total_second_length, latent_window_size, steps, cfg, gs, rs,
|
| 910 |
+
gpu_memory_preservation, use_teacache
|
| 911 |
+
)
|
| 912 |
|
| 913 |
output_filename = None
|
| 914 |
prev_output_filename = None
|
|
|
|
| 917 |
while True:
|
| 918 |
try:
|
| 919 |
flag, data = stream.output_queue.next()
|
|
|
|
| 920 |
if flag == 'file':
|
| 921 |
output_filename = data
|
| 922 |
prev_output_filename = output_filename
|
| 923 |
yield output_filename, gr.update(), gr.update(), '', gr.update(interactive=False), gr.update(interactive=True)
|
| 924 |
+
elif flag == 'progress':
|
|
|
|
| 925 |
preview, desc, html = data
|
| 926 |
yield gr.update(), gr.update(visible=True, value=preview), desc, html, gr.update(interactive=False), gr.update(interactive=True)
|
| 927 |
+
elif flag == 'error':
|
|
|
|
| 928 |
error_message = data
|
| 929 |
+
print(f"Got error: {error_message}")
|
| 930 |
+
elif flag == 'end':
|
| 931 |
+
if output_filename is None and prev_output_filename:
|
|
|
|
| 932 |
output_filename = prev_output_filename
|
|
|
|
| 933 |
if error_message:
|
| 934 |
+
err_html = create_error_html(error_message)
|
| 935 |
+
yield (
|
| 936 |
+
output_filename, gr.update(visible=False), gr.update(),
|
| 937 |
+
err_html, gr.update(interactive=True), gr.update(interactive=False)
|
| 938 |
+
)
|
| 939 |
else:
|
| 940 |
+
yield (
|
| 941 |
+
output_filename, gr.update(visible=False), gr.update(),
|
| 942 |
+
'', gr.update(interactive=True), gr.update(interactive=False)
|
| 943 |
+
)
|
| 944 |
break
|
| 945 |
except Exception as e:
|
| 946 |
+
print(f"Loop error: {e}")
|
| 947 |
+
if (time.time() - last_update_time) > 60:
|
| 948 |
+
print("No updates for 60 seconds, possible hang or timeout.")
|
|
|
|
| 949 |
if prev_output_filename:
|
| 950 |
+
err_html = create_error_html("partial video has been generated", is_timeout=True)
|
| 951 |
+
yield (
|
| 952 |
+
prev_output_filename, gr.update(visible=False), gr.update(),
|
| 953 |
+
err_html, gr.update(interactive=True), gr.update(interactive=False)
|
| 954 |
+
)
|
| 955 |
else:
|
| 956 |
+
err_html = create_error_html(f"Processing timed out: {e}", is_timeout=True)
|
| 957 |
+
yield (
|
| 958 |
+
None, gr.update(visible=False), gr.update(),
|
| 959 |
+
err_html, gr.update(interactive=True), gr.update(interactive=False)
|
| 960 |
+
)
|
| 961 |
break
|
| 962 |
except Exception as e:
|
| 963 |
+
print(f"Start process error: {e}")
|
| 964 |
traceback.print_exc()
|
| 965 |
+
err_html = create_error_html(str(e))
|
| 966 |
+
yield None, gr.update(visible=False), gr.update(), err_html, gr.update(interactive=True), gr.update(interactive=False)
|
| 967 |
+
|
|
|
|
|
|
|
| 968 |
process = process_with_gpu
|
| 969 |
else:
|
| 970 |
+
def process(
|
| 971 |
+
input_image, prompt, n_prompt, seed,
|
| 972 |
+
total_second_length, latent_window_size, steps,
|
| 973 |
+
cfg, gs, rs, gpu_memory_preservation, use_teacache
|
| 974 |
+
):
|
| 975 |
global stream
|
| 976 |
+
assert input_image is not None, "No input image given."
|
|
|
|
|
|
|
| 977 |
|
| 978 |
+
yield None, None, "", "", gr.update(interactive=False), gr.update(interactive=True)
|
| 979 |
try:
|
| 980 |
stream = AsyncStream()
|
| 981 |
+
async_run(
|
| 982 |
+
worker,
|
| 983 |
+
input_image, prompt, n_prompt, seed,
|
| 984 |
+
total_second_length, latent_window_size, steps, cfg, gs, rs,
|
| 985 |
+
gpu_memory_preservation, use_teacache
|
| 986 |
+
)
|
| 987 |
|
| 988 |
output_filename = None
|
| 989 |
prev_output_filename = None
|
|
|
|
| 992 |
while True:
|
| 993 |
try:
|
| 994 |
flag, data = stream.output_queue.next()
|
|
|
|
| 995 |
if flag == 'file':
|
| 996 |
output_filename = data
|
| 997 |
prev_output_filename = output_filename
|
| 998 |
yield output_filename, gr.update(), gr.update(), '', gr.update(interactive=False), gr.update(interactive=True)
|
| 999 |
+
elif flag == 'progress':
|
|
|
|
| 1000 |
preview, desc, html = data
|
| 1001 |
yield gr.update(), gr.update(visible=True, value=preview), desc, html, gr.update(interactive=False), gr.update(interactive=True)
|
| 1002 |
+
elif flag == 'error':
|
|
|
|
| 1003 |
error_message = data
|
| 1004 |
+
print(f"Got error: {error_message}")
|
| 1005 |
+
elif flag == 'end':
|
| 1006 |
+
if output_filename is None and prev_output_filename:
|
|
|
|
| 1007 |
output_filename = prev_output_filename
|
|
|
|
| 1008 |
if error_message:
|
| 1009 |
+
err_html = create_error_html(error_message)
|
| 1010 |
+
yield (
|
| 1011 |
+
output_filename, gr.update(visible=False), gr.update(),
|
| 1012 |
+
err_html, gr.update(interactive=True), gr.update(interactive=False)
|
| 1013 |
+
)
|
| 1014 |
else:
|
| 1015 |
+
yield (
|
| 1016 |
+
output_filename, gr.update(visible=False), gr.update(),
|
| 1017 |
+
'', gr.update(interactive=True), gr.update(interactive=False)
|
| 1018 |
+
)
|
| 1019 |
break
|
| 1020 |
except Exception as e:
|
| 1021 |
+
print(f"Loop error: {e}")
|
| 1022 |
+
if (time.time() - last_update_time) > 60:
|
| 1023 |
+
print("No update for 60 seconds, possible hang or timeout.")
|
|
|
|
| 1024 |
if prev_output_filename:
|
| 1025 |
+
err_html = create_error_html("partial video has been generated", is_timeout=True)
|
| 1026 |
+
yield (
|
| 1027 |
+
prev_output_filename, gr.update(visible=False), gr.update(),
|
| 1028 |
+
err_html, gr.update(interactive=True), gr.update(interactive=False)
|
| 1029 |
+
)
|
| 1030 |
else:
|
| 1031 |
+
err_html = create_error_html(f"Processing timed out: {e}", is_timeout=True)
|
| 1032 |
+
yield (
|
| 1033 |
+
None, gr.update(visible=False), gr.update(),
|
| 1034 |
+
err_html, gr.update(interactive=True), gr.update(interactive=False)
|
| 1035 |
+
)
|
| 1036 |
break
|
| 1037 |
except Exception as e:
|
| 1038 |
+
print(f"Start process error: {e}")
|
| 1039 |
traceback.print_exc()
|
| 1040 |
+
err_html = create_error_html(str(e))
|
| 1041 |
+
yield None, gr.update(visible=False), gr.update(), err_html, gr.update(interactive=True), gr.update(interactive=False)
|
|
|
|
|
|
|
| 1042 |
|
| 1043 |
def end_process():
|
| 1044 |
+
"""
|
| 1045 |
+
Stop generation by pushing 'end' to the worker queue
|
| 1046 |
+
"""
|
| 1047 |
+
print("User clicked stop, sending 'end' signal...")
|
| 1048 |
+
global stream
|
| 1049 |
if 'stream' in globals() and stream is not None:
|
| 1050 |
try:
|
| 1051 |
+
top_signal = stream.input_queue.top()
|
| 1052 |
+
print(f"Queue top signal = {top_signal}")
|
| 1053 |
except Exception as e:
|
| 1054 |
+
print(f"Error checking queue top: {e}")
|
| 1055 |
try:
|
| 1056 |
stream.input_queue.push('end')
|
| 1057 |
+
print("Pushed 'end' successfully.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1058 |
except Exception as e:
|
| 1059 |
+
print(f"Error pushing 'end': {e}")
|
| 1060 |
else:
|
| 1061 |
+
print("Warning: Stream not initialized, cannot stop.")
|
| 1062 |
return None
|
| 1063 |
|
| 1064 |
+
# 예시 빠른 프롬프트
|
| 1065 |
quick_prompts = [
|
| 1066 |
+
["The girl dances gracefully, with clear movements, full of charm."],
|
| 1067 |
+
["A character doing some simple body movements."]
|
| 1068 |
]
|
|
|
|
| 1069 |
|
| 1070 |
+
# CSS
|
| 1071 |
def make_custom_css():
|
| 1072 |
+
base_progress_css = make_progress_bar_css()
|
| 1073 |
+
enhanced_css = """
|
| 1074 |
+
/* Visual & layout improvement */
|
| 1075 |
+
body {
|
| 1076 |
+
background: #f9fafb !important;
|
| 1077 |
+
font-family: "Noto Sans", sans-serif;
|
| 1078 |
+
}
|
| 1079 |
#app-container {
|
| 1080 |
+
max-width: 1200px;
|
| 1081 |
margin: 0 auto;
|
| 1082 |
+
padding: 1rem;
|
| 1083 |
+
position: relative;
|
| 1084 |
}
|
| 1085 |
+
#app-container h1 {
|
| 1086 |
+
color: #2d3748;
|
| 1087 |
+
margin-bottom: 1.2rem;
|
| 1088 |
+
font-weight: 700;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1089 |
}
|
| 1090 |
+
.gr-panel {
|
| 1091 |
+
background: #fff;
|
| 1092 |
+
border: 1px solid #cbd5e0;
|
| 1093 |
+
border-radius: 8px;
|
| 1094 |
+
padding: 1rem;
|
| 1095 |
+
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
|
| 1096 |
}
|
| 1097 |
+
.button-container button {
|
|
|
|
| 1098 |
min-height: 45px;
|
| 1099 |
font-size: 1rem;
|
| 1100 |
+
font-weight: 600;
|
| 1101 |
}
|
| 1102 |
+
.button-container button#start-button {
|
| 1103 |
+
background-color: #3182ce !important;
|
| 1104 |
+
color: #fff !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1105 |
}
|
| 1106 |
+
.button-container button#stop-button {
|
| 1107 |
+
background-color: #e53e3e !important;
|
| 1108 |
+
color: #fff !important;
|
|
|
|
|
|
|
| 1109 |
}
|
| 1110 |
+
.button-container button:hover {
|
| 1111 |
+
filter: brightness(0.95);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1112 |
}
|
| 1113 |
+
.preview-container, .video-container {
|
| 1114 |
+
border: 1px solid #cbd5e0;
|
| 1115 |
+
border-radius: 8px;
|
| 1116 |
+
overflow: hidden;
|
| 1117 |
}
|
|
|
|
| 1118 |
.progress-container {
|
| 1119 |
+
margin-top: 15px;
|
| 1120 |
+
margin-bottom: 15px;
|
| 1121 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1122 |
.error-message {
|
| 1123 |
+
background-color: #fff5f5;
|
| 1124 |
+
border: 1px solid #fed7d7;
|
| 1125 |
+
color: #e53e3e;
|
| 1126 |
padding: 10px;
|
| 1127 |
border-radius: 4px;
|
| 1128 |
margin-top: 10px;
|
|
|
|
| 1129 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1130 |
.error-icon {
|
| 1131 |
+
color: #e53e3e;
|
|
|
|
| 1132 |
margin-right: 8px;
|
| 1133 |
}
|
| 1134 |
+
#error-message {
|
| 1135 |
+
color: #ff4444;
|
| 1136 |
+
font-weight: bold;
|
| 1137 |
+
padding: 10px;
|
| 1138 |
+
border-radius: 4px;
|
| 1139 |
+
margin-top: 10px;
|
| 1140 |
}
|
| 1141 |
+
@media (max-width: 768px) {
|
| 1142 |
+
#app-container {
|
| 1143 |
+
padding: 0.5rem;
|
| 1144 |
+
}
|
| 1145 |
+
.mobile-full-width {
|
| 1146 |
+
flex-direction: column !important;
|
| 1147 |
+
}
|
| 1148 |
+
.mobile-full-width > .gr-block {
|
| 1149 |
+
width: 100% !important;
|
| 1150 |
+
}
|
| 1151 |
}
|
| 1152 |
"""
|
| 1153 |
+
return base_progress_css + enhanced_css
|
|
|
|
| 1154 |
|
| 1155 |
css = make_custom_css()
|
| 1156 |
+
|
| 1157 |
+
# Gradio UI
|
| 1158 |
block = gr.Blocks(css=css).queue()
|
| 1159 |
with block:
|
| 1160 |
+
# 상단 제목
|
| 1161 |
+
gr.HTML("<div id='app-container'><h1>FramePack - Image to Video Generation</h1></div>")
|
| 1162 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1163 |
with gr.Row(elem_classes="mobile-full-width"):
|
| 1164 |
+
with gr.Column(scale=1, elem_classes="gr-panel"):
|
| 1165 |
input_image = gr.Image(
|
| 1166 |
+
label="Upload Image",
|
| 1167 |
+
sources='upload',
|
| 1168 |
+
type="numpy",
|
| 1169 |
elem_id="input-image",
|
| 1170 |
height=320
|
| 1171 |
)
|
| 1172 |
+
prompt = gr.Textbox(label="Prompt", value='', elem_id="prompt-input")
|
| 1173 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1174 |
example_quick_prompts = gr.Dataset(
|
| 1175 |
+
samples=quick_prompts,
|
| 1176 |
+
label="Quick Prompts",
|
| 1177 |
+
samples_per_page=1000,
|
| 1178 |
components=[prompt]
|
| 1179 |
)
|
| 1180 |
example_quick_prompts.click(
|
| 1181 |
+
fn=lambda x: x[0],
|
| 1182 |
+
inputs=[example_quick_prompts],
|
| 1183 |
+
outputs=prompt,
|
| 1184 |
+
show_progress=False,
|
| 1185 |
queue=False
|
| 1186 |
)
|
| 1187 |
+
with gr.Column(scale=1, elem_classes="gr-panel"):
|
| 1188 |
with gr.Row(elem_classes="button-container"):
|
| 1189 |
start_button = gr.Button(
|
| 1190 |
+
value="Generate",
|
|
|
|
| 1191 |
elem_id="start-button",
|
| 1192 |
variant="primary"
|
| 1193 |
)
|
|
|
|
| 1194 |
end_button = gr.Button(
|
| 1195 |
+
value="Stop",
|
|
|
|
| 1196 |
elem_id="stop-button",
|
| 1197 |
interactive=False
|
| 1198 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1199 |
|
| 1200 |
result_video = gr.Video(
|
| 1201 |
+
label="Generated Video",
|
| 1202 |
+
autoplay=True,
|
|
|
|
|
|
|
| 1203 |
loop=True,
|
| 1204 |
+
height=320,
|
| 1205 |
elem_classes="video-container",
|
| 1206 |
elem_id="result-video"
|
| 1207 |
)
|
| 1208 |
+
preview_image = gr.Image(
|
| 1209 |
+
label="Preview",
|
| 1210 |
+
visible=False,
|
| 1211 |
+
height=150,
|
| 1212 |
+
elem_classes="preview-container"
|
| 1213 |
+
)
|
| 1214 |
+
|
| 1215 |
+
gr.Markdown(get_translation("sampling_note"))
|
| 1216 |
|
| 1217 |
with gr.Group(elem_classes="progress-container"):
|
| 1218 |
+
progress_desc = gr.Markdown('')
|
| 1219 |
+
progress_bar = gr.HTML('')
|
| 1220 |
|
| 1221 |
error_message = gr.HTML('', elem_id='error-message', visible=True)
|
|
|
|
|
|
|
| 1222 |
|
| 1223 |
+
# 고급 파라미터 Accordion
|
| 1224 |
+
with gr.Accordion("Advanced Settings", open=False, elem_classes="gr-panel"):
|
| 1225 |
+
use_teacache = gr.Checkbox(
|
| 1226 |
+
label=get_translation("use_teacache"),
|
| 1227 |
+
value=True,
|
| 1228 |
+
info=get_translation("teacache_info")
|
| 1229 |
+
)
|
| 1230 |
+
n_prompt = gr.Textbox(label=get_translation("negative_prompt"), value="", visible=False)
|
| 1231 |
+
seed = gr.Number(
|
| 1232 |
+
label=get_translation("seed"),
|
| 1233 |
+
value=31337,
|
| 1234 |
+
precision=0
|
| 1235 |
+
)
|
| 1236 |
+
total_second_length = gr.Slider(
|
| 1237 |
+
label=get_translation("video_length"),
|
| 1238 |
+
minimum=1,
|
| 1239 |
+
maximum=5,
|
| 1240 |
+
value=5,
|
| 1241 |
+
step=0.1
|
| 1242 |
+
)
|
| 1243 |
+
latent_window_size = gr.Slider(
|
| 1244 |
+
label=get_translation("latent_window"),
|
| 1245 |
+
minimum=1,
|
| 1246 |
+
maximum=33,
|
| 1247 |
+
value=9,
|
| 1248 |
+
step=1,
|
| 1249 |
+
visible=False
|
| 1250 |
+
)
|
| 1251 |
+
steps = gr.Slider(
|
| 1252 |
+
label=get_translation("steps"),
|
| 1253 |
+
minimum=1,
|
| 1254 |
+
maximum=100,
|
| 1255 |
+
value=25,
|
| 1256 |
+
step=1,
|
| 1257 |
+
info=get_translation("steps_info")
|
| 1258 |
+
)
|
| 1259 |
+
cfg = gr.Slider(
|
| 1260 |
+
label=get_translation("cfg_scale"),
|
| 1261 |
+
minimum=1.0,
|
| 1262 |
+
maximum=32.0,
|
| 1263 |
+
value=1.0,
|
| 1264 |
+
step=0.01,
|
| 1265 |
+
visible=False
|
| 1266 |
+
)
|
| 1267 |
+
gs = gr.Slider(
|
| 1268 |
+
label=get_translation("distilled_cfg"),
|
| 1269 |
+
minimum=1.0,
|
| 1270 |
+
maximum=32.0,
|
| 1271 |
+
value=10.0,
|
| 1272 |
+
step=0.01,
|
| 1273 |
+
info=get_translation("distilled_cfg_info")
|
| 1274 |
+
)
|
| 1275 |
+
rs = gr.Slider(
|
| 1276 |
+
label=get_translation("cfg_rescale"),
|
| 1277 |
+
minimum=0.0,
|
| 1278 |
+
maximum=1.0,
|
| 1279 |
+
value=0.0,
|
| 1280 |
+
step=0.01,
|
| 1281 |
+
visible=False
|
| 1282 |
+
)
|
| 1283 |
+
gpu_memory_preservation = gr.Slider(
|
| 1284 |
+
label=get_translation("gpu_memory"),
|
| 1285 |
+
minimum=6,
|
| 1286 |
+
maximum=128,
|
| 1287 |
+
value=6,
|
| 1288 |
+
step=0.1,
|
| 1289 |
+
info=get_translation("gpu_memory_info")
|
| 1290 |
+
)
|
| 1291 |
+
|
| 1292 |
+
# 처리 함수 연결
|
| 1293 |
+
ips = [
|
| 1294 |
+
input_image, prompt, n_prompt, seed,
|
| 1295 |
+
total_second_length, latent_window_size, steps,
|
| 1296 |
+
cfg, gs, rs, gpu_memory_preservation, use_teacache
|
| 1297 |
+
]
|
| 1298 |
+
start_button.click(
|
| 1299 |
+
fn=process,
|
| 1300 |
+
inputs=ips,
|
| 1301 |
+
outputs=[result_video, preview_image, progress_desc, progress_bar, start_button, end_button]
|
| 1302 |
+
)
|
| 1303 |
end_button.click(fn=end_process)
|
| 1304 |
|
| 1305 |
block.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|