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      A							:


   def __init__(self     :						Any       ,  __UpperCAmelCase     :						Any       ,  __UpperCAmelCase     :						Tuple=1_3       ,  __UpperCAmelCase     :						int=7       ,  __UpperCAmelCase     :						List[Any]=True       ,  __UpperCAmelCase     :						Any=True       ,  __UpperCAmelCase     :						Dict=True       ,  __UpperCAmelCase     :						str=True       ,  __UpperCAmelCase     :						Union[str, Any]=9_9       ,  __UpperCAmelCase     :						Tuple=3_2       ,  __UpperCAmelCase     :						List[Any]=2       ,  __UpperCAmelCase     :						List[Any]=4       ,  __UpperCAmelCase     :						str=3_7       ,  __UpperCAmelCase     :						Union[str, Any]="gelu"       ,  __UpperCAmelCase     :						Tuple=0.1       ,  __UpperCAmelCase     :						List[Any]=0.1       ,  __UpperCAmelCase     :						Optional[Any]=5_1_2       ,  __UpperCAmelCase     :						Tuple=1_6       ,  __UpperCAmelCase     :						Any=2       ,  __UpperCAmelCase     :						Union[str, Any]=0.02       ,  __UpperCAmelCase     :						Optional[int]=3       ,  __UpperCAmelCase     :						str=4       ,  __UpperCAmelCase     :						str=None       ,  )						->				Any:
         """simple docstring"""



         UpperCAmelCase__								=  parent
         UpperCAmelCase__								=  1_3
         UpperCAmelCase__								=  7
         UpperCAmelCase__								=  True
         UpperCAmelCase__								=  True
         UpperCAmelCase__								=  True
         UpperCAmelCase__								=  True
         UpperCAmelCase__								=  9_9
         UpperCAmelCase__								=  3_8_4
         UpperCAmelCase__								=  2
         UpperCAmelCase__								=  4
         UpperCAmelCase__								=  3_7
         UpperCAmelCase__								=  "gelu"
         UpperCAmelCase__								=  0.1
         UpperCAmelCase__								=  0.1
         UpperCAmelCase__								=  5_1_2
         UpperCAmelCase__								=  1_6
         UpperCAmelCase__								=  2
         UpperCAmelCase__								=  0.02
         UpperCAmelCase__								=  3
         UpperCAmelCase__								=  4
         UpperCAmelCase__								=  1_2_8
         UpperCAmelCase__								=  2
         UpperCAmelCase__								=  9
         UpperCAmelCase__								=  1
         UpperCAmelCase__								=  None


   def 			lowercase_      (self     :						Any		)						->				str:
         """simple docstring"""



         UpperCAmelCase__								=  ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size		)

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

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

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

         UpperCAmelCase__								=  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=__UpperCAmelCase       ,  )

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


   def 			lowercase_      (self     :						Optional[int]       ,  __UpperCAmelCase     :						List[str]       ,  __UpperCAmelCase     :						Dict       ,  __UpperCAmelCase     :						List[Any]       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						Tuple		)						->				Optional[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  TFConvBertModel(config=__UpperCAmelCase		)
         UpperCAmelCase__								=  {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

         UpperCAmelCase__								=  [input_ids, input_mask]
         UpperCAmelCase__								=  model(__UpperCAmelCase		)

         UpperCAmelCase__								=  model(__UpperCAmelCase		)

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


   def 			lowercase_      (self     :						Optional[int]       ,  __UpperCAmelCase     :						Union[str, Any]       ,  __UpperCAmelCase     :						Tuple       ,  __UpperCAmelCase     :						List[str]       ,  __UpperCAmelCase     :						Tuple       ,  __UpperCAmelCase     :						Dict       ,  __UpperCAmelCase     :						Dict       ,  __UpperCAmelCase     :						int		)						->				Optional[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  TFConvBertForMaskedLM(config=__UpperCAmelCase		)
         UpperCAmelCase__								=  {
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }
         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.vocab_size)		)


   def 			lowercase_      (self     :						List[Any]       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						str       ,  __UpperCAmelCase     :						Optional[int]       ,  __UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						List[str]       ,  __UpperCAmelCase     :						Union[str, Any]       ,  __UpperCAmelCase     :						List[Any]		)						->				Tuple:
         """simple docstring"""



         UpperCAmelCase__								=  self.num_labels
         UpperCAmelCase__								=  TFConvBertForSequenceClassification(config=__UpperCAmelCase		)
         UpperCAmelCase__								=  {
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }

         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_labels)		)


   def 			lowercase_      (self     :						Optional[int]       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						List[str]       ,  __UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						str       ,  __UpperCAmelCase     :						List[str]		)						->				str:
         """simple docstring"""



         UpperCAmelCase__								=  self.num_choices
         UpperCAmelCase__								=  TFConvBertForMultipleChoice(config=__UpperCAmelCase		)
         UpperCAmelCase__								=  tf.tile(tf.expand_dims(__UpperCAmelCase       ,  1		)       ,  (1, self.num_choices, 1)		)
         UpperCAmelCase__								=  tf.tile(tf.expand_dims(__UpperCAmelCase       ,  1		)       ,  (1, self.num_choices, 1)		)
         UpperCAmelCase__								=  tf.tile(tf.expand_dims(__UpperCAmelCase       ,  1		)       ,  (1, self.num_choices, 1)		)
         UpperCAmelCase__								=  {
             "input_ids": multiple_choice_inputs_ids,
             "attention_mask": multiple_choice_input_mask,
             "token_type_ids": multiple_choice_token_type_ids,
         }
         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_choices)		)


   def 			lowercase_      (self     :						Any       ,  __UpperCAmelCase     :						Optional[int]       ,  __UpperCAmelCase     :						Optional[int]       ,  __UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						List[Any]       ,  __UpperCAmelCase     :						Dict       ,  __UpperCAmelCase     :						str       ,  __UpperCAmelCase     :						int		)						->				int:
         """simple docstring"""



         UpperCAmelCase__								=  self.num_labels
         UpperCAmelCase__								=  TFConvBertForTokenClassification(config=__UpperCAmelCase		)
         UpperCAmelCase__								=  {
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }
         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.num_labels)		)


   def 			lowercase_      (self     :						str       ,  __UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						Any       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						List[str]       ,  __UpperCAmelCase     :						str       ,  __UpperCAmelCase     :						Optional[int]       ,  __UpperCAmelCase     :						Any		)						->				Optional[int]:
         """simple docstring"""



         UpperCAmelCase__								=  TFConvBertForQuestionAnswering(config=__UpperCAmelCase		)
         UpperCAmelCase__								=  {
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }

         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         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 			lowercase_      (self     :						Tuple		)						->				Dict:
         """simple docstring"""



         UpperCAmelCase__								=  self.prepare_config_and_inputs()
         (
             (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      
         )								=  config_and_inputs
         UpperCAmelCase__								=  {"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 : Tuple      =			(
       (
           TFConvBertModel,
           TFConvBertForMaskedLM,
           TFConvBertForQuestionAnswering,
           TFConvBertForSequenceClassification,
           TFConvBertForTokenClassification,
           TFConvBertForMultipleChoice,
       )
       if is_tf_available()
       else ()
   )
   __UpperCAmelCase : Optional[Any]      =			(
       {
           'feature-extraction': TFConvBertModel,
           'fill-mask': TFConvBertForMaskedLM,
           'question-answering': TFConvBertForQuestionAnswering,
           'text-classification': TFConvBertForSequenceClassification,
           'token-classification': TFConvBertForTokenClassification,
           'zero-shot': TFConvBertForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   __UpperCAmelCase : List[Any]      =			False
   __UpperCAmelCase : Optional[Any]      =			False
   __UpperCAmelCase : str      =			False


   def 			lowercase_      (self     :						Tuple		)						->				str:
         """simple docstring"""



         UpperCAmelCase__								=  TFConvBertModelTester(self		)
         UpperCAmelCase__								=  ConfigTester(self       ,  config_class=__UpperCAmelCase       ,  hidden_size=3_7		)


   def 			lowercase_      (self     :						str		)						->				Dict:
         """simple docstring"""



         self.config_tester.run_common_tests()


   def 			lowercase_      (self     :						Optional[int]		)						->				Dict:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__UpperCAmelCase		)


   def 			lowercase_      (self     :						str		)						->				Tuple:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase		)


   def 			lowercase_      (self     :						int		)						->				Dict:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*__UpperCAmelCase		)


   def 			lowercase_      (self     :						Tuple		)						->				str:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase		)


   def 			lowercase_      (self     :						Optional[Any]		)						->				int:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase		)


   def 			lowercase_      (self     :						Optional[int]		)						->				str:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase		)


   @slow
   def 			lowercase_      (self     :						Union[str, Any]		)						->				Any:
         """simple docstring"""



         UpperCAmelCase__		,      UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()
         UpperCAmelCase__								=  True
         UpperCAmelCase__								=  True

         if hasattr(__UpperCAmelCase       ,  "use_cache"		):
               UpperCAmelCase__								=  True

         UpperCAmelCase__								=  getattr(self.model_tester       ,  "encoder_seq_length"       ,  self.model_tester.seq_length		)
         UpperCAmelCase__								=  getattr(self.model_tester       ,  "key_length"       ,  __UpperCAmelCase		)

         for model_class in self.all_model_classes:
               UpperCAmelCase__								=  self._prepare_for_class(__UpperCAmelCase       ,  __UpperCAmelCase		)
               UpperCAmelCase__								=  model_class(__UpperCAmelCase		)
               UpperCAmelCase__								=  len(model(__UpperCAmelCase		)		)

               with tempfile.TemporaryDirectory() as tmpdirname:
                     model.save_pretrained(__UpperCAmelCase       ,  saved_model=__UpperCAmelCase		)
                     UpperCAmelCase__								=  os.path.join(__UpperCAmelCase       ,  "saved_model"       ,  "1"		)
                     UpperCAmelCase__								=  tf.keras.models.load_model(__UpperCAmelCase		)
                     UpperCAmelCase__								=  model(__UpperCAmelCase		)

                     if self.is_encoder_decoder:
                           UpperCAmelCase__								=  outputs["encoder_hidden_states"]
                           UpperCAmelCase__								=  outputs["encoder_attentions"]
                     else:
                           UpperCAmelCase__								=  outputs["hidden_states"]
                           UpperCAmelCase__								=  outputs["attentions"]

                     self.assertEqual(len(__UpperCAmelCase		)       ,  __UpperCAmelCase		)

                     UpperCAmelCase__								=  getattr(
                         self.model_tester       ,  "expected_num_hidden_layers"       ,  self.model_tester.num_hidden_layers + 1		)

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

                     self.assertEqual(len(__UpperCAmelCase		)       ,  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 			lowercase_      (self     :						Any		)						->				Any:
         """simple docstring"""



         UpperCAmelCase__								=  TFConvBertModel.from_pretrained("YituTech/conv-bert-base"		)
         self.assertIsNotNone(__UpperCAmelCase		)





   def 			lowercase_      (self     :						int		)						->				List[str]:
         """simple docstring"""



         UpperCAmelCase__		,      UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()
         UpperCAmelCase__								=  True
         UpperCAmelCase__								=  getattr(self.model_tester       ,  "decoder_seq_length"       ,  self.model_tester.seq_length		)
         UpperCAmelCase__								=  getattr(self.model_tester       ,  "encoder_seq_length"       ,  self.model_tester.seq_length		)
         UpperCAmelCase__								=  getattr(self.model_tester       ,  "key_length"       ,  __UpperCAmelCase		)
         UpperCAmelCase__								=  getattr(self.model_tester       ,  "key_length"       ,  __UpperCAmelCase		)

         def check_decoder_attentions_output(__UpperCAmelCase     :						List[str]		):
               UpperCAmelCase__								=  len(__UpperCAmelCase		)
               self.assertEqual(out_len % 2       ,  0		)
               UpperCAmelCase__								=  outputs.decoder_attentions
               self.assertEqual(len(__UpperCAmelCase		)       ,  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(__UpperCAmelCase     :						List[str]		):
               UpperCAmelCase__								=  [
                   t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
               ]
               self.assertEqual(len(__UpperCAmelCase		)       ,  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:
               UpperCAmelCase__								=  True
               UpperCAmelCase__								=  False
               UpperCAmelCase__								=  model_class(__UpperCAmelCase		)
               UpperCAmelCase__								=  model(self._prepare_for_class(__UpperCAmelCase       ,  __UpperCAmelCase		)		)
               UpperCAmelCase__								=  len(__UpperCAmelCase		)
               self.assertEqual(config.output_hidden_states       ,  __UpperCAmelCase		)
               check_encoder_attentions_output(__UpperCAmelCase		)

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

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

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

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





@require_tf
class      A							(				unittest.TestCase     ):


   @slow
   def 			lowercase_      (self     :						Dict		)						->				Any:
         """simple docstring"""



         UpperCAmelCase__								=  TFConvBertModel.from_pretrained("YituTech/conv-bert-base"		)
         UpperCAmelCase__								=  tf.constant([[0, 1, 2, 3, 4, 5]]		)
         UpperCAmelCase__								=  model(__UpperCAmelCase		)[0]

         UpperCAmelCase__								=  [1, 6, 7_6_8]
         self.assertEqual(output.shape       ,  __UpperCAmelCase		)

         UpperCAmelCase__								=  tf.constant(
             [
                 [
                     [-0.03475493, -0.4686034, -0.30638832],
                     [0.22637248, -0.26988646, -0.7423424],
                     [0.10324868, -0.45013508, -0.58280784],
                 ]
             ]		)
         tf.debugging.assert_near(output[:, :3, :3]       ,  __UpperCAmelCase       ,  atol=1E-4		)




from datetime import datetime

import requests
from bsa import BeautifulSoup

if __name__ == "__main__":
   __lowerCamelCase			: str							   =		input('''Enter image url: ''').strip()
   print(F"""Downloading image from {url} ...""")
   __lowerCamelCase			: Any							   =		BeautifulSoup(requests.get(url).content, '''html.parser''')
   # The image URL is in the content field of the first meta tag with property og:image
   __lowerCamelCase			: List[Any]							   =		soup.find('''meta''', {'''property''': '''og:image'''})['''content''']
   __lowerCamelCase			: Tuple							   =		requests.get(image_url).content
   __lowerCamelCase			: Union[str, Any]							   =		F"""{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg"""
   with open(file_name, '''wb''') as fp:
      fp.write(image_data)
   print(F"""Done. Image saved to disk as {file_name}.""")
