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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python

from __future__ import annotations

import os
import sys
import warnings
# os.system("cd Make-A-Protagonist/experts/GroundedSAM")
# os.system("python -m pip install -e segment_anything")
# os.system("python -m pip install -e GroundingDINO")
# os.system("cd ../../..")
# os.system("python -m pip install -e Make-A-Protagonist/experts/GroundedSAM/GroundingDINO")
# os.system("pip install --upgrade diffusers[torch]")
warnings.filterwarnings("ignore")

import gradio as gr

from inference import InferencePipeline


class InferenceUtil:
    def __init__(self, hf_token: str | None):
        self.hf_token = hf_token

    def load_model_info(self, model_id: str) -> tuple[str, str]:
        ## TODO the modelcard is in the readme of huggingface repo, should know how to write it
        try:
            card = InferencePipeline.get_model_card(model_id, self.hf_token)
        except Exception:
            return '', ''
            # return ''
        base_model = getattr(card.data, 'base_model', '')
        protagonist = getattr(card.data, 'protagonist', '')
        training_prompt = getattr(card.data, 'training_prompt', '')
        return protagonist, training_prompt
        # return training_prompt


# TITLE = '# [Tune-A-Video](https://tuneavideo.github.io/)'
HF_TOKEN = os.getenv('HF_TOKEN')
# print("HF Token ===> ", HF_TOKEN)
pipe = InferencePipeline(HF_TOKEN)
app = InferenceUtil(HF_TOKEN)

