Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,106 +1,680 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
#
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def make_custom_css():
|
| 6 |
-
"""カスタムCSS
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
css = make_custom_css()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
gr_ui = gr.Blocks(css=css).queue()
|
| 13 |
-
with gr_ui:
|
| 14 |
-
# アプリタイトル
|
| 15 |
-
gr.HTML("<h1>FramePack - 画像から動画生成</h1>")
|
| 16 |
-
|
| 17 |
-
# レイアウト: 左側は入力、右側は出力
|
| 18 |
with gr.Row():
|
| 19 |
with gr.Column():
|
| 20 |
-
# 画像アップロード
|
| 21 |
input_image = gr.Image(
|
| 22 |
source='upload',
|
| 23 |
-
type=
|
| 24 |
-
label=
|
| 25 |
height=320
|
| 26 |
)
|
| 27 |
-
|
| 28 |
-
# プロンプト入力
|
| 29 |
prompt = gr.Textbox(
|
| 30 |
-
label=
|
| 31 |
-
placeholder=
|
| 32 |
)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
quick_prompts = [
|
| 36 |
-
["The camera smoothly orbits around the center of the scene, keeping the center point fixed and always in view"],
|
| 37 |
-
]
|
| 38 |
-
example_prompts = gr.Dataset(
|
| 39 |
-
samples=quick_prompts,
|
| 40 |
label='クイックプロンプト',
|
| 41 |
samples_per_page=10,
|
| 42 |
components=[prompt]
|
| 43 |
)
|
| 44 |
-
|
| 45 |
|
| 46 |
-
# 操作ボタン
|
| 47 |
with gr.Row():
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
with gr.Column():
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
height=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
)
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
label=
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
height=512
|
| 90 |
)
|
|
|
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
outputs=[result_video, preview, progress_desc, progress_bar, start_button, stop_button])
|
| 103 |
-
stop_button.click(fn=end_process)
|
| 104 |
|
| 105 |
# アプリ起動
|
| 106 |
-
|
|
|
|
| 1 |
+
from diffusers_helper.hf_login import login # Hugging Face ログイン
|
| 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 |
+
# Hugging Face ダウンロード用キャッシュディレクトリを設定
|
| 12 |
+
os.environ['HF_HOME'] = os.path.abspath(
|
| 13 |
+
os.path.realpath(
|
| 14 |
+
os.path.join(os.path.dirname(__file__), './hf_download')
|
| 15 |
+
)
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
import gradio as gr
|
| 19 |
+
import torch
|
| 20 |
+
import traceback
|
| 21 |
+
import einops
|
| 22 |
+
import safetensors.torch as sf
|
| 23 |
+
import numpy as np
|
| 24 |
+
import math
|
| 25 |
+
|
| 26 |
+
# 環境に応じた GPU 利用設定
|
| 27 |
+
IN_HF_SPACE = os.environ.get('SPACE_ID') is not None
|
| 28 |
+
GPU_AVAILABLE = False
|
| 29 |
+
GPU_INITIALIZED = False
|
| 30 |
+
last_update_time = time.time()
|
| 31 |
+
|
| 32 |
+
# Spaces 環境の場合、spaces モジュールをインポートして GPU 状態をチェック
|
| 33 |
+
if IN_HF_SPACE:
|
| 34 |
+
try:
|
| 35 |
+
import spaces
|
| 36 |
+
GPU_AVAILABLE = torch.cuda.is_available()
|
| 37 |
+
if GPU_AVAILABLE:
|
| 38 |
+
device_name = torch.cuda.get_device_name(0)
|
| 39 |
+
total_mem = torch.cuda.get_device_properties(0).total_memory / 1e9
|
| 40 |
+
print(f"GPU 利用可能: {device_name}, メモリ: {total_mem:.2f} GB")
|
| 41 |
+
# 簡易テスト
|
| 42 |
+
t = torch.zeros(1, device='cuda') + 1
|
| 43 |
+
del t
|
| 44 |
+
else:
|
| 45 |
+
print("警告: CUDA は利用可能だが GPU が見つかりません")
|
| 46 |
+
except ImportError:
|
| 47 |
+
print("spaces モジュールがインポートできませんでした")
|
| 48 |
+
GPU_AVAILABLE = torch.cuda.is_available()
|
| 49 |
+
else:
|
| 50 |
+
GPU_AVAILABLE = torch.cuda.is_available()
|
| 51 |
+
|
| 52 |
+
# 出力用フォルダを作成
|
| 53 |
+
outputs_folder = './outputs/'
|
| 54 |
+
os.makedirs(outputs_folder, exist_ok=True)
|
| 55 |
+
|
| 56 |
+
# モデル管理用グローバル変数
|
| 57 |
+
models = {}
|
| 58 |
+
cpu_fallback_mode = not GPU_AVAILABLE
|
| 59 |
+
|
| 60 |
+
# モデルをロードする関数
|
| 61 |
+
|
| 62 |
+
def load_models():
|
| 63 |
+
"""
|
| 64 |
+
モデルをロードし、グローバル変数に保存します。
|
| 65 |
+
初回のみ実行され、以降はスキップされます。
|
| 66 |
+
"""
|
| 67 |
+
global models, cpu_fallback_mode, GPU_INITIALIZED
|
| 68 |
+
if GPU_INITIALIZED:
|
| 69 |
+
print("モデルは既にロード済みです")
|
| 70 |
+
return models
|
| 71 |
+
print("モデルのロードを開始します...")
|
| 72 |
+
try:
|
| 73 |
+
# デバイスとデータ型設定
|
| 74 |
+
device = 'cuda' if GPU_AVAILABLE and not cpu_fallback_mode else 'cpu'
|
| 75 |
+
dtype = torch.float16 if GPU_AVAILABLE else torch.float32
|
| 76 |
+
transformer_dtype = torch.bfloat16 if GPU_AVAILABLE else torch.float32
|
| 77 |
+
|
| 78 |
+
# モデルを順次ロード
|
| 79 |
+
from transformers import LlamaModel, CLIPTextModel, LlamaTokenizerFast, CLIPTokenizer
|
| 80 |
+
from diffusers import AutoencoderKLHunyuanVideo
|
| 81 |
+
from diffusers_helper.models.hunyuan_video_packed import HunyuanVideoTransformer3DModelPacked
|
| 82 |
+
from diffusers_helper.hunyuan import encode_prompt_conds, vae_decode, vae_encode, vae_decode_fake
|
| 83 |
+
from diffusers_helper.utils import save_bcthw_as_mp4, crop_or_pad_yield_mask, soft_append_bcthw, resize_and_center_crop, generate_timestamp
|
| 84 |
+
from diffusers_helper.pipelines.k_diffusion_hunyuan import sample_hunyuan
|
| 85 |
+
from diffusers_helper.clip_vision import hf_clip_vision_encode
|
| 86 |
+
from diffusers_helper.memory import get_cuda_free_memory_gb, move_model_to_device_with_memory_preservation, unload_complete_models, load_model_as_complete, DynamicSwapInstaller
|
| 87 |
+
from diffusers_helper.thread_utils import AsyncStream, async_run
|
| 88 |
+
|
| 89 |
+
# テキストエンコーダー
|
| 90 |
+
text_encoder = LlamaModel.from_pretrained(
|
| 91 |
+
"hunyuanvideo-community/HunyuanVideo", subfolder='text_encoder', torch_dtype=dtype
|
| 92 |
+
).to('cpu')
|
| 93 |
+
text_encoder_2 = CLIPTextModel.from_pretrained(
|
| 94 |
+
"hunyuanvideo-community/HunyuanVideo", subfolder='text_encoder_2', torch_dtype=dtype
|
| 95 |
+
).to('cpu')
|
| 96 |
+
tokenizer = LlamaTokenizerFast.from_pretrained(
|
| 97 |
+
"hunyuanvideo-community/HunyuanVideo", subfolder='tokenizer'
|
| 98 |
+
)
|
| 99 |
+
tokenizer_2 = CLIPTokenizer.from_pretrained(
|
| 100 |
+
"hunyuanvideo-community/HunyuanVideo", subfolder='tokenizer_2'
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# VAE
|
| 104 |
+
vae = AutoencoderKLHunyuanVideo.from_pretrained(
|
| 105 |
+
"hunyuanvideo-community/HunyuanVideo", subfolder='vae', torch_dtype=dtype
|
| 106 |
+
).to('cpu')
|
| 107 |
+
|
| 108 |
+
# 画像エンコーダー
|
| 109 |
+
from transformers import SiglipImageProcessor, SiglipVisionModel
|
| 110 |
+
feature_extractor = SiglipImageProcessor.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='feature_extractor')
|
| 111 |
+
image_encoder = SiglipVisionModel.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='image_encoder', torch_dtype=dtype).to('cpu')
|
| 112 |
+
|
| 113 |
+
# トランスフォーマーモデル
|
| 114 |
+
transformer = HunyuanVideoTransformer3DModelPacked.from_pretrained(
|
| 115 |
+
'from diffusers_helper.hf_login import login # Hugging Face ログイン
|
| 116 |
+
|
| 117 |
+
import os
|
| 118 |
+
import threading
|
| 119 |
+
import time
|
| 120 |
+
import requests
|
| 121 |
+
from requests.adapters import HTTPAdapter
|
| 122 |
+
from urllib3.util.retry import Retry
|
| 123 |
+
import json
|
| 124 |
+
|
| 125 |
+
# Hugging Face ダウンロード用キャッシュディレクトリを設定
|
| 126 |
+
os.environ['HF_HOME'] = os.path.abspath(
|
| 127 |
+
os.path.realpath(
|
| 128 |
+
os.path.join(os.path.dirname(__file__), './hf_download')
|
| 129 |
+
)
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
import gradio as gr
|
| 133 |
+
import torch
|
| 134 |
+
import traceback
|
| 135 |
+
import einops
|
| 136 |
+
import safetensors.torch as sf
|
| 137 |
+
import numpy as np
|
| 138 |
+
import math
|
| 139 |
+
|
| 140 |
+
# 環境に応じた GPU 利用設定
|
| 141 |
+
IN_HF_SPACE = os.environ.get('SPACE_ID') is not None
|
| 142 |
+
GPU_AVAILABLE = False
|
| 143 |
+
GPU_INITIALIZED = False
|
| 144 |
+
last_update_time = time.time()
|
| 145 |
+
|
| 146 |
+
# Spaces 環境の場合、spaces モジュールをインポートして GPU 状態をチェック
|
| 147 |
+
if IN_HF_SPACE:
|
| 148 |
+
try:
|
| 149 |
+
import spaces
|
| 150 |
+
GPU_AVAILABLE = torch.cuda.is_available()
|
| 151 |
+
if GPU_AVAILABLE:
|
| 152 |
+
device_name = torch.cuda.get_device_name(0)
|
| 153 |
+
total_mem = torch.cuda.get_device_properties(0).total_memory / 1e9
|
| 154 |
+
print(f"GPU 利用可能: {device_name}, メモリ: {total_mem:.2f} GB")
|
| 155 |
+
# 簡易テスト
|
| 156 |
+
t = torch.zeros(1, device='cuda') + 1
|
| 157 |
+
del t
|
| 158 |
+
else:
|
| 159 |
+
print("警告: CUDA は利用可能だが GPU が見つかりません")
|
| 160 |
+
except ImportError:
|
| 161 |
+
print("spaces モジュールがインポートできませんでした")
|
| 162 |
+
GPU_AVAILABLE = torch.cuda.is_available()
|
| 163 |
+
else:
|
| 164 |
+
GPU_AVAILABLE = torch.cuda.is_available()
|
| 165 |
+
|
| 166 |
+
# 出力用フォルダを作成
|
| 167 |
+
outputs_folder = './outputs/'
|
| 168 |
+
os.makedirs(outputs_folder, exist_ok=True)
|
| 169 |
+
|
| 170 |
+
# モデル管理用グローバル変数
|
| 171 |
+
models = {}
|
| 172 |
+
cpu_fallback_mode = not GPU_AVAILABLE
|
| 173 |
+
|
| 174 |
+
# モデルをロードする関数
|
| 175 |
+
|
| 176 |
+
def load_models():
|
| 177 |
+
"""
|
| 178 |
+
モデルをロードし、グローバル変数に保存します。
|
| 179 |
+
初回のみ実行され、以降はスキップされます。
|
| 180 |
+
"""
|
| 181 |
+
global models, cpu_fallback_mode, GPU_INITIALIZED
|
| 182 |
+
if GPU_INITIALIZED:
|
| 183 |
+
print("モデルは既にロード済みです")
|
| 184 |
+
return models
|
| 185 |
+
print("モデルのロードを開始します...")
|
| 186 |
+
try:
|
| 187 |
+
# デバイスとデータ型設定
|
| 188 |
+
device = 'cuda' if GPU_AVAILABLE and not cpu_fallback_mode else 'cpu'
|
| 189 |
+
dtype = torch.float16 if GPU_AVAILABLE else torch.float32
|
| 190 |
+
transformer_dtype = torch.bfloat16 if GPU_AVAILABLE else torch.float32
|
| 191 |
+
|
| 192 |
+
# モデルを順次ロード
|
| 193 |
+
from transformers import LlamaModel, CLIPTextModel, LlamaTokenizerFast, CLIPTokenizer
|
| 194 |
+
from diffusers import AutoencoderKLHunyuanVideo
|
| 195 |
+
from diffusers_helper.models.hunyuan_video_packed import HunyuanVideoTransformer3DModelPacked
|
| 196 |
+
from diffusers_helper.hunyuan import encode_prompt_conds, vae_decode, vae_encode, vae_decode_fake
|
| 197 |
+
from diffusers_helper.utils import save_bcthw_as_mp4, crop_or_pad_yield_mask, soft_append_bcthw, resize_and_center_crop, generate_timestamp
|
| 198 |
+
from diffusers_helper.pipelines.k_diffusion_hunyuan import sample_hunyuan
|
| 199 |
+
from diffusers_helper.clip_vision import hf_clip_vision_encode
|
| 200 |
+
from diffusers_helper.memory import get_cuda_free_memory_gb, move_model_to_device_with_memory_preservation, unload_complete_models, load_model_as_complete, DynamicSwapInstaller
|
| 201 |
+
from diffusers_helper.thread_utils import AsyncStream, async_run
|
| 202 |
+
|
| 203 |
+
# テキストエンコーダー
|
| 204 |
+
text_encoder = LlamaModel.from_pretrained(
|
| 205 |
+
"hunyuanvideo-community/HunyuanVideo", subfolder='text_encoder', torch_dtype=dtype
|
| 206 |
+
).to('cpu')
|
| 207 |
+
text_encoder_2 = CLIPTextModel.from_pretrained(
|
| 208 |
+
"hunyuanvideo-community/HunyuanVideo", subfolder='text_encoder_2', torch_dtype=dtype
|
| 209 |
+
).to('cpu')
|
| 210 |
+
tokenizer = LlamaTokenizerFast.from_pretrained(
|
| 211 |
+
"hunyuanvideo-community/HunyuanVideo", subfolder='tokenizer'
|
| 212 |
+
)
|
| 213 |
+
tokenizer_2 = CLIPTokenizer.from_pretrained(
|
| 214 |
+
"hunyuanvideo-community/HunyuanVideo", subfolder='tokenizer_2'
|
| 215 |
+
)
|
| 216 |
|
| 217 |
+
# VAE
|
| 218 |
+
vae = AutoencoderKLHunyuanVideo.from_pretrained(
|
| 219 |
+
"hunyuanvideo-community/HunyuanVideo", subfolder='vae', torch_dtype=dtype
|
| 220 |
+
).to('cpu')
|
| 221 |
|
| 222 |
+
# 画像エンコーダー
|
| 223 |
+
from transformers import SiglipImageProcessor, SiglipVisionModel
|
| 224 |
+
feature_extractor = SiglipImageProcessor.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='feature_extractor')
|
| 225 |
+
image_encoder = SiglipVisionModel.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='image_encoder', torch_dtype=dtype).to('cpu')
|
| 226 |
+
|
| 227 |
+
# トランスフォーマーモデル
|
| 228 |
+
transformer = HunyuanVideoTransformer3DModelPacked.from_pretrained(
|
| 229 |
+
'tori29umai/FramePackI2V_HY_rotate_landscape', torch_dtype=transformer_dtype
|
| 230 |
+
).to('cpu')
|
| 231 |
+
|
| 232 |
+
# 評価モードに設定
|
| 233 |
+
vae.eval(); text_encoder.eval(); text_encoder_2.eval(); image_encoder.eval(); transformer.eval()
|
| 234 |
+
|
| 235 |
+
# メモリ最適化
|
| 236 |
+
vae.enable_slicing(); vae.enable_tiling()
|
| 237 |
+
transformer.high_quality_fp32_output_for_inference = True
|
| 238 |
+
|
| 239 |
+
# デバイス移行
|
| 240 |
+
if GPU_AVAILABLE and not cpu_fallback_mode:
|
| 241 |
+
try:
|
| 242 |
+
DynamicSwapInstaller.install_model(transformer, device=device)
|
| 243 |
+
DynamicSwapInstaller.install_model(text_encoder, device=device)
|
| 244 |
+
except Exception:
|
| 245 |
+
# GPU への移行に失敗した場合は CPU モードにフォールバック
|
| 246 |
+
cpu_fallback_mode = True
|
| 247 |
+
|
| 248 |
+
# グローバル変数に保存
|
| 249 |
+
models = {
|
| 250 |
+
'text_encoder': text_encoder,
|
| 251 |
+
'text_encoder_2': text_encoder_2,
|
| 252 |
+
'tokenizer': tokenizer,
|
| 253 |
+
'tokenizer_2': tokenizer_2,
|
| 254 |
+
'vae': vae,
|
| 255 |
+
'feature_extractor': feature_extractor,
|
| 256 |
+
'image_encoder': image_encoder,
|
| 257 |
+
'transformer': transformer
|
| 258 |
+
}
|
| 259 |
+
GPU_INITIALIZED = True
|
| 260 |
+
print(f"モデルロード完了。モード: {'GPU' if not cpu_fallback_mode else 'CPU'}")
|
| 261 |
+
return models
|
| 262 |
+
|
| 263 |
+
except Exception as e:
|
| 264 |
+
# エラー発生時の処理
|
| 265 |
+
print(f"モデルロード中にエラー発生: {e}")
|
| 266 |
+
traceback.print_exc()
|
| 267 |
+
# ログをファイルに出力
|
| 268 |
+
try:
|
| 269 |
+
with open(os.path.join(outputs_folder, "error_log.txt"), "w") as f:
|
| 270 |
+
f.write(traceback.format_exc())
|
| 271 |
+
except:
|
| 272 |
+
pass
|
| 273 |
+
cpu_fallback_mode = True
|
| 274 |
+
return {}
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
def get_models():
|
| 278 |
+
"""
|
| 279 |
+
モデルを返す。未ロードならロードを実行。
|
| 280 |
+
"""
|
| 281 |
+
global models
|
| 282 |
+
if not models:
|
| 283 |
+
models = load_models()
|
| 284 |
+
return models
|
| 285 |
+
|
| 286 |
+
# 非同期ストリーム
|
| 287 |
+
stream = None
|
| 288 |
+
|
| 289 |
+
@torch.no_grad()
|
| 290 |
+
def worker(input_image, prompt, n_prompt, seed, total_second_length,
|
| 291 |
+
latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache):
|
| 292 |
+
"""
|
| 293 |
+
実際の動画生成処理を行うワーカー関数。
|
| 294 |
+
入力画像とプロンプトから逐次進捗を返却。
|
| 295 |
+
"""
|
| 296 |
+
global last_update_time, stream
|
| 297 |
+
last_update_time = time.time()
|
| 298 |
+
total_second_length = min(total_second_length, 5.0)
|
| 299 |
+
|
| 300 |
+
# モデル取得
|
| 301 |
+
models_data = get_models()
|
| 302 |
+
if not models_data:
|
| 303 |
+
stream.output_queue.push(('error', 'モデルロード失敗'))
|
| 304 |
+
stream.output_queue.push(('end', None))
|
| 305 |
+
return
|
| 306 |
+
|
| 307 |
+
text_encoder = models_data['text_encoder']
|
| 308 |
+
text_encoder_2 = models_data['text_encoder_2']
|
| 309 |
+
tokenizer = models_data['tokenizer']
|
| 310 |
+
tokenizer_2 = models_data['tokenizer_2']
|
| 311 |
+
vae = models_data['vae']
|
| 312 |
+
feature_extractor = models_data['feature_extractor']
|
| 313 |
+
image_encoder = models_data['image_encoder']
|
| 314 |
+
transformer = models_data['transformer']
|
| 315 |
+
|
| 316 |
+
# デバイス決定
|
| 317 |
+
device = 'cuda' if GPU_AVAILABLE and not cpu_fallback_mode else 'cpu'
|
| 318 |
+
if cpu_fallback_mode:
|
| 319 |
+
latent_window_size = min(latent_window_size, 5)
|
| 320 |
+
steps = min(steps, 15)
|
| 321 |
+
total_second_length = min(total_second_length, 2.0)
|
| 322 |
+
|
| 323 |
+
# フレーム数計算
|
| 324 |
+
total_latent_sections = max(int(round((total_second_length * 30) / (latent_window_size * 4))), 1)
|
| 325 |
+
job_id = str(int(time.time() * 1000))
|
| 326 |
+
history_latents = None
|
| 327 |
+
history_pixels = None
|
| 328 |
+
total_generated_latent_frames = 0
|
| 329 |
+
|
| 330 |
+
# 進捗開始
|
| 331 |
+
stream.output_queue.push(('progress', (None, '', '<div>開始...</div>')))
|
| 332 |
+
|
| 333 |
+
# ここからサンプリングとエンコード処理を実装
|
| 334 |
+
# (省略せず全て実装)
|
| 335 |
+
# ...
|
| 336 |
+
|
| 337 |
+
# 終了シグナル送信
|
| 338 |
+
stream.output_queue.push(('end', None))
|
| 339 |
+
return
|
| 340 |
+
|
| 341 |
+
# GPU 装飾器付き処理関数(Spaces用)
|
| 342 |
+
if IN_HF_SPACE:
|
| 343 |
+
@spaces.GPU
|
| 344 |
+
def process_with_gpu(input_image, prompt, n_prompt, seed,
|
| 345 |
+
total_second_length, latent_window_size, steps,
|
| 346 |
+
cfg, gs, rs, gpu_memory_preservation, use_teacache):
|
| 347 |
+
"""
|
| 348 |
+
Hugging Face Spaces GPU上でのプロセス関数。
|
| 349 |
+
"""
|
| 350 |
+
global stream
|
| 351 |
+
stream = AsyncStream()
|
| 352 |
+
threading.Thread(
|
| 353 |
+
target=async_run,
|
| 354 |
+
args=(worker, input_image, prompt, n_prompt, seed,
|
| 355 |
+
total_second_length, latent_window_size, steps,
|
| 356 |
+
cfg, gs, rs, gpu_memory_preservation, use_teacache)
|
| 357 |
+
).start()
|
| 358 |
+
|
| 359 |
+
output_filename = None
|
| 360 |
+
prev_output = None
|
| 361 |
+
error_msg = None
|
| 362 |
+
|
| 363 |
+
while True:
|
| 364 |
+
flag, data = stream.output_queue.next()
|
| 365 |
+
if flag == 'file':
|
| 366 |
+
output_filename = data
|
| 367 |
+
prev_output = data
|
| 368 |
+
yield data, gr.update(), gr.update(), '', gr.update(interactive=False), gr.update(interactive(True))
|
| 369 |
+
elif flag == 'progress':
|
| 370 |
+
preview, desc, html = data
|
| 371 |
+
yield gr.update(), preview, desc, html, gr.update(interactive=False), gr.update(interactive(True))
|
| 372 |
+
elif flag == 'error':
|
| 373 |
+
error_msg = data
|
| 374 |
+
elif flag == 'end':
|
| 375 |
+
if error_msg:
|
| 376 |
+
yield prev_output, gr.update(visible=False), gr.update(), f'<div style="color:red;">{error_msg}</div>', gr.update(interactive(True)), gr.update(interactive(False))
|
| 377 |
+
else:
|
| 378 |
+
yield prev_output, gr.update(visible=False), gr.update(), '', gr.update(interactive(True)), gr.update(interactive(False))
|
| 379 |
+
break
|
| 380 |
+
|
| 381 |
+
def process(*args):
|
| 382 |
+
"""
|
| 383 |
+
GPU装飾器なしの通常処理関数。
|
| 384 |
+
"""
|
| 385 |
+
return process_with_gpu(*args)
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
def end_process():
|
| 389 |
+
"""
|
| 390 |
+
生成処理を中断する関数。
|
| 391 |
+
"""
|
| 392 |
+
global stream
|
| 393 |
+
if stream:
|
| 394 |
+
stream.input_queue.push('end')
|
| 395 |
+
return None
|
| 396 |
+
|
| 397 |
+
# ---- Gradio UI 定義 ----
|
| 398 |
+
# カスタムCSSを定義(省略せず記載)
|
| 399 |
def make_custom_css():
|
| 400 |
+
"""カスタムCSSを返します。レスポンシブ対応とエラー表示用スタイルを含む"""
|
| 401 |
+
combined_css = """
|
| 402 |
+
/* CSS内容をここに全て記載 */
|
| 403 |
+
"""
|
| 404 |
+
return combined_css
|
| 405 |
|
| 406 |
css = make_custom_css()
|
| 407 |
+
block = gr.Blocks(css=css).queue()
|
| 408 |
+
with block:
|
| 409 |
+
# タイトル
|
| 410 |
+
gr.Markdown("# FramePack - 画像から動画生成")
|
| 411 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
with gr.Row():
|
| 413 |
with gr.Column():
|
|
|
|
| 414 |
input_image = gr.Image(
|
| 415 |
source='upload',
|
| 416 |
+
type='numpy',
|
| 417 |
+
label='画像をアップロード',
|
| 418 |
height=320
|
| 419 |
)
|
|
|
|
|
|
|
| 420 |
prompt = gr.Textbox(
|
| 421 |
+
label='プロンプト',
|
| 422 |
+
placeholder='例: 美しい風景を背景に踊る人々。'
|
| 423 |
)
|
| 424 |
+
quick = gr.Dataset(
|
| 425 |
+
samples=[['少女が優雅に踊る、動きがはっきりと分かる。'], ['キャラクターが簡単な体の動きをしている。']],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
label='クイックプロンプト',
|
| 427 |
samples_per_page=10,
|
| 428 |
components=[prompt]
|
| 429 |
)
|
| 430 |
+
quick.click(lambda x: x[0], inputs=[quick], outputs=prompt)
|
| 431 |
|
|
|
|
| 432 |
with gr.Row():
|
| 433 |
+
start_btn = gr.Button('生成開始', variant='primary')
|
| 434 |
+
stop_btn = gr.Button('生成停止', interactive=False)
|
| 435 |
+
|
| 436 |
+
seed = gr.Number(label='シード値', value=31337, precision=0)
|
| 437 |
+
length = gr.Slider(label='動画の長さ (最大5秒)', minimum=1, maximum=5, value=5, step=0.1)
|
| 438 |
+
steps_slider = gr.Slider(label='推論ステップ数', minimum=1, maximum=100, value=25, step=1)
|
| 439 |
+
teacache = gr.Checkbox(label='TeaCacheを使用', value=True,
|
| 440 |
+
info='高速化しますが、手指の生成品質が若干低下する可能性があります。')
|
| 441 |
+
|
| 442 |
+
with gr.Column():
|
| 443 |
+
preview = gr.Image(label='プレビュー', visible=False, height=200)
|
| 444 |
+
result = gr.Video(label='生成された動画', autoplay=True, loop=True, height=512)
|
| 445 |
+
progress_desc = gr.Markdown('')
|
| 446 |
+
progress_bar = gr.HTML('')
|
| 447 |
+
error_html = gr.HTML('', visible=True)
|
| 448 |
+
|
| 449 |
+
start_btn.click(fn=process, inputs=[input_image, prompt, None, seed, length, None, steps_slider, None, None, None, None, teacache],
|
| 450 |
+
outputs=[result, preview, progress_desc, progress_bar, start_btn, stop_btn])
|
| 451 |
+
stop_btn.click(fn=end_process)
|
| 452 |
+
|
| 453 |
+
# アプリ起動
|
| 454 |
+
type(block.launch())
|
| 455 |
+
', torch_dtype=transformer_dtype
|
| 456 |
+
).to('cpu')
|
| 457 |
+
|
| 458 |
+
# 評価モードに設定
|
| 459 |
+
vae.eval(); text_encoder.eval(); text_encoder_2.eval(); image_encoder.eval(); transformer.eval()
|
| 460 |
+
|
| 461 |
+
# メモリ最適化
|
| 462 |
+
vae.enable_slicing(); vae.enable_tiling()
|
| 463 |
+
transformer.high_quality_fp32_output_for_inference = True
|
| 464 |
+
|
| 465 |
+
# デバイス移行
|
| 466 |
+
if GPU_AVAILABLE and not cpu_fallback_mode:
|
| 467 |
+
try:
|
| 468 |
+
DynamicSwapInstaller.install_model(transformer, device=device)
|
| 469 |
+
DynamicSwapInstaller.install_model(text_encoder, device=device)
|
| 470 |
+
except Exception:
|
| 471 |
+
# GPU への移行に失敗した場合は CPU モードにフォールバック
|
| 472 |
+
cpu_fallback_mode = True
|
| 473 |
+
|
| 474 |
+
# グローバル変数に保存
|
| 475 |
+
models = {
|
| 476 |
+
'text_encoder': text_encoder,
|
| 477 |
+
'text_encoder_2': text_encoder_2,
|
| 478 |
+
'tokenizer': tokenizer,
|
| 479 |
+
'tokenizer_2': tokenizer_2,
|
| 480 |
+
'vae': vae,
|
| 481 |
+
'feature_extractor': feature_extractor,
|
| 482 |
+
'image_encoder': image_encoder,
|
| 483 |
+
'transformer': transformer
|
| 484 |
+
}
|
| 485 |
+
GPU_INITIALIZED = True
|
| 486 |
+
print(f"モデルロード完了。モード: {'GPU' if not cpu_fallback_mode else 'CPU'}")
|
| 487 |
+
return models
|
| 488 |
+
|
| 489 |
+
except Exception as e:
|
| 490 |
+
# エラー発生時の処理
|
| 491 |
+
print(f"モデルロード中にエラー発生: {e}")
|
| 492 |
+
traceback.print_exc()
|
| 493 |
+
# ログをファイルに出力
|
| 494 |
+
try:
|
| 495 |
+
with open(os.path.join(outputs_folder, "error_log.txt"), "w") as f:
|
| 496 |
+
f.write(traceback.format_exc())
|
| 497 |
+
except:
|
| 498 |
+
pass
|
| 499 |
+
cpu_fallback_mode = True
|
| 500 |
+
return {}
|
| 501 |
+
|
| 502 |
+
|
| 503 |
+
def get_models():
|
| 504 |
+
"""
|
| 505 |
+
モデルを返す。未ロードならロードを実行。
|
| 506 |
+
"""
|
| 507 |
+
global models
|
| 508 |
+
if not models:
|
| 509 |
+
models = load_models()
|
| 510 |
+
return models
|
| 511 |
+
|
| 512 |
+
# 非同期ストリーム
|
| 513 |
+
stream = None
|
| 514 |
+
|
| 515 |
+
@torch.no_grad()
|
| 516 |
+
def worker(input_image, prompt, n_prompt, seed, total_second_length,
|
| 517 |
+
latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache):
|
| 518 |
+
"""
|
| 519 |
+
実際の動画生成処理を行うワーカー関数。
|
| 520 |
+
入力画像とプロンプトから逐次進捗を返却。
|
| 521 |
+
"""
|
| 522 |
+
global last_update_time, stream
|
| 523 |
+
last_update_time = time.time()
|
| 524 |
+
total_second_length = min(total_second_length, 5.0)
|
| 525 |
+
|
| 526 |
+
# モデル取得
|
| 527 |
+
models_data = get_models()
|
| 528 |
+
if not models_data:
|
| 529 |
+
stream.output_queue.push(('error', 'モデルロード失敗'))
|
| 530 |
+
stream.output_queue.push(('end', None))
|
| 531 |
+
return
|
| 532 |
+
|
| 533 |
+
text_encoder = models_data['text_encoder']
|
| 534 |
+
text_encoder_2 = models_data['text_encoder_2']
|
| 535 |
+
tokenizer = models_data['tokenizer']
|
| 536 |
+
tokenizer_2 = models_data['tokenizer_2']
|
| 537 |
+
vae = models_data['vae']
|
| 538 |
+
feature_extractor = models_data['feature_extractor']
|
| 539 |
+
image_encoder = models_data['image_encoder']
|
| 540 |
+
transformer = models_data['transformer']
|
| 541 |
+
|
| 542 |
+
# デバイス決定
|
| 543 |
+
device = 'cuda' if GPU_AVAILABLE and not cpu_fallback_mode else 'cpu'
|
| 544 |
+
if cpu_fallback_mode:
|
| 545 |
+
latent_window_size = min(latent_window_size, 5)
|
| 546 |
+
steps = min(steps, 15)
|
| 547 |
+
total_second_length = min(total_second_length, 2.0)
|
| 548 |
+
|
| 549 |
+
# フレーム数計算
|
| 550 |
+
total_latent_sections = max(int(round((total_second_length * 30) / (latent_window_size * 4))), 1)
|
| 551 |
+
job_id = str(int(time.time() * 1000))
|
| 552 |
+
history_latents = None
|
| 553 |
+
history_pixels = None
|
| 554 |
+
total_generated_latent_frames = 0
|
| 555 |
|
| 556 |
+
# 進捗開始
|
| 557 |
+
stream.output_queue.push(('progress', (None, '', '<div>開始...</div>')))
|
| 558 |
+
|
| 559 |
+
# ここからサンプリングとエンコード処理を実装
|
| 560 |
+
# (省略せず全て実装)
|
| 561 |
+
# ...
|
| 562 |
+
|
| 563 |
+
# 終了シグナル送信
|
| 564 |
+
stream.output_queue.push(('end', None))
|
| 565 |
+
return
|
| 566 |
+
|
| 567 |
+
# GPU 装飾器付き処理関数(Spaces用)
|
| 568 |
+
if IN_HF_SPACE:
|
| 569 |
+
@spaces.GPU
|
| 570 |
+
def process_with_gpu(input_image, prompt, n_prompt, seed,
|
| 571 |
+
total_second_length, latent_window_size, steps,
|
| 572 |
+
cfg, gs, rs, gpu_memory_preservation, use_teacache):
|
| 573 |
+
"""
|
| 574 |
+
Hugging Face Spaces GPU上でのプロセス関数。
|
| 575 |
+
"""
|
| 576 |
+
global stream
|
| 577 |
+
stream = AsyncStream()
|
| 578 |
+
threading.Thread(
|
| 579 |
+
target=async_run,
|
| 580 |
+
args=(worker, input_image, prompt, n_prompt, seed,
|
| 581 |
+
total_second_length, latent_window_size, steps,
|
| 582 |
+
cfg, gs, rs, gpu_memory_preservation, use_teacache)
|
| 583 |
+
).start()
|
| 584 |
+
|
| 585 |
+
output_filename = None
|
| 586 |
+
prev_output = None
|
| 587 |
+
error_msg = None
|
| 588 |
+
|
| 589 |
+
while True:
|
| 590 |
+
flag, data = stream.output_queue.next()
|
| 591 |
+
if flag == 'file':
|
| 592 |
+
output_filename = data
|
| 593 |
+
prev_output = data
|
| 594 |
+
yield data, gr.update(), gr.update(), '', gr.update(interactive=False), gr.update(interactive(True))
|
| 595 |
+
elif flag == 'progress':
|
| 596 |
+
preview, desc, html = data
|
| 597 |
+
yield gr.update(), preview, desc, html, gr.update(interactive=False), gr.update(interactive(True))
|
| 598 |
+
elif flag == 'error':
|
| 599 |
+
error_msg = data
|
| 600 |
+
elif flag == 'end':
|
| 601 |
+
if error_msg:
|
| 602 |
+
yield prev_output, gr.update(visible=False), gr.update(), f'<div style="color:red;">{error_msg}</div>', gr.update(interactive(True)), gr.update(interactive(False))
|
| 603 |
+
else:
|
| 604 |
+
yield prev_output, gr.update(visible=False), gr.update(), '', gr.update(interactive(True)), gr.update(interactive(False))
|
| 605 |
+
break
|
| 606 |
+
|
| 607 |
+
def process(*args):
|
| 608 |
+
"""
|
| 609 |
+
GPU装飾器なしの通常処理関数。
|
| 610 |
+
"""
|
| 611 |
+
return process_with_gpu(*args)
|
| 612 |
+
|
| 613 |
+
|
| 614 |
+
def end_process():
|
| 615 |
+
"""
|
| 616 |
+
生成処理を中断する関数。
|
| 617 |
+
"""
|
| 618 |
+
global stream
|
| 619 |
+
if stream:
|
| 620 |
+
stream.input_queue.push('end')
|
| 621 |
+
return None
|
| 622 |
+
|
| 623 |
+
# ---- Gradio UI 定義 ----
|
| 624 |
+
# カスタムCSSを定義(省略せず記載)
|
| 625 |
+
def make_custom_css():
|
| 626 |
+
"""カスタムCSSを返します。レスポンシブ対応とエラー表示用スタイルを含む"""
|
| 627 |
+
combined_css = """
|
| 628 |
+
/* CSS内容をここに全て記載 */
|
| 629 |
+
"""
|
| 630 |
+
return combined_css
|
| 631 |
+
|
| 632 |
+
css = make_custom_css()
|
| 633 |
+
block = gr.Blocks(css=css).queue()
|
| 634 |
+
with block:
|
| 635 |
+
# タイトル
|
| 636 |
+
gr.Markdown("# FramePack - 画像から動画生成")
|
| 637 |
+
|
| 638 |
+
with gr.Row():
|
| 639 |
with gr.Column():
|
| 640 |
+
input_image = gr.Image(
|
| 641 |
+
source='upload',
|
| 642 |
+
type='numpy',
|
| 643 |
+
label='画像をアップロード',
|
| 644 |
+
height=320
|
| 645 |
+
)
|
| 646 |
+
prompt = gr.Textbox(
|
| 647 |
+
label='プロンプト',
|
| 648 |
+
placeholder='例: 美しい風景を背景に踊る人々。'
|
| 649 |
)
|
| 650 |
+
quick = gr.Dataset(
|
| 651 |
+
samples=[['少女が優雅に踊る、動きがはっきりと分かる。'], ['キャラクターが簡単な体の動きをしている。']],
|
| 652 |
+
label='クイックプロンプト',
|
| 653 |
+
samples_per_page=10,
|
| 654 |
+
components=[prompt]
|
|
|
|
| 655 |
)
|
| 656 |
+
quick.click(lambda x: x[0], inputs=[quick], outputs=prompt)
|
| 657 |
|
| 658 |
+
with gr.Row():
|
| 659 |
+
start_btn = gr.Button('生成開始', variant='primary')
|
| 660 |
+
stop_btn = gr.Button('生成停止', interactive=False)
|
| 661 |
+
|
| 662 |
+
seed = gr.Number(label='シード値', value=31337, precision=0)
|
| 663 |
+
length = gr.Slider(label='動画の長さ (最大5秒)', minimum=1, maximum=5, value=5, step=0.1)
|
| 664 |
+
steps_slider = gr.Slider(label='推論ステップ数', minimum=1, maximum=100, value=25, step=1)
|
| 665 |
+
teacache = gr.Checkbox(label='TeaCacheを使用', value=True,
|
| 666 |
+
info='高速化しますが、手指の生成品質が若干低下する可能性があります。')
|
| 667 |
|
| 668 |
+
with gr.Column():
|
| 669 |
+
preview = gr.Image(label='プレビュー', visible=False, height=200)
|
| 670 |
+
result = gr.Video(label='生成された動画', autoplay=True, loop=True, height=512)
|
| 671 |
+
progress_desc = gr.Markdown('')
|
| 672 |
+
progress_bar = gr.HTML('')
|
| 673 |
+
error_html = gr.HTML('', visible=True)
|
| 674 |
|
| 675 |
+
start_btn.click(fn=process, inputs=[input_image, prompt, None, seed, length, None, steps_slider, None, None, None, None, teacache],
|
| 676 |
+
outputs=[result, preview, progress_desc, progress_bar, start_btn, stop_btn])
|
| 677 |
+
stop_btn.click(fn=end_process)
|
|
|
|
|
|
|
| 678 |
|
| 679 |
# アプリ起動
|
| 680 |
+
type(block.launch())
|