File size: 29,449 Bytes
a86c725
f4f731b
 
 
 
 
 
51ae812
dd6b4ee
 
 
f4f731b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
021771f
f4f731b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
375239b
 
f4f731b
 
 
 
 
 
 
 
 
375239b
 
 
f4f731b
 
 
 
 
 
 
 
 
 
 
 
 
375239b
f4f731b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
375239b
 
 
 
 
f4f731b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
021771f
 
f4f731b
 
 
 
 
 
 
 
 
 
375239b
f4f731b
 
 
 
 
 
 
 
dd6b4ee
f4f731b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd6b4ee
f4f731b
 
 
 
dd6b4ee
 
33dd4ea
dd6b4ee
 
 
f4f731b
 
dd6b4ee
f4f731b
dd6b4ee
 
 
 
 
 
 
f4f731b
 
 
 
dd6b4ee
a86c725
 
 
 
 
 
 
 
 
f4f731b
a86c725
 
 
 
 
f4f731b
 
a86c725
f4f731b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
375239b
3291e2d
 
375239b
3291e2d
 
375239b
 
3291e2d
 
375239b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4f731b
 
 
 
 
 
a039337
375239b
3291e2d
 
 
 
 
 
 
 
 
f4f731b
 
375239b
f4f731b
 
 
375239b
 
 
f4f731b
375239b
 
f4f731b
375239b
 
 
 
f4f731b
 
 
375239b
f4f731b
 
375239b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4f731b
 
a86c725
f4f731b
375239b
f4f731b
375239b
 
f4f731b
375239b
f16f54b
375239b
 
 
 
f4f731b
375239b
 
f4f731b
 
 
 
375239b
 
f4f731b
375239b
ef24038
375239b
 
 
 
 
 
 
f4f731b
 
 
ef24038
f4f731b
375239b
 
 
 
51ae812
 
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
import os
# from openai import OpenAI
# if "OPENAI" in os.environ:
#     print('Loaded OPENAI Key.')
# else:
#     print('Can not load the OPENAI key.')
# client = OpenAI(api_key = os.environ['OPENAI'])

import pandas as pd
from huggingface_hub import hf_hub_download

# from lib.MMMU.eval.utils.data_utils import save_json, CAT_SHORT2LONG
print('current dir:', os.getcwd())
# list all files and folders in the current directory
print('list all files and folders in the current directory:', os.listdir())

# from lib.MMMU.eval.utils.eval_utils import evaluate, parse_multi_choice_response, parse_open_response, calculate_ins_level_acc

import numpy as np
import tqdm
import torch
import re
from torch import bfloat16

from transformers import AutoModelForCausalLM, AutoTokenizer
import json
from tqdm import tqdm
# from eval_mixtral import LLM_eval, creat_prompt
# from eval_mixtral import creat_prompt
# from llm_evaluator import chat_llm

from dotenv import load_dotenv

from genai.client import Client
from genai.credentials import Credentials
from genai.schema import (
    DecodingMethod,
    HumanMessage,
    ModerationHAP,
    ModerationHAPInput,
    ModerationHAPOutput,
    ModerationParameters,
    SystemMessage,
    TextGenerationParameters,
)
from genai.text.generation import CreateExecutionOptions

load_dotenv()


#######################################################################################
#######################################################################################
############################  Mixtral eval ############################################
#######################################################################################
#######################################################################################
def heading(text: str) -> str:
    """Helper function for centering text."""
    return "\n" + f" {text} ".center(80, "=") + "\n"


def chat_llm_batch(model_id, prompts, limit= 20):
    parameters = TextGenerationParameters(
        decoding_method=DecodingMethod.SAMPLE, max_new_tokens=128, min_new_tokens=30, temperature=0, top_k=50, top_p=1, random_seed=42
    )
    client = Client(credentials=Credentials.from_env())
    response_list = []
    for response in client.text.generation.create(
        model_id = model_id,
        inputs = prompts, #  here each prompt is the concatenation of system prompt and user prompt
        execution_options=CreateExecutionOptions(concurrency_limit=limit, ordered=False),
        parameters=parameters,
    ):
        response_list.append(response.results[0].generated_text)
    return response_list

