Spaces:
Running
on
Zero
Running
on
Zero
File size: 19,255 Bytes
abb49c0 2d9a6f9 abb49c0 b991f7f 56c0d97 683f192 abb49c0 4a17306 abb49c0 683f192 3a03985 b95275b 3a03985 abb49c0 683f192 abb49c0 683f192 abb49c0 0ab8ba2 abb49c0 56c0d97 4d9f075 b95275b 4d9f075 c727d79 4d9f075 cdc781d b95275b 4d9f075 b95275b 4d9f075 b95275b 743929c b95275b 743929c 4d9f075 743929c 4e07d6b 4d9f075 743929c cdc781d 4d9f075 9aca42f 4d9f075 9aca42f 4d9f075 9aca42f 4d9f075 743929c bf8d717 b95275b f498d85 bf8d717 78244e7 bf8d717 b95275b e977193 b95275b bf8d717 b95275b bf8d717 4d9f075 12d3925 abb49c0 fd56559 b95275b f498d85 fd56559 dde723b abb49c0 4d9f075 b95275b 4d9f075 b95275b 4d9f075 b95275b 4d9f075 b95275b 4d9f075 b95275b 4d9f075 b95275b 4d9f075 b95275b 4d9f075 b95275b abb49c0 b95275b 4d9f075 9aca42f b95275b abb49c0 683f192 abb49c0 ca698bb bf8d717 ca698bb ccee409 ca698bb 683f192 ca698bb 8828f95 ca698bb 490cabd 4257e1b b95275b ca698bb c727d79 b95275b abb49c0 56c0d97 ca698bb abb49c0 585777e 17b74df b95275b 17b74df 1ca0b58 a86b0c1 17b74df 1ca0b58 91a8229 ca698bb 585777e ca698bb a86b0c1 de40855 c727d79 de40855 a86b0c1 d6489d1 de40855 c727d79 a86b0c1 de40855 ca698bb 585777e c727d79 aac0050 ca698bb ef19c14 c727d79 b95275b ca698bb 585777e c727d79 b95275b ca698bb abb49c0 585777e ef19c14 c727d79 ef19c14 b95275b fd56559 b95275b ca698bb fd56559 abb49c0 743929c b95275b abb49c0 683f192 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
import spaces
from huggingface_hub import snapshot_download, hf_hub_download
import os
import subprocess
import importlib, site
from PIL import Image
import uuid
import shutil
# Re-discover all .pth/.egg-link files
for sitedir in site.getsitepackages():
site.addsitedir(sitedir)
# Clear caches so importlib will pick up new modules
importlib.invalidate_caches()
def sh(cmd): subprocess.check_call(cmd, shell=True)
flash_attention_installed = False
try:
print("Attempting to download and install FlashAttention wheel...")
flash_attention_wheel = hf_hub_download(
repo_id="rahul7star/flash-attn-3",
repo_type="model",
filename="128/flash_attn_3-3.0.0b1-cp39-abi3-linux_x86_64.whl",
)
sh(f"pip install {flash_attention_wheel}")
# tell Python to re-scan site-packages now that the egg-link exists
import importlib, site; site.addsitedir(site.getsitepackages()[0]); importlib.invalidate_caches()
flash_attention_installed = True
print("FlashAttention installed successfully.")
except Exception as e:
print(f"⚠️ Could not install FlashAttention: {e}")
print("Continuing without FlashAttention...")
import torch
print(f"Torch version: {torch.__version__}")
print(f"FlashAttention available: {flash_attention_installed}")
os.environ["PROCESSED_RESULTS"] = f"{os.getcwd()}/processed_results"
import gradio as gr
import argparse
from ovi.ovi_fusion_engine import OviFusionEngine, DEFAULT_CONFIG
from diffusers import DiffusionPipeline
import tempfile
from ovi.utils.io_utils import save_video
from ovi.utils.processing_utils import clean_text, scale_hw_to_area_divisible
# ----------------------------
# Parse CLI Args
# ----------------------------
parser = argparse.ArgumentParser(description="Ovi Joint Video + Audio Gradio Demo")
parser.add_argument(
"--cpu_offload",
action="store_true",
help="Enable CPU offload for both OviFusionEngine and FluxPipeline"
)
args = parser.parse_args()
ckpt_dir = "./ckpts"
# Wan2.2
wan_dir = os.path.join(ckpt_dir, "Wan2.2-TI2V-5B")
snapshot_download(
repo_id="Wan-AI/Wan2.2-TI2V-5B",
local_dir=wan_dir,
allow_patterns=[
"google/*",
"models_t5_umt5-xxl-enc-bf16.pth",
"Wan2.2_VAE.pth"
]
)
# MMAudio
mm_audio_dir = os.path.join(ckpt_dir, "MMAudio")
snapshot_download(
repo_id="hkchengrex/MMAudio",
local_dir=mm_audio_dir,
allow_patterns=[
"ext_weights/best_netG.pt",
"ext_weights/v1-16.pth"
]
)
ovi_dir = os.path.join(ckpt_dir, "Ovi")
snapshot_download(
repo_id="chetwinlow1/Ovi",
local_dir=ovi_dir,
allow_patterns=[
"model.safetensors"
]
)
# Initialize OviFusionEngine
enable_cpu_offload = args.cpu_offload
print(f"loading model...")
DEFAULT_CONFIG['cpu_offload'] = enable_cpu_offload # always use cpu offload if image generation is enabled
DEFAULT_CONFIG['mode'] = "t2v" # hardcoded since it is always cpu offloaded
ovi_engine = OviFusionEngine()
try:
flux_model = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-Krea-dev", torch_dtype=torch.bfloat16)
image_example = None
except Exception as e:
flux_model = None
image_example = "example_prompts/pngs/8.png"
print("loaded model")
def resize_for_model(image_path):
# Open image
img = Image.open(image_path)
w, h = img.size
aspect_ratio = w / h
# Decide target size based on aspect ratio
if aspect_ratio > 1.5: # wide image
target_size = (992, 512)
elif aspect_ratio < 0.66: # tall image
target_size = (512, 992)
else: # roughly square
target_size = (512, 512)
# Resize while preserving aspect ratio, then pad
img.thumbnail(target_size, Image.Resampling.LANCZOS)
# Create a new image with target size and paste centered
new_img = Image.new("RGB", target_size, (0, 0, 0))
new_img.paste(
img,
((target_size[0] - img.size[0]) // 2,
(target_size[1] - img.size[1]) // 2)
)
return new_img, target_size
def _ensure_output_dir(session_id):
output_dir = os.path.join(os.environ["PROCESSED_RESULTS"], session_id)
os.makedirs(output_dir, exist_ok=True)
return output_dir
@spaces.GPU()
def generate_image(text_prompt, session_id, image_height = 1024, image_width = 1024):
"""
Generates an image from text_prompt using flux_model if available.
Always returns a filepath (string) or raises a gr.Error on failure.
"""
print("image generation used")
text_prompt = clean_text(text_prompt or "")
print(text_prompt)
# If flux_model isn't loaded, fall back to example image (if available)
output_dir = _ensure_output_dir(session_id)
output_path = os.path.join(output_dir, "generate_image.png")
if flux_model is None:
# fallback to example image if provided
if image_example and os.path.exists(image_example):
# copy example into session folder so downstream can always rely on a path under processed_results
shutil.copy(image_example, output_path)
print(f"Flux model not available — using example image {image_example}")
return output_path
else:
raise gr.Error("Image generation model not available and no example image found.")
# ensure requested dims are divisible/compatible
image_h, image_w = scale_hw_to_area_divisible(int(image_height), int(image_width), area=1024 * 1024)
try:
# move model to GPU, generate, then move model back to CPU
flux_model.to("cuda")
gen = flux_model(
text_prompt,
height=image_h,
width=image_w,
num_inference_steps = 28,
guidance_scale=4.5,
generator=torch.Generator(device="cuda").manual_seed(1234)
)
image = gen.images[0]
image.save(output_path)
print(f"Saved generated image to {output_path}")
return output_path
except Exception as e:
# provide helpful error message and fallback to example if present
print(f"⚠️ generate_image failed: {e}")
if image_example and os.path.exists(image_example):
shutil.copy(image_example, output_path)
print(f"Falling back to example image {image_example}")
return output_path
raise gr.Error(f"Image generation failed: {e}")
finally:
try:
flux_model.to("cpu")
except Exception:
pass
def generate_scene(
text_prompt,
sample_steps = 50,
image = None,
session_id = None,
video_seed = 100,
solver_name = "unipc",
shift = 5,
video_guidance_scale = 4,
audio_guidance_scale = 3,
slg_layer = 11,
video_negative_prompt = "",
audio_negative_prompt = "",
progress=gr.Progress(track_tqdm=True)
):
"""
Top-level helper that ensures there's an image (generates one if necessary)
and then calls generate_video.
"""
text_prompt_processed = (text_prompt or "").strip()
if session_id is None:
session_id = uuid.uuid4().hex
if not text_prompt_processed:
raise gr.Error("Please enter a prompt.")
print(text_prompt)
# If user did not supply an image (None or empty), try to generate one and use it.
if not image:
print("No image provided; attempting to generate one.")
image = generate_image(text_prompt_processed, session_id)
print(f"Generated/fallback image path: {image}")
# If image is a dict-like from Gradio, try to extract file path (defensive)
if isinstance(image, dict) and "name" in image:
image = image["name"]
# final check - ensure file exists
if not image or not os.path.exists(image):
raise gr.Error("No usable image available (generation failed and no fallback).")
print(f"{session_id} is generating scene with {sample_steps} steps (image: {image})")
return generate_video(
text_prompt=text_prompt_processed,
sample_steps=sample_steps,
image=image,
session_id=session_id,
video_seed=video_seed,
solver_name=solver_name,
shift=shift,
video_guidance_scale=video_guidance_scale,
audio_guidance_scale=audio_guidance_scale,
slg_layer=slg_layer,
video_negative_prompt=video_negative_prompt,
audio_negative_prompt=audio_negative_prompt,
progress=progress
)
def get_duration(
text_prompt,
sample_steps,
image,
session_id,
video_seed,
solver_name,
shift,
video_guidance_scale,
audio_guidance_scale,
slg_layer,
video_negative_prompt,
audio_negative_prompt,
progress,
):
image_generation_s = 0
if not image:
image_generation_s = 40
warmup = 20
return int(sample_steps * 3 + warmup + image_generation_s)
@spaces.GPU(duration=get_duration)
def generate_video(
text_prompt,
sample_steps = 50,
image = None,
session_id = None,
video_seed = 100,
solver_name = "unipc",
shift = 5,
video_guidance_scale = 4,
audio_guidance_scale = 3,
slg_layer = 11,
video_negative_prompt = "",
audio_negative_prompt = "",
progress=gr.Progress(track_tqdm=True)
):
"""
Generates a video using ovi_engine given a guaranteed image path (string).
"""
print("generate_video called")
if session_id is None:
session_id = uuid.uuid4().hex
# If image is not provided for any reason, try generating one now.
if not image:
print("No image passed to generate_video; generating now...")
image = generate_image(text_prompt, session_id)
# If Gradio passed a dict or other structure, extract file path
if isinstance(image, dict) and "name" in image:
image_path = image["name"]
else:
image_path = image
if not image_path or not os.path.exists(image_path):
raise gr.Error("Image path is missing or the file does not exist. Cannot generate video.")
output_dir = _ensure_output_dir(session_id)
output_path = os.path.join(output_dir, "generated_video.mp4")
# Resize/pad and get the target dims for the model
try:
_, target_size = resize_for_model(image_path)
except Exception as e:
raise gr.Error(f"Failed to open/resize image: {e}")
video_frame_width = target_size[0]
video_frame_height = target_size[1]
# Call your ovi_engine (unchanged)
generated_video, generated_audio, _ = ovi_engine.generate(
text_prompt=text_prompt,
image_path=image_path,
video_frame_height_width=[video_frame_height, video_frame_width],
seed=video_seed,
solver_name=solver_name,
sample_steps=sample_steps,
shift=shift,
video_guidance_scale=video_guidance_scale,
audio_guidance_scale=audio_guidance_scale,
slg_layer=slg_layer,
video_negative_prompt=video_negative_prompt,
audio_negative_prompt=audio_negative_prompt,
)
save_video(output_path, generated_video, generated_audio, fps=24, sample_rate=16000)
print(f"{session_id} video generation succeeded: {output_path}")
return output_path
def cleanup(request: gr.Request):
sid = request.session_hash
if sid:
d1 = os.path.join(os.environ["PROCESSED_RESULTS"], sid)
shutil.rmtree(d1, ignore_errors=True)
def start_session(request: gr.Request):
return request.session_hash
css = """
#col-container {
margin: 0 auto;
max-width: 1024px;
}
"""
theme = gr.themes.Ocean()
with gr.Blocks(css=css, theme=theme) as demo:
session_state = gr.State()
demo.load(start_session, outputs=[session_state])
with gr.Column(elem_id="col-container"):
with gr.Row():
with gr.Column():
# Image section
video_text_prompt = gr.Textbox(label="Scene Prompt",
lines=5,
placeholder="Describe your scene...")
sample_steps = gr.Slider(
value=20,
label="Sample Steps",
minimum=20,
maximum=100,
step=1.0
)
run_btn = gr.Button("Action 🎬", variant="primary")
image = gr.Image(type="filepath", label="Image Ref", height=360)
gr.Markdown(
"""
💡 **Prompt Guidelines**
```
Describe the Scene and Character(s) performance
, <S>Dialogue line<E>
<AUDCAP>character voice & atmosphere of the scene<ENDAUDCAP>
```
""",
elem_classes="guideline-bubble"
)
with gr.Accordion("🎬 Video Generation Options", open=False, visible=True):
video_height = gr.Number(minimum=128, maximum=1280, value=512, step=32, label="Video Height")
video_width = gr.Number(minimum=128, maximum=1280, value=992, step=32, label="Video Width")
video_seed = gr.Number(minimum=0, maximum=100000, value=100, label="Video Seed")
solver_name = gr.Dropdown(
choices=["unipc", "euler", "dpm++"], value="unipc", label="Solver Name"
)
shift = gr.Slider(minimum=0.0, maximum=20.0, value=5.0, step=1.0, label="Shift")
video_guidance_scale = gr.Slider(minimum=0.0, maximum=10.0, value=4.0, step=0.5, label="Video Guidance Scale")
audio_guidance_scale = gr.Slider(minimum=0.0, maximum=10.0, value=3.0, step=0.5, label="Audio Guidance Scale")
slg_layer = gr.Number(minimum=-1, maximum=30, value=11, step=1, label="SLG Layer")
video_negative_prompt = gr.Textbox(label="Video Negative Prompt", placeholder="Things to avoid in video")
audio_negative_prompt = gr.Textbox(label="Audio Negative Prompt", placeholder="Things to avoid in audio")
with gr.Column():
output_path = gr.Video(label="Generated Video", height=360)
gr.Examples(
examples=[
[
"What's the difference between having a job and having no life?",
20,
"example_prompts/pngs/91.png",
],
[
"a alien creature looking to the right and slowly turning to the camera while drooling from her teeth and says <S>Hiss, You thought I can't talk.<E> then start screaming in a high pitch voice <AUDCAP>the alien has a raspy voice<ENDAUDCAP>",
20,
"example_prompts/pngs/90.png",
],
[
"The video opens with a close-up of a woman with vibrant reddish-orange, shoulder-length hair and heavy dark eye makeup. She is wearing a dark brown leather jacket over a grey hooded top. She looks intently to her right, her mouth slightly agape, and her expression is serious and focused. The background shows a room with light green walls and dark wooden cabinets on the left, and a green plant on the right. She speaks, her voice clear and direct, saying, <S>doing<E>. She then pauses briefly, her gaze unwavering, and continues, <S>And I need you to trust them.<E>. Her mouth remains slightly open, indicating she is either about to speak more or has just finished a sentence, with a look of intense sincerity.. <AUDCAP>Tense, dramatic background music, clear female voice.<ENDAUDCAP>",
20,
image_example,
],
[
"A young woman with long, wavy blonde hair and light-colored eyes is shown in a medium shot against a blurred backdrop of lush green foliage. She wears a denim jacket over a striped top. Initially, her eyes are closed and her mouth is slightly open as she speaks, <S>Enjoy this moment<E>. Her eyes then slowly open, looking slightly upwards and to the right, as her expression shifts to one of thoughtful contemplation. She continues to speak, <S>No matter where it's taking<E>, her gaze then settling with a serious and focused look towards someone off-screen to her right.. <AUDCAP>Clear female voice, faint ambient outdoor sounds.<ENDAUDCAP>",
20,
"example_prompts/pngs/2.png",
],
[
"A bearded man wearing large dark sunglasses and a blue patterned cardigan sits in a studio, actively speaking into a large, suspended microphone. He has headphones on and gestures with his hands, displaying rings on his fingers. Behind him, a wall is covered with red, textured sound-dampening foam on the left, and a white banner on the right features the ""CHOICE FM"" logo and various social media handles like ""@ilovechoicefm"" with ""RALEIGH"" below it. The man intently addresses the microphone, articulating, <S>is talent. It's all about authenticity. You gotta be who you really are, especially if you're working<E>. He leans forward slightly as he speaks, maintaining a serious expression behind his sunglasses.. <AUDCAP>Clear male voice speaking into a microphone, a low background hum.<ENDAUDCAP>",
20,
"example_prompts/pngs/5.png",
],
[
"The scene is set outdoors with a blurry, bright green background, suggesting grass and a sunny environment. On the left, a woman with long, dark hair, wearing a red top and a necklace with a white pendant, faces towards the right. Her expression is serious and slightly perturbed as she speaks, with her lips slightly pursed. She says, <S>UFO, UFC thing.<E> On the right, the back of a man's head and his right ear are visible, indicating he is facing away from the camera, listening to the woman. He has short, dark hair. The woman continues speaking, her expression remaining serious, <S>And if you're not watching that, it's one of those ancient movies from an era that's<E> as the frame holds steady on the two figures.. <AUDCAP>Clear female speech, distant low-frequency hum.<ENDAUDCAP>",
20,
"example_prompts/pngs/9.png",
],
],
inputs=[video_text_prompt, sample_steps, image],
outputs=[output_path],
fn=generate_video,
cache_examples=True,
)
run_btn.click(
fn=generate_scene,
inputs=[video_text_prompt, sample_steps, image, session_state],
outputs=[output_path],
)
if __name__ == "__main__":
demo.unload(cleanup)
demo.queue()
demo.launch(ssr_mode=False, share=True)
|