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





	def __init__(		self	: Tuple    ,__SCREAMING_SNAKE_CASE	: Dict    ,__SCREAMING_SNAKE_CASE	: List[Any]=1_3    ,__SCREAMING_SNAKE_CASE	: List[Any]=7    ,__SCREAMING_SNAKE_CASE	: int=True    ,__SCREAMING_SNAKE_CASE	: Dict=True    ,__SCREAMING_SNAKE_CASE	: List[str]=True    ,__SCREAMING_SNAKE_CASE	: List[Any]=True    ,__SCREAMING_SNAKE_CASE	: int=9_9    ,__SCREAMING_SNAKE_CASE	: int=3_2    ,__SCREAMING_SNAKE_CASE	: str=2    ,__SCREAMING_SNAKE_CASE	: Tuple=4    ,__SCREAMING_SNAKE_CASE	: Tuple=3_7    ,__SCREAMING_SNAKE_CASE	: Tuple="gelu"    ,__SCREAMING_SNAKE_CASE	: int=0.1    ,__SCREAMING_SNAKE_CASE	: Optional[int]=0.1    ,__SCREAMING_SNAKE_CASE	: Any=5_1_2    ,__SCREAMING_SNAKE_CASE	: Optional[int]=1_6    ,__SCREAMING_SNAKE_CASE	: List[str]=2    ,__SCREAMING_SNAKE_CASE	: Union[str, Any]=0.02    ,__SCREAMING_SNAKE_CASE	: List[str]=3    ,__SCREAMING_SNAKE_CASE	: Optional[Any]=4    ,__SCREAMING_SNAKE_CASE	: Dict=None    ,):
						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 	_UpperCAmelCase						(		self	: Tuple       ):
						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=_lowercase    ,)

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





	def 	_UpperCAmelCase						(		self	: Any    ,__SCREAMING_SNAKE_CASE	: Optional[Any]    ,__SCREAMING_SNAKE_CASE	: int    ,__SCREAMING_SNAKE_CASE	: List[Any]    ,__SCREAMING_SNAKE_CASE	: Union[str, Any]    ,__SCREAMING_SNAKE_CASE	: Optional[Any]    ,__SCREAMING_SNAKE_CASE	: str    ,__SCREAMING_SNAKE_CASE	: str       ):
						UpperCAmelCase				   =  TFConvBertModel(config=_lowercase       )
						UpperCAmelCase				   =  {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

						UpperCAmelCase				   =  [input_ids, input_mask]
						UpperCAmelCase				   =  model(_lowercase       )

						UpperCAmelCase				   =  model(_lowercase       )

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





	def 	_UpperCAmelCase						(		self	: Any    ,__SCREAMING_SNAKE_CASE	: Union[str, Any]    ,__SCREAMING_SNAKE_CASE	: List[str]    ,__SCREAMING_SNAKE_CASE	: Any    ,__SCREAMING_SNAKE_CASE	: Tuple    ,__SCREAMING_SNAKE_CASE	: Tuple    ,__SCREAMING_SNAKE_CASE	: Tuple    ,__SCREAMING_SNAKE_CASE	: Dict       ):
						UpperCAmelCase				   =  TFConvBertForMaskedLM(config=_lowercase       )
						UpperCAmelCase				   =  {
						    "input_ids": input_ids,
						    "attention_mask": input_mask,
						    "token_type_ids": token_type_ids,
						}
						UpperCAmelCase				   =  model(_lowercase       )
						self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.seq_length, self.vocab_size)       )





	def 	_UpperCAmelCase						(		self	: List[str]    ,__SCREAMING_SNAKE_CASE	: Union[str, Any]    ,__SCREAMING_SNAKE_CASE	: Tuple    ,__SCREAMING_SNAKE_CASE	: str    ,__SCREAMING_SNAKE_CASE	: List[Any]    ,__SCREAMING_SNAKE_CASE	: Optional[Any]    ,__SCREAMING_SNAKE_CASE	: Any    ,__SCREAMING_SNAKE_CASE	: str       ):
						UpperCAmelCase				   =  self.num_labels
						UpperCAmelCase				   =  TFConvBertForSequenceClassification(config=_lowercase       )
						UpperCAmelCase				   =  {
						    "input_ids": input_ids,
						    "attention_mask": input_mask,
						    "token_type_ids": token_type_ids,
						}

						UpperCAmelCase				   =  model(_lowercase       )
						self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.num_labels)       )





	def 	_UpperCAmelCase						(		self	: Optional[int]    ,__SCREAMING_SNAKE_CASE	: List[str]    ,__SCREAMING_SNAKE_CASE	: List[Any]    ,__SCREAMING_SNAKE_CASE	: Optional[Any]    ,__SCREAMING_SNAKE_CASE	: Dict    ,__SCREAMING_SNAKE_CASE	: int    ,__SCREAMING_SNAKE_CASE	: Dict    ,__SCREAMING_SNAKE_CASE	: Dict       ):
						UpperCAmelCase				   =  self.num_choices
						UpperCAmelCase				   =  TFConvBertForMultipleChoice(config=_lowercase       )
						UpperCAmelCase				   =  tf.tile(tf.expand_dims(_lowercase    ,1       )    ,(1, self.num_choices, 1)       )
						UpperCAmelCase				   =  tf.tile(tf.expand_dims(_lowercase    ,1       )    ,(1, self.num_choices, 1)       )
						UpperCAmelCase				   =  tf.tile(tf.expand_dims(_lowercase    ,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(_lowercase       )
						self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.num_choices)       )





	def 	_UpperCAmelCase						(		self	: List[Any]    ,__SCREAMING_SNAKE_CASE	: Tuple    ,__SCREAMING_SNAKE_CASE	: Union[str, Any]    ,__SCREAMING_SNAKE_CASE	: Any    ,__SCREAMING_SNAKE_CASE	: Tuple    ,__SCREAMING_SNAKE_CASE	: Dict    ,__SCREAMING_SNAKE_CASE	: Union[str, Any]    ,__SCREAMING_SNAKE_CASE	: List[str]       ):
						UpperCAmelCase				   =  self.num_labels
						UpperCAmelCase				   =  TFConvBertForTokenClassification(config=_lowercase       )
						UpperCAmelCase				   =  {
						    "input_ids": input_ids,
						    "attention_mask": input_mask,
						    "token_type_ids": token_type_ids,
						}
						UpperCAmelCase				   =  model(_lowercase       )
						self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.seq_length, self.num_labels)       )





	def 	_UpperCAmelCase						(		self	: Optional[int]    ,__SCREAMING_SNAKE_CASE	: Optional[int]    ,__SCREAMING_SNAKE_CASE	: List[str]    ,__SCREAMING_SNAKE_CASE	: Dict    ,__SCREAMING_SNAKE_CASE	: List[Any]    ,__SCREAMING_SNAKE_CASE	: List[Any]    ,__SCREAMING_SNAKE_CASE	: List[Any]    ,__SCREAMING_SNAKE_CASE	: Any       ):
						UpperCAmelCase				   =  TFConvBertForQuestionAnswering(config=_lowercase       )
						UpperCAmelCase				   =  {
						    "input_ids": input_ids,
						    "attention_mask": input_mask,
						    "token_type_ids": token_type_ids,
						}

						UpperCAmelCase				   =  model(_lowercase       )
						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 	_UpperCAmelCase						(		self	: Optional[Any]       ):
						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  __magic_name__						(     _SCREAMING_SNAKE_CASE  ,							_SCREAMING_SNAKE_CASE  ,							unittest.TestCase):
	_UpperCAmelCase	:			Tuple		   =     (
	    (
	        TFConvBertModel,
	        TFConvBertForMaskedLM,
	        TFConvBertForQuestionAnswering,
	        TFConvBertForSequenceClassification,
	        TFConvBertForTokenClassification,
	        TFConvBertForMultipleChoice,
	    )
	    if is_tf_available()
	    else ()
	)
	_UpperCAmelCase	:			str		   =     (
	    {
	        '''feature-extraction''': TFConvBertModel,
	        '''fill-mask''': TFConvBertForMaskedLM,
	        '''question-answering''': TFConvBertForQuestionAnswering,
	        '''text-classification''': TFConvBertForSequenceClassification,
	        '''token-classification''': TFConvBertForTokenClassification,
	        '''zero-shot''': TFConvBertForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	_UpperCAmelCase	:			Optional[Any]		   =     False
	_UpperCAmelCase	:			str		   =     False
	_UpperCAmelCase	:			Any		   =     False





	def 	_UpperCAmelCase						(		self	: Any       ):
						UpperCAmelCase				   =  TFConvBertModelTester(self       )
						UpperCAmelCase				   =  ConfigTester(self    ,config_class=_lowercase    ,hidden_size=3_7       )





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





	def 	_UpperCAmelCase						(		self	: List[Any]       ):
						UpperCAmelCase				   =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*_lowercase       )





	def 	_UpperCAmelCase						(		self	: Optional[Any]       ):
						UpperCAmelCase				   =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*_lowercase       )





	def 	_UpperCAmelCase						(		self	: Optional[Any]       ):
						UpperCAmelCase				   =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*_lowercase       )





	def 	_UpperCAmelCase						(		self	: Optional[int]       ):
						UpperCAmelCase				   =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*_lowercase       )





	def 	_UpperCAmelCase						(		self	: Dict       ):
						UpperCAmelCase				   =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*_lowercase       )





	def 	_UpperCAmelCase						(		self	: List[str]       ):
						UpperCAmelCase				   =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*_lowercase       )





	@slow
	def 	_UpperCAmelCase						(		self	: List[str]       ):
						UpperCAmelCase   ,     UpperCAmelCase				   =  self.model_tester.prepare_config_and_inputs_for_common()
						UpperCAmelCase				   =  True
						UpperCAmelCase				   =  True

						if hasattr(_lowercase    ,"use_cache"       ):
											UpperCAmelCase				   =  True

						UpperCAmelCase				   =  getattr(self.model_tester    ,"encoder_seq_length"    ,self.model_tester.seq_length       )
						UpperCAmelCase				   =  getattr(self.model_tester    ,"key_length"    ,_lowercase       )

						for model_class in self.all_model_classes:
											UpperCAmelCase				   =  self._prepare_for_class(_lowercase    ,_lowercase       )
											UpperCAmelCase				   =  model_class(_lowercase       )
											UpperCAmelCase				   =  len(model(_lowercase       )       )

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

																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(_lowercase       )    ,_lowercase       )

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

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

																self.assertEqual(len(_lowercase       )    ,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 	_UpperCAmelCase						(		self	: str       ):
						UpperCAmelCase				   =  TFConvBertModel.from_pretrained("YituTech/conv-bert-base"       )
						self.assertIsNotNone(_lowercase       )





	def 	_UpperCAmelCase						(		self	: Tuple       ):
						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"    ,_lowercase       )
						UpperCAmelCase				   =  getattr(self.model_tester    ,"key_length"    ,_lowercase       )

						def check_decoder_attentions_output(__SCREAMING_SNAKE_CASE	: Any       ):
											UpperCAmelCase				   =  len(_lowercase       )
											self.assertEqual(out_len % 2    ,0       )
											UpperCAmelCase				   =  outputs.decoder_attentions
											self.assertEqual(len(_lowercase       )    ,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(__SCREAMING_SNAKE_CASE	: int       ):
											UpperCAmelCase				   =  [
											    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
											]
											self.assertEqual(len(_lowercase       )    ,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(_lowercase       )
											UpperCAmelCase				   =  model(self._prepare_for_class(_lowercase    ,_lowercase       )       )
											UpperCAmelCase				   =  len(_lowercase       )
											self.assertEqual(config.output_hidden_states    ,_lowercase       )
											check_encoder_attentions_output(_lowercase       )

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

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

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

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







@require_tf
class  __magic_name__						(     unittest.TestCase):





	@slow
	def 	_UpperCAmelCase						(		self	: Tuple       ):
						UpperCAmelCase				   =  TFConvBertModel.from_pretrained("YituTech/conv-bert-base"       )
						UpperCAmelCase				   =  tf.constant([[0, 1, 2, 3, 4, 5]]       )
						UpperCAmelCase				   =  model(_lowercase       )[0]

						UpperCAmelCase				   =  [1, 6, 7_6_8]
						self.assertEqual(output.shape    ,_lowercase       )

						UpperCAmelCase				   =  tf.constant(
						    [
						        [
						            [-0.0347_5493, -0.468_6034, -0.3063_8832],
						            [0.2263_7248, -0.2698_8646, -0.742_3424],
						            [0.1032_4868, -0.4501_3508, -0.5828_0784],
						        ]
						    ]       )
						tf.debugging.assert_near(output[:, :3, :3]    ,_lowercase    ,atol=1e-4       )




'''simple docstring'''



import sys
import webbrowser

import requests
from bsa import BeautifulSoup
from fake_useragent import UserAgent

if __name__ == "__main__":
						print("""Googling.....""")
						_lowercase          =						"""https://www.google.com/search?q=""" + """ """.join(sys.argv[1:])
						_lowercase          =						requests.get(url, headers={"""UserAgent""": UserAgent().random})
						# res.raise_for_status()
						with open("""project1a.html""", """wb""") as out_file:  # only for knowing the class
												for data in res.iter_content(10000):
																		out_file.write(data)
						_lowercase          =						BeautifulSoup(res.text, """html.parser""")
						_lowercase          =						list(soup.select(""".eZt8xd"""))[:5]

						print(len(links))
						for link in links:
												if link.text == "Maps":
																		webbrowser.open(link.get("""href"""))
												else:
																		webbrowser.open(F"""https://google.com{link.get('href')}""")