def creat_prompt(model_id, tokenizer=None, question=None, answer=None, pred=None,):
    messages = [
        {
            "role": "system",
            "content":
                "You are an intelligent chatbot designed for evaluating the correctness of generative outputs for question-answer pairs. "
                "Your task is to compare the predicted answer with the correct answer and determine if they match meaningfully. Here's how you can accomplish the task:\n"
                "------\n"
                "##INSTRUCTIONS:\n"
                "- Focus on the meaningful match between the predicted answer and the correct answer.\n"
                "- Consider synonyms or paraphrases as valid matches.\n"
                "- Evaluate the correctness of the prediction compared to the answer."
        },
        {
            "role": "user",
            "content":
                "Please evaluate the following question-answer pair:\n\n"
                f"Question: {question.capitalize()}\n"
                f"Correct Answer: {answer.lower()}\n"
                f"Predicted Answer: {pred.lower()}\n\n"
                "Evaluate if the answer is correct with yes/no and assign a correctness score between 0 and 5, where 0 indicates incorrect answer, and 5 signifies the highest meaningful match. "
                "Please generate the response in the form of a Python dictionary string with keys 'pred' and 'score', where value of 'pred' is  a string of 'yes' or 'no' and value of 'score' is in INTEGER, not STRING. "
                "DO NOT PROVIDE ANY OTHER OUTPUT TEXT OR EXPLANATION. Only provide the Python dictionary string. "
                "For example, your response should look like this: {'pred': 'no', 'score': 0}."
        }
    ]

    if 'mistralai' in model_id:
        prompt = f'<s>[INST] {messages[0]["content"].strip()}\n\n{messages[1]["content"].strip()} [/INST]'
    elif 'NousResearch' in model_id:
        prompt = tokenizer.apply_chat_template(messages, tokenize=False)
        prompt = prompt + '<|im_start|>assistant'
    elif 'api' in model_id:
        prompt = messages
    else:
        raise NotImplementedError
    return prompt

## Mixtral Evaluation
def mixtral_eval_api(submission_dict_of_dict, solution_dict_of_dict, category=None,
                     model_id = "mistralai/mixtral-8x7b-instruct-v01", batch_size = 16,
                     category_to_sample_ids_dict = None, answer_dict_for_mixtral_eval = None, limit = None,
                     return_score = False):
    id_list = category_to_sample_ids_dict[category]
    examples_to_eval = []
    evals = []

    sample_id_list = []
    # for row, output in submission_dict_of_dict.iterrows():
    for output_id, output in submission_dict_of_dict.items():
        if output_id in id_list:
            sample_id_list.append(output_id)

    # assert len(sample_id_list) == len(
    #     id_list), f'For dataset {category}, the number of submitted samples ({len(sample_id_list)}) and the number of samples in this dataset ({len(id_list)}) are not the same!'
    steps = int(np.ceil(len(sample_id_list) / batch_size))
    for step in tqdm(range(steps)):
        prompts = []

        # put prompts of a batch of samples into a list
        for item in sample_id_list[step * batch_size: (step + 1) * batch_size]:
            # sample_key = submission_dict_of_dict.iloc[item]['id']
            # answer = str(submission_dict_of_dict.iloc[item]['pred'])
            # label = str(solution_dict_of_dict.iloc[item]['pred'])
            output_id = item
            sample_key = output_id
            answer = str(submission_dict_of_dict[output_id]['pred'])
            label = str(solution_dict_of_dict[output_id]['pred'])
            # print( f'sample_key: {sample_key}, answer: {answer}')
            gt_answer = label
            pred_answer = answer
            question = answer_dict_for_mixtral_eval[sample_key]['question']
            examples_to_eval.append({
                'id': sample_key,
                "question_type": answer_dict_for_mixtral_eval[sample_key]['question_type'],
                'answer': gt_answer,
                'question': question,
                'parsed_pred': pred_answer,
            })
            # the system prompt and user prompt are concatenated
            prompt = creat_prompt(model_id='mistralai', question=question, answer=gt_answer, pred=pred_answer)
            prompts.append(prompt)
        response_list = chat_llm_batch(model_id, prompts, limit=limit)
        evals.extend(response_list)

    judge_dict = {}
    pred_correct = 0
    score_sum = 0
    for sample_data, sample_eval in zip(examples_to_eval, evals):
        try:
            sample_eval = re.match(r".*(\{.*?\}).*", sample_eval, re.S).group(1)
            sample_eval = sample_eval.replace("'", '"')
            sample_eval = json.loads(sample_eval)
            pred = sample_eval['pred']
            sample_score = sample_eval['score']
            if pred == 'yes':
                judge_dict[sample_data['id']] = {'pred': 'Correct', 'score': sample_score}
                pred_correct += 1
            else:
                judge_dict[sample_data['id']] = {'pred': 'Wrong', 'score': sample_score}
            score_sum += sample_score
        except:
            judge_dict[sample_data['id']] = {'pred': 'Wrong', 'score': 0}
    if len(examples_to_eval) == 0:
        return {'acc': 0, 'avg_score': 0}

    if return_score:
        return pred_correct / len(examples_to_eval),  score_sum / len(examples_to_eval)
    else:
        return pred_correct / len(examples_to_eval)


