Datasets:

ArXiv:
File size: 6,809 Bytes
9f3bc09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re, time, os
from tqdm import tqdm
import json
from datetime import datetime
import argparse
import Levenshtein

from base_agent import BaseAgent
from system_prompts import sys_prompts
from tools import ToolCalling
from process import *
import pandas as pd



def parse_args():
    parser = argparse.ArgumentParser(description='Eval-Agent-VBench', formatter_class=argparse.RawTextHelpFormatter)

    parser.add_argument(
        "--user_query",
        type=str,
        required=True,
        help="user query",
    )
    parser.add_argument(
        "--model",
        type=str,
        default="latte1",
        help="target model",
    )

    args = parser.parse_args()
    return args




def most_similar_string(prompt, string_list):
    similarities = [Levenshtein.distance(prompt, item["Prompt"]) for item in string_list]
    most_similar_idx = similarities.index(min(similarities))
    return string_list[most_similar_idx]


def check_and_fix_prompt(chosed_prompts, prompt_list):
    results_dict={}

    for key, item in chosed_prompts.items():
        thought = item["Thought"]
        sim_item = most_similar_string(item["Prompt"], prompt_list)
        sim_item["Thought"] = thought
        results_dict[key] = sim_item
        
    return results_dict


def format_dimension_as_string(df, dimension_name):
    row = df.loc[df['Dimension'] == dimension_name]
    if row.empty:
        return f"No data found for dimension: {dimension_name}"
    
    formatted_string = (
        f"{row['Dimension'].values[0]}: "
        f"Very High -> {row['Very High'].values[0]}, "
        f"High -> {row['High'].values[0]}, "
        f"Moderate -> {row['Moderate'].values[0]}, "
        f"Low -> {row['Low'].values[0]}, "
        f"Very Low -> {row['Very Low'].values[0]}"
    )
    
    return formatted_string



class EvalAgent:
    def __init__(self, sample_model="latte1", save_mode="video", refer_file="vbench_dimension_scores.tsv"):
        # self.tools = ToolCalling(sample_model=sample_model, save_mode=save_mode)
        self.sample_model = sample_model
        self.user_query = ""
        self.tsv_file_path = refer_file
    
    def init_agent(self):

        self.prompt_agent = BaseAgent(system_prompt=sys_prompts["vbench-prompt-sys"], use_history=False, temp=0.7)
        self.plan_agent = BaseAgent(system_prompt=sys_prompts["vbench-plan-sys"], use_history=True, temp=0.7)



    def search_auxiliary(self, designed_prompts, prompt):
        for _, value in designed_prompts.items():
            if value['Prompt'] == prompt:
                return value["auxiliary_info"]
        raise "Didn't find auxiliary info, please check your json."


    def sample_and_eval(self, designed_prompts, save_path, tool_name):
        prompts = [item["Prompt"] for _, item in designed_prompts.items()]
        video_pairs = self.tools.sample(prompts, save_path)
        if 'auxiliary_info' in designed_prompts["Step 1"]:
            for item in video_pairs:
                item["auxiliary_info"] = self.search_auxiliary(designed_prompts, item["prompt"])
        
        eval_results = self.tools.eval(tool_name, video_pairs)
        return eval_results


    def reference_prompt(self, search_dim):
        file_path = "./eval_tools/vbench/VBench_full_info.json"
        data = json.load(open(file_path, "r"))

        results = []
        for item in data:
            if search_dim in item["dimension"]:
                item.pop("dimension")
                item["Prompt"] = item.pop("prompt_en")
                if 'auxiliary_info' in item and search_dim in item['auxiliary_info']:
                    item["auxiliary_info"] = list(item["auxiliary_info"][search_dim].values())[0]
                results.append(item)
        
        return results



    def format_eval_result(self, results, reference_table):
        question = results["Sub-aspect"]
        tool_name = results["Tool"]
        average_score = results["eval_results"]["score"][0]
        video_results = results["eval_results"]["score"][1]
        
        
        output = f"Sub-aspect: {question}\n"
        output += f"The score categorization table for the numerical results evaluated by the '{tool_name}' is as follows:\n{reference_table}\n\n"
        output += f"Observation: The evaluation results using '{tool_name}' are summarized below.\n"
        output += f"Average Score: {average_score:.4f}\n"
        output += "Detailed Results:\n"

        for i, video in enumerate(video_results, 1):
            prompt = video["prompt"]
            score = video["video_results"]
            output += f"\t{i}. Prompt: {prompt}\n"
            output += f"\tScore: {score:.4f}\n"
        
        return output


    def update_info(self):
        folder_name = datetime.now().strftime('%Y-%m-%d-%H:%M:%S') + "-" + self.user_query.replace(" ", "_")
        self.save_path = f"./eval_vbench_results/{self.sample_model}/{folder_name}"
        os.makedirs(self.save_path, exist_ok=True)
        
        self.video_folder = os.path.join(self.save_path, "videos")
        self.file_name = os.path.join(self.save_path, f"eval_results.json")



    def explore(self, query, all_chat=[]):
        
        self.user_query = query
        self.update_info()
        self.init_agent()
        df = pd.read_csv(self.tsv_file_path, sep='\t')


        plan_query = query
        all_chat.append(plan_query)
        
        n = 0
        while True:
            breakpoint()
            plans = self.plan_agent(plan_query, parse=True)
            if plans.get("Analysis"):
                all_chat.append(plans)
                print("Finish!")
                break
            
            tool_name = plans["Tool"].lower().strip().replace(" ", "_")
            reference_table = format_dimension_as_string(df, plans["Tool"])
            
            prompt_query = json.dumps(plans)
            prompt_list = self.reference_prompt(tool_name)
            prompt_query = f"Context:\n{prompt_query}\n\nPrompt list:\n{json.dumps(prompt_list)}"
            
            designed_prompts = self.prompt_agent(prompt_query, parse=True)
            designed_prompts = check_and_fix_prompt(designed_prompts, prompt_list)

            plans["eval_results"] = self.sample_and_eval(designed_prompts, self.video_folder, tool_name)
            plan_query = self.format_eval_result(plans, reference_table=reference_table)

            all_chat.append(plans)
            
            if n > 9:
                break
            n += 1


        all_chat.append(self.plan_agent.messages)
        save_json(all_chat, self.file_name)


def main():
    args = parse_args()
    user_query = args.user_query
    eval_agent = EvalAgent(sample_model=args.model, save_mode="video")
    eval_agent.explore(user_query)


if __name__ == "__main__":
    main()