Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,135 +1,3 @@
|
|
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 |
-
'tori29umai/FramePackI2V_HY_rotate_landscape', torch_dtype=transformer_dtype
|
116 |
-
).to('cpu')
|
117 |
-
|
118 |
-
import os
|
119 |
-
import threading
|
120 |
-
import time
|
121 |
-
import requests
|
122 |
-
from requests.adapters import HTTPAdapter
|
123 |
-
from urllib3.util.retry import Retry
|
124 |
-
import json
|
125 |
-
|
126 |
-
# Hugging Face ダウンロード用キャッシュディレクトリを設定
|
127 |
-
os.environ['HF_HOME'] = os.path.abspath(
|
128 |
-
os.path.realpath(
|
129 |
-
os.path.join(os.path.dirname(__file__), './hf_download')
|
130 |
-
)
|
131 |
-
)
|
132 |
-
|
133 |
import gradio as gr
|
134 |
import torch
|
135 |
import traceback
|
@@ -420,10 +288,10 @@ with block:
|
|
420 |
)
|
421 |
prompt = gr.Textbox(
|
422 |
label='プロンプト',
|
423 |
-
placeholder='
|
424 |
)
|
425 |
quick = gr.Dataset(
|
426 |
-
samples=[['
|
427 |
label='クイックプロンプト',
|
428 |
samples_per_page=10,
|
429 |
components=[prompt]
|
@@ -556,10 +424,6 @@ def worker(input_image, prompt, n_prompt, seed, total_second_length,
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import traceback
|
|
|
288 |
)
|
289 |
prompt = gr.Textbox(
|
290 |
label='プロンプト',
|
291 |
+
placeholder='The camera smoothly orbits around the center of the scene, keeping the center point fixed and always in view'
|
292 |
)
|
293 |
quick = gr.Dataset(
|
294 |
+
samples=[['The camera smoothly orbits around the center of the scene, keeping the center point fixed and always in view']],
|
295 |
label='クイックプロンプト',
|
296 |
samples_per_page=10,
|
297 |
components=[prompt]
|
|
|
424 |
# 進捗開始
|
425 |
stream.output_queue.push(('progress', (None, '', '<div>開始...</div>')))
|
426 |
|
|
|
|
|
|
|
|
|
427 |
# 終了シグナル送信
|
428 |
stream.output_queue.push(('end', None))
|
429 |
return
|