#######################################################################################
#######################################################################################
############################  MMMU eval ###############################################
#######################################################################################
#######################################################################################
def extract_numbers(string):
    """
    Exact all forms of numbers from a string with regex.
    """
    # Pattern for numbers with commas
    pattern_commas = r'-?\b\d{1,3}(?:,\d{3})+\b'
    # Pattern for scientific notation
    pattern_scientific = r'-?\d+(?:\.\d+)?[eE][+-]?\d+'
    # Pattern for simple numbers without commas
    pattern_simple = r'-?(?:\d+\.\d+|\.\d+|\d+\b)(?![eE][+-]?\d+)(?![,\d])'

    # Extract numbers with commas
    numbers_with_commas = re.findall(pattern_commas, string)
    # Extract numbers in scientific notation
    numbers_scientific = re.findall(pattern_scientific, string)
    # Extract simple numbers without commas
    numbers_simple = re.findall(pattern_simple, string)

    # Combine all extracted numbers
    all_numbers = numbers_with_commas + numbers_scientific + numbers_simple
    return all_numbers

def parse_open_response(response):
    """
    Parse the prediction from the generated response.
    Return a list of predicted strings or numbers.
    """

    # content = content.strip("\n").strip(".").strip(" ")
    def get_key_subresponses(response):
        key_responses = []
        response = response.strip().strip(".").lower()
        sub_responses = re.split(r'\.\s(?=[A-Z])|\n', response)
        indicators_of_keys = ['could be ', 'so ', 'is ',
                              'thus ', 'therefore ', 'final ', 'answer ', 'result ']
        key_responses = []
        for index, resp in enumerate(sub_responses):
            # if last one, accept it's an equation (the entire response can be just one sentence with equation)
            if index == len(sub_responses) - 1:
                indicators_of_keys.extend(['='])
            shortest_key_response = None  # the shortest response that may contain the answer (tail part of the response)
            for indicator in indicators_of_keys:
                if indicator in resp:
                    if not shortest_key_response:
                        shortest_key_response = resp.split(indicator)[-1].strip()
                    else:
                        if len(resp.split(indicator)[-1].strip()) < len(shortest_key_response):
                            shortest_key_response = resp.split(indicator)[-1].strip()
                    # key_responses.append(resp.split(indicator)[1].strip())

            if shortest_key_response:
                # and it's not trivial
                if shortest_key_response.strip() not in [":", ",", ".", "!", "?", ";", ":", "'"]:
                    key_responses.append(shortest_key_response)
        if len(key_responses) == 0:  # did not found any
            return [response]
        return key_responses

    # pdb.set_trace()
    key_responses = get_key_subresponses(response)

    pred_list = key_responses.copy()  # keep the original string response
    for resp in key_responses:
        pred_list.extend(extract_numbers(resp))

    tmp_pred_list = []
    for i in range(len(pred_list)):
        tmp_pred_list.extend(normalize_str(pred_list[i]))
    pred_list = tmp_pred_list

    # remove duplicates
    pred_list = list(set(pred_list))

    return pred_list