with gr.Blocks(css='style.css') as demo:
    # gr.Markdown(TITLE)
    
    gr.HTML(
    """
    <div style="text-align: center; max-width: 1200px; margin: 20px auto;">
    <h1 style="font-weight: 900; font-size: 2rem; margin: 0rem">
            Make-A-Protagonist:
            <br>
    Generic Video Editing with An Ensemble of Experts
    </h1>
    <h2 style="font-weight: 450; font-size: 1rem; margin: 0rem">
            <a href="https://yuyangzhao.com">Yuyang Zhao</a><sup>1</sup>
            <a href="https://xieenze.github.io/">Enze Xie</a><sup>2</sup> 
            <a href="https://scholar.google.com.sg/citations?user=2p7x6OUAAAAJ&hl=en">Lanqing Hong</a><sup>2</sup>
            <a href="https://scholar.google.com.sg/citations?user=XboZC1AAAAAJ&hl=en">Zhenguo Li</a><sup>2</sup>
            <a href="https://www.comp.nus.edu.sg/~leegh/">Gim Hee Lee</a><sup>1</sup>
    </h2>

    <h2 style="font-weight: 450; font-size: 1rem; margin: 0rem">
            <sup>1 </sup>National University of Singapore
            <sup>2 </sup>Huawei Noah's Ark Lab</span>
    </h2>

    <h2 style="font-weight: 450; font-size: 1rem; margin: 0rem">
                  <span class="link-block">
                    [<a href="https://arxiv.org/abs/2305.08850" target="_blank"
                    class="external-link ">
                    <span class="icon">
                      <i class="ai ai-arxiv"></i>
                    </span>
                    <span>arXiv</span>
                  </a>]
                </span>

                  <!-- Github link -->
                  <span class="link-block">
                    [<a href="https://github.com/Make-A-Protagonist/Make-A-Protagonist" target="_blank"
                    class="external-link ">
                    <span class="icon">
                      <i class="fab fa-github"></i>
                    </span>
                    <span>Code</span>
                  </a>]
                </span>

                <!-- Github link -->
                  <span class="link-block">
                    [<a href="https://make-a-protagonist.github.io/" target="_blank"
                    class="external-link ">
                    <span class="icon">
                      <i class="fab fa-github"></i>
                    </span>
                    <span>Homepage</span>
                  </a>]
                </span>

    </h2>
    <h2 style="font-weight: 450; font-size: 1rem; margin-top: 0.5rem; margin-bottom: 0.5rem">
        TL;DR: The first framework for generic video editing with both visual and textual clues.
    </h2>
    </div>
    """)


    gr.HTML("""
    <p>We provide a <a href="https://github.com/Make-A-Protagonist/Make-A-Protagonist/blob/main/docs/demo_guidance.md"> Demo Guidance </a> to help users to choose hyperparameters when editing videos.
    <p>You may duplicate the space and upgrade GPU for better performance and faster inference without waiting in the queue.
    <p>Alternatively, try our GitHub <a href=https://github.com/Make-A-Protagonist/Make-A-Protagonist> code </a> on your GPU. 
    </p>""")



    with gr.Row():
        with gr.Column():
            with gr.Box():
                model_id = gr.Dropdown(
                    label='Model ID',
                    choices=[
                        'Make-A-Protagonist/ikun',
                        'Make-A-Protagonist/huaqiang',
                        'Make-A-Protagonist/yanzi',
                        'Make-A-Protagonist/car-turn',
                    ],
                    value='Make-A-Protagonist/ikun')

                with gr.Row():
                    base_model_used_for_training = gr.Textbox(
                        label='Protagonist', interactive=False, value='man')
                    prompt_used_for_training = gr.Textbox(
                        label='Training prompt', interactive=False, value='A man is playing basketball')
            with gr.Box():
                ref_image = gr.Image(label='Reference Image', type='pil', visible=True).style(height="auto")
                ref_pro_prompt = gr.Textbox(label='Reference Image Protagonist Prompt',
                    max_lines=1,
                    placeholder='Example: "man"')
                    
            prompt = gr.Textbox(label='Prompt',
                                max_lines=1,
                                placeholder='Example: "A panda is surfing"')
            video_length = gr.Slider(label='Video length',
                                     minimum=4,
                                     maximum=6,
                                     step=1,
                                     value=6)
            fps = gr.Slider(label='FPS',
                            minimum=1,
                            maximum=6,
                            step=1,
                            value=3)
            seed = gr.Slider(label='Seed',
                             minimum=0,
                             maximum=100000,
                             step=1,
                             value=0)

            with gr.Accordion('ControlNet Parameters', open=True):
                control_pose = gr.Slider(label='Pose',
                                      minimum=0,
                                      maximum=1,
                                      step=0.1,
                                      value=.5)
                control_depth = gr.Slider(label='Depth',
                                      minimum=0,
                                      maximum=1,
                                      step=0.1,
                                      value=.5)

            with gr.Accordion('Editing Function', open=True):
                with gr.Row():
                    source_pro = gr.Slider(label='Source Protagonist',
                                      minimum=0,
                                      maximum=1,
                                      step=1,
                                      value=0)
                    source_bg = gr.Slider(label='Source Background',
                                      minimum=0,
                                      maximum=1,
                                      step=1,
                                      value=0)

            with gr.Accordion('Other Parameters', open=False):
                num_steps = gr.Slider(label='Number of Steps',
                                      minimum=0,
                                      maximum=100,
                                      step=1,
                                      value=50)

                start_step = gr.Slider(label='Mask Starting Step',
                                      minimum=0,
                                      maximum=100,
                                      step=1,
                                      value=0)

                guidance_scale = gr.Slider(label='CFG Scale',
                                           minimum=0,
                                           maximum=50,
                                           step=0.1,
                                           value=12.5)

                noise_level = gr.Slider(label='Noise Level',
                                           minimum=0,
                                           maximum=999,
                                           step=1,
                                           value=0)


            run_button = gr.Button('Generate')

            gr.Markdown('''
            - It takes a few minutes to download model first.
            - It takes one minute to load model and conduct DDIM inverse
            ''')
        with gr.Column():
            result = gr.Video(label='Result')
    with gr.Row():
        examples = [
            [
                'Make-A-Protagonist/ikun',
                'A man is playing basketball on the beach, anime style.',
                6,
                3,
                33,
                50,
                12.5,
                'data/ikun/reference_images/zhongli.jpg',
                'man',
                0,
                0,
                0.5,
                0.5,
                0,
                0
            ],

            [
                'Make-A-Protagonist/huaqiang',
                'Elon Musk walking down the street.',
                6,
                3,
                33,
                50,
                12.5,
                'data/huaqiang/reference_images/musk.jpg',
                'man',
                0,
                0,
                0.5,
                0.5,
                0,
                1,
            ],

            [
                'Make-A-Protagonist/yanzi',
                'A panda walking down the snowy street.',
                6,
                3,
                33,
                50,
                12.5,
                'data/yanzi/reference_images/panda.jpeg',
                'panda',
                0,
                0,
                0.5,
                0.5,
                0,
                0
            ],

            [
                'Make-A-Protagonist/car-turn',
                'A car moving in the desert.',
                6,
                3,
                33,
                50,
                12.5,
                'data/car-turn/reference_images/audi.jpeg',
                'car',
                0,
                0,
                0.0,
                1.0,
                0,
                0
            ],

            [
                'Make-A-Protagonist/car-turn',
                'A Suzuki Jimny driving down a mountain road in the rain.',
                6,
                3,
                33,
                50,
                12.5,
                'data/car-turn/images/0000.jpg',
                'car',
                0,
                0,
                0.0,
                1.0,
                1,
                0
            ],

        ]
        gr.Examples(examples=examples,
                    inputs=[
                        model_id,
                        prompt,
                        video_length,
                        fps,
                        seed,
                        num_steps,
                        guidance_scale,
                        ref_image,
                        ref_pro_prompt,
                        noise_level,
                        start_step,
                        control_pose,
                        control_depth,
                        source_pro,
                        source_bg,
                    ],
                    outputs=result,
                    fn=pipe.run,
                    cache_examples=os.getenv('SYSTEM') == 'spaces')

    model_id.change(fn=app.load_model_info,
                    inputs=model_id,
                    outputs=[
                        base_model_used_for_training,
                        prompt_used_for_training,
                    ])
    


    inputs = [
        model_id,
        prompt,
        video_length,
        fps,
        seed,
        num_steps,
        guidance_scale,
        ref_image,
        ref_pro_prompt,
        noise_level,
        start_step,
        control_pose,
        control_depth,
        source_pro,
        source_bg,
    ]
    prompt.submit(fn=pipe.run, inputs=inputs, outputs=result)
    run_button.click(fn=pipe.run, inputs=inputs, outputs=result)

demo.queue().launch()