text: import random
  import unittest
  
  import numpy as np
  
  from bert2tf import Executor, GPT, BertTokenizer, GPTAutoRegressive, GPTChineseAutoRegressive
  from tests import Bert2TFTestCase
  
  
  class MyTestCase(Bert2TFTestCase):
      @unittest.skip('just run on local machine')
      def test_create_gpt_model(self):
          model = Executor.load_config('GPT', use_with={
              'config': '../../resources/gpt/model_config.json'})
          self.assertEqual(isinstance(model, GPT), True)
  
          model = GPT(config='../../resources/gpt/model_config.json')
          self.assertEqual(isinstance(model, GPT), True)
  
      @unittest.skip('just run on local machine')
      def test_gpt_encode(self):
          model = GPT(config='../../resources/gpt/model_config.json')
          outputs = model(np.array([random.sample(range(21127), 8)]))
          self.assertEqual(outputs.numpy().shape[0], 1)
          self.assertEqual(outputs.numpy().shape[1], 8)
          self.assertEqual(outputs.numpy().shape[2], 768)
  
      @unittest.skip('just run on local machine')
      def test_huawei_gpt_generate(self):
          tokenizer = BertTokenizer('../../resources/pre_models/huawei-gpt/vocab.txt')
          model = GPTAutoRegressive(config='../../resources/pre_models/huawei-gpt/cn_gpt.json',
                                    pretrained_weights_path='../../resources/pre_models/huawei-gpt/cn_gpt',
                                    end_id=511,
                                    min_len=16,
                                    top_k=4,
                                    num_samples=1,
                                    name='bert')
  
          input_ids = tokenizer.encode('本拉登')[0][:-1]
  
          all_ids = model.beam_search([np.array([input_ids])])
          text = ''.join(tokenizer.convert_ids_to_tokens(list(all_ids)))
          print(text)
  
          all_ids = model.random_sample([np.array([input_ids])])[0]
          text = ''.join(tokenizer.convert_ids_to_tokens(list(all_ids)))
          print(text)
  
      @unittest.skip('just run on local machine')
      def test_gpt_chinese_generate(self):
          tokenizer = BertTokenizer('../../resources/pre_models/GPT_LCCC-base-tf/vocab.txt')
  
          model = GPTChineseAutoRegressive(config='../../resources/pre_models/GPT_LCCC-base-tf/gpt_config.json',
                                           pretrained_weights_path='../../resources/pre_models/GPT_LCCC-base-tf/gpt_model.ckpt',
                                           end_id=2,
                                           top_k=4,
                                           min_len=4)
  
          speakers = [
              tokenizer.vocabs['[speaker1]'],
              tokenizer.vocabs['[speaker2]']
          ]
  
          token_ids = [tokenizer.cls_token_id, speakers[0]]
          segment_ids = [tokenizer.cls_token_id, speakers[0]]
  
          texts = [u'这边是直接删除答疑']
  
          for idx, text in enumerate(texts):
              ids = tokenizer.encode(text)[0][1:-1] + [speakers[(idx + 1) % 2]]
              token_ids.extend(ids)
              segment_ids.extend([speakers[idx % 2]] * len(ids))
              segment_ids[-1] = speakers[(idx + 1) % 2]
  
          results = model.random_sample([np.array([token_ids]), np.array([segment_ids])])[0]
          print(''.join(tokenizer.convert_ids_to_tokens(list(results))))
  
          token_ids = [tokenizer.cls_token_id, speakers[0]]
          segment_ids = [tokenizer.cls_token_id, speakers[0]]
  
          texts = [u'别爱我没结果', u'你这样会失去我的', u'失去了又能怎样']
  
          for idx, text in enumerate(texts):
              ids = tokenizer.encode(text)[0][1:-1] + [speakers[(idx + 1) % 2]]
              token_ids.extend(ids)
              segment_ids.extend([speakers[idx % 2]] * len(ids))
              segment_ids[-1] = speakers[(idx + 1) % 2]
  
          results = model.beam_search([np.array([token_ids]), np.array([segment_ids])])
          text = ''.join(tokenizer.convert_ids_to_tokens(list(results)))
          print(text)
  