Spaces:
Sleeping

File size: 3,089 Bytes
3b059eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
652d2e8
3b059eb
 
 
915f52a
 
 
3b059eb
 
652d2e8
3b059eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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()