File size: 14,413 Bytes
3828b19
 
e428df4
 
 
5193f48
 
e428df4
 
 
 
 
 
 
 
 
 
 
000c55e
e428df4
 
 
46b9d73
e428df4
 
 
 
 
03c1c79
c17f8ec
03c1c79
c17f8ec
e428df4
 
 
 
 
 
 
 
 
5193f48
 
 
e428df4
 
 
 
 
 
 
5193f48
 
e428df4
 
 
 
5193f48
 
c92a6b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e428df4
8c55b8f
e428df4
 
 
 
 
6bc6b84
e428df4
 
 
5193f48
e428df4
 
 
 
5193f48
0c50b58
e428df4
cf69be0
e428df4
 
 
 
 
 
 
 
 
ee3d0a5
000c55e
 
e428df4
 
 
 
 
 
 
 
 
 
 
 
 
cf69be0
 
 
e428df4
 
 
 
 
 
 
 
 
12e9783
ee3d0a5
e428df4
 
 
 
 
ee3d0a5
e428df4
 
 
 
 
7e442ac
 
 
e428df4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c17f8ec
 
 
cf69be0
e428df4
 
 
 
 
 
 
b76f6c0
e428df4
b76f6c0
e428df4
ee3d0a5
e428df4
 
ee3d0a5
 
 
 
 
 
 
e428df4
 
ee3d0a5
 
e428df4
 
ee3d0a5
 
e428df4
 
7e442ac
e428df4
 
 
ee3d0a5
 
 
 
e428df4
000c55e
 
 
 
e428df4
8c55b8f
 
 
 
 
 
 
000c55e
8c55b8f
fe61520
8c55b8f
 
 
 
 
 
 
93426a3
8c55b8f
c574208
cf69be0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c55b8f
 
 
 
46b9d73
 
e428df4
8c55b8f
 
 
 
 
 
 
 
 
 
 
cf69be0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c55b8f
 
 
 
7e442ac
 
ee3d0a5
 
8c55b8f
7e442ac
 
ee3d0a5
 
7e442ac
ee3d0a5
 
7e442ac
 
 
 
ee3d0a5
8c55b8f
 
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
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
import spaces

import os

import torch
import gradio as gr

import sys
sys.path.append('./')
from videollama2.constants import MMODAL_TOKEN_INDEX, DEFAULT_MMODAL_TOKEN
from videollama2.conversation import conv_templates, SeparatorStyle, Conversation
from videollama2.model.builder import load_pretrained_model
from videollama2.mm_utils import KeywordsStoppingCriteria, tokenizer_MMODAL_token, get_model_name_from_path, process_image, process_video


title_markdown = ("""
<div style="display: flex; justify-content: center; align-items: center; text-align: center;">
  <a href="https://github.com/DAMO-NLP-SG/VideoLLaMA2" style="margin-right: 20px; text-decoration: none; display: flex; align-items: center;">
    <img src="https://s2.loli.net/2024/06/03/D3NeXHWy5az9tmT.png" alt="VideoLLaMA 2 πŸ”₯πŸš€πŸ”₯" style="max-width: 120px; height: auto;">
  </a>
  <div>
    <h1 >VideoLLaMA 2: Advancing Spatial-Temporal Modeling and Audio Understanding in Video-LLMs</h1>
    <h5 style="margin: 0;">If this demo please you, please give us a star ⭐ on Github or πŸ’– on this space.</h5>
  </div>
</div>


<div align="center">
    <div style="display:flex; gap: 0.25rem; margin-top: 10px;" align="center">
        <a href="https://github.com/DAMO-NLP-SG/VideoLLaMA2"><img src='https://img.shields.io/badge/Github-VideoLLaMA2-9C276A'></a>
        <a href="https://arxiv.org/pdf/2406.07476.pdf"><img src="https://img.shields.io/badge/Arxiv-2406.07476-AD1C18"></a>
        <a href="https://github.com/DAMO-NLP-SG/VideoLLaMA2/stargazers"><img src="https://img.shields.io/github/stars/DAMO-NLP-SG/VideoLLaMA2.svg?style=social"></a>
    </div>
</div>
""")


