code: def main(cfg):
      cwd = utils.get_original_cwd()
      # cwd = cwd[0:-5]
      cfg.cwd = cwd
      cfg.pos_size = 2 * cfg.pos_limit + 2
      print(cfg.pretty())
  
      # get predict instance
      instance = _get_predict_instance(cfg)
      data = [instance]
  
      # preprocess data
      data, rels = _preprocess_data(data, cfg)
  
      # model
      __Model__ = {
          "cnn": models.PCNN,
          "rnn": models.BiLSTM,
          "transformer": models.Transformer,
          "gcn": models.GCN,
          "capsule": models.Capsule,
          "lm": models.LM,
      }
  
      # 最好在 cpu 上预测
      cfg.use_gpu = False
      if cfg.use_gpu and torch.cuda.is_available():
          device = torch.device("cuda", cfg.gpu_id)
      else:
          device = torch.device("cpu")
      logger.info(f"device: {device}")
  
      model = __Model__[cfg.model_name](cfg)
      logger.info(f"model name: {cfg.model_name}")
      logger.info(f"\n {model}")
      model.load(cfg.fp, device=device)
      model.to(device)
      model.eval()
  
      x = dict()
      x["word"], x["lens"] = torch.tensor(
          [
              data[0]["token2idx"]
              + [0] * (512 - len(data[0]["token2idx"]))
          ]
      ), torch.tensor([data[0]["seq_len"]])
  
      if cfg.model_name != "lm":
          x["head_pos"], x["tail_pos"] = torch.tensor(
              data[0]["head_pos"]
              + [0] * (512 - len(data[0]["token2idx"]))
          ), torch.tensor(
              data[0]["tail_pos"]
              + [0] * (512 - len(data[0]["token2idx"]))
          )
          if cfg.model_name == "cnn":
              if cfg.use_pcnn:
                  x["pcnn_mask"] = torch.tensor(
                      [data[0]["entities_pos"]]
                  )
          if cfg.model_name == "gcn":
              # 没找到合适的做 parsing tree 的工具，暂时随机初始化
              adj = torch.empty(1, 512, 512).random_(2)
              x["adj"] = adj
  
      for key in x.keys():
          x[key] = x[key].to(device)
  
      with torch.no_grad():
          y_pred = model(x)
          y_pred = torch.softmax(y_pred, dim=-1)[0]
          prob = y_pred.max().item()
          prob_rel = list(rels.keys())[y_pred.argmax().item()]
          logger.info(
              f"\"{data[0]['head']}\" 和 \"{data[0]['tail']}\" 在句中关系为：\"{prob_rel}\"，置信度为{prob:.2f}。"
          )
  
      if cfg.predict_plot:
          # maplot 默认显示不支持中文
          plt.rcParams["font.family"] = "Arial Unicode MS"
          x = list(rels.keys())
          height = list(y_pred.cpu().numpy())
          plt.bar(x, height)
          for x, y in zip(x, height):
              plt.text(x, y, "%.2f" % y, ha="center", va="bottom")
          plt.xlabel("关系")
          plt.ylabel("置信度")
          plt.xticks(rotation=315)
          plt.show()
  
file_path: DeepKE/example/re/standard/predict.py
imports: import os
  import sys
  import torch
  import logging
  import hydra
  from hydra import utils
  from deepke.relation_extraction.standard.tools import Serializer
  from deepke.relation_extraction.standard.tools import _serialize_sentence, _convert_tokens_into_index, _add_pos_seq, _handle_relation_data, _lm_serialize
  import matplotlib.pyplot
  from deepke.relation_extraction.standard.utils import load_pkl, load_csv
  import deepke.relation_extraction.standard.models
label_desc: Use image node_id 3 for calling a global function with example usage: main(cfg) without return types