File size: 6,822 Bytes
8d14048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python

from __future__ import annotations

import os

import gradio as gr

from gradio_demo.runner import Runner


def create_demo(runner: Runner,
                pipe: InferencePipeline | None = None) -> gr.Blocks:
    hf_token = os.getenv('HF_TOKEN')
    with gr.Blocks() as demo:
        with gr.Row():
            with gr.Column():
                with gr.Box():
                    gr.Markdown('Input Data')
                    input_video = gr.File(label='Input video')
                    input_prompt = gr.Textbox(
                        label='Input prompt',
                        max_lines=1,
                        placeholder='A car is moving on the road.')
                    gr.Markdown('''
                        - Upload a video and write a `Input Prompt` that describes the video.
                        ''')

            with gr.Column():
                with gr.Box():
                    gr.Markdown('Input Parameters')
                    with gr.Row():
                        model_path = gr.Text(
                            label='Path to off-the-shelf model',
                            value='CompVis/stable-diffusion-v1-4',
                            max_lines=1)
                        resolution = gr.Dropdown(choices=['512', '768'],
                                                 value='512',
                                                 label='Resolution',
                                                 visible=False)

                    with gr.Accordion('Advanced settings', open=False):
                        sample_start_idx = gr.Number(
                            label='Start Frame Index',value=0)
                        sample_frame_rate = gr.Number(
                            label='Frame Rate',value=1)
                        n_sample_frames = gr.Number(
                            label='Number of Frames',value=8)
                        guidance_scale = gr.Number(
                            label='Guidance Scale', value=7.5)
                        seed = gr.Slider(label='Seed',
                                         minimum=0,
                                         maximum=100000,
                                         step=1,
                                         randomize=True,
                                         value=33)
                        input_token = gr.Text(label='Hugging Face Write Token',
                                              placeholder='',
                                              visible=False if hf_token else True)
                    gr.Markdown('''
                        - Upload input video or choose an exmple blow
                        - Set hyperparameters & click start
                        - It takes a few minutes to download model first
                    ''')

        with gr.Row():
            with gr.Column():
                validation_prompt = gr.Text(
                    label='Validation Prompt',
                    placeholder=
                    'prompt to test the model, e.g: a Lego man is surfing')

        remove_gpu_after_running = gr.Checkbox(
            label='Remove GPU after running',
            value=False,
            interactive=bool(os.getenv('SPACE_ID')),
            visible=False)

        with gr.Row():
            result = gr.Video(label='Result')

        # examples
        with gr.Row():
            examples = [
                [
                    'CompVis/stable-diffusion-v1-4',
                    "data/car-moving.mp4",
                    'A car is moving on the road.',
                    8, 0, 1,
                    'A jeep car is moving on the desert.',
                    7.5, 512, 33,
                    False, None,
                ],

                [
                    'CompVis/stable-diffusion-v1-4',
                    "data/black-swan.mp4",
                    'A blackswan is swimming on the water.',
                    8, 0, 4,
                    'A white swan is swimming on the water.',
                    7.5, 512, 33,
                    False, None,
                ],

                [
                    'CompVis/stable-diffusion-v1-4',
                    "data/child-riding.mp4",
                    'A child is riding a bike on the road.',
                    8, 0, 1,
                    'A lego child is riding a bike on the road.',
                    7.5, 512, 33,
                    False, None,
                ],

                [
                    'CompVis/stable-diffusion-v1-4',
                    "data/car-turn.mp4",
                    'A jeep car is moving on the road.',
                    8, 0, 6,
                    'A jeep car is moving on the snow.',
                    7.5, 512, 33,
                    False, None,
                ],

                [
                    'CompVis/stable-diffusion-v1-4',
                    "data/rabbit-watermelon.mp4",
                    'A rabbit is eating a watermelon.',
                    8, 0, 6,
                    'A puppy is eating an orange.',
                    7.5, 512, 33,
                    False, None,
                ],

                [
                    'CompVis/stable-diffusion-v1-4',
                    "data/brown-bear.mp4",
                    'A brown bear is sitting on the ground.',
                    8, 0, 6,
                    'A black bear is sitting on the grass.',
                    7.5, 512, 33,
                    False, None,
                ],
            ]
            gr.Examples(examples=examples,
                        fn=runner.run_vid2vid_zero,
                        inputs=[
                            model_path, input_video, input_prompt,
                            n_sample_frames, sample_start_idx, sample_frame_rate,
                            validation_prompt, guidance_scale, resolution, seed,
                            remove_gpu_after_running,
                            input_token,
                        ],
                        outputs=result,
                        cache_examples=os.getenv('SYSTEM') == 'spaces'
                        )

        # run
        run_button_vid2vid_zero = gr.Button('Start vid2vid-zero')
        run_button_vid2vid_zero.click(
            fn=runner.run_vid2vid_zero,
            inputs=[
                model_path, input_video, input_prompt,
                n_sample_frames, sample_start_idx, sample_frame_rate,
                validation_prompt, guidance_scale, resolution, seed,
                remove_gpu_after_running,
                input_token,
            ],
            outputs=result)

    return demo


if __name__ == '__main__':
    hf_token = os.getenv('HF_TOKEN')
    runner = Runner(hf_token)
    demo = create_demo(runner)
    demo.queue(max_size=1).launch(share=False)