


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       :





     def __init__(     self       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					Union[str, Any]     ,						):
        __lowerCamelCase:					Tuple	 =						parent
        __lowerCamelCase:					Any	 =						13
        __lowerCamelCase:					str	 =						7
        __lowerCamelCase:					Optional[Any]	 =						True
        __lowerCamelCase:					List[Any]	 =						True
        __lowerCamelCase:					Union[str, Any]	 =						True
        __lowerCamelCase:					List[str]	 =						True
        __lowerCamelCase:					List[str]	 =						True
        __lowerCamelCase:					Tuple	 =						99
        __lowerCamelCase:					Optional[Any]	 =						32
        __lowerCamelCase:					Tuple	 =						2
        __lowerCamelCase:					Optional[int]	 =						4
        __lowerCamelCase:					int	 =						37
        __lowerCamelCase:					Dict	 =						"""gelu"""
        __lowerCamelCase:					Optional[int]	 =						0.1
        __lowerCamelCase:					int	 =						0.1
        __lowerCamelCase:					List[Any]	 =						512
        __lowerCamelCase:					List[Any]	 =						16
        __lowerCamelCase:					Optional[int]	 =						2
        __lowerCamelCase:					List[str]	 =						0.02
        __lowerCamelCase:					Dict	 =						3
        __lowerCamelCase:					str	 =						4
        __lowerCamelCase:					Any	 =						None
        __lowerCamelCase:					int	 =						self.vocab_size - 1





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[str]				):
        __lowerCamelCase:					str	 =						ids_tensor([self.batch_size, self.seq_length]     ,						self.vocab_size				)

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

        __lowerCamelCase:					Union[str, Any]	 =						None
        if self.use_token_type_ids:
           __lowerCamelCase:					Tuple	 =						ids_tensor([self.batch_size, self.seq_length]     ,						self.type_vocab_size				)

        __lowerCamelCase:					Union[str, Any]	 =						None
        if self.use_mc_token_ids:
           __lowerCamelCase:					Optional[Any]	 =						ids_tensor([self.batch_size, self.num_choices]     ,						self.seq_length				)

        __lowerCamelCase:					Optional[Any]	 =						None
        __lowerCamelCase:					Optional[int]	 =						None
        __lowerCamelCase:					Tuple	 =						None
        if self.use_labels:
           __lowerCamelCase:					Optional[int]	 =						ids_tensor([self.batch_size]     ,						self.type_sequence_label_size				)
           __lowerCamelCase:					List[str]	 =						ids_tensor([self.batch_size, self.seq_length]     ,						self.num_labels				)
           __lowerCamelCase:					Dict	 =						ids_tensor([self.batch_size]     ,						self.num_choices				)

        __lowerCamelCase:					List[Any]	 =						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     ,						)

        __lowerCamelCase:					List[str]	 =						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 			SCREAMING_SNAKE_CASE__							(     self       :					Union[str, Any]     ,						SCREAMING_SNAKE_CASE_       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					int     ,						SCREAMING_SNAKE_CASE_       :					str     ,						SCREAMING_SNAKE_CASE_       :					Any     ,						*SCREAMING_SNAKE_CASE_       :					int				):
        __lowerCamelCase:					str	 =						TFOpenAIGPTModel(config=SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					List[Any]	 =						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
        __lowerCamelCase:					Any	 =						model(SCREAMING_SNAKE_CASE_				)

        __lowerCamelCase:					Dict	 =						[input_ids, input_mask]
        __lowerCamelCase:					List[Any]	 =						model(SCREAMING_SNAKE_CASE_				)

        __lowerCamelCase:					Optional[int]	 =						model(SCREAMING_SNAKE_CASE_				)

        self.parent.assertEqual(result.last_hidden_state.shape     ,						(self.batch_size, self.seq_length, self.hidden_size)				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					Any     ,						SCREAMING_SNAKE_CASE_       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					int     ,						SCREAMING_SNAKE_CASE_       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					Union[str, Any]     ,						*SCREAMING_SNAKE_CASE_       :					str				):
        __lowerCamelCase:					List[Any]	 =						TFOpenAIGPTLMHeadModel(config=SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					List[Any]	 =						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
        __lowerCamelCase:					List[str]	 =						model(SCREAMING_SNAKE_CASE_				)
        self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.seq_length, self.vocab_size)				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[Any]     ,						SCREAMING_SNAKE_CASE_       :					int     ,						SCREAMING_SNAKE_CASE_       :					str     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]     ,						SCREAMING_SNAKE_CASE_       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					Union[str, Any]     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]     ,						*SCREAMING_SNAKE_CASE_       :					Tuple				):
        __lowerCamelCase:					Union[str, Any]	 =						TFOpenAIGPTDoubleHeadsModel(config=SCREAMING_SNAKE_CASE_				)

        __lowerCamelCase:					int	 =						tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_     ,						1				)     ,						(1, self.num_choices, 1)				)
        __lowerCamelCase:					List[str]	 =						tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_     ,						1				)     ,						(1, self.num_choices, 1)				)
        __lowerCamelCase:					Optional[int]	 =						tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_     ,						1				)     ,						(1, self.num_choices, 1)				)

        __lowerCamelCase:					int	 =						{
            """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,
        }
        __lowerCamelCase:					str	 =						model(SCREAMING_SNAKE_CASE_				)
        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 			SCREAMING_SNAKE_CASE__							(     self       :					int     ,						SCREAMING_SNAKE_CASE_       :					List[Any]     ,						SCREAMING_SNAKE_CASE_       :					int     ,						SCREAMING_SNAKE_CASE_       :					List[str]     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					str     ,						*SCREAMING_SNAKE_CASE_       :					List[str]				):
        __lowerCamelCase:					Any	 =						self.num_labels
        __lowerCamelCase:					int	 =						ids_tensor([self.batch_size]     ,						self.type_sequence_label_size				)
        __lowerCamelCase:					Dict	 =						{
            """input_ids""": input_ids,
            """attention_mask""": input_mask,
            """token_type_ids""": token_type_ids,
            """labels""": sequence_labels,
        }
        __lowerCamelCase:					Optional[Any]	 =						TFOpenAIGPTForSequenceClassification(SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					Optional[int]	 =						model(SCREAMING_SNAKE_CASE_				)
        self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.num_labels)				)







     def 			SCREAMING_SNAKE_CASE__							(     self       :					Union[str, Any]				):
        __lowerCamelCase:					Union[str, Any]	 =						self.prepare_config_and_inputs()

        (
            (
            __lowerCamelCase
        ) ,  (
            __lowerCamelCase
        ) ,  (
            __lowerCamelCase
        ) ,  (
            __lowerCamelCase
        ) ,  (
            __lowerCamelCase
        ) ,  (
            __lowerCamelCase
        ) ,  (
            __lowerCamelCase
        ) ,  (
            __lowerCamelCase
        ) ,  (
            __lowerCamelCase
        ) ,  
        ):					str	 =						config_and_inputs

        __lowerCamelCase:					Union[str, Any]	 =						{"""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 ):
     UpperCAmelCase__			:	Any       =						(
         (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
         if is_tf_available()
         else ()
     )
     UpperCAmelCase__			:	Any       =						(
         (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     UpperCAmelCase__			:	List[Any]       =						(
         {
             "feature-extraction": TFOpenAIGPTModel,
             "text-classification": TFOpenAIGPTForSequenceClassification,
             "text-generation": TFOpenAIGPTLMHeadModel,
             "zero-shot": TFOpenAIGPTForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     UpperCAmelCase__			:	Dict       =						False
     UpperCAmelCase__			:	Union[str, Any]       =						False





     def 			SCREAMING_SNAKE_CASE__							(     self       :					str     ,						SCREAMING_SNAKE_CASE_       :					Tuple     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					str     ,						SCREAMING_SNAKE_CASE_       :					str				):
        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 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[Any]				):
        __lowerCamelCase:					Dict	 =						TFOpenAIGPTModelTester(self				)
        __lowerCamelCase:					str	 =						ConfigTester(self     ,						config_class=SCREAMING_SNAKE_CASE_     ,						n_embd=37				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[int]				):
        self.config_tester.run_common_tests()





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[Any]				):
        __lowerCamelCase:					Tuple	 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Dict				):
        __lowerCamelCase:					Union[str, Any]	 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_lm_head(*SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					str				):
        __lowerCamelCase:					Optional[Any]	 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_double_head(*SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Tuple				):
        __lowerCamelCase:					Dict	 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*SCREAMING_SNAKE_CASE_				)







     @slow
     def 			SCREAMING_SNAKE_CASE__							(     self       :					Union[str, Any]				):
        for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __lowerCamelCase:					List[Any]	 =						TFOpenAIGPTModel.from_pretrained(SCREAMING_SNAKE_CASE_				)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)



