code: def predict(self, text: str):
      if self.cfg.model_name == "lstmcrf":
          words = [[self.word2id.get(w, 0) for w in text]]
          mask = [[1] * len(words[0])]
          y_pred = self.model(
              torch.tensor(words).to(self.device),
              torch.tensor(mask).bool().to(self.device),
          )
  
          """Just adjust the format of output"""
          words = [w for w in text]
          labels = [(self.id2label[id], None) for id in y_pred[0]]
      elif self.cfg.model_name == "bert":
          (
              input_ids,
              input_mask,
              segment_ids,
              valid_ids,
          ) = self.preprocess(text)
          input_ids = torch.tensor(
              [input_ids], dtype=torch.long, device=self.device
          )
          input_mask = torch.tensor(
              [input_mask], dtype=torch.long, device=self.device
          )
          segment_ids = torch.tensor(
              [segment_ids], dtype=torch.long, device=self.device
          )
          valid_ids = torch.tensor(
              [valid_ids], dtype=torch.long, device=self.device
          )
          with torch.no_grad():
              logits = self.model(
                  input_ids=input_ids,
                  token_type_ids=segment_ids,
                  attention_mask=input_mask,
                  valid_ids=valid_ids,
              )
          logits = F.softmax(logits, dim=2)
          logits_label = torch.argmax(logits, dim=2)
          logits_label = logits_label.detach().cpu().numpy().tolist()[0]
  
          logits_confidence = [
              values[label].item()
              for values, label in zip(logits[0], logits_label)
          ]
  
          logits = []
          pos = 0
          for index, mask in enumerate(valid_ids[0]):
              if index == 0:
                  continue
              if mask == 1:
                  logits.append(
                      (
                          logits_label[index - pos],
                          logits_confidence[index - pos],
                      )
                  )
              else:
                  pos += 1
          logits.pop()
  
          labels = [
              (self.label_map[label], confidence)
              for label, confidence in logits
          ]
          if self.cfg.lan == "en":
              nltk.download("punkt")
              words = word_tokenize(text)
          else:
              words = list(text)
          assert len(labels) == len(words)
      else:
          raise NotImplementedError(
              f"model type {self.cfg.model_name} not supported"
          )
  
      result = []
      for word, (label, confidence) in zip(words, labels):
          if label != "O":
              result.append((word, label))
      # tmp = []
      # tag = OrderedDict()
      # tag['PER'] = []
      # tag['LOC'] = []
      # tag['ORG'] = []
  
      # for i, (word, label) in enumerate(result):
      #     if label=='B-PER' or label=='B-LOC' or label=='B-ORG':
      #         if i==0:
      #             tmp.append(word)
      #         else:
      #             wordstype = result[i-1][1][2:]
      #             tag[wordstype].append(''.join(tmp))
      #             tmp.clear()
      #             tmp.append(word)
      #     else:
      #         tmp.append(word)
  
      #     if i==len(result)-1:
      #         if label=='B-PER' or label=='B-LOC' or label=='B-ORG':
      #             tmp.append(word)
      #         wordstype = result[i][1][2:]
      #         tag[wordstype].append(''.join(tmp))
  
      return result
  
file_path: DeepKE/src/deepke/name_entity_re/standard/models/InferBert.py
imports: import json
  import os
  import torch
  import torch.nn.functional
  from transformers import BertConfig, BertForTokenClassification, BertTokenizer
  from collections import OrderedDict
  from .BiLSTM_CRF import *
  import hydra
  from hydra import utils
  import nltk
  from nltk import word_tokenize
label_desc: Use image node_id 5 for calling the InferNer obj's underlying member method code with example usage: obj.predict(text) and returns: result