# Copyright (2024) Bytedance Ltd. and/or its affiliates # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # copy and modify from: https://github.com/OpenGVLab/Ask-Anything/blob/main/video_chat2/demo/demo.py import spaces from copy import deepcopy import gradio as gr from gradio.themes.utils import colors, fonts, sizes from tools.conversation import Chat, conv_templates from tools.utils import load_model_and_processor, file_to_base64 from dataset.processor import Processor import os import torch # huggingface-cli login device = 'cuda' model_path = os.getenv("MODEL_PATH", "omni-research/Tarsier2-7b") max_n_frames = int(os.getenv("MAX_N_FRAMES", 8)) debug = False # ======================================== # Model Initialization # ======================================== def init_model(): print("Start Initialization...") # if torch.cuda.is_available(): if not debug: model, processor = load_model_and_processor(model_path, max_n_frames) else: print(f"No Valid GPU! Lauch in debug mode!") processor = Processor(model_path, max_n_frames) model = None chat = Chat(model, processor, device, debug) print('Initialization Finished') return chat # ======================================== # Gradio Setting # ======================================== def gradio_reset(chat_state, img_file, img_list): if chat_state is not None: chat_state.messages = [] img_file = None if img_list is not None: img_list = [] return None, gr.update(value=None, interactive=True), gr.update(value=None, interactive=True), gr.update(value=None, interactive=True), gr.update(placeholder='Please upload your video first', interactive=False),gr.update(value="Upload & Start Chat", interactive=True), chat_state, img_file, img_list def upload_img(gr_img, gr_video, gr_gif, chat_state, num_frames): print(gr_img, gr_video) conv_type = '' if 'tarsier2-7b' in model_path.lower(): conv_type = 'tarsier2-7b' elif '7b' in model_path.lower(): conv_type = 'tarsier-7b' elif '13b' in model_path.lower(): conv_type = 'tarsier-13b' elif '34b' in model_path.lower(): conv_type = 'tarsier-34b' else: raise ValueError(f"Unknow model: {model_path}") chat_state = deepcopy(conv_templates[conv_type]) img_list = [] if gr_img is None and gr_video is None and gr_gif is None: return None, None, None, gr.update(interactive=True), gr.update(interactive=True, placeholder='Please upload video/image first!'), chat_state, None, None if gr_video or gr_img or gr_gif: for img_file in [gr_video, gr_video, gr_gif]: if img_file is not None: break return gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True, placeholder='Type and press Enter'), gr.update(value="Start Chatting", interactive=False), chat_state, img_file, img_list def gradio_ask(user_message, chatbot, chat_state): if len(user_message) == 0: return gr.update(interactive=True, placeholder='Input should not be empty!'), chatbot, chat_state chat_state = chat.ask(user_message, chat_state) chatbot = chatbot + [[user_message, None]] return '', chatbot, chat_state @spaces.GPU(duration=120) def gradio_answer(chatbot, chat_state, img_file, img_list, top_p, temperature, n_frames=None): llm_message, chat_state, img_list = chat.answer(conv=chat_state, visual_data_file=img_file, images=img_list, n_frames=n_frames, max_new_tokens=512, num_beams=1, temperature=temperature, top_p=top_p) chatbot[-1][1] = llm_message print(chat_state) print(f"Answer: {llm_message}") return chatbot, chat_state, img_list class OpenGVLab(gr.themes.base.Base): def __init__( self, *, primary_hue=colors.blue, secondary_hue=colors.sky, neutral_hue=colors.gray, spacing_size=sizes.spacing_md, radius_size=sizes.radius_sm, text_size=sizes.text_md, font=( fonts.GoogleFont("Noto Sans"), "ui-sans-serif", "sans-serif", ), font_mono=( fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace", ), ): super().__init__( primary_hue=primary_hue, secondary_hue=secondary_hue, neutral_hue=neutral_hue, spacing_size=spacing_size, radius_size=radius_size, text_size=text_size, font=font, font_mono=font_mono, ) super().set( body_background_fill="*neutral_50", ) gvlabtheme = OpenGVLab(primary_hue=colors.blue, secondary_hue=colors.sky, neutral_hue=colors.gray, spacing_size=sizes.spacing_md, radius_size=sizes.radius_sm, text_size=sizes.text_md, ) logo_b64 = file_to_base64("assets/figures/tarsier_logo.jpg") title = f"""