File size: 10,031 Bytes
2c924d3
 
 
 
 
 
 
 
71e9a42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c924d3
527b597
 
2c924d3
527b597
 
2c924d3
 
527b597
 
2c924d3
 
 
 
527b597
2c924d3
 
527b597
 
 
 
 
 
 
 
 
 
 
2c924d3
527b597
2c924d3
 
 
 
 
 
 
 
 
f2d7a97
2c924d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2d7a97
 
 
 
 
71e9a42
 
 
 
 
 
 
 
 
f2d7a97
71e9a42
f2d7a97
 
527b597
f2d7a97
 
71e9a42
 
 
 
 
f2d7a97
71e9a42
 
 
 
 
 
527b597
71e9a42
 
2c924d3
527b597
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c924d3
527b597
 
2c924d3
 
 
 
 
527b597
2c924d3
 
 
 
 
 
71e9a42
 
 
 
 
 
2c924d3
71e9a42
 
 
 
2c924d3
 
527b597
2c924d3
 
 
 
71e9a42
 
 
 
 
 
 
 
 
 
2c924d3
71e9a42
527b597
2c924d3
 
 
527b597
 
 
 
 
 
 
 
71e9a42
 
 
 
 
 
 
 
2c924d3
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import gradio as gr
from text_to_animation.model import ControlAnimationModel
import os
from utils.hf_utils import get_model_list

huggingspace_name = os.environ.get("SPACE_AUTHOR_NAME")
on_huggingspace = huggingspace_name if huggingspace_name is not None else False

examples = [["A surfer in miami walking by the beach",
            None,
            "Motion 3",
            None,
            3,
            0,
            None,
            None,
            None,
            None,
            None,
            None,
            0],
            ]
# examples = [
#     ["an astronaut waving the arm on the moon"],
#     ["a sloth surfing on a wakeboard"],
#     ["an astronaut walking on a street"],
#     ["a cute cat walking on grass"],
#     ["a horse is galloping on a street"],
#     ["an astronaut is skiing down the hill"],
#     ["a gorilla walking alone down the street"],
#     ["a gorilla dancing on times square"],
#     ["A panda dancing dancing like crazy on Times Square"],
# ]

def on_video_path_update(evt: gr.EventData):
    return f"Selection: **{evt._data}**"

def pose_gallery_callback(evt: gr.SelectData):
    return f"Motion {evt.index+1}"


def get_frame_index(evt: gr.SelectData):
    return evt.index


