File size: 6,050 Bytes
ab49e08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import json
import os
import random
import numpy as np
from cryptography.fernet import Fernet

random.seed(0)

# Helper function to load and decrypt the encrypted answer.json
def load_and_decrypt_answer(secret_key):
    try:
        # Read encrypted answer file
        with open("data/answer.enc", "rb") as enc_file:
            encrypted_data = enc_file.read()
        
        # Initialize Fernet cipher with the secret key
        cipher = Fernet(secret_key.encode())
        
        # Decrypt the file
        decrypted_data = cipher.decrypt(encrypted_data).decode("utf-8")
        
        # Parse JSON
        return json.loads(decrypted_data)
    except Exception as e:
        raise ValueError(f"Failed to decrypt answer file: {str(e)}")

def parse_multi_choice_response(response, all_choices, index2ans):
    # (Code unchanged)
    response = str(response)
    for char in [',', '.', '!', '?', ';', ':', "'"]:
        response = response.strip(char)
    response = " " + response + " " # add space to avoid partial match

    index_ans = True
    ans_with_brack = False
    candidates = []
    for choice in all_choices:  # e.g., (A) (B) (C) (D)
        if f'({choice})' in response or f'{choice}. ' in response:
            candidates.append(choice)
            ans_with_brack = True

    if len(candidates) == 0:
        for choice in all_choices: # e.g., A B C D
            if f' {choice} ' in response:
                candidates.append(choice)

    if len(candidates) == 0 and len(response.split()) > 5:
        for index, ans in index2ans.items():
            if ans.lower() in response.lower():
                candidates.append(index)
                index_ans = False

    if len(candidates) == 0:
        pred_index = random.choice(all_choices)
    elif len(candidates) > 1:
        start_indexes = []
        if index_ans:
            if ans_with_brack: 
                for can in candidates:
                    index = response.rfind(f'({can})')
                    start_indexes.append(index)
            else:
                for can in candidates:
                    index = response.rfind(f" {can} ")
                    start_indexes.append(index)
        else:
            for can in candidates:
                index = response.lower().rfind(index2ans[can].lower())
                start_indexes.append(index)
        pred_index = candidates[np.argmax(start_indexes)]
    else:
        pred_index = candidates[0]
    return pred_index

def get_mc_score(row, use_parse = True):
    if use_parse:
        if pd.isna(row["A"]):
            return False
        response = row["prediction"]
        all_choices = []
        for i in range(9):
            if chr(65+i) in row and pd.isna(row[chr(65+i)])== False:
                all_choices.append(chr(65+i))
        index2ans = {index: row[index] for index in all_choices}
        pred_index = parse_multi_choice_response(response, all_choices, index2ans)
    else:
        pred_index = row["output"]
    return pred_index == row["answer"]

def process_json(file):
    try:
        data = json.load(open(file))
    except json.JSONDecodeError:
        return "Error: Invalid JSON format. Please upload a valid JSON file."

    if not isinstance(data, list):
        return "Error: JSON must be a list of records."
    
    required_fields = ['index', 'prediction']
    for record in data:
        if not all(field in record for field in required_fields):
            return f"Error: Each record must contain the following fields: {', '.join(required_fields)}"

    # Decrypt answer.json
    try:
        secret_key = os.getenv("SECRET_KEY")
        answer_data = load_and_decrypt_answer(secret_key)
    except ValueError as e:
        return str(e)

    # Convert to DataFrame
    df = pd.DataFrame(data)
    df = df[['index', 'prediction']]
    answer_df = pd.DataFrame(answer_data)
    df = df.merge(answer_df, on="index", how="left")
    
    # Example categories
    general_datasets = ["SEEDBench", "MMStar", "A-OKVQA", "VizWiz", "MMVet", 
                      "VQAv2", "OKVQA"]
    reason_datasets = ["MMMU", "MathVista", "ScienceQA", "RealWorldQA",  "GQA", "MathVision"]
    ocr_datasets = ["TextVQA", "OCRVQA"]
    doc_datasets = ["AI2D", "ChartQA","DocVQA", "InfoVQA",  "TableVQABench"]
    try:
        score = df.apply(get_mc_score, axis=1) * 100
        df['score'] = score.round(2)
    except Exception as e:
        return f"Error during scoring: {str(e)}"

    # Calculate metrics for each category
    results = {}
    for category in df['category'].unique():
        category_df = df[df['category'] == category]
        category_result = category_df['score'].mean()
        results[category] = category_result
    results['General'] = np.array([results[category] for category in general_datasets]).mean()
    results['Reasoning'] = np.array([results[category] for category in reason_datasets]).mean()
    results['OCR'] = np.array([results[category] for category in ocr_datasets]).mean()
    results['Doc & Chart'] = np.array([results[category] for category in doc_datasets]).mean()
    results['Overall'] = np.array([results[category] for category in df['category'].unique()]).mean()

    return json.dumps(results, indent=4)

def main_gradio():
    example_json = '''[
      {
        "index": 1,
        "prediction": "A"
      },
      {
        "index": 2,
        "prediction": "The answer is C. cat"
      }
    ]'''

    interface = gr.Interface(
        fn=process_json,
        inputs=gr.File(label="Upload JSON File"),
        outputs=gr.Textbox(label="Evaluation Results", interactive=False),
        title="Automated Evaluation for VMCBench",
        description=f"Upload a JSON file containing question index and model prediction to evaluate the performance.\n\n"
                    f"Example JSON format:\n\n{example_json}\n\n"
                    "Each record should contain the fields: 'index', 'prediction'."
    )
    interface.launch(share=True)

if __name__ == "__main__":
    main_gradio()