import os import json import argparse from tqdm import tqdm import time import torch from transformers import AutoModelForCausalLM, AutoProcessor LEVEL_DIRS = ["level1", "level2", "level3"] GENERIC_RESULT_PATTERN = "_result.json" def get_media_type(file_path: str) -> str: ext = os.path.splitext(file_path)[1].lower() if ext in ['.mp4', '.avi', '.mov', '.mkv', '.webm']: return 'video' elif ext in ['.jpg', '.jpeg', '.png', '.bmp', '.gif', '.webp']: return 'image' else: raise ValueError(f"Unsupported file extension: {ext} in file {file_path}") def process_task(task_path: str, model, processor, result_suffix: str, device: str): source_json_files = [ f for f in os.listdir(task_path) if f.endswith('.json') and GENERIC_RESULT_PATTERN not in f ] if not source_json_files: return for json_filename in source_json_files: dataset_json_path = os.path.join(task_path, json_filename) result_json_path = os.path.join(task_path, f"{os.path.splitext(json_filename)[0]}{result_suffix}") if os.path.exists(result_json_path): continue try: with open(dataset_json_path, 'r', encoding='utf-8') as f: data = json.load(f) except (json.JSONDecodeError, FileNotFoundError) as e: continue all_results = [] for item in tqdm(data, desc=f" Processing {json_filename}"): start_time = time.time() model_output = "N/A" try: prompt_text = item['conversations'][0]['value'] ground_truth = item['conversations'][1]['value'] media_path_key = 'image' if 'image' in item else 'video' media_relative_path = item.get(media_path_key) if not media_relative_path: raise ValueError("Missing 'image' or 'video' key in JSON entry.") media_full_path = os.path.join(task_path, media_relative_path) if not os.path.exists(media_full_path): raise FileNotFoundError(f"Media file not found: {media_full_path}") media_type = get_media_type(media_full_path) clean_prompt = prompt_text.replace("", "").replace("