Spaces:
Runtime error
Runtime error
chandansocial7
commited on
Commit
•
79a541c
1
Parent(s):
c94f11a
Upload 4 files
Browse files- .gitattributes +35 -35
- README.md +13 -13
- app.py +493 -0
- requirements.txt +4 -0
.gitattributes
CHANGED
@@ -1,35 +1,35 @@
|
|
1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
-
---
|
2 |
-
title: Thisismine
|
3 |
-
emoji: 📉
|
4 |
-
colorFrom: purple
|
5 |
-
colorTo: green
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 4.31.4
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
license: apache-2.0
|
11 |
-
---
|
12 |
-
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
---
|
2 |
+
title: Thisismine
|
3 |
+
emoji: 📉
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: green
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 4.31.4
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: apache-2.0
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,493 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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+
import argparse
|
10 |
+
import importlib
|
11 |
+
import os
|
12 |
+
import subprocess
|
13 |
+
import sys
|
14 |
+
import re
|
15 |
+
import json
|
16 |
+
import math
|
17 |
+
|
18 |
+
import spaces
|
19 |
+
import torch
|
20 |
+
|
21 |
+
import gradio as gr
|
22 |
+
|
23 |
+
|
24 |
+
MODEL_TYPES = ["v1.1"]
|
25 |
+
CONFIG_MAP = {
|
26 |
+
"v1.1-stage2": "configs/opensora-v1-1/inference/sample-ref.py",
|
27 |
+
"v1.1-stage3": "configs/opensora-v1-1/inference/sample-ref.py",
|
28 |
+
}
|
29 |
+
HF_STDIT_MAP = {
|
30 |
+
"v1.1-stage2": "hpcai-tech/OpenSora-STDiT-v2-stage2",
|
31 |
+
"v1.1-stage3": "hpcai-tech/OpenSora-STDiT-v2-stage3",
|
32 |
+
}
|
33 |
+
RESOLUTION_MAP = {
|
34 |
+
"144p": (256, 144),
|
35 |
+
"240p": (426, 240),
|
36 |
+
"360p": (480, 360),
|
37 |
+
"480p": (858, 480),
|
38 |
+
"720p": (1280, 720),
|
39 |
+
"1080p": (1920, 1080)
|
40 |
+
}
|
41 |
+
|
42 |
+
|
43 |
+
# ============================
|
44 |
+
# Utils
|
45 |
+
# ============================
|
46 |
+
def collect_references_batch(reference_paths, vae, image_size):
|
47 |
+
from opensora.datasets.utils import read_from_path
|
48 |
+
|
49 |
+
refs_x = []
|
50 |
+
for reference_path in reference_paths:
|
51 |
+
if reference_path is None:
|
52 |
+
refs_x.append([])
|
53 |
+
continue
|
54 |
+
ref_path = reference_path.split(";")
|
55 |
+
ref = []
|
56 |
+
for r_path in ref_path:
|
57 |
+
r = read_from_path(r_path, image_size, transform_name="resize_crop")
|
58 |
+
r_x = vae.encode(r.unsqueeze(0).to(vae.device, vae.dtype))
|
59 |
+
r_x = r_x.squeeze(0)
|
60 |
+
ref.append(r_x)
|
61 |
+
refs_x.append(ref)
|
62 |
+
# refs_x: [batch, ref_num, C, T, H, W]
|
63 |
+
return refs_x
|
64 |
+
|
65 |
+
|
66 |
+
def process_mask_strategy(mask_strategy):
|
67 |
+
mask_batch = []
|
68 |
+
mask_strategy = mask_strategy.split(";")
|
69 |
+
for mask in mask_strategy:
|
70 |
+
mask_group = mask.split(",")
|
71 |
+
assert len(mask_group) >= 1 and len(mask_group) <= 6, f"Invalid mask strategy: {mask}"
|
72 |
+
if len(mask_group) == 1:
|
73 |
+
mask_group.extend(["0", "0", "0", "1", "0"])
|
74 |
+
elif len(mask_group) == 2:
|
75 |
+
mask_group.extend(["0", "0", "1", "0"])
|
76 |
+
elif len(mask_group) == 3:
|
77 |
+
mask_group.extend(["0", "1", "0"])
|
78 |
+
elif len(mask_group) == 4:
|
79 |
+
mask_group.extend(["1", "0"])
|
80 |
+
elif len(mask_group) == 5:
|
81 |
+
mask_group.append("0")
|
82 |
+
mask_batch.append(mask_group)
|
83 |
+
return mask_batch
|
84 |
+
|
85 |
+
|
86 |
+
def apply_mask_strategy(z, refs_x, mask_strategys, loop_i):
|
87 |
+
masks = []
|
88 |
+
for i, mask_strategy in enumerate(mask_strategys):
|
89 |
+
mask = torch.ones(z.shape[2], dtype=torch.float, device=z.device)
|
90 |
+
if mask_strategy is None:
|
91 |
+
masks.append(mask)
|
92 |
+
continue
|
93 |
+
mask_strategy = process_mask_strategy(mask_strategy)
|
94 |
+
for mst in mask_strategy:
|
95 |
+
loop_id, m_id, m_ref_start, m_target_start, m_length, edit_ratio = mst
|
96 |
+
loop_id = int(loop_id)
|
97 |
+
if loop_id != loop_i:
|
98 |
+
continue
|
99 |
+
m_id = int(m_id)
|
100 |
+
m_ref_start = int(m_ref_start)
|
101 |
+
m_length = int(m_length)
|
102 |
+
m_target_start = int(m_target_start)
|
103 |
+
edit_ratio = float(edit_ratio)
|
104 |
+
ref = refs_x[i][m_id] # [C, T, H, W]
|
105 |
+
if m_ref_start < 0:
|
106 |
+
m_ref_start = ref.shape[1] + m_ref_start
|
107 |
+
if m_target_start < 0:
|
108 |
+
# z: [B, C, T, H, W]
|
109 |
+
m_target_start = z.shape[2] + m_target_start
|
110 |
+
z[i, :, m_target_start : m_target_start + m_length] = ref[:, m_ref_start : m_ref_start + m_length]
|
111 |
+
mask[m_target_start : m_target_start + m_length] = edit_ratio
|
112 |
+
masks.append(mask)
|
113 |
+
masks = torch.stack(masks)
|
114 |
+
return masks
|
115 |
+
|
116 |
+
|
117 |
+
def process_prompts(prompts, num_loop):
|
118 |
+
from opensora.models.text_encoder.t5 import text_preprocessing
|
119 |
+
|
120 |
+
ret_prompts = []
|
121 |
+
for prompt in prompts:
|
122 |
+
if prompt.startswith("|0|"):
|
123 |
+
prompt_list = prompt.split("|")[1:]
|
124 |
+
text_list = []
|
125 |
+
for i in range(0, len(prompt_list), 2):
|
126 |
+
start_loop = int(prompt_list[i])
|
127 |
+
text = prompt_list[i + 1]
|
128 |
+
text = text_preprocessing(text)
|
129 |
+
end_loop = int(prompt_list[i + 2]) if i + 2 < len(prompt_list) else num_loop
|
130 |
+
text_list.extend([text] * (end_loop - start_loop))
|
131 |
+
assert len(text_list) == num_loop, f"Prompt loop mismatch: {len(text_list)} != {num_loop}"
|
132 |
+
ret_prompts.append(text_list)
|
133 |
+
else:
|
134 |
+
prompt = text_preprocessing(prompt)
|
135 |
+
ret_prompts.append([prompt] * num_loop)
|
136 |
+
return ret_prompts
|
137 |
+
|
138 |
+
|
139 |
+
def extract_json_from_prompts(prompts):
|
140 |
+
additional_infos = []
|
141 |
+
ret_prompts = []
|
142 |
+
for prompt in prompts:
|
143 |
+
parts = re.split(r"(?=[{\[])", prompt)
|
144 |
+
assert len(parts) <= 2, f"Invalid prompt: {prompt}"
|
145 |
+
ret_prompts.append(parts[0])
|
146 |
+
if len(parts) == 1:
|
147 |
+
additional_infos.append({})
|
148 |
+
else:
|
149 |
+
additional_infos.append(json.loads(parts[1]))
|
150 |
+
return ret_prompts, additional_infos
|
151 |
+
|
152 |
+
|
153 |
+
# ============================
|
154 |
+
# Runtime Environment
|
155 |
+
# ============================
|
156 |
+
def install_dependencies(enable_optimization=False):
|
157 |
+
"""
|
158 |
+
Install the required dependencies for the demo if they are not already installed.
|
159 |
+
"""
|
160 |
+
|
161 |
+
def _is_package_available(name) -> bool:
|
162 |
+
try:
|
163 |
+
importlib.import_module(name)
|
164 |
+
return True
|
165 |
+
except (ImportError, ModuleNotFoundError):
|
166 |
+
return False
|
167 |
+
|
168 |
+
# flash attention is needed no matter optimization is enabled or not
|
169 |
+
# because Hugging Face transformers detects flash_attn is a dependency in STDiT
|
170 |
+
# thus, we need to install it no matter what
|
171 |
+
if not _is_package_available("flash_attn"):
|
172 |
+
subprocess.run(
|
173 |
+
f"{sys.executable} -m pip install flash-attn --no-build-isolation",
|
174 |
+
env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
|
175 |
+
shell=True,
|
176 |
+
)
|
177 |
+
|
178 |
+
if enable_optimization:
|
179 |
+
# install apex for fused layernorm
|
180 |
+
if not _is_package_available("apex"):
|
181 |
+
subprocess.run(
|
182 |
+
f'{sys.executable} -m pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --config-settings "--build-option=--cpp_ext" --config-settings "--build-option=--cuda_ext" git+https://github.com/NVIDIA/apex.git',
|
183 |
+
shell=True,
|
184 |
+
)
|
185 |
+
|
186 |
+
# install ninja
|
187 |
+
if not _is_package_available("ninja"):
|
188 |
+
subprocess.run(f"{sys.executable} -m pip install ninja", shell=True)
|
189 |
+
|
190 |
+
# install xformers
|
191 |
+
if not _is_package_available("xformers"):
|
192 |
+
subprocess.run(
|
193 |
+
f"{sys.executable} -m pip install -v -U git+https://github.com/facebookresearch/xformers.git@main#egg=xformers",
|
194 |
+
shell=True,
|
195 |
+
)
|
196 |
+
|
197 |
+
|
198 |
+
# ============================
|
199 |
+
# Model-related
|
200 |
+
# ============================
|
201 |
+
def read_config(config_path):
|
202 |
+
"""
|
203 |
+
Read the configuration file.
|
204 |
+
"""
|
205 |
+
from mmengine.config import Config
|
206 |
+
|
207 |
+
return Config.fromfile(config_path)
|
208 |
+
|
209 |
+
|
210 |
+
def build_models(model_type, config, enable_optimization=False):
|
211 |
+
"""
|
212 |
+
Build the models for the given model type and configuration.
|
213 |
+
"""
|
214 |
+
# build vae
|
215 |
+
from opensora.registry import MODELS, build_module
|
216 |
+
|
217 |
+
vae = build_module(config.vae, MODELS).cuda()
|
218 |
+
|
219 |
+
# build text encoder
|
220 |
+
text_encoder = build_module(config.text_encoder, MODELS) # T5 must be fp32
|
221 |
+
text_encoder.t5.model = text_encoder.t5.model.cuda()
|
222 |
+
|
223 |
+
# build stdit
|
224 |
+
# we load model from HuggingFace directly so that we don't need to
|
225 |
+
# handle model download logic in HuggingFace Space
|
226 |
+
from transformers import AutoModel
|
227 |
+
|
228 |
+
stdit = AutoModel.from_pretrained(
|
229 |
+
HF_STDIT_MAP[model_type],
|
230 |
+
enable_flash_attn=enable_optimization,
|
231 |
+
trust_remote_code=True,
|
232 |
+
).cuda()
|
233 |
+
|
234 |
+
# build scheduler
|
235 |
+
from opensora.registry import SCHEDULERS
|
236 |
+
|
237 |
+
scheduler = build_module(config.scheduler, SCHEDULERS)
|
238 |
+
|
239 |
+
# hack for classifier-free guidance
|
240 |
+
text_encoder.y_embedder = stdit.y_embedder
|
241 |
+
|
242 |
+
# move modelst to device
|
243 |
+
vae = vae.to(torch.bfloat16).eval()
|
244 |
+
text_encoder.t5.model = text_encoder.t5.model.eval() # t5 must be in fp32
|
245 |
+
stdit = stdit.to(torch.bfloat16).eval()
|
246 |
+
|
247 |
+
# clear cuda
|
248 |
+
torch.cuda.empty_cache()
|
249 |
+
return vae, text_encoder, stdit, scheduler
|
250 |
+
|
251 |
+
|
252 |
+
def parse_args():
|
253 |
+
parser = argparse.ArgumentParser()
|
254 |
+
parser.add_argument(
|
255 |
+
"--model-type",
|
256 |
+
default="v1.1-stage3",
|
257 |
+
choices=MODEL_TYPES,
|
258 |
+
help=f"The type of model to run for the Gradio App, can only be {MODEL_TYPES}",
|
259 |
+
)
|
260 |
+
parser.add_argument("--output", default="./outputs", type=str, help="The path to the output folder")
|
261 |
+
parser.add_argument("--port", default=None, type=int, help="The port to run the Gradio App on.")
|
262 |
+
parser.add_argument("--host", default=None, type=str, help="The host to run the Gradio App on.")
|
263 |
+
parser.add_argument("--share", action="store_true", help="Whether to share this gradio demo.")
|
264 |
+
parser.add_argument(
|
265 |
+
"--enable-optimization",
|
266 |
+
action="store_true",
|
267 |
+
help="Whether to enable optimization such as flash attention and fused layernorm",
|
268 |
+
)
|
269 |
+
return parser.parse_args()
|
270 |
+
|
271 |
+
|
272 |
+
# ============================
|
273 |
+
# Main Gradio Script
|
274 |
+
# ============================
|
275 |
+
# as `run_inference` needs to be wrapped by `spaces.GPU` and the input can only be the prompt text
|
276 |
+
# so we can't pass the models to `run_inference` as arguments.
|
277 |
+
# instead, we need to define them globally so that we can access these models inside `run_inference`
|
278 |
+
|
279 |
+
# read config
|
280 |
+
args = parse_args()
|
281 |
+
config = read_config(CONFIG_MAP[args.model_type])
|
282 |
+
|
283 |
+
# make outputs dir
|
284 |
+
os.makedirs(args.output, exist_ok=True)
|
285 |
+
|
286 |
+
# disable torch jit as it can cause failure in gradio SDK
|
287 |
+
# gradio sdk uses torch with cuda 11.3
|
288 |
+
torch.jit._state.disable()
|
289 |
+
|
290 |
+
# set up
|
291 |
+
install_dependencies(enable_optimization=args.enable_optimization)
|
292 |
+
|
293 |
+
# import after installation
|
294 |
+
from opensora.datasets import IMG_FPS, save_sample
|
295 |
+
from opensora.utils.misc import to_torch_dtype
|
296 |
+
|
297 |
+
# some global variables
|
298 |
+
dtype = to_torch_dtype(config.dtype)
|
299 |
+
device = torch.device("cuda")
|
300 |
+
|
301 |
+
# build model
|
302 |
+
vae, text_encoder, stdit, scheduler = build_models(args.model_type, config, enable_optimization=args.enable_optimization)
|
303 |
+
|
304 |
+
|
305 |
+
@spaces.GPU(duration=200)
|
306 |
+
def run_inference(mode, prompt_text, resolution, length, reference_image):
|
307 |
+
with torch.inference_mode():
|
308 |
+
# ======================
|
309 |
+
# 1. Preparation
|
310 |
+
# ======================
|
311 |
+
# parse the inputs
|
312 |
+
resolution = RESOLUTION_MAP[resolution]
|
313 |
+
|
314 |
+
# compute number of loops
|
315 |
+
num_seconds = int(length.rstrip('s'))
|
316 |
+
total_number_of_frames = num_seconds * config.fps / config.frame_interval
|
317 |
+
num_loop = math.ceil(total_number_of_frames / config.num_frames)
|
318 |
+
|
319 |
+
# prepare model args
|
320 |
+
model_args = dict()
|
321 |
+
height = torch.tensor([resolution[0]], device=device, dtype=dtype)
|
322 |
+
width = torch.tensor([resolution[1]], device=device, dtype=dtype)
|
323 |
+
num_frames = torch.tensor([config.num_frames], device=device, dtype=dtype)
|
324 |
+
ar = torch.tensor([resolution[0] / resolution[1]], device=device, dtype=dtype)
|
325 |
+
if config.num_frames == 1:
|
326 |
+
config.fps = IMG_FPS
|
327 |
+
fps = torch.tensor([config.fps], device=device, dtype=dtype)
|
328 |
+
model_args["height"] = height
|
329 |
+
model_args["width"] = width
|
330 |
+
model_args["num_frames"] = num_frames
|
331 |
+
model_args["ar"] = ar
|
332 |
+
model_args["fps"] = fps
|
333 |
+
|
334 |
+
# compute latent size
|
335 |
+
input_size = (config.num_frames, *resolution)
|
336 |
+
latent_size = vae.get_latent_size(input_size)
|
337 |
+
|
338 |
+
# process prompt
|
339 |
+
prompt_raw = [prompt_text]
|
340 |
+
prompt_raw, _ = extract_json_from_prompts(prompt_raw)
|
341 |
+
prompt_loops = process_prompts(prompt_raw, num_loop)
|
342 |
+
video_clips = []
|
343 |
+
|
344 |
+
# prepare mask strategy
|
345 |
+
if mode == "Text2Video":
|
346 |
+
mask_strategy = [None]
|
347 |
+
elif mode == "Image2Video":
|
348 |
+
mask_strategy = ['0']
|
349 |
+
else:
|
350 |
+
raise ValueError(f"Invalid mode: {mode}")
|
351 |
+
|
352 |
+
# =========================
|
353 |
+
# 2. Load reference images
|
354 |
+
# =========================
|
355 |
+
if mode == "Text2Video":
|
356 |
+
refs_x = collect_references_batch([None], vae, resolution)
|
357 |
+
elif mode == "Image2Video":
|
358 |
+
# save image to disk
|
359 |
+
from PIL import Image
|
360 |
+
im = Image.fromarray(reference_image)
|
361 |
+
im.save("test.jpg")
|
362 |
+
refs_x = collect_references_batch(["test.jpg"], vae, resolution)
|
363 |
+
else:
|
364 |
+
raise ValueError(f"Invalid mode: {mode}")
|
365 |
+
|
366 |
+
# 4.3. long video generation
|
367 |
+
for loop_i in range(num_loop):
|
368 |
+
# 4.4 sample in hidden space
|
369 |
+
batch_prompts = [prompt[loop_i] for prompt in prompt_loops]
|
370 |
+
z = torch.randn(len(batch_prompts), vae.out_channels, *latent_size, device=device, dtype=dtype)
|
371 |
+
|
372 |
+
# 4.5. apply mask strategy
|
373 |
+
masks = None
|
374 |
+
|
375 |
+
# if cfg.reference_path is not None:
|
376 |
+
if loop_i > 0:
|
377 |
+
ref_x = vae.encode(video_clips[-1])
|
378 |
+
for j, refs in enumerate(refs_x):
|
379 |
+
if refs is None:
|
380 |
+
refs_x[j] = [ref_x[j]]
|
381 |
+
else:
|
382 |
+
refs.append(ref_x[j])
|
383 |
+
if mask_strategy[j] is None:
|
384 |
+
mask_strategy[j] = ""
|
385 |
+
else:
|
386 |
+
mask_strategy[j] += ";"
|
387 |
+
mask_strategy[
|
388 |
+
j
|
389 |
+
] += f"{loop_i},{len(refs)-1},-{config.condition_frame_length},0,{config.condition_frame_length}"
|
390 |
+
|
391 |
+
masks = apply_mask_strategy(z, refs_x, mask_strategy, loop_i)
|
392 |
+
|
393 |
+
# 4.6. diffusion sampling
|
394 |
+
samples = scheduler.sample(
|
395 |
+
stdit,
|
396 |
+
text_encoder,
|
397 |
+
z=z,
|
398 |
+
prompts=batch_prompts,
|
399 |
+
device=device,
|
400 |
+
additional_args=model_args,
|
401 |
+
mask=masks, # scheduler must support mask
|
402 |
+
)
|
403 |
+
samples = vae.decode(samples.to(dtype))
|
404 |
+
video_clips.append(samples)
|
405 |
+
|
406 |
+
# 4.7. save video
|
407 |
+
if loop_i == num_loop - 1:
|
408 |
+
video_clips_list = [
|
409 |
+
video_clips[0][0]] + [video_clips[i][0][:, config.condition_frame_length :]
|
410 |
+
for i in range(1, num_loop)
|
411 |
+
]
|
412 |
+
video = torch.cat(video_clips_list, dim=1)
|
413 |
+
save_path = f"{args.output}/sample"
|
414 |
+
saved_path = save_sample(video, fps=config.fps // config.frame_interval, save_path=save_path, force_video=True)
|
415 |
+
return saved_path
|
416 |
+
|
417 |
+
|
418 |
+
def main():
|
419 |
+
# create demo
|
420 |
+
with gr.Blocks() as demo:
|
421 |
+
with gr.Row():
|
422 |
+
with gr.Column():
|
423 |
+
gr.HTML(
|
424 |
+
"""
|
425 |
+
<div style='text-align: center;'>
|
426 |
+
<p align="center">
|
427 |
+
<img src="https://github.com/hpcaitech/Open-Sora/raw/main/assets/readme/icon.png" width="250"/>
|
428 |
+
</p>
|
429 |
+
<div style="display: flex; gap: 10px; justify-content: center;">
|
430 |
+
<a href="https://github.com/hpcaitech/Open-Sora/stargazers"><img src="https://img.shields.io/github/stars/hpcaitech/Open-Sora?style=social"></a>
|
431 |
+
<a href="https://hpcaitech.github.io/Open-Sora/"><img src="https://img.shields.io/badge/Gallery-View-orange?logo=&"></a>
|
432 |
+
<a href="https://discord.gg/kZakZzrSUT"><img src="https://img.shields.io/badge/Discord-join-blueviolet?logo=discord&"></a>
|
433 |
+
<a href="https://join.slack.com/t/colossalaiworkspace/shared_invite/zt-247ipg9fk-KRRYmUl~u2ll2637WRURVA"><img src="https://img.shields.io/badge/Slack-ColossalAI-blueviolet?logo=slack&"></a>
|
434 |
+
<a href="https://twitter.com/yangyou1991/status/1769411544083996787?s=61&t=jT0Dsx2d-MS5vS9rNM5e5g"><img src="https://img.shields.io/badge/Twitter-Discuss-blue?logo=twitter&"></a>
|
435 |
+
<a href="https://raw.githubusercontent.com/hpcaitech/public_assets/main/colossalai/img/WeChat.png"><img src="https://img.shields.io/badge/微信-小助手加群-green?logo=wechat&"></a>
|
436 |
+
<a href="https://hpc-ai.com/blog/open-sora-v1.0"><img src="https://img.shields.io/badge/Open_Sora-Blog-blue"></a>
|
437 |
+
</div>
|
438 |
+
<h1 style='margin-top: 5px;'>Open-Sora: Democratizing Efficient Video Production for All</h1>
|
439 |
+
</div>
|
440 |
+
"""
|
441 |
+
)
|
442 |
+
|
443 |
+
with gr.Row():
|
444 |
+
with gr.Column():
|
445 |
+
mode = gr.Radio(
|
446 |
+
choices=["Text2Video", "Image2Video"],
|
447 |
+
value="Text2Video",
|
448 |
+
label="Usage",
|
449 |
+
info="Choose your usage scenario",
|
450 |
+
)
|
451 |
+
prompt_text = gr.Textbox(
|
452 |
+
label="Prompt",
|
453 |
+
placeholder="Describe your video here",
|
454 |
+
lines=4,
|
455 |
+
)
|
456 |
+
resolution = gr.Radio(
|
457 |
+
choices=["144p", "240p", "360p", "480p", "720p", "1080p"],
|
458 |
+
value="144p",
|
459 |
+
label="Resolution",
|
460 |
+
)
|
461 |
+
length = gr.Radio(
|
462 |
+
choices=["2s", "4s", "8s"],
|
463 |
+
value="2s",
|
464 |
+
label="Video Length",
|
465 |
+
info="8s may fail as Hugging Face ZeroGPU has the limitation of max 200 seconds inference time."
|
466 |
+
)
|
467 |
+
|
468 |
+
reference_image = gr.Image(
|
469 |
+
label="Reference Image (only used for Image2Video)",
|
470 |
+
)
|
471 |
+
|
472 |
+
with gr.Column():
|
473 |
+
output_video = gr.Video(
|
474 |
+
label="Output Video",
|
475 |
+
height="100%"
|
476 |
+
)
|
477 |
+
|
478 |
+
with gr.Row():
|
479 |
+
submit_button = gr.Button("Generate video")
|
480 |
+
|
481 |
+
|
482 |
+
submit_button.click(
|
483 |
+
fn=run_inference,
|
484 |
+
inputs=[mode, prompt_text, resolution, length, reference_image],
|
485 |
+
outputs=output_video
|
486 |
+
)
|
487 |
+
|
488 |
+
# launch
|
489 |
+
demo.launch(server_port=args.port, server_name=args.host, share=args.share)
|
490 |
+
|
491 |
+
|
492 |
+
if __name__ == "__main__":
|
493 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
xformers
|
2 |
+
transformers
|
3 |
+
pandarallel
|
4 |
+
git+https://github.com/hpcaitech/Open-Sora.git#egg=opensora
|