

'''simple docstring'''




from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, 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 import (
          TFConvBertForMaskedLM,
          TFConvBertForMultipleChoice,
          TFConvBertForQuestionAnswering,
          TFConvBertForSequenceClassification,
          TFConvBertForTokenClassification,
          TFConvBertModel,
      )






class     _SCREAMING_SNAKE_CASE       :



 def __init__(       self						:       Tuple					, a__						:       Any					, a__						:       Optional[int]=13					, a__						:       Optional[int]=7					, a__						:       int=True					, a__						:       List[str]=True					, a__						:       Union[str, Any]=True					, a__						:       str=True					, a__						:       str=99					, a__						:       Dict=32					, a__						:       List[Any]=2					, a__						:       Any=4					, a__						:       str=37					, a__						:       Tuple="gelu"					, a__						:       List[str]=0.1					, a__						:       Union[str, Any]=0.1					, a__						:       List[str]=512					, a__						:       Any=16					, a__						:       Union[str, Any]=2					, a__						:       Optional[Any]=0.02					, a__						:       Any=3					, a__						:       Union[str, Any]=4					, a__						:       Tuple=None					, ):
      __magic_name__		  =							parent
      __magic_name__		  =							13
      __magic_name__		  =							7
      __magic_name__		  =							True
      __magic_name__		  =							True
      __magic_name__		  =							True
      __magic_name__		  =							True
      __magic_name__		  =							99
      __magic_name__		  =							384
      __magic_name__		  =							2
      __magic_name__		  =							4
      __magic_name__		  =							37
      __magic_name__		  =							'gelu'
      __magic_name__		  =							0.1
      __magic_name__		  =							0.1
      __magic_name__		  =							512
      __magic_name__		  =							16
      __magic_name__		  =							2
      __magic_name__		  =							0.02
      __magic_name__		  =							3
      __magic_name__		  =							4
      __magic_name__		  =							128
      __magic_name__		  =							2
      __magic_name__		  =							9
      __magic_name__		  =							1
      __magic_name__		  =							None



 def 					snake_case__    (       self						:       Tuple     ):
      __magic_name__		  =							ids_tensor([self.batch_size, self.seq_length]					, self.vocab_size     )

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

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

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

      __magic_name__		  =							ConvBertConfig(
          vocab_size=self.vocab_size					, hidden_size=self.hidden_size					, num_hidden_layers=self.num_hidden_layers					, num_attention_heads=self.num_attention_heads					, intermediate_size=self.intermediate_size					, hidden_act=self.hidden_act					, hidden_dropout_prob=self.hidden_dropout_prob					, attention_probs_dropout_prob=self.attention_probs_dropout_prob					, max_position_embeddings=self.max_position_embeddings					, type_vocab_size=self.type_vocab_size					, initializer_range=self.initializer_range					, return_dict=a__					, )

      return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



 def 					snake_case__    (       self						:       Dict					, a__						:       Dict					, a__						:       Tuple					, a__						:       Union[str, Any]					, a__						:       Optional[Any]					, a__						:       str					, a__						:       str					, a__						:       Optional[int]     ):
      __magic_name__		  =							TFConvBertModel(config=a__     )
      __magic_name__		  =							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

      __magic_name__		  =							[input_ids, input_mask]
      __magic_name__		  =							model(a__     )

      __magic_name__		  =							model(a__     )

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



 def 					snake_case__    (       self						:       Union[str, Any]					, a__						:       Optional[Any]					, a__						:       List[Any]					, a__						:       Tuple					, a__						:       int					, a__						:       List[Any]					, a__						:       List[str]					, a__						:       Optional[int]     ):
      __magic_name__		  =							TFConvBertForMaskedLM(config=a__     )
      __magic_name__		  =							{
          'input_ids': input_ids,
          'attention_mask': input_mask,
          'token_type_ids': token_type_ids,
      }
      __magic_name__		  =							model(a__     )
      self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.seq_length, self.vocab_size)     )



 def 					snake_case__    (       self						:       Tuple					, a__						:       List[Any]					, a__						:       Any					, a__						:       Union[str, Any]					, a__						:       str					, a__						:       Optional[int]					, a__						:       List[str]					, a__						:       List[str]     ):
      __magic_name__		  =							self.num_labels
      __magic_name__		  =							TFConvBertForSequenceClassification(config=a__     )
      __magic_name__		  =							{
          'input_ids': input_ids,
          'attention_mask': input_mask,
          'token_type_ids': token_type_ids,
      }

      __magic_name__		  =							model(a__     )
      self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.num_labels)     )



 def 					snake_case__    (       self						:       Union[str, Any]					, a__						:       List[str]					, a__						:       Tuple					, a__						:       str					, a__						:       int					, a__						:       str					, a__						:       List[str]					, a__						:       List[Any]     ):
      __magic_name__		  =							self.num_choices
      __magic_name__		  =							TFConvBertForMultipleChoice(config=a__     )
      __magic_name__		  =							tf.tile(tf.expand_dims(a__					, 1     )					, (1, self.num_choices, 1)     )
      __magic_name__		  =							tf.tile(tf.expand_dims(a__					, 1     )					, (1, self.num_choices, 1)     )
      __magic_name__		  =							tf.tile(tf.expand_dims(a__					, 1     )					, (1, self.num_choices, 1)     )
      __magic_name__		  =							{
          'input_ids': multiple_choice_inputs_ids,
          'attention_mask': multiple_choice_input_mask,
          'token_type_ids': multiple_choice_token_type_ids,
      }
      __magic_name__		  =							model(a__     )
      self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.num_choices)     )



 def 					snake_case__    (       self						:       str					, a__						:       Optional[Any]					, a__						:       Tuple					, a__						:       int					, a__						:       str					, a__						:       int					, a__						:       Optional[Any]					, a__						:       Union[str, Any]     ):
      __magic_name__		  =							self.num_labels
      __magic_name__		  =							TFConvBertForTokenClassification(config=a__     )
      __magic_name__		  =							{
          'input_ids': input_ids,
          'attention_mask': input_mask,
          'token_type_ids': token_type_ids,
      }
      __magic_name__		  =							model(a__     )
      self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.seq_length, self.num_labels)     )



 def 					snake_case__    (       self						:       Any					, a__						:       Any					, a__						:       Any					, a__						:       Tuple					, a__						:       List[Any]					, a__						:       List[Any]					, a__						:       Tuple					, a__						:       Optional[int]     ):
      __magic_name__		  =							TFConvBertForQuestionAnswering(config=a__     )
      __magic_name__		  =							{
          'input_ids': input_ids,
          'attention_mask': input_mask,
          'token_type_ids': token_type_ids,
      }

      __magic_name__		  =							model(a__     )
      self.parent.assertEqual(result.start_logits.shape					, (self.batch_size, self.seq_length)     )
      self.parent.assertEqual(result.end_logits.shape					, (self.batch_size, self.seq_length)     )



 def 					snake_case__    (       self						:       Union[str, Any]     ):
      __magic_name__		  =							self.prepare_config_and_inputs()
      (
          __magic_name__  
      )		  =							config_and_inputs
      __magic_name__		  =							{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict






@require_tf
class     _SCREAMING_SNAKE_CASE       (       SCREAMING_SNAKE_CASE_						,SCREAMING_SNAKE_CASE_						,unittest.TestCase      ):
 __SCREAMING_SNAKE_CASE	:Tuple     			=	(
     (
         TFConvBertModel,
         TFConvBertForMaskedLM,
         TFConvBertForQuestionAnswering,
         TFConvBertForSequenceClassification,
         TFConvBertForTokenClassification,
         TFConvBertForMultipleChoice,
     )
     if is_tf_available()
     else ()
 )
 __SCREAMING_SNAKE_CASE	:List[Any]     			=	(
     {
         'feature-extraction': TFConvBertModel,
         'fill-mask': TFConvBertForMaskedLM,
         'question-answering': TFConvBertForQuestionAnswering,
         'text-classification': TFConvBertForSequenceClassification,
         'token-classification': TFConvBertForTokenClassification,
         'zero-shot': TFConvBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 __SCREAMING_SNAKE_CASE	:str     			=	False
 __SCREAMING_SNAKE_CASE	:Dict     			=	False
 __SCREAMING_SNAKE_CASE	:str     			=	False



 def 					snake_case__    (       self						:       Dict     ):
      __magic_name__		  =							TFConvBertModelTester(self     )
      __magic_name__		  =							ConfigTester(self					, config_class=a__					, hidden_size=37     )



 def 					snake_case__    (       self						:       List[str]     ):
      self.config_tester.run_common_tests()



 def 					snake_case__    (       self						:       str     ):
      __magic_name__		  =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*a__     )



 def 					snake_case__    (       self						:       str     ):
      __magic_name__		  =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*a__     )



 def 					snake_case__    (       self						:       List[Any]     ):
      __magic_name__		  =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*a__     )



 def 					snake_case__    (       self						:       Tuple     ):
      __magic_name__		  =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*a__     )



 def 					snake_case__    (       self						:       Union[str, Any]     ):
      __magic_name__		  =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*a__     )



 def 					snake_case__    (       self						:       Tuple     ):
      __magic_name__		  =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*a__     )



 @slow
 def 					snake_case__    (       self						:       Optional[Any]     ):
      __magic_name__		  =							self.model_tester.prepare_config_and_inputs_for_common()
      __magic_name__		  =							True
      __magic_name__		  =							True

      if hasattr(a__					, '''use_cache'''     ):
           __magic_name__		  =							True

      __magic_name__		  =							getattr(self.model_tester					, '''encoder_seq_length'''					, self.model_tester.seq_length     )
      __magic_name__		  =							getattr(self.model_tester					, '''key_length'''					, a__     )

      for model_class in self.all_model_classes:
           __magic_name__		  =							self._prepare_for_class(a__					, a__     )
           __magic_name__		  =							model_class(a__     )
           __magic_name__		  =							len(model(a__     )     )

           with tempfile.TemporaryDirectory() as tmpdirname:
                model.save_pretrained(a__					, saved_model=a__     )
                __magic_name__		  =							os.path.join(a__					, '''saved_model'''					, '''1'''     )
                __magic_name__		  =							tf.keras.models.load_model(a__     )
                __magic_name__		  =							model(a__     )

                if self.is_encoder_decoder:
                     __magic_name__		  =							outputs['encoder_hidden_states']
                     __magic_name__		  =							outputs['encoder_attentions']
                else:
                     __magic_name__		  =							outputs['hidden_states']
                     __magic_name__		  =							outputs['attentions']

                self.assertEqual(len(a__     )					, a__     )

                __magic_name__		  =							getattr(
                    self.model_tester					, '''expected_num_hidden_layers'''					, self.model_tester.num_hidden_layers + 1     )

                self.assertEqual(len(a__     )					, a__     )
                self.assertListEqual(
                    list(output_hidden_states[0].shape[-2:]     )					, [self.model_tester.seq_length, self.model_tester.hidden_size]					, )

                self.assertEqual(len(a__     )					, self.model_tester.num_hidden_layers     )
                self.assertListEqual(
                    list(output_attentions[0].shape[-3:]     )					, [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]					, )



 @slow
 def 					snake_case__    (       self						:       Tuple     ):
      __magic_name__		  =							TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''     )
      self.assertIsNotNone(a__     )



 def 					snake_case__    (       self						:       Optional[Any]     ):
      __magic_name__		  =							self.model_tester.prepare_config_and_inputs_for_common()
      __magic_name__		  =							True
      __magic_name__		  =							getattr(self.model_tester					, '''decoder_seq_length'''					, self.model_tester.seq_length     )
      __magic_name__		  =							getattr(self.model_tester					, '''encoder_seq_length'''					, self.model_tester.seq_length     )
      __magic_name__		  =							getattr(self.model_tester					, '''key_length'''					, a__     )
      __magic_name__		  =							getattr(self.model_tester					, '''key_length'''					, a__     )

      def check_decoder_attentions_output(a__						:       Dict     ):
           __magic_name__		  =							len(a__     )
           self.assertEqual(out_len % 2					, 0     )
           __magic_name__		  =							outputs.decoder_attentions
           self.assertEqual(len(a__     )					, self.model_tester.num_hidden_layers     )
           self.assertListEqual(
               list(decoder_attentions[0].shape[-3:]     )					, [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]					, )

      def check_encoder_attentions_output(a__						:       Any     ):
           __magic_name__		  =							[
               t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
           ]
           self.assertEqual(len(a__     )					, self.model_tester.num_hidden_layers     )
           self.assertListEqual(
               list(attentions[0].shape[-3:]     )					, [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]					, )

      for model_class in self.all_model_classes:
           __magic_name__		  =							True
           __magic_name__		  =							False
           __magic_name__		  =							model_class(a__     )
           __magic_name__		  =							model(self._prepare_for_class(a__					, a__     )     )
           __magic_name__		  =							len(a__     )
           self.assertEqual(config.output_hidden_states					, a__     )
           check_encoder_attentions_output(a__     )

           if self.is_encoder_decoder:
                __magic_name__		  =							model_class(a__     )
                __magic_name__		  =							model(self._prepare_for_class(a__					, a__     )     )
                self.assertEqual(config.output_hidden_states					, a__     )
                check_decoder_attentions_output(a__     )

           # Check that output attentions can also be changed via the config
           del inputs_dict["output_attentions"]
           __magic_name__		  =							True
           __magic_name__		  =							model_class(a__     )
           __magic_name__		  =							model(self._prepare_for_class(a__					, a__     )     )
           self.assertEqual(config.output_hidden_states					, a__     )
           check_encoder_attentions_output(a__     )

           # Check attention is always last and order is fine
           __magic_name__		  =							True
           __magic_name__		  =							True
           __magic_name__		  =							model_class(a__     )
           __magic_name__		  =							model(self._prepare_for_class(a__					, a__     )     )

           self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)					, len(a__     )     )
           self.assertEqual(model.config.output_hidden_states					, a__     )
           check_encoder_attentions_output(a__     )






