Bible / app.py
apspoliveira's picture
Fit video request within remaining ZeroGPU quota
051f39c verified
Raw History Blame Contribute Delete
2.17 kB
import tempfile
from pathlib import Path
import gradio as gr
import spaces
import torch
from diffusers import AutoencoderKLWan, WanPipeline
from diffusers.utils import export_to_video
MODEL_ID = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
NEGATIVE_PROMPT = (
"subtitles, text, logos, watermarks, blurry food, deformed hands, "
"inconsistent ingredients, still image, cartoon, illustration"
)
vae = AutoencoderKLWan.from_pretrained(
MODEL_ID,
subfolder="vae",
torch_dtype=torch.float32,
)
pipeline = WanPipeline.from_pretrained(
MODEL_ID,
vae=vae,
torch_dtype=torch.bfloat16,
)
pipeline.to("cuda")
@spaces.GPU(duration=120)
def generate_video(prompt: str, seed: int) -> str:
"""Generate a short video clip for a recipe prompt."""
prompt = prompt.strip()
if not prompt:
raise gr.Error("The recipe prompt cannot be empty.")
if len(prompt) > 2500:
raise gr.Error("The prompt must be 2,500 characters or fewer.")
generator = torch.Generator(device="cuda").manual_seed(int(seed))
frames = pipeline(
prompt=prompt,
negative_prompt=NEGATIVE_PROMPT,
height=480,
width=832,
num_frames=49,
num_inference_steps=20,
guidance_scale=5.0,
generator=generator,
).frames[0]
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as output_file:
output_path = Path(output_file.name)
export_to_video(frames, str(output_path), fps=15)
return str(output_path)
demo = gr.Interface(
fn=generate_video,
inputs=[
gr.Textbox(label="Recipe prompt", lines=8, max_lines=16),
gr.Number(label="Seed", precision=0, value=42),
],
outputs=gr.Video(label="Generated cooking video"),
title="Panorama Meal Video Generator",
description=(
"Generate a short, realistic cooking video. A free ZeroGPU account has "
"limited daily GPU time, so generation may queue or stop when the quota is used."
),
api_name="generate_video",
flagging_mode="never",
)
demo.queue(default_concurrency_limit=1).launch()