code: def evaluate(opt, inference_mode, input_file, out, method):
      print("# input file", input_file)
      run_results = {}
      # Build the translator (along with the model)
      if inference_mode == "py":
          print("Inference with py ...")
          from onmt.inference_engine import InferenceEnginePY
  
          engine = InferenceEnginePY(opt)
      elif inference_mode == "ct2":
          print("Inference with ct2 ...")
          from onmt.inference_engine import InferenceEngineCT2
  
          opt.src_subword_vocab = opt.models[0] + "/vocabulary.json"
          engine = InferenceEngineCT2(opt)
      start = time.time()
      if method == "file":
          engine.opt.src = input_file
          scores, preds = engine.infer_file()
      elif method == "list":
          src = open(input_file, ("r")).readlines()
          scores, preds = engine.infer_list(src)
      engine.terminate()
      dur = time.time() - start
      print(f"Time to generate {len(preds)} answers: {dur}s")
      if inference_mode == "py":
          scores = [
              [_score.cpu().numpy().tolist() for _score in _scores]
              for _scores in scores
          ]
      run_results = {
          "pred_answers": preds,
          "score": scores,
          "duration": dur,
      }
      output_filename = out + f"_{method}.json"
      with open(output_filename, "w") as f:
          json.dump(run_results, f, ensure_ascii=False, indent=2)
  
file_path: OpenNMT-py/onmt/tests/test_inference_engines.py
imports: import json
  import time
  import onmt.opts
  from onmt.utils.parse import ArgumentParser
  from onmt.utils.misc import use_gpu, set_random_seed
label_desc: Use image node_id 2 for calling a global function with example usage: evaluate(opt, inference_mode, input_file, out, method) without return types