def inference(question, include_blacklisted=True): answers = inference_helper(question) answers = detokenize(answers) answers = replace_in_answers(answers, 'answers') answers_rate = score_answers(answers) try: index = answers_rate.index(1) score = 1 except: index = None if index is None and include_blacklisted: try: index = answers_rate.index(0) score = 0 except: index = 0 score = -1 if index is None: index = 0 score = -1 return {'answers': answers, 'index': index, 'score': score}
def process_questions(questions, include_blacklisted = True): # Make a list if not isinstance(questions, list): questions = [questions] # Clean and tokenize prepared_questions = [] for question in questions: question = question.strip() prepared_questions.append(apply_bpe(tokenize(question)) if question else '##emptyquestion##') # Run inference answers_list = inference_helper(prepared_questions) # Process answers prepared_answers_list = [] for index, answers in enumerate(answers_list): answers = detokenize(answers) answers = replace_in_answers(answers) answers_score = score_answers(answers) best_index, best_score = get_best_score(answers_score, include_blacklisted) if prepared_questions[index] == '##emptyquestion##': prepared_answers_list.append(None) else: prepared_answers_list.append({'answers': answers, 'scores': answers_score, 'best_index': best_index, 'best_score': best_score}) return prepared_answers_list
import sys sys.path.insert(0, '../') from core.sentence import score_answers from colorama import Fore, init tests = [ ['<unk>', -1], ['Word <unk> word', -1], ['[ ]', 0], ['I \'', 0], ['It \'', 0], ['You \' re right , I', 0], ['That \' s what I \'', 0], ['Thank you', 1], ['You', 0], ['What \' s your', 0], ] init() for test in tests: scored = score_answers([test[0]]) print('[{}] {} -> {}{}'.format( Fore.GREEN + 'PASS' + Fore.RESET if scored[0] == test[1] else Fore.RED + 'FAIL' + Fore.RESET, test[0], test[1], '' if scored[0] == test[1] else ' Result: {}'.format(scored[0])))
def inference_internal(question): answers = inference_helper(question) answers = detokenize(answers) answers = replace_in_answers(answers, 'answers') answers_rate = score_answers(answers, 'answers') return (answers, answers_rate)
import sys sys.path.insert(0, '../') from core.sentence import score_answers from colorama import Fore, init tests = [ ['<unk>', -1], ['Word <unk> word', -1], ['[ ]', 0], ['I \'', 0], ['It \'', 0], ['You \' re right , I', 0], ['That \' s what I \'', 0], ['Thank you', 1], ['You', 0], ['What \' s your', 0], ] init() for test in tests: scored = score_answers([test[0]]) print('[{}] {} -> {}{}'.format(Fore.GREEN + 'PASS' + Fore.RESET if scored[0] == test[1] else Fore.RED + 'FAIL' + Fore.RESET, test[0], test[1], '' if scored[0] == test[1] else ' Result: {}'.format(scored[0])))
def inference_internal(self, question): answers = self.do_inference(tokenize(question)) answers = detokenize(answers) answers = replace_in_answers(answers, 'answers') answers_rate = score_answers(answers, 'answers') return (answers, answers_rate)