Spaces:
Sleeping
Sleeping
Download app.py from dfasdfasdfadfasdf/HunyuanVideo: direct link, hf CLI and curl.
- Browser
- Download file 5.22 kB
-
https://huggingface.co/spaces/dfasdfasdfadfasdf/HunyuanVideo/resolve/main/app.py
- Command line
-
hf download hf://spaces/dfasdfasdfadfasdf/HunyuanVideo/app.py
-
curl -L -o app.py https://huggingface.co/spaces/dfasdfasdfadfasdf/HunyuanVideo/resolve/main/app.py
5.22 kB
| import spaces # must come before torch / diffusers | |
| import random | |
| import tempfile | |
| import time | |
| import gradio as gr | |
| import torch | |
| from diffusers import HunyuanVideoPipeline, HunyuanVideoTransformer3DModel | |
| from diffusers.utils import export_to_video | |
| MODEL_ID = "hunyuanvideo-community/HunyuanVideo" # diffusers-format tencent/HunyuanVideo | |
| MAX_SEED = 2**32 - 1 | |
| FPS = 15 | |
| transformer = HunyuanVideoTransformer3DModel.from_pretrained(MODEL_ID, subfolder="transformer", torch_dtype=torch.bfloat16) | |
| pipe = HunyuanVideoPipeline.from_pretrained(MODEL_ID, transformer=transformer, torch_dtype=torch.float16) | |
| pipe.vae.enable_tiling() | |
| pipe.to("cuda") | |
| RESOLUTIONS = { | |
| "368×640 (16:9)": (368, 640), | |
| "640×368 (9:16)": (640, 368), | |
| "480×480 (1:1)": (480, 480), | |
| } | |
| # ZeroGPU multiplies this by 1.5 on Blackwell; free accounts reject ~>120s requested, so cap at 80 | |
| def _duration(prompt, resolution="368×640 (16:9)", num_frames=33, steps=20, *args, **kwargs): | |
| height, width = RESOLUTIONS.get(resolution, (368, 640)) | |
| tokens = (height // 16) * (width // 16) * ((int(num_frames) - 1) // 4 + 1) | |
| return min(80, int(10 + steps * tokens / 3000)) # measured: 21 frames, 15 steps @368x640 → 24s | |
| def generate( | |
| prompt: str, | |
| resolution: str = "368×640 (16:9)", | |
| num_frames: int = 33, | |
| steps: int = 20, | |
| guidance_scale: float = 6.0, | |
| flow_shift: float = 7.0, | |
| seed: int = 42, | |
| randomize_seed: bool = True, | |
| progress=gr.Progress(track_tqdm=True), | |
| ): | |
| """Generate a short video from a text prompt with HunyuanVideo. | |
| Args: | |
| prompt: Description of the video. | |
| resolution: One of the preset resolutions (height×width). | |
| num_frames: Number of frames (4k+1), played at 15 fps. | |
| steps: Number of denoising steps. | |
| guidance_scale: Embedded guidance scale. | |
| flow_shift: Scheduler flow shift. | |
| seed: Random seed. | |
| randomize_seed: Pick a random seed instead of `seed`. | |
| Returns: | |
| Path to the mp4, the seed used and a status line. | |
| """ | |
| if not prompt or not prompt.strip(): | |
| raise gr.Error("Enter a prompt.") | |
| height, width = RESOLUTIONS[resolution] | |
| num_frames = (int(num_frames) - 1) // 4 * 4 + 1 | |
| if randomize_seed: | |
| seed = random.randint(0, MAX_SEED) | |
| seed = int(seed) | |
| pipe.scheduler = type(pipe.scheduler).from_config(pipe.scheduler.config, shift=float(flow_shift)) | |
| start = time.perf_counter() | |
| frames = pipe( | |
| prompt=prompt, | |
| height=height, | |
| width=width, | |
| num_frames=num_frames, | |
| num_inference_steps=int(steps), | |
| guidance_scale=float(guidance_scale), | |
| generator=torch.Generator("cpu").manual_seed(seed), | |
| ).frames[0] | |
| elapsed = time.perf_counter() - start | |
| path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name | |
| export_to_video(frames, path, fps=FPS) | |
| return path, seed, f"{width}×{height} · {num_frames} frames · {int(steps)} steps · seed {seed} · {elapsed:.1f}s" | |
| EXAMPLES = [ | |
| ["A cat walks on the grass, realistic style."], | |
| ["A red fox trotting through a snowy birch forest at golden hour, cinematic tracking shot"], | |
| ["Aerial drone shot over turquoise ocean waves crashing on black volcanic rocks"], | |
| ["A steaming cup of coffee on a wooden table by a rainy window, slow push-in, shallow depth of field"], | |
| ] | |
| with gr.Blocks(title="HunyuanVideo") as demo: | |
| gr.Markdown( | |
| "# 🎬 HunyuanVideo\n" | |
| "Text-to-video with [tencent/HunyuanVideo](https://huggingface.co/tencent/HunyuanVideo) " | |
| "(13B, diffusers weights from [hunyuanvideo-community](https://huggingface.co/hunyuanvideo-community/HunyuanVideo)). " | |
| "Short, low-res clips to fit a ZeroGPU slot." | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| prompt = gr.Textbox(label="Prompt", lines=4, placeholder="Describe the video…") | |
| run = gr.Button("Generate", variant="primary") | |
| with gr.Accordion("Advanced settings", open=False): | |
| resolution = gr.Dropdown(list(RESOLUTIONS), value="368×640 (16:9)", label="Resolution") | |
| num_frames = gr.Slider(21, 41, value=33, step=4, label="Frames (15 fps)") | |
| steps = gr.Slider(10, 20, value=20, step=1, label="Steps") | |
| guidance_scale = gr.Slider(1.0, 10.0, value=6.0, step=0.5, label="Guidance") | |
| flow_shift = gr.Slider(1.0, 15.0, value=7.0, step=0.5, label="Flow shift") | |
| with gr.Row(): | |
| seed = gr.Number(value=42, precision=0, label="Seed", minimum=0, maximum=MAX_SEED) | |
| randomize_seed = gr.Checkbox(value=True, label="Random seed") | |
| with gr.Column(): | |
| video = gr.Video(label="Result", autoplay=True, loop=True) | |
| status = gr.Markdown() | |
| gr.Examples(examples=EXAMPLES, inputs=[prompt], outputs=[video, seed, status], fn=generate, | |
| cache_examples=True, cache_mode="lazy") | |
| inputs = [prompt, resolution, num_frames, steps, guidance_scale, flow_shift, seed, randomize_seed] | |
| gr.on([run.click, prompt.submit], fn=generate, inputs=inputs, outputs=[video, seed, status]) | |
| demo.launch(mcp_server=True) | |