@require_tf
class 							a       (      unittest.TestCase ):





     @slow
     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[Any]				):
        __lowerCamelCase:					Optional[int]	 =						TFOpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""				)
        __lowerCamelCase:					Dict	 =						tf.convert_to_tensor([[481, 4735, 544]]     ,						dtype=tf.intaa				)  # the president is
        __lowerCamelCase:					Any	 =						[
            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

        __lowerCamelCase:					Optional[Any]	 =						model.generate(SCREAMING_SNAKE_CASE_     ,						do_sample=SCREAMING_SNAKE_CASE_				)
        self.assertListEqual(output_ids[0].numpy().tolist()     ,						SCREAMING_SNAKE_CASE_				)






from string import ascii_uppercase



def      __lowerCAmelCase    (				snake_case						:     str    ,   snake_case						:     str    ,   snake_case						:     bool = False    ,   snake_case						:     str = ascii_uppercase			)							->				str:
   __lowerCamelCase:					List[str]	 =						keyword.upper()
   __lowerCamelCase:					Tuple	 =						plaintext.upper()
   __lowerCamelCase:					Optional[Any]	 =						set(snake_case			)

   # create a list of unique characters in the keyword - their order matters
   # it determines how we will map plaintext characters to the ciphertext
   __lowerCamelCase:					Tuple	 =						[]
   for char in keyword:
      if char in alphabet_set and char not in unique_chars:
         unique_chars.append(snake_case			)
    # the number of those unique characters will determine the number of rows
   __lowerCamelCase:					Tuple	 =						len(snake_case			)

   # create a shifted version of the alphabet
   __lowerCamelCase:					List[str]	 =						unique_chars + [
       char for char in alphabet if char not in unique_chars
   ]

   # create a modified alphabet by splitting the shifted alphabet into rows
   __lowerCamelCase:					List[str]	 =						[
       shifted_alphabet[k : k + num_unique_chars_in_keyword]
       for k in range(0    ,   26    ,   snake_case			)
   ]

   # map the alphabet characters to the modified alphabet characters
   # going 'vertically' through the modified alphabet - consider columns first
   __lowerCamelCase:					Union[str, Any]	 =						{}
   __lowerCamelCase:					str	 =						0
   for column in range(snake_case			):
      for row in modified_alphabet:
         # if current row (the last one) is too short, break out of loop
         if len(snake_case			) <= column:
            break

         # map current letter to letter in modified alphabet
         __lowerCamelCase:					Union[str, Any]	 =						row[column]
         letter_index += 1

   if verbose:
      print(snake_case			)
   # create the encrypted text by mapping the plaintext to the modified alphabet
   return "".join(mapping[char] if char in mapping else char for char in plaintext			)


if __name__ == "__main__":
   # example use
   print(mixed_keyword('''college''', '''UNIVERSITY'''))


