Spaces:
Running
on
Zero
Running
on
Zero
Delete app-backup.py
Browse files- app-backup.py +0 -1305
app-backup.py
DELETED
@@ -1,1305 +0,0 @@
|
|
1 |
-
from diffusers_helper.hf_login import login
|
2 |
-
|
3 |
-
import os
|
4 |
-
import threading
|
5 |
-
import time
|
6 |
-
import requests
|
7 |
-
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 |
-
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",
|
19 |
-
"upload_image": "Upload Image",
|
20 |
-
"prompt": "Prompt",
|
21 |
-
"quick_prompts": "Quick Prompts",
|
22 |
-
"start_generation": "Generate",
|
23 |
-
"stop_generation": "Stop",
|
24 |
-
"use_teacache": "Use TeaCache",
|
25 |
-
"teacache_info": "Faster speed, but may result in slightly worse finger and hand generation.",
|
26 |
-
"negative_prompt": "Negative Prompt",
|
27 |
-
"seed": "Seed",
|
28 |
-
"video_length": "Video Length (max 5 seconds)",
|
29 |
-
"latent_window": "Latent Window Size",
|
30 |
-
"steps": "Inference Steps",
|
31 |
-
"steps_info": "Changing this value is not recommended.",
|
32 |
-
"cfg_scale": "CFG Scale",
|
33 |
-
"distilled_cfg": "Distilled CFG Scale",
|
34 |
-
"distilled_cfg_info": "Changing this value is not recommended.",
|
35 |
-
"cfg_rescale": "CFG Rescale",
|
36 |
-
"gpu_memory": "GPU Memory Preservation (GB) (larger means slower)",
|
37 |
-
"gpu_memory_info": "Set this to a larger value if you encounter OOM errors. Larger values cause slower speed.",
|
38 |
-
"next_latents": "Next Latents",
|
39 |
-
"generated_video": "Generated Video",
|
40 |
-
"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.",
|
41 |
-
"error_message": "Error",
|
42 |
-
"processing_error": "Processing error",
|
43 |
-
"network_error": "Network connection is unstable, model download timed out. Please try again later.",
|
44 |
-
"memory_error": "GPU memory insufficient, please try increasing GPU memory preservation value or reduce video length.",
|
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
|
61 |
-
import einops
|
62 |
-
import safetensors.torch as sf
|
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()
|
282 |
-
text_encoder_2.eval()
|
283 |
-
image_encoder.eval()
|
284 |
-
transformer.eval()
|
285 |
-
|
286 |
-
if not high_vram or cpu_fallback_mode:
|
287 |
-
vae.enable_slicing()
|
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)
|
295 |
-
vae.to(dtype=dtype)
|
296 |
-
image_encoder.to(dtype=dtype)
|
297 |
-
text_encoder.to(dtype=dtype)
|
298 |
-
text_encoder_2.to(dtype=dtype)
|
299 |
-
|
300 |
-
vae.requires_grad_(False)
|
301 |
-
text_encoder.requires_grad_(False)
|
302 |
-
text_encoder_2.requires_grad_(False)
|
303 |
-
image_encoder.requires_grad_(False)
|
304 |
-
transformer.requires_grad_(False)
|
305 |
-
|
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:
|
312 |
-
text_encoder.to(device)
|
313 |
-
text_encoder_2.to(device)
|
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 = {
|
323 |
-
'text_encoder': text_encoder,
|
324 |
-
'text_encoder_2': text_encoder_2,
|
325 |
-
'tokenizer': tokenizer,
|
326 |
-
'tokenizer_2': tokenizer_2,
|
327 |
-
'vae': vae,
|
328 |
-
'feature_extractor': feature_extractor,
|
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']
|
485 |
-
tokenizer_2 = models_local['tokenizer_2']
|
486 |
-
vae = models_local['vae']
|
487 |
-
feature_extractor = models_local['feature_extractor']
|
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 |
-
|
510 |
-
job_id = generate_timestamp()
|
511 |
-
last_output_filename = None
|
512 |
-
history_pixels = None
|
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:
|
521 |
-
if not high_vram and not cpu_fallback_mode:
|
522 |
-
try:
|
523 |
-
unload_complete_models(
|
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)
|
626 |
-
clip_l_pooler = clip_l_pooler.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))
|
708 |
-
continue
|
709 |
-
|
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)
|
727 |
-
|
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,
|
759 |
-
sampler='unipc',
|
760 |
-
width=width,
|
761 |
-
height=height,
|
762 |
-
frames=num_frames,
|
763 |
-
real_guidance_scale=cfg,
|
764 |
-
distilled_guidance_scale=gs,
|
765 |
-
guidance_rescale=rs,
|
766 |
-
num_inference_steps=steps,
|
767 |
-
generator=rnd,
|
768 |
-
prompt_embeds=llama_vec,
|
769 |
-
prompt_embeds_mask=llama_attention_mask,
|
770 |
-
prompt_poolers=clip_l_pooler,
|
771 |
-
negative_prompt_embeds=llama_vec_n,
|
772 |
-
negative_prompt_embeds_mask=llama_attention_mask_n,
|
773 |
-
negative_prompt_poolers=clip_l_pooler_n,
|
774 |
-
device=device,
|
775 |
-
dtype=transformer.dtype,
|
776 |
-
image_embeddings=image_encoder_last_hidden_state,
|
777 |
-
latent_indices=latent_indices,
|
778 |
-
clean_latents=clean_latents,
|
779 |
-
clean_latent_indices=clean_latent_indices,
|
780 |
-
clean_latents_2x=clean_latents_2x,
|
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
|
915 |
-
error_message = None
|
916 |
-
|
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
|
990 |
-
error_message = None
|
991 |
-
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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