block_css = """
#buttons button {
    min-width: min(120px,100%);
}
"""


tos_markdown = ("""
### Terms of use
By using this service, users are required to agree to the following terms:
The service is a research preview intended for non-commercial use only. It only provides limited safety measures and may generate offensive content. It must not be used for any illegal, harmful, violent, racist, or sexual purposes. The service may collect user dialogue data for future research.
Please click the "Flag" button if you get any inappropriate answer! We will collect those to keep improving our moderator.
For an optimal experience, please use desktop computers for this demo, as mobile devices may compromise its quality.
""")


learn_more_markdown = ("""
### License
The service is a research preview intended for non-commercial use only, subject to the model [License](https://github.com/facebookresearch/llama/blob/main/MODEL_CARD.md) of LLaMA, [Terms of Use](https://openai.com/policies/terms-of-use) of the data generated by OpenAI, and [Privacy Practices](https://chrome.google.com/webstore/detail/sharegpt-share-your-chatg/daiacboceoaocpibfodeljbdfacokfjb) of ShareGPT. Please contact us if you find any potential violation.
""")


plum_color = gr.themes.colors.Color(
    name='plum',
    c50='#F8E4EF',
    c100='#EDD2DF',
    c200='#E4BFD2',
    c300='#DBACC5',
    c400='#D299B8',
    c500='#C986AB',
    c600='#C0739E',
    c700='#B76091',
    c800='#AE4D84',
    c900='#A53A77',
    c950='#9C276A',
)


class Chat:
    def __init__(self, model_path, conv_mode, model_base=None, load_8bit=False, load_4bit=False):
        # disable_torch_init()
        model_name = get_model_name_from_path(model_path)
        self.tokenizer, self.model, processor, context_len = load_pretrained_model(
            model_path, model_base, model_name,
            load_8bit, load_4bit,
            offload_folder="save_folder")
        self.processor = processor
        self.conv_mode = conv_mode
        self.conv = conv_templates[conv_mode].copy()

    def get_prompt(self, qs, state):
        state.append_message(state.roles[0], qs)
        state.append_message(state.roles[1], None)
        return state

    @spaces.GPU(duration=120)
    @torch.inference_mode()
    def generate(self, tensor: list, modals: list, prompt: str, first_run: bool, state, temperature, top_p, max_output_tokens):
        # TODO: support multiple turns of conversation.
        assert len(tensor) == len(modals)

        # 1. prepare model, tokenizer, and processor.
        tokenizer, model, processor = self.tokenizer, self.model, self.processor

        # 2. text preprocess (tag process & generate prompt).
        state = self.get_prompt(prompt, state)
        prompt = state.get_prompt()

        input_ids = tokenizer_MMODAL_token(prompt, tokenizer, MMODAL_TOKEN_INDEX[modals[0]], return_tensors='pt')
        input_ids = input_ids.unsqueeze(0).to(self.model.device)

        # 3. generate response according to visual signals and prompts. 
        stop_str = self.conv.sep if self.conv.sep_style in [SeparatorStyle.SINGLE] else self.conv.sep2
        # keywords = ["<s>", "</s>"]
        keywords = [stop_str]
        stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)

        with torch.inference_mode():
            output_ids = model.generate(
                input_ids,
                images_or_videos=tensor,
                modal_list=modals,
                do_sample=True,
                temperature=temperature,
                top_p=top_p,
                max_new_tokens=max_output_tokens,
                use_cache=True,
                stopping_criteria=[stopping_criteria],
            )

        outputs = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]
        print(outputs)
        return outputs, state


