File size: 4,613 Bytes
8eb5d1d
 
 
 
 
 
 
 
 
 
 
f10dc54
8eb5d1d
 
 
 
 
 
 
 
 
 
 
833a666
 
8eb5d1d
2cd1841
 
 
 
 
 
8eb5d1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f10dc54
8eb5d1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7642b02
 
 
8eb5d1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
833a666
8eb5d1d
 
 
 
 
 
a542340
8eb5d1d
 
 
 
 
 
 
 
 
 
 
a542340
8eb5d1d
 
 
 
a542340
8eb5d1d
 
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
#!/usr/bin/env python

from __future__ import annotations

import os
import random
import tempfile

import gradio as gr
import imageio
import numpy as np
import spaces
import torch
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler

DESCRIPTION = '# zeroscope v2'
if not torch.cuda.is_available():
    DESCRIPTION += '\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>'

MAX_NUM_FRAMES = int(os.getenv('MAX_NUM_FRAMES', '200'))
DEFAULT_NUM_FRAMES = min(MAX_NUM_FRAMES,
                         int(os.getenv('DEFAULT_NUM_FRAMES', '24')))
MAX_SEED = np.iinfo(np.int32).max
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv(
    'CACHE_EXAMPLES') == '1'

if torch.cuda.is_available():
    pipe = DiffusionPipeline.from_pretrained('cerspense/zeroscope_v2_576w',
                                             torch_dtype=torch.float16)
    pipe.enable_model_cpu_offload()
else:
    pipe = DiffusionPipeline.from_pretrained('cerspense/zeroscope_v2_576w')
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe.enable_vae_slicing()


def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    return seed


def to_video(frames: list[np.ndarray], fps: int) -> str:
    out_file = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
    writer = imageio.get_writer(out_file.name, format='FFMPEG', fps=fps)
    for frame in frames:
        writer.append_data(frame)
    writer.close()
    return out_file.name


@spaces.GPU
def generate(prompt: str, seed: int, num_frames: int,
             num_inference_steps: int) -> str:
    generator = torch.Generator().manual_seed(seed)
    frames = pipe(prompt,
                  num_inference_steps=num_inference_steps,
                  num_frames=num_frames,
                  width=576,
                  height=320,
                  generator=generator).frames
    return to_video(frames, 8)


examples = [
    ['An astronaut riding a horse', 0, 24, 25],
    ['A panda eating bamboo on a rock', 0, 24, 25],
    ['Spiderman is surfing', 0, 24, 25],
]

with gr.Blocks(css='style.css') as demo:
    gr.Markdown(DESCRIPTION)
    gr.DuplicateButton(value='Duplicate Space for private use',
                       elem_id='duplicate-button',
                       visible=os.getenv('SHOW_DUPLICATE_BUTTON') == '1')
    with gr.Box():
        with gr.Row():
            prompt = gr.Text(label='Prompt',
                             show_label=False,
                             max_lines=1,
                             placeholder='Enter your prompt',
                             container=False)
            run_button = gr.Button('Generate video', scale=0)
        result = gr.Video(label='Result', show_label=False)
        with gr.Accordion('Advanced options', open=False):
            seed = gr.Slider(label='Seed',
                             minimum=0,
                             maximum=MAX_SEED,
                             step=1,
                             value=0)
            randomize_seed = gr.Checkbox(label='Randomize seed', value=True)
            num_frames = gr.Slider(
                label='Number of frames',
                minimum=24,
                maximum=MAX_NUM_FRAMES,
                step=1,
                value=24,
                info=
                'Note that the content of the video also changes when you change the number of frames.'
            )
            num_inference_steps = gr.Slider(label='Number of inference steps',
                                            minimum=10,
                                            maximum=50,
                                            step=1,
                                            value=25)

    inputs = [
        prompt,
        seed,
        num_frames,
        num_inference_steps,
    ]
    gr.Examples(examples=examples,
                inputs=inputs,
                outputs=result,
                fn=generate,
                cache_examples=CACHE_EXAMPLES)

    prompt.submit(
        fn=randomize_seed_fn,
        inputs=[seed, randomize_seed],
        outputs=seed,
        queue=False,
        api_name=False,
    ).then(
        fn=generate,
        inputs=inputs,
        outputs=result,
        api_name='run',
    )
    run_button.click(
        fn=randomize_seed_fn,
        inputs=[seed, randomize_seed],
        outputs=seed,
        queue=False,
        api_name=False,
    ).then(
        fn=generate,
        inputs=inputs,
        outputs=result,
        api_name=False,
    )
demo.queue(max_size=10).launch()