import gradio as gr import spaces import os import time import json import numpy as np import av import torch from PIL import Image import functools from transformers import AutoProcessor, Idefics2ForConditionalGeneration from models.conversation import conv_templates from typing import List processor = AutoProcessor.from_pretrained("Mantis-VL/mantis-8b-idefics2-video-eval-refined-40k_4096_generation") model = Idefics2ForConditionalGeneration.from_pretrained("Mantis-VL/mantis-8b-idefics2-video-eval-refined-40k_4096_generation", device_map="auto", torch_dtype=torch.bfloat16).eval() MAX_NUM_FRAMES = 24 conv_template = conv_templates["idefics_2"] with open("./examples/all_subsets.json", 'r') as f: examples = json.load(f) for item in examples: video_id = item['images'][0].split("_")[0] item['images'] = [os.path.join("./examples", video_id, x) for x in item['images']] item['video'] = os.path.join("./examples", item['video']) with open("./examples/hd.json", 'r') as f: hd_examples = json.load(f) for item in hd_examples: item['video'] = os.path.join("./examples", item['video']) examples = hd_examples + examples VIDEO_EVAL_PROMPT = """ Suppose you are an expert in judging and evaluating the quality of AI-generated videos, please watch the following frames of a given video and see the text prompt for generating the video, then give scores from 5 different dimensions: (1) visual quality: the quality of the video in terms of clearness, resolution, brightness, and color (2) temporal consistency, the consistency of objects or humans in video (3) dynamic degree, the degree of dynamic changes (4) text-to-video alignment, the alignment between the text prompt and the video content (5) factual consistency, the consistency of the video content with the common-sense and factual knowledge For each dimension, output a number from [1,2,3,4], in which '1' means 'Bad', '2' means 'Average', '3' means 'Good', '4' means 'Real' or 'Perfect' (the video is like a real video) Here is an output example: visual quality: 4 temporal consistency: 4 dynamic degree: 3 text-to-video alignment: 1 factual consistency: 2 For this video, the text prompt is "{text_prompt}", all the frames of video are as follows: """ @spaces.GPU(duration=60) def generate(text:str, images:List[Image.Image], history: List[dict], **kwargs): model.to("cuda") if not images: images = None user_role = conv_template.roles[0] assistant_role = conv_template.roles[1] idefics_2_message = [] cur_img_idx = 0 cur_vid_idx = 0 all_videos = [x for x in images if isinstance(x, list)] flatten_images = [] for x in images: if isinstance(x, list): flatten_images.extend(x) else: flatten_images.append(x) print(history) for i, message in enumerate(history): if message["role"] == user_role: idefics_2_message.append({ "role": user_role, "content": [] }) message_text = message["text"] num_video_tokens_in_text = message_text.count("