@require_tf
class     _SCREAMING_SNAKE_CASE       (       unittest.TestCase      ):



 @slow
 def 					snake_case__    (       self						:       List[str]     ):
      __magic_name__		  =							TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''     )
      __magic_name__		  =							tf.constant([[0, 1, 2, 3, 4, 5]]     )
      __magic_name__		  =							model(a__     )[0]

      __magic_name__		  =							[1, 6, 768]
      self.assertEqual(output.shape					, a__     )

      __magic_name__		  =							tf.constant(
          [
              [
                  [-0.03_475_493, -0.4_686_034, -0.30_638_832],
                  [0.22_637_248, -0.26_988_646, -0.7_423_424],
                  [0.10_324_868, -0.45_013_508, -0.58_280_784],
              ]
          ]     )
      tf.debugging.assert_near(output[:, :3, :3]					, a__					, atol=1E-4     )











import requests

_lowerCamelCase    :		int		      =    """https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey="""

def        __a			(   __lowerCAmelCase   )     ->     None:
					# fetching a list of articles in json format
					SCREAMING_SNAKE_CASE							:     List[str]						=				requests.get(_NEWS_API + bbc_news_api_key   ).json()
					# each article in the list is a dict
					for i, article in enumerate(bbc_news_page['articles']      ,	1   ):
										print(F'''{i}.) {article['title']}'''   )


if __name__ == "__main__":
			fetch_bbc_news(bbc_news_api_key="""<Your BBC News API key goes here>""")