File size: 6,059 Bytes
2a37fe9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
import gradio as gr
import torch

from video_diffusion.tuneavideo.models.unet import UNet3DConditionModel
from video_diffusion.tuneavideo.pipelines.pipeline_tuneavideo import TuneAVideoPipeline
from video_diffusion.tuneavideo.util import save_videos_grid
from video_diffusion.utils.model_list import stable_model_list

video_diffusion_model_list = [
    "Tune-A-Video-library/a-man-is-surfing",
    "Tune-A-Video-library/mo-di-bear-guitar",
    "Tune-A-Video-library/redshift-man-skiing",
]


class TunaVideoText2VideoGenerator:
    def __init__(self):
        self.pipe = None
        self.unet = None

    def load_model(self, video_diffusion_model_list, stable_model_list):
        if self.pipe is None:
            if self.unet is None:
                self.unet = UNet3DConditionModel.from_pretrained(
                    video_diffusion_model_list, subfolder="unet", torch_dtype=torch.float16
                ).to("cuda")

            self.pipe = TuneAVideoPipeline.from_pretrained(
                stable_model_list, unet=self.unet, torch_dtype=torch.float16
            )
            self.pipe.to("cuda")
            self.pipe.enable_xformers_memory_efficient_attention()
            
        return self.pipe

    def generate_video(
        self,
        video_diffusion_model: str,
        stable_model_list: str,
        prompt: str,
        negative_prompt: str,
        video_length: int,
        height: int,
        width: int,
        num_inference_steps: int,
        guidance_scale: int,
        fps: int,
    ):
        pipe = self.load_model(video_diffusion_model, stable_model_list)
        video = pipe(
            prompt,
            negative_prompt=negative_prompt,
            video_length=video_length,
            height=height,
            width=width,
            num_inference_steps=num_inference_steps,
            guidance_scale=guidance_scale,
        ).videos

        save_videos_grid(videos=video, path="output.gif", fps=fps)
        return "output.gif"

    def app():
        with gr.Blocks():
            with gr.Row():
                with gr.Column():
                    tunevideo_video_diffusion_model_list = gr.Dropdown(
                        choices=video_diffusion_model_list,
                        label="Video Diffusion Model",
                        value=video_diffusion_model_list[0],
                    )
                    tunevideo_stable_model_list = gr.Dropdown(
                        choices=stable_model_list,
                        label="Stable Model List",
                        value=stable_model_list[0],
                    )
                    with gr.Row():
                        with gr.Column():
                            tunevideo_prompt = gr.Textbox(
                                lines=1,
                                placeholder="Prompt",
                                show_label=False,
                            )
                            tunevideo_video_length = gr.Slider(
                                minimum=1,
                                maximum=100,
                                step=1,
                                value=10,
                                label="Video Length",
                            )
                            tunevideo_num_inference_steps = gr.Slider(
                                minimum=1,
                                maximum=100,
                                step=1,
                                value=50,
                                label="Num Inference Steps",
                            )
                            tunevideo_fps = gr.Slider(
                                minimum=1,
                                maximum=60,
                                step=1,
                                value=5,
                                label="Fps",
                            )
                        with gr.Row():
                            with gr.Column():
                                tunevideo_negative_prompt = gr.Textbox(
                                    lines=1,
                                    placeholder="Negative Prompt",
                                    show_label=False,
                                )
                                tunevideo_guidance_scale = gr.Slider(
                                    minimum=1,
                                    maximum=15,
                                    step=1,
                                    value=7.5,
                                    label="Guidance Scale",
                                )
                                tunevideo_height = gr.Slider(
                                    minimum=1,
                                    maximum=1280,
                                    step=32,
                                    value=512,
                                    label="Height",
                                )
                                tunevideo_width = gr.Slider(
                                    minimum=1,
                                    maximum=1280,
                                    step=32,
                                    value=512,
                                    label="Width",
                                )
                    tunevideo_generate = gr.Button(value="Generator")

                with gr.Column():
                    tunevideo_output = gr.Video(label="Output")

            tunevideo_generate.click(
                fn=TunaVideoText2VideoGenerator().generate_video,
                inputs=[
                    tunevideo_video_diffusion_model_list,
                    tunevideo_stable_model_list,
                    tunevideo_prompt,
                    tunevideo_negative_prompt,
                    tunevideo_video_length,
                    tunevideo_height,
                    tunevideo_width,
                    tunevideo_num_inference_steps,
                    tunevideo_guidance_scale,
                    tunevideo_fps,
                ],
                outputs=tunevideo_output,
            )