def check_is_number(string):
    """
    Check if the given string a number.
    """
    try:
        float(string.replace(',', ''))
        return True
    except ValueError:
        # check if there's comma inside
        return False
def normalize_str(string):
    """
    Normalize the str to lower case and make them float numbers if possible.
    """
    # check if characters in the string

    # if number, numerize it.
    string = string.strip()

    is_number = check_is_number(string)

    if is_number:
        string = string.replace(',', '')
        string = float(string)
        # leave 2 decimal
        string = round(string, 2)
        return [string]
    else: # it's likely to be a string
        # lower it
        string = string.lower()
        if len(string) == 1:
            return [" " + string, string + " "] # avoid trivial matches
        return [string]
def eval_open(gold_i, pred_i):
    """
    Evaluate an open question instance
    """
    correct = False
    if isinstance(gold_i, list):
        # use float to avoid trivial matches
        norm_answers = []
        for answer in gold_i:
            norm_answers.extend(normalize_str(answer))
    else:
        norm_answers = normalize_str(gold_i)
    for pred in pred_i: # pred is already normalized in parse response phase
        if isinstance(pred, str): # if it's a string, then find if ans in the pred_i
            for norm_ans in norm_answers:
                # only see if the string answer in the string pred
                if isinstance(norm_ans, str) and norm_ans in pred:
                    if not correct:
                        correct = True
                    break
        else: # it's a float number
            if pred in norm_answers:
                if not correct:
                    correct = True
                break
    return correct
def eval_multi_choice(gold_i, pred_i):
    """
    Evaluate a multiple choice instance.
    """
    correct = False
    # only they are exactly the same, we consider it as correct
    if isinstance(gold_i, list):
        for answer in gold_i:
            if answer == pred_i:
                correct = True
                break
    else: # gold_i is a string
        if gold_i == pred_i:
            correct = True
    return correct

def evaluate(samples):
    """
    Batch evaluation for multiple choice and open questions.
    """
    pred_correct = 0
    judge_dict = dict()
    for sample in samples:
        gold_i = sample['answer']
        pred_i = sample['parsed_pred']
        if sample['question_type'] == 'multiple-choice':
            correct = eval_multi_choice(gold_i, pred_i)
        else: # open question
            correct = eval_open(gold_i, pred_i)

        if correct:
            judge_dict[sample['id']] = 'Correct'
            pred_correct += 1
        else:
            judge_dict[sample['id']] = 'Wrong'

    if len(samples) == 0:
        return None, {'acc': 0}
    return judge_dict, {'acc': pred_correct / len(samples)}


def mmmu_eval(submission_dict_of_dict, solution_dict_of_dict, category=None, category_to_sample_ids_dict = None):
    """
    MMMU evaluation on 6 datasets in the 1st phase: iconqa_fill_in_blank,funsd,iconqa_choose_txt,wildreceipt,textbookqa,tabfact
    only checking whether gt answer is contained in the prediction

    :param submission_dict_of_dict: outputs are predictions of all samples of all datasets
    :param solution_dict_of_dict:  targets are gt of all samples of all datasets
    :return:
    """
    id_list = category_to_sample_ids_dict[category]
    examples_to_eval = []

    # for row, output in submission_dict_of_dict.iterrows():  # row is the sample id, output is the prediction of one sample
    for output_id, output in submission_dict_of_dict.items():  # row is the sample id, output is the prediction of one sample
        if output_id in id_list:
            # print('outputs type', type(submission_dict_of_dict), 'targets type', type(submission_dict_of_dict))
            answer = str(output['pred'])
            # label = str(solution_dict_of_dict.iloc[row]['pred'])
            label = str(solution_dict_of_dict[output_id]['pred'])
            # print('answer:', answer)
            # print('label:', label)

            # this is applied on samples with question type of "short-answer"
            # currently all the samples have the question type of "short-answer"
            parse_pred = parse_open_response(answer) # mainly for type consistency , e.g. for answer of number, convert str to float
            examples_to_eval.append({
                'id': output['id'],
                "question_type": "short-answer",
                'answer': label,
                'parsed_pred': parse_pred,
            })
    # assert len(examples_to_eval) == len(id_list), f'For dataset {category}, the number of submitted samples ({len(examples_to_eval)}) and the number of samples in this dataset ({len(id_list)}) are not the same!'
    # judge_dict: dict of sample_id: 'Correct'/'Wrong'
    # metric_dict: dict of metric_name: metric_value, e.g.  'acc': accuracy
    judge_dict, metric_dict = evaluate(examples_to_eval)
    metric_dict.update({'num_example': len(examples_to_eval)})
    return metric_dict['acc']



