Spaces:
Running
Running
import string | |
import copy as cp | |
import os | |
from ..smp import * | |
def can_infer_option(answer, choices): | |
verbose = os.environ.get('VERBOSE', 0) | |
# Choices is a dictionary | |
if 'Failed to obtain answer via API' in answer: | |
return False | |
reject_to_answer = [ | |
"Sorry, I can't help with images of people yet.", | |
"I can't process this file.", | |
"I'm sorry, but without the image provided", | |
'Cannot determine the answer' | |
] | |
for err in reject_to_answer: | |
if err in answer: | |
return 'Z' | |
def count_choice(splits, choices, prefix='', suffix=''): | |
cnt = 0 | |
for c in choices: | |
if prefix + c + suffix in splits: | |
cnt += 1 | |
return cnt | |
answer_mod = cp.copy(answer) | |
chars = '.()[],:;!*#{}' | |
for c in chars: | |
answer_mod = answer_mod.replace(c, ' ') | |
splits = [x.strip() for x in answer_mod.split()] | |
count = count_choice(splits, choices) | |
if count == 1: | |
for ch in choices: | |
if 'A' in splits and len(splits) > 3 and verbose: | |
logger = get_logger('Evaluation') | |
logger.info(f'A might be a quantifier in the string: {answer}.') | |
return False | |
if ch in splits: | |
return ch | |
elif count == 0 and count_choice(splits, {'Z', ''}) == 1: | |
return 'Z' | |
return False | |
def can_infer_text(answer, choices): | |
answer = answer.lower() | |
assert isinstance(choices, dict) | |
for k in choices: | |
assert k in string.ascii_uppercase | |
choices[k] = str(choices[k]).lower() | |
cands = [] | |
for k in choices: | |
if choices[k] in answer: | |
cands.append(k) | |
if len(cands) == 1: | |
return cands[0] | |
return False | |
def can_infer(answer, choices): | |
answer = str(answer) | |
copt = can_infer_option(answer, choices) | |
return copt if copt else can_infer_text(answer, choices) | |