def translate(output_handler: OutputHandler, trans_inputs: List[inference.TranslatorInput], translator: inference.Translator) -> float: """ Translates each line from source_data, calling output handler after translating a batch. :param output_handler: A handler that will be called once with the output of each translation. :param trans_inputs: A enumerable list of translator inputs. :param translator: The translator that will be used for each line of input. :return: Total time taken. """ tic = time.time() trans_outputs = translator.translate(trans_inputs) total_time = time.time() - tic batch_time = total_time / len(trans_inputs) for trans_input, trans_output in zip(trans_inputs, trans_outputs): output_handler.handle(trans_input, trans_output, batch_time) return total_time
def translate(output_handler: OutputHandler, dynamic_batch_mode_enabled: bool, trans_inputs: List[inference.TranslatorInput], translator: inference.Translator) -> float: """ Translates each line from source_data, calling output handler after translating a batch. :param output_handler: A handler that will be called once with the output of each translation. :param dynamic_batch_mode_enabled: Flag set to allow dynamic batches in translation, rather than fixed value :param trans_inputs: A enumerable list of translator inputs. :param translator: The translator that will be used for each line of input. :return: Total time taken. """ tic = time.time() if dynamic_batch_mode_enabled: trans_outputs = translator.translate_dynamic_batch_size( trans_inputs=trans_inputs) else: trans_outputs = translator.translate(trans_inputs) total_time = time.time() - tic batch_time = total_time / len(trans_inputs) for trans_input, trans_output in zip(trans_inputs, trans_outputs): output_handler.handle(trans_input, trans_output, batch_time) return total_time
def translate(output_handler: OutputHandler, trans_inputs: List[inference.TranslatorInput], translator: inference.Translator, num_translations: int = 1) -> float: """ Translates each line from source_data, calling output handler after translating a batch. :param output_handler: A handler that will be called once with the output of each translation. :param trans_inputs: A enumerable list of translator inputs. :param translator: The translator that will be used for each line of input. :return: Total time taken. """ tic = time.time() if num_translations > 1: trans_outputs_list = [] for i in range(num_translations): trans_outputs = translator.translate(trans_inputs) trans_outputs_list.append(trans_outputs) total_time = time.time() - tic batch_time = total_time / len(trans_inputs) cnt = 0 for trans_input in trans_inputs: for trans_outputs in trans_outputs_list: output_handler.handle(trans_input, trans_outputs[cnt], batch_time) output_handler.handle(trans_input, None, batch_time) cnt = cnt + 1 else: trans_outputs = translator.translate(trans_inputs) total_time = time.time() - tic batch_time = total_time / len(trans_inputs) for trans_input, trans_output in zip(trans_inputs, trans_outputs): output_handler.handle(trans_input, trans_output, batch_time) output_handler.handle(trans_input, None, batch_time) return total_time