def create_demo(model: ControlAnimationModel):
    with gr.Blocks() as demo:
        with gr.Column():
            with gr.Row():
                with gr.Column():
                    # TODO: update so that model_link is customizable
                    model_link = gr.Dropdown(
                        label="Model Link",
                        choices=["runwayml/stable-diffusion-v1-5"],
                        value="runwayml/stable-diffusion-v1-5",
                    )
                    prompt = gr.Textbox(
                        placeholder="Prompt",
                        show_label=False,
                        lines=2,
                        info="Give a prompt for an animation you would like to generate. The prompt will be used to create the first initial frame and then the animation.",
                    )
                    negative_prompt = gr.Textbox(
                        placeholder="Negative Prompt (optional)",
                        show_label=False,
                        lines=2,
                    )

                    gen_frames_button = gr.Button(
                        value="Generate Initial Frames", variant="primary"
                    )

                    with gr.Accordion("Advanced options", open=False):
                        if on_huggingspace:
                            video_length = gr.Slider(
                                label="Video length", minimum=8, maximum=16, step=1
                            )
                        else:
                            video_length = gr.Number(
                                label="Video length", value=8, precision=0
                            )

                        seed = gr.Slider(
                            label="Seed",
                            info="-1 for random seed on each run. Otherwise, the seed will be fixed.",
                            minimum=-1,
                            maximum=65536,
                            value=0,
                            step=1,
                        )

                        motion_field_strength_x = gr.Slider(
                            label="Global Translation $\\delta_{x}$",
                            minimum=-20,
                            maximum=20,
                            value=12,
                            step=1,
                        )

                        motion_field_strength_y = gr.Slider(
                            label="Global Translation $\\delta_{y}$",
                            minimum=-20,
                            maximum=20,
                            value=12,
                            step=1,
                        )

                        t0 = gr.Slider(
                            label="Timestep t0",
                            minimum=0,
                            maximum=47,
                            value=44,
                            step=1,
                            info="Perform DDPM steps from t0 to t1. The larger the gap between t0 and t1, the more variance between the frames. Ensure t0 < t1 ",
                        )

                        t1 = gr.Slider(
                            label="Timestep t1",
                            minimum=1,
                            info="Perform DDPM steps from t0 to t1. The larger the gap between t0 and t1, the more variance between the frames. Ensure t0 < t1",
                            maximum=48,
                            value=47,
                            step=1,
                        )

                        chunk_size = gr.Slider(
                            label="Chunk size",
                            minimum=2,
                            maximum=16,
                            value=8,
                            step=1,
                            visible=not on_huggingspace,
                            info="Number of frames processed at once. Reduce for lower memory usage.",
                        )
                        merging_ratio = gr.Slider(
                            label="Merging ratio",
                            minimum=0.0,
                            maximum=0.9,
                            step=0.1,
                            value=0.0,
                            visible=not on_huggingspace,
                            info="Ratio of how many tokens are merged. The higher the more compression (less memory and faster inference).",
                        )

                with gr.Column():
                    gallery_pose_sequence = gr.Gallery(
                        label="Pose Sequence",
                        value=[
                            ("__assets__/walk_01.gif", "Motion 1"),
                            ("__assets__/walk_02.gif", "Motion 2"),
                            ("__assets__/walk_03.gif", "Motion 3"),
                            ("__assets__/run.gif", "Motion 4"),
                            ("__assets__/dance1.gif", "Motion 5"),
                            ("__assets__/dance2.gif", "Motion 6"),
                            ("__assets__/dance3.gif", "Motion 7"),
                            ("__assets__/dance4.gif", "Motion 8"),
                            ("__assets__/dance5.gif", "Motion 9"),
                        ],
                    ).style(columns=3)
                    input_video_path = gr.Textbox(
                        label="Pose Sequence", visible=False, value="Motion 1"
                    )
                    pose_sequence_selector = gr.Markdown("Pose Sequence: **Motion 1**")

            with gr.Row():
                with gr.Column(visible=True) as frame_selection_view:
                    initial_frames = gr.Gallery(
                        label="Initial Frames", show_label=False
                    ).style(columns=4, rows=1, object_fit="contain", preview=True)

                    gr.Markdown("Select an initial frame to start your animation with.")

                    gen_animation_button = gr.Button(
                        value="Select Initial Frame & Generate Animation",
                        variant="secondary",
                    )

                with gr.Column(visible=True) as animation_view:
                    result = gr.Image(label="Generated Video")

        with gr.Box(visible=False):
            initial_frame_index = gr.Number(
                label="Selected Initial Frame Index", value=-1, precision=0
            )

        input_video_path.change(on_video_path_update, None, pose_sequence_selector)
        gallery_pose_sequence.select(pose_gallery_callback, None, input_video_path)
        initial_frames.select(fn=get_frame_index, outputs=initial_frame_index)

        frame_inputs = [
            prompt,
            input_video_path,
            negative_prompt,
        ]

        animation_inputs = [
            prompt,
            initial_frame_index,
            input_video_path,
            model_link,
            motion_field_strength_x,
            motion_field_strength_y,
            t0,
            t1,
            negative_prompt,
            chunk_size,
            video_length,
            merging_ratio,
            seed,
        ]

        # def submit_select(initial_frame_index: int):
        #     if initial_frame_index != -1:  # More to next step
        #         return {
        #             frame_selection_view: gr.update(visible=False),
        #             animation_view: gr.update(visible=True),
        #         }

        #     return {
        #         frame_selection_view: gr.update(visible=True),
        #         animation_view: gr.update(visible=False),
        #     }

        gen_frames_button.click(
            fn=model.generate_initial_frames,
            inputs=frame_inputs,
            outputs=initial_frames,
        )

        # gen_animation_button.click(
        #     fn=submit_select,
        #     inputs=initial_frame_index,
        #     outputs=[frame_selection_view, animation_view],
        # ).then(
        #     fn=model.generate_animation,
        #     inputs=animation_inputs,
        #     outputs=result,
        # )

        gen_animation_button.click(
            fn=model.generate_animation,
            inputs=animation_inputs,
            outputs=result,
        )

        # gr.Examples(examples=examples,
        #             inputs=inputs,
        #             outputs=result,
        #             fn=None,
        #             run_on_click=False,
        #             cache_examples=on_huggingspace,
        # )

        gr.Examples(examples=examples,
                    inputs=animation_inputs,
                    outputs=result,
                    fn=model.generate_animation,
                    cache_examples=on_huggingspace,
                    run_on_click=True,
                    )

    return demo