def compute(params):
    # Download the two json file
    # category_
    category_to_sample_ids_dict_file = hf_hub_download(
        repo_id=params.competition_id,
        filename="category_to_sample_ids_dict.json",
        token=params.token,
        repo_type="dataset",
    )
    # answer dictionary for mixtral evaluation
    answer_dict_for_mixtral_eval_file = hf_hub_download(
        repo_id=params.competition_id,
        filename="answer_dict_for_mixtral_eval.json",
        token=params.token,
        repo_type="dataset",
    )

    category_to_sample_ids_dict = json.load(open(category_to_sample_ids_dict_file))
    answer_dict_for_mixtral_eval = json.load(open(answer_dict_for_mixtral_eval_file))

    print(f'params  {params}')
    print(f'params.submission_id_col  {params.submission_id_col}')

    print('Downloading solution files ...')
    solution_file = hf_hub_download(
        repo_id=params.competition_id,
        filename="solution.csv",
        token=params.token,
        repo_type="dataset",
    )
    # Download the solution file
    solution_df = pd.read_csv(solution_file)  # 3 columns: submission_id, pred, split

    print('Downloading submission files ...')
    submission_filename = f"submissions/{params.team_id}-{params.submission_id}.csv"
    submission_file = hf_hub_download(
        repo_id=params.competition_id,
        filename=submission_filename,
        token=params.token,
        repo_type="dataset",
    )
    # Download the submission file
    submission_df = pd.read_csv(submission_file) # submission data of a team



    public_ids = solution_df[solution_df.split == "public"][params.submission_id_col].values
    private_ids = solution_df[solution_df.split == "private"][params.submission_id_col].values

    public_solution_df = solution_df[solution_df[params.submission_id_col].isin(public_ids)]
    public_submission_df = submission_df[submission_df[params.submission_id_col].isin(public_ids)]

    private_solution_df = solution_df[solution_df[params.submission_id_col].isin(private_ids)]
    private_submission_df = submission_df[submission_df[params.submission_id_col].isin(private_ids)]

    public_solution_df = public_solution_df.sort_values(params.submission_id_col).reset_index(drop=True) # sort by submission_id
    public_submission_df = public_submission_df.sort_values(params.submission_id_col).reset_index(drop=True)

    private_solution_df = private_solution_df.sort_values(params.submission_id_col).reset_index(drop=True)
    private_submission_df = private_submission_df.sort_values(params.submission_id_col).reset_index(drop=True)

    print('public_solution_df', public_solution_df)
    print('private_solution_df', private_solution_df)



    ### turn csv data into dict of dict
    def csv_to_dict_of_dict(df):
        dict_of_dict = {}
        for row, output in df.iterrows():
            dict_of_dict[output['id']] = output
        return dict_of_dict
    public_solution_dict_of_dict = csv_to_dict_of_dict(public_solution_df)
    public_submission_dict_of_dict = csv_to_dict_of_dict(public_submission_df)
    private_solution_dict_of_dict = csv_to_dict_of_dict(private_solution_df)
    private_submission_dict_of_dict = csv_to_dict_of_dict(private_submission_df)

    # print(public_submission_dict_of_dict)

    ####################################################### Wei's part ###############################################

    # phase1_datasets_for_mmmu_eval = ['iconqa_fill','funsd','iconqa_choose','wildreceipt','textbookqa','tabfact']
    # phase1_datasets_for_mixtral_eval = ['docvqa','infographicvqa','websrc','wtq']

    phase1_datasets_for_mmmu_eval = []
    phase1_datasets_for_mixtral_eval = []

    phase2_datasets_for_mmmu_eval = []
    # phase2_datasets_for_mixtral_eval = ['mydoc', 'mychart', 'myinfographic' ]
    phase2_datasets_for_mixtral_eval = ['mydoc',  ]


    all_keys = [

        'iconqa_fill_acc','funsd_acc','iconqa_choose_acc','wildreceipt_acc','textbookqa_acc','tabfact_acc', # phase 1 datasets for MMMU evaluation
                'docvqa_acc','infographicvqa_acc','websrc_acc','wtq_acc',  # phase 2 datasets for Mixtral evaluation
                'phase1_overall_acc',  # phase 1 overall acc

                'mydoc_acc', # phase 2  oshri data
                'mydoc_rating',
                'mychart_acc', # phase 2 sivanharary data
                'mychart_rating',
                'myinfographic_acc', # phase 2 infographics data
                'myinfographic_rating',

                'phase2_overall_acc', # phase 2 overall acc
                'phase2_overall_rating' # phase 2 overall rating
                ]
    mixtral_eval_batch_size = 16
    limit = 20 # concurrency limit
    public_category_score_dict = {}
    private_category_score_dict = {}
    for key_ in all_keys:
        public_category_score_dict.update({key_: 0})
        private_category_score_dict.update({key_: 0})

    # ############################################# Check if all the sample ids are in the submission file  ########################################################################################
    # missing_sample_ids = []
    # for datasetname, sample_ids in category_to_sample_ids_dict.items():
    #     for sample_id in sample_ids:
    #         if sample_id not in public_submission_dict_of_dict:
    #             missing_sample_ids.append(sample_id)
    # if len(missing_sample_ids) > 0:
    #     print(f'Error: missing sample ids in the submission file: {missing_sample_ids}')
    #     return

    ###################################################################################################################################################
    ############################################# phase 1 eval (only acc)  ########################################################################################
    ###################################################################################################################################################

    ## MMMU evaluation
    for datasetname in phase1_datasets_for_mmmu_eval:
        print(f'#################################################### Phase 1 Eval: MMMU evaluation for {datasetname} ###########################################')
        public_category_score_dict[f'{datasetname}_acc'] = mmmu_eval(public_submission_dict_of_dict, public_solution_dict_of_dict, datasetname,  category_to_sample_ids_dict=  category_to_sample_ids_dict)
    ## Mixtral evaluation
    for datasetname in phase1_datasets_for_mixtral_eval:
        print(f'#################################################### Phase 1 Eval: Mixtral evaluation for {datasetname} ###########################################')
        # here the returned result is a dict {'acc': accuracy, 'avg_score': average score}
        public_category_score_dict[f'{datasetname}_acc'] = mixtral_eval_api(public_submission_dict_of_dict, public_solution_dict_of_dict, datasetname,
         category_to_sample_ids_dict =category_to_sample_ids_dict, answer_dict_for_mixtral_eval = answer_dict_for_mixtral_eval, batch_size=mixtral_eval_batch_size, limit=limit)




    ###################################################################################################################################################
    ############################################# phase 2 eval (acc and rating )  #######################################################################################
    ###################################################################################################################################################
    ## Mixtral evaluation
    for datasetname in phase2_datasets_for_mixtral_eval:
        print(f'#################################################### Phase 2 Eval: Mixtral evaluation for {datasetname} ###########################################')
        public_category_score_dict[f'{datasetname}_acc'], public_category_score_dict[f'{datasetname}_rating'] =  mixtral_eval_api(public_submission_dict_of_dict, public_solution_dict_of_dict, datasetname,
                         category_to_sample_ids_dict=category_to_sample_ids_dict,
                         answer_dict_for_mixtral_eval=answer_dict_for_mixtral_eval, batch_size=mixtral_eval_batch_size,
                         limit=limit, return_score=True)

    ###################################################################################################################################################
    ############################################# print summary  ########################################################################################
    ###################################################################################################################################################

    print('################# Phase 1 Evaluation #################')
    # print the heading of category n_samples acc
    for metric_type in ['acc']:
        n_total = 0
        metric_sum = 0
        print('category'.ljust(29, ' '), 'n_samples'.ljust(10, ' '), metric_type.ljust(9, ' '))
        for datasetname in phase1_datasets_for_mmmu_eval + phase1_datasets_for_mixtral_eval:
            metric_name = f'{datasetname}_{metric_type}'
            metric = public_category_score_dict[metric_name]
            dataset_size = len(category_to_sample_ids_dict[datasetname])
            dataset_metric_sum = metric * dataset_size
            # print(f'{category}\t{dataset_total}\t{acc}')
            print(f'{metric_name:<30}{dataset_size:<10}{metric:<10.4f}')
            metric_sum += dataset_metric_sum
            n_total += dataset_size
        overall_metric = 0 if n_total == 0 else metric_sum / float(n_total)
        print(f'overall_{metric_type}'.ljust(29, ' '), f'{n_total}'.ljust(10, ' '), f'{overall_metric:.4f}'.ljust(9, ' '))
        public_category_score_dict[f'phase1_overall_{metric_type}'] = overall_metric
        print('\n')


    print('################# Phase 2 Evaluation #################')
    for metric_type in ['acc', 'rating']:
        n_total = 0
        metric_sum = 0
        print('category'.ljust(29, ' '), 'n_samples'.ljust(10, ' '), metric_type.ljust(9, ' '))
        for datasetname in phase2_datasets_for_mixtral_eval:
            metric_name = f'{datasetname}_{metric_type}'
            metric = public_category_score_dict[metric_name]
            dataset_size = len(category_to_sample_ids_dict[datasetname])
            dataset_metric_sum = metric * dataset_size

            print(f'{metric_name:<30}{dataset_size:<10}{metric:<10.4f}')
            metric_sum += dataset_metric_sum
            n_total += dataset_size

        overall_metric = 0 if n_total == 0 else metric_sum / float(n_total)
        print(f'overall_{metric_type}'.ljust(29, ' '), f'{n_total}'.ljust(10, ' '), f'{overall_metric:.4f}'.ljust(9, ' '))
        public_category_score_dict[f'phase2_overall_{metric_type}'] = overall_metric
        print('\n')

    """
    
        metric_dict = {
            "public_score":  # the user will see the results immediately after submitting 
                {
                phase1dataset1_acc: xx, 
                phase1dataset2_acc: xx, 
                ...	
                phase1_overall_acc: xx
                
                phase2dataset1_acc: xx,   
                phase2dataset1_rating: xx,   
                phase2dataset2_acc: xx,   
                phase2dataset2_rating: xx,   
                ...	
                phase2_overall_acc: xx,   
                phase2_overall_rating: xx,   
                },
                           
            "private_score": 
                {
                phase1dataset1_acc: 0, 
                phase1dataset2_acc: 0, 
                ...	
                phase1_overall_acc: 0,
                
                phase2dataset1_acc: 0,   
                phase2dataset1_rating: 0,   
                phase2dataset2_acc: 0,   
                phase2dataset2_rating: 0,   
                ...	
                phase2_overall_acc: 0,   
                phase2_overall_rating: 0   
                }
        }  
    """

    metric_dict = {"public_score": public_category_score_dict,
                   "private_score": private_category_score_dict } # keys of public_score, private_score is a must
    # metric_dict = {**public_category_score_dict, **private_category_score_dict} 
    print("metric_dict:=========")
    print(metric_dict)

    return metric_dict