

from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers.models.openai.modeling_tf_openai import (
     TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
     TFOpenAIGPTDoubleHeadsModel,
     TFOpenAIGPTForSequenceClassification,
     TFOpenAIGPTLMHeadModel,
     TFOpenAIGPTModel,
 )



class 		a_	:



      """simple docstring"""







      def __init__(				self					:							Any      ,snake_case					:							Dict      ,):
           SCREAMING_SNAKE_CASE										=parent
           SCREAMING_SNAKE_CASE										=13
           SCREAMING_SNAKE_CASE										=7
           SCREAMING_SNAKE_CASE										=True
           SCREAMING_SNAKE_CASE										=True
           SCREAMING_SNAKE_CASE										=True
           SCREAMING_SNAKE_CASE										=True
           SCREAMING_SNAKE_CASE										=True
           SCREAMING_SNAKE_CASE										=99
           SCREAMING_SNAKE_CASE										=32
           SCREAMING_SNAKE_CASE										=2
           SCREAMING_SNAKE_CASE										=4
           SCREAMING_SNAKE_CASE										=37
           SCREAMING_SNAKE_CASE										='gelu'
           SCREAMING_SNAKE_CASE										=0.1
           SCREAMING_SNAKE_CASE										=0.1
           SCREAMING_SNAKE_CASE										=512
           SCREAMING_SNAKE_CASE										=16
           SCREAMING_SNAKE_CASE										=2
           SCREAMING_SNAKE_CASE										=0.02
           SCREAMING_SNAKE_CASE										=3
           SCREAMING_SNAKE_CASE										=4
           SCREAMING_SNAKE_CASE										=None
           SCREAMING_SNAKE_CASE										=self.vocab_size - 1
      def 	_lowerCAmelCase					(				self					:							Dict    ):
           SCREAMING_SNAKE_CASE										=ids_tensor([self.batch_size, self.seq_length]      ,self.vocab_size    )

           SCREAMING_SNAKE_CASE										=None
           if self.use_input_mask:
                SCREAMING_SNAKE_CASE										=random_attention_mask([self.batch_size, self.seq_length]    )

           SCREAMING_SNAKE_CASE										=None
           if self.use_token_type_ids:
                SCREAMING_SNAKE_CASE										=ids_tensor([self.batch_size, self.seq_length]      ,self.type_vocab_size    )

           SCREAMING_SNAKE_CASE										=None
           if self.use_mc_token_ids:
                SCREAMING_SNAKE_CASE										=ids_tensor([self.batch_size, self.num_choices]      ,self.seq_length    )

           SCREAMING_SNAKE_CASE										=None
           SCREAMING_SNAKE_CASE										=None
           SCREAMING_SNAKE_CASE										=None
           if self.use_labels:
                SCREAMING_SNAKE_CASE										=ids_tensor([self.batch_size]      ,self.type_sequence_label_size    )
                SCREAMING_SNAKE_CASE										=ids_tensor([self.batch_size, self.seq_length]      ,self.num_labels    )
                SCREAMING_SNAKE_CASE										=ids_tensor([self.batch_size]      ,self.num_choices    )

           SCREAMING_SNAKE_CASE										=OpenAIGPTConfig(
               vocab_size=self.vocab_size      ,n_embd=self.hidden_size      ,n_layer=self.num_hidden_layers      ,n_head=self.num_attention_heads      ,n_positions=self.max_position_embeddings      ,pad_token_id=self.pad_token_id      ,)

           SCREAMING_SNAKE_CASE										=ids_tensor([self.num_hidden_layers, self.num_attention_heads]      ,2    )

           return (
               config,
               input_ids,
               input_mask,
               head_mask,
               token_type_ids,
               mc_token_ids,
               sequence_labels,
               token_labels,
               choice_labels,
           )
      def 	_lowerCAmelCase					(				self					:							Tuple      ,snake_case					:							int      ,snake_case					:							Dict      ,snake_case					:							Dict      ,snake_case					:							List[Any]      ,snake_case					:							str      ,*snake_case					:							int    ):
           SCREAMING_SNAKE_CASE										=TFOpenAIGPTModel(config=lowercase_    )
           SCREAMING_SNAKE_CASE										={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           SCREAMING_SNAKE_CASE										=model(lowercase_    )

           SCREAMING_SNAKE_CASE										=[input_ids, input_mask]
           SCREAMING_SNAKE_CASE										=model(lowercase_    )

           SCREAMING_SNAKE_CASE										=model(lowercase_    )

           self.parent.assertEqual(result.last_hidden_state.shape      ,(self.batch_size, self.seq_length, self.hidden_size)    )
      def 	_lowerCAmelCase					(				self					:							int      ,snake_case					:							Optional[Any]      ,snake_case					:							Dict      ,snake_case					:							List[Any]      ,snake_case					:							Tuple      ,snake_case					:							Union[str, Any]      ,*snake_case					:							List[str]    ):
           SCREAMING_SNAKE_CASE										=TFOpenAIGPTLMHeadModel(config=lowercase_    )
           SCREAMING_SNAKE_CASE										={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           SCREAMING_SNAKE_CASE										=model(lowercase_    )
           self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.seq_length, self.vocab_size)    )
      def 	_lowerCAmelCase					(				self					:							Any      ,snake_case					:							int      ,snake_case					:							List[str]      ,snake_case					:							Optional[Any]      ,snake_case					:							Dict      ,snake_case					:							int      ,snake_case					:							List[Any]      ,*snake_case					:							List[str]    ):
           SCREAMING_SNAKE_CASE										=TFOpenAIGPTDoubleHeadsModel(config=lowercase_    )

           SCREAMING_SNAKE_CASE										=tf.tile(tf.expand_dims(lowercase_      ,1    )      ,(1, self.num_choices, 1)    )
           SCREAMING_SNAKE_CASE										=tf.tile(tf.expand_dims(lowercase_      ,1    )      ,(1, self.num_choices, 1)    )
           SCREAMING_SNAKE_CASE										=tf.tile(tf.expand_dims(lowercase_      ,1    )      ,(1, self.num_choices, 1)    )

           SCREAMING_SNAKE_CASE										={
               'input_ids': multiple_choice_inputs_ids,
               'mc_token_ids': mc_token_ids,
               'attention_mask': multiple_choice_input_mask,
               'token_type_ids': multiple_choice_token_type_ids,
           }
           SCREAMING_SNAKE_CASE										=model(lowercase_    )
           self.parent.assertEqual(
               result.logits.shape      ,(self.batch_size, self.num_choices, self.seq_length, self.vocab_size)    )
           self.parent.assertEqual(result.mc_logits.shape      ,(self.batch_size, self.num_choices)    )
      def 	_lowerCAmelCase					(				self					:							Union[str, Any]      ,snake_case					:							Any      ,snake_case					:							Union[str, Any]      ,snake_case					:							Optional[int]      ,snake_case					:							int      ,snake_case					:							List[Any]      ,*snake_case					:							List[Any]    ):
           SCREAMING_SNAKE_CASE										=self.num_labels
           SCREAMING_SNAKE_CASE										=ids_tensor([self.batch_size]      ,self.type_sequence_label_size    )
           SCREAMING_SNAKE_CASE										={
               'input_ids': input_ids,
               'attention_mask': input_mask,
               'token_type_ids': token_type_ids,
               'labels': sequence_labels,
           }
           SCREAMING_SNAKE_CASE										=TFOpenAIGPTForSequenceClassification(lowercase_    )
           SCREAMING_SNAKE_CASE										=model(lowercase_    )
           self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.num_labels)    )


      def 	_lowerCAmelCase					(				self					:							List[str]    ):
           SCREAMING_SNAKE_CASE										=self.prepare_config_and_inputs()

           (
               SCREAMING_SNAKE_CASE					
           )										=config_and_inputs

           SCREAMING_SNAKE_CASE										={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
           return config, inputs_dict







@require_tf
class 		a_	(							UpperCamelCase_			,      UpperCamelCase_			,      unittest.TestCase					):



      """simple docstring"""







      __UpperCAmelCase      		=				(
          (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
          if is_tf_available()
          else ()
      )
      __UpperCAmelCase      		=				(
          (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
      )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
      __UpperCAmelCase      		=				(
          {
              'feature-extraction': TFOpenAIGPTModel,
              'text-classification': TFOpenAIGPTForSequenceClassification,
              'text-generation': TFOpenAIGPTLMHeadModel,
              'zero-shot': TFOpenAIGPTForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      __UpperCAmelCase      		=				False
      __UpperCAmelCase      		=				False
      def 	_lowerCAmelCase					(				self					:							Optional[Any]      ,snake_case					:							Union[str, Any]      ,snake_case					:							Optional[int]      ,snake_case					:							List[str]      ,snake_case					:							List[Any]      ,snake_case					:							Any    ):




           if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
                # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
                # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
                # tiny config could not be created.
                return True

           return False
      def 	_lowerCAmelCase					(				self					:							Optional[Any]    ):
           SCREAMING_SNAKE_CASE										=TFOpenAIGPTModelTester(self    )
           SCREAMING_SNAKE_CASE										=ConfigTester(self      ,config_class=lowercase_      ,n_embd=37    )
      def 	_lowerCAmelCase					(				self					:							Any    ):
           self.config_tester.run_common_tests()
      def 	_lowerCAmelCase					(				self					:							str    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_model(*lowercase_    )
      def 	_lowerCAmelCase					(				self					:							str    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_lm_head(*lowercase_    )
      def 	_lowerCAmelCase					(				self					:							Optional[Any]    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_double_head(*lowercase_    )
      def 	_lowerCAmelCase					(				self					:							Optional[Any]    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowercase_    )


      @slow
      def 	_lowerCAmelCase					(				self					:							List[str]    ):




           for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                SCREAMING_SNAKE_CASE										=TFOpenAIGPTModel.from_pretrained(lowercase_    )
                self.assertIsNotNone(lowercase_    )







@require_tf
class 		a_	(							unittest.TestCase					):



      """simple docstring"""







      @slow
      def 	_lowerCAmelCase					(				self					:							Optional[int]    ):
           SCREAMING_SNAKE_CASE										=TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'    )
           SCREAMING_SNAKE_CASE										=tf.convert_to_tensor([[481, 4735, 544]]      ,dtype=tf.intaa    )  # the president is
           SCREAMING_SNAKE_CASE										=[
               481,
               4735,
               544,
               246,
               963,
               870,
               762,
               239,
               244,
               40477,
               244,
               249,
               719,
               881,
               487,
               544,
               240,
               244,
               603,
               481,
           ]  # the president is a very good man. " \n " i\'m sure he is, " said the

           SCREAMING_SNAKE_CASE										=model.generate(lowercase_      ,do_sample=lowercase_    )
           self.assertListEqual(output_ids[0].numpy().tolist()      ,lowercase_    )




import os
import sys
import time

from . import transposition_cipher as trans_cipher







def       snake_case__    (       ):





     """simple docstring"""







     SCREAMING_SNAKE_CASE										='Prehistoric Men.txt'
     SCREAMING_SNAKE_CASE										='Output.txt'
     SCREAMING_SNAKE_CASE										=int(input('Enter key: '      )      )
     SCREAMING_SNAKE_CASE										=input('Encrypt/Decrypt [e/d]: '      )

     if not os.path.exists(lowerCAmelCase_      ):
          print(F'File {input_file} does not exist. Quitting...'      )
          sys.exit()
     if os.path.exists(lowerCAmelCase_      ):
          print(F'Overwrite {output_file}? [y/n]'      )
          SCREAMING_SNAKE_CASE										=input('> '      )
          if not response.lower().startswith('y'      ):
               sys.exit()

     SCREAMING_SNAKE_CASE										=time.time()
     if mode.lower().startswith('e'      ):
          with open(lowerCAmelCase_      ) as f:
               SCREAMING_SNAKE_CASE										=f.read()
          SCREAMING_SNAKE_CASE										=trans_cipher.encrypt_message(lowerCAmelCase_,   lowerCAmelCase_      )
     elif mode.lower().startswith('d'      ):
          with open(lowerCAmelCase_      ) as f:
               SCREAMING_SNAKE_CASE										=f.read()
          SCREAMING_SNAKE_CASE										=trans_cipher.decrypt_message(lowerCAmelCase_,   lowerCAmelCase_      )

     with open(lowerCAmelCase_,   'w'      ) as output_obj:
          output_obj.write(lowerCAmelCase_      )

     SCREAMING_SNAKE_CASE										=round(time.time() - start_time,   2      )
     print(('Done (', total_time, 'seconds )')      )


if __name__ == "__main__":
 main()