@spaces.GPU(duration=120)
def generate(image, video, state, state_, textbox_in, temperature, top_p, max_output_tokens, dtype=torch.float16):
    if not textbox_in:
        if len(state_.messages) > 0:
            textbox_in = state_.messages[-1][1]
            state_.messages.pop(-1)
        else:
            assert "Please enter instruction"

    image = image if image else "none"
    video = video if video else "none"
    assert not (os.path.exists(image) and os.path.exists(video))

    tensor = []
    modals = []

    if type(state) is not Conversation:
        state = conv_templates[conv_mode].copy()
        state_ = conv_templates[conv_mode].copy()

    first_run = False if len(state.messages) > 0 else True

    text_en_in = textbox_in.replace("picture", "image")

    processor = handler.processor
    if os.path.exists(image) and not os.path.exists(video):
        tensor.append(process_image(image, processor).to(handler.model.device, dtype=dtype))
        modals.append('IMAGE')
    if not os.path.exists(image) and os.path.exists(video):
        tensor.append(process_video(video, processor).to(handler.model.device, dtype=dtype))
        modals.append('VIDEO')
    if os.path.exists(image) and os.path.exists(video):
        raise NotImplementedError("Not support image and video at the same time")

    # BUG: Only support single video and image inference now.
    if os.path.exists(image) and not os.path.exists(video):
        text_en_in = text_en_in.replace(DEFAULT_MMODAL_TOKEN['IMAGE'], '').strip()
        text_en_in = DEFAULT_MMODAL_TOKEN['IMAGE'] + '\n' + text_en_in
    if not os.path.exists(image) and os.path.exists(video):
        text_en_in = text_en_in.replace(DEFAULT_MMODAL_TOKEN['VIDEO'], '').strip()
        text_en_in = DEFAULT_MMODAL_TOKEN['VIDEO'] + '\n' + text_en_in
    if os.path.exists(image) and os.path.exists(video):
        text_en_in = text_en_in.replace(DEFAULT_MMODAL_TOKEN['VIDEO'], '').strip()
        text_en_in = DEFAULT_MMODAL_TOKEN['VIDEO'] + '\n' + text_en_in
    text_en_out, state_ = handler.generate(tensor, modals, text_en_in, first_run=first_run, state=state_, temperature=temperature, top_p=top_p, max_output_tokens=max_output_tokens)
    state_.messages[-1] = (state_.roles[1], text_en_out)

    text_en_out = text_en_out.split('#')[0]
    textbox_out = text_en_out

    show_images = ""
    if os.path.exists(image):
        show_images += f'<img src="./file={image}" style="display: inline-block;width: 250px;max-height: 400px;">'
    if os.path.exists(video):
        show_images += f'<video controls playsinline width="500" style="display: inline-block;"  src="./file={video}"></video>'

    state.append_message(state.roles[0], textbox_in + "\n" + show_images)
    state.append_message(state.roles[1], textbox_out)

    # BUG: only support single turn conversation now.
    state_.messages.pop(-1)
    state_.messages.pop(-1)

    return (gr.update(value=image if os.path.exists(image) else None, interactive=True), 
            gr.update(value=video if os.path.exists(video) else None, interactive=True), 
            state.to_gradio_chatbot(), state, state_)


def regenerate(state, state_):
    state.messages.pop(-1)
    state.messages.pop(-1)
    if len(state.messages) > 0:
        return state.to_gradio_chatbot(), state, state_
    return state.to_gradio_chatbot(), state, state_


def clear_history(state, state_):
    state = conv_templates[conv_mode].copy()
    state_ = conv_templates[conv_mode].copy()
    return (gr.update(value=None, interactive=True),
            gr.update(value=None, interactive=True),
            state.to_gradio_chatbot(), state, state_, 
            gr.update(value=None, interactive=True))


# BUG of Zero Environment
# 1. The environment is fixed to torch==2.0.1+cu117, gradio>=4.x.x
# 2. The operation or tensor which requires cuda are limited in those functions wrapped via spaces.GPU
# 3. The function can't return tensor or other cuda objects.

conv_mode = "llama_2"
model_path = 'DAMO-NLP-SG/VideoLLaMA2-7B'

device = torch.device("cuda")

handler = Chat(model_path, conv_mode=conv_mode, load_8bit=False, load_4bit=True)

textbox = gr.Textbox(show_label=False, placeholder="Enter text and press ENTER", container=False)

