Spaces:
Running on Zero

adlib-preview / app.py
azhtom's picture
Video within the free per-task GPU limit: 110 s, 16 steps
ea9d387 verified
Raw History Blame Contribute Delete
3.09 kB
# adlib preview Space: free, preview-quality shots for adlib's free mode.
# Runs on ZeroGPU; every call uses the CALLER's daily GPU quota (adlib sends
# the user's own Hugging Face token). Endpoint names and input order are a
# contract with adlib's catalog (huggingface-space/image and /video).
import math
import os
import tempfile
import gradio as gr
import spaces
import torch
from diffusers import ZImagePipeline, WanImageToVideoPipeline, AutoencoderKLWan
from diffusers.utils import export_to_video
from PIL import Image
IMAGE_MODEL = "Tongyi-MAI/Z-Image-Turbo" # Apache-2.0, not gated
VIDEO_MODEL = "Wan-AI/Wan2.2-TI2V-5B-Diffusers" # Apache-2.0
# One model per Space: both together do not fit the default ZeroGPU size
# (48 GB). ADLIB_MODE is set per Space: "image" or "video".
MODE = os.environ.get("ADLIB_MODE", "video")
if MODE == "image":
image_pipe = ZImagePipeline.from_pretrained(IMAGE_MODEL, torch_dtype=torch.bfloat16).to("cuda")
else:
vae = AutoencoderKLWan.from_pretrained(VIDEO_MODEL, subfolder="vae", torch_dtype=torch.float32)
video_pipe = WanImageToVideoPipeline.from_pretrained(VIDEO_MODEL, vae=vae, torch_dtype=torch.bfloat16).to("cuda")
SIZES = {"9:16": (768, 1344), "16:9": (1344, 768), "1:1": (1024, 1024), "4:5": (896, 1120)}
FPS = 24
@spaces.GPU(duration=30)
def image(prompt: str, aspect: str):
w, h = SIZES.get(aspect, SIZES["9:16"])
out = image_pipe(prompt=prompt, width=w, height=h, num_inference_steps=9, guidance_scale=0.0).images[0] # 8 DiT steps, no guidance (Turbo)
path = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
out.save(path)
return path
@spaces.GPU(duration=110)
def video(prompt: str, start: str, seconds: float):
seconds = max(2.0, min(5.0, float(seconds or 5)))
img = Image.open(start).convert("RGB")
# Preview quality: 480p (Wan 2.2 TI2V-5B renders 720p natively, which is
# too slow for a ZeroGPU call). Sizes are multiples of 32.
w, h = (480, 832) if img.height >= img.width else (832, 480)
img = img.resize((w, h))
frames = 4 * math.ceil(seconds * FPS / 4) + 1 # 4k+1 frames
out = video_pipe(image=img, prompt=prompt, height=h, width=w, num_frames=frames, num_inference_steps=16, guidance_scale=5.0).frames[0]
path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
export_to_video(out, path, fps=FPS)
return path
with gr.Blocks(title="adlib preview") as demo:
gr.Markdown("Free preview shots for adlib. Calls use your own ZeroGPU quota.")
if MODE == "image":
p1 = gr.Textbox(label="prompt")
a1 = gr.Dropdown(list(SIZES), value="9:16", label="aspect")
o1 = gr.Image(type="filepath")
gr.Button("Generate").click(image, [p1, a1], o1, api_name="image")
else:
p2 = gr.Textbox(label="prompt")
i2 = gr.Image(type="filepath", label="start image")
s2 = gr.Number(value=5, label="seconds")
o2 = gr.Video()
gr.Button("Generate").click(video, [p2, i2, s2], o2, api_name="video")
demo.queue(default_concurrency_limit=1).launch()