import spaces import os import re import torch import gradio as gr import sys sys.path.append('./') from videollama2 import model_init, mm_infer from videollama2.utils import disable_torch_init title_markdown = ("""
VideoLLaMA 2 đŸ”Ĩ🚀đŸ”Ĩ

VideoLLaMA 2: Advancing Spatial-Temporal Modeling and Audio Understanding in Video-LLMs

If this demo please you, please give us a star ⭐ on Github or 💖 on this space.
""") block_css = """ #buttons button { min-width: min(120px,100%); color: #9C276A } """ 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 This project is released under the Apache 2.0 license as found in the LICENSE file. The service is a research preview intended for non-commercial use ONLY, subject to the model Licenses of LLaMA and Mistral, Terms of Use of the data generated by OpenAI, and Privacy Practices of ShareGPT. Please get in touch with us if you find any potential violations. """) plum_color = gr.themes.colors.Color( name='plum', c50='#F8E4EF', c100='#E9D0DE', c200='#DABCCD', c300='#CBA8BC', c400='#BC94AB', c500='#AD809A', c600='#9E6C89', c700='#8F5878', c800='#804467', c900='#713056', c950='#662647', ) class Chat: def __init__(self, model_path, load_8bit=False, load_4bit=False): disable_torch_init() self.model, self.processor, self.tokenizer = model_init(model_path, load_8bit=load_8bit, load_4bit=load_4bit) @spaces.GPU(duration=120) @torch.inference_mode() def generate(self, data: list, message, temperature, top_p, max_output_tokens): # TODO: support multiple turns of conversation. assert len(data) == 1 tensor, modal = data[0] response = mm_infer(tensor, message, self.model, self.tokenizer, modal=modal.strip('<>'), do_sample=True if temperature > 0.0 else False, temperature=temperature, top_p=top_p, max_new_tokens=max_output_tokens) return response @spaces.GPU(duration=120) def generate(image, video, audio, message, chatbot, va_tag, textbox_in, temperature, top_p, max_output_tokens, dtype=torch.float16): data = [] processor = handler.processor try: if image is not None: data.append((processor['image'](image).to(handler.model.device, dtype=dtype), '')) elif video is not None: video_audio = processor['video'](video, va=va_tag=="Audio Vision") if va_tag=="Audio Vision": for k,v in video_audio.items(): video_audio[k] = v.to(handler.model.device, dtype=dtype) else: video_audio = video_audio.to(handler.model.device, dtype=dtype) data.append((video_audio, '