code: def build_translator(
      opt, device_id=0, report_score=True, logger=None, out_file=None
  ):
      if out_file is None:
          out_file = codecs.open(opt.output, "w+", "utf-8")
  
      load_test_model = (
          onmt.decoders.ensemble.load_test_model
          if len(opt.models) > 1
          else onmt.model_builder.load_test_model
      )
  
      vocabs, model, model_opt = load_test_model(opt, device_id)
  
      scorer = onmt.translate.GNMTGlobalScorer.from_opt(opt)
  
      if model_opt.model_task == ModelTask.LANGUAGE_MODEL:
          translator = GeneratorLM.from_opt(
              model,
              vocabs,
              opt,
              model_opt,
              global_scorer=scorer,
              out_file=out_file,
              report_align=opt.report_align,
              report_score=report_score,
              logger=logger,
          )
      else:
          translator = Translator.from_opt(
              model,
              vocabs,
              opt,
              model_opt,
              global_scorer=scorer,
              out_file=out_file,
              report_align=opt.report_align,
              report_score=report_score,
              logger=logger,
          )
      return translator
  
imports: import torch
  from torch.nn.functional import log_softmax
  from torch.nn.utils.rnn import pad_sequence
  import codecs
  from time import time
  from math import exp
  from itertools import count, zip_longest
  from copy import deepcopy
  import onmt.model_builder
  import onmt.decoders.ensemble
  from onmt.constants import DefaultTokens
  from onmt.translate.beam_search import BeamSearch, BeamSearchLM
  from onmt.translate.greedy_search import GreedySearch, GreedySearchLM
  from onmt.utils.misc import tile, set_random_seed, report_matrix
  from onmt.utils.alignment import extract_alignment, build_align_pharaoh
  from onmt.modules.copy_generator import collapse_copy_scores
  from onmt.constants import ModelTask
  from onmt.transforms import TransformPipe
label_desc: Use image node_id 1 for calling a global function with example usage: build_translator(opt, device_id, report_score, logger, out_file) and returns: translator