with gr.Blocks(title='VideoLLaMA 2 πŸ”₯πŸš€πŸ”₯', theme=gr.themes.Default(primary_hue=gr.themes.colors.violet), css=block_css) as demo:
    gr.Markdown(title_markdown)
    state = gr.State()
    state_ = gr.State()

    with gr.Row():
        with gr.Column(scale=3):
            image = gr.Image(label="Input Image", type="filepath")
            video = gr.Video(label="Input Video")

            with gr.Accordion("Parameters", open=True) as parameter_row:
                # num_beams = gr.Slider(
                #     minimum=1,
                #     maximum=10,
                #     value=1,
                #     step=1,
                #     interactive=True,
                #     label="beam search numbers",
                # )

                temperature = gr.Slider(
                    minimum=0.1,
                    maximum=1.0,
                    value=0.2,
                    step=0.1,
                    interactive=True,
                    label="Temperature",
                )

                top_p = gr.Slider(
                        minimum=0.0,
                        maximum=1.0,
                        value=0.7,
                        step=0.1,
                        interactive=True,
                        label="Top P",
                )

                max_output_tokens = gr.Slider(
                    minimum=64,
                    maximum=1024,
                    value=512,
                    step=64,
                    interactive=True,
                    label="Max output tokens",
                )

        with gr.Column(scale=7):
            chatbot = gr.Chatbot(label="VideoLLaMA 2", bubble_full_width=True, height=750)
            with gr.Row():
                with gr.Column(scale=8):
                    textbox.render()
                with gr.Column(scale=1, min_width=50):
                    submit_btn = gr.Button(value="Send", variant="primary", interactive=True)
            with gr.Row(elem_id="buttons") as button_row:
                upvote_btn     = gr.Button(value="πŸ‘  Upvote", interactive=True)
                downvote_btn   = gr.Button(value="πŸ‘Ž  Downvote", interactive=True)
                # flag_btn     = gr.Button(value="⚠️  Flag", interactive=True)
                # stop_btn     = gr.Button(value="⏹️  Stop Generation", interactive=False)
                regenerate_btn = gr.Button(value="πŸ”„  Regenerate", interactive=True)
                clear_btn      = gr.Button(value="πŸ—‘οΈ  Clear history", interactive=True)

        with gr.Column():
            cur_dir = os.path.dirname(os.path.abspath(__file__))
            gr.Examples(
                examples=[
                    [
                        f"{cur_dir}/examples/sora.png",
                        "What happens in this image?",
                    ],
                    [
                        f"{cur_dir}/examples/waterview.jpg",
                        "What are the things I should be cautious about when I visit here?",
                    ],
                    [
                        f"{cur_dir}/examples/desert.jpg",
                        "If there are factual errors in the questions, point it out; if not, proceed answering the question. What’s happening in the desert?",
                    ],
                ],
                inputs=[image, textbox],
            )
            gr.Examples(
                examples=[
                    [
                        f"{cur_dir}/examples/rap.mp4",
                        "What happens in this video?",
                    ],
                    [
                        f"{cur_dir}/examples/demo2.mp4",
                        "Do you think it's morning or night in this video? Why?",
                    ],
                    [
                        f"{cur_dir}/examples/demo3.mp4",
                        "At the intersection, in which direction does the red car turn?",
                    ],
                ],
                inputs=[video, textbox],
            )

    gr.Markdown(tos_markdown)
    gr.Markdown(learn_more_markdown)

    submit_btn.click(
        generate, 
        [image, video, state, state_, textbox, temperature, top_p, max_output_tokens],
        [image, video, chatbot, state, state_])

    regenerate_btn.click(
        regenerate, 
        [state, state_], 
        [chatbot, state, state_]).then(
        generate, 
        [image, video, state, state_, textbox, temperature, top_p, max_output_tokens], 
        [image, video, chatbot, state, state_])

    clear_btn.click(
        clear_history, 
        [state, state_],
        [image, video, chatbot, state, state_, textbox])

demo.launch()