in lmms_eval/tasks/cmmmu/utils.py [0:0]
def get_multi_choice_prediction(response, all_choices, index2ans):
for char in [",", ".", "!", "?", ";", ":", "'"]:
response = response.strip(char)
response = " " + response + " " # add space to avoid partial match
candidates = []
for choice in all_choices: # (A) (B) (C) (D)
# Add the choice to candidates each time it appears in the response
candidates.extend([choice for _ in range(response.count(f"({choice})"))])
if len(candidates) == 0:
for choice in all_choices: # A B C D
# Similarly, add the choice for each occurrence
candidates.extend([choice for _ in range(response.count(f"{choice}"))])
if len(candidates) == 0 and len(response.split()) >= 1:
for index, ans in index2ans.items():
# Add index for each occurrence of ans in response
candidates.extend([index for _ in range(response.count(ans))])
# if all above doesn't get candidates, check if the content is larger than 5 tokens and try to parse the example
if len(candidates) == 0 and len(response.split()) >= 1:
for index, ans in index2ans.items():
if ans in response:
candidates.append(index)
index_ans = False # it's content ans.
if len(candidates) == 0: # still not get answer, randomly choose one.
return random.choice(all_choices)
# return ''
else:
# Count the occurrence of each candidate
candidate_counts = Counter(candidates)
# Select the most frequent candidates
max_count = max(candidate_counts.values())
most_frequent_candidates = [c for c in all_choices if candidate_counts.get(c, 0) == max_count]
# Combine the most frequent candidates in ABCD order
return "".join(most_frequent_candidates)