




import gc
import unittest

from transformers import CTRLConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
			    CTRLForSequenceClassification,
			    CTRLLMHeadModel,
			    CTRLModel,
			)






class  __magic_name__						:
				'''simple docstring'''







				def __init__(							self:   Dict		,					_lowerCamelCase:   int		,					_lowerCamelCase:   Optional[int]=14		,					_lowerCamelCase:   Dict=7		,					_lowerCamelCase:   str=True		,					_lowerCamelCase:   int=True		,					_lowerCamelCase:   Optional[int]=True		,					_lowerCamelCase:   Optional[int]=True		,					_lowerCamelCase:   List[Any]=True		,					_lowerCamelCase:   Union[str, Any]=99		,					_lowerCamelCase:   Dict=32		,					_lowerCamelCase:   List[Any]=5		,					_lowerCamelCase:   Tuple=4		,					_lowerCamelCase:   List[str]=37		,					_lowerCamelCase:   str="gelu"		,					_lowerCamelCase:   List[Any]=0.1		,					_lowerCamelCase:   int=0.1		,					_lowerCamelCase:   List[Any]=5_12		,					_lowerCamelCase:   Any=16		,					_lowerCamelCase:   int=2		,					_lowerCamelCase:   Union[str, Any]=0.02		,					_lowerCamelCase:   Optional[int]=3		,					_lowerCamelCase:   Any=4		,					_lowerCamelCase:   str=None		,					):
											SCREAMING_SNAKE_CASE_   	=    parent
											SCREAMING_SNAKE_CASE_   	=    batch_size
											SCREAMING_SNAKE_CASE_   	=    seq_length
											SCREAMING_SNAKE_CASE_   	=    is_training
											SCREAMING_SNAKE_CASE_   	=    use_token_type_ids
											SCREAMING_SNAKE_CASE_   	=    use_input_mask
											SCREAMING_SNAKE_CASE_   	=    use_labels
											SCREAMING_SNAKE_CASE_   	=    use_mc_token_ids
											SCREAMING_SNAKE_CASE_   	=    vocab_size
											SCREAMING_SNAKE_CASE_   	=    hidden_size
											SCREAMING_SNAKE_CASE_   	=    num_hidden_layers
											SCREAMING_SNAKE_CASE_   	=    num_attention_heads
											SCREAMING_SNAKE_CASE_   	=    intermediate_size
											SCREAMING_SNAKE_CASE_   	=    hidden_act
											SCREAMING_SNAKE_CASE_   	=    hidden_dropout_prob
											SCREAMING_SNAKE_CASE_   	=    attention_probs_dropout_prob
											SCREAMING_SNAKE_CASE_   	=    max_position_embeddings
											SCREAMING_SNAKE_CASE_   	=    type_vocab_size
											SCREAMING_SNAKE_CASE_   	=    type_sequence_label_size
											SCREAMING_SNAKE_CASE_   	=    initializer_range
											SCREAMING_SNAKE_CASE_   	=    num_labels
											SCREAMING_SNAKE_CASE_   	=    num_choices
											SCREAMING_SNAKE_CASE_   	=    scope
											SCREAMING_SNAKE_CASE_   	=    self.vocab_size - 1


				def     _A							(							self:   int		):
											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_   	=    self.get_config()

											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     _A							(							self:   Any		):
											return CTRLConfig(
											    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		,					)


				def     _A							(							self:   str		,					_lowerCamelCase:   List[str]		,					_lowerCamelCase:   int		,					_lowerCamelCase:   Any		,					_lowerCamelCase:   List[str]		,					_lowerCamelCase:   Dict		,					*_lowerCamelCase:   List[str]		):
											SCREAMING_SNAKE_CASE_   	=    CTRLModel(config=__snake_case		)
											model.to(__snake_case		)
											model.eval()

											model(__snake_case		,					token_type_ids=__snake_case		,					head_mask=__snake_case		)
											model(__snake_case		,					token_type_ids=__snake_case		)
											SCREAMING_SNAKE_CASE_   	=    model(__snake_case		)
											self.parent.assertEqual(result.last_hidden_state.shape		,					(self.batch_size, self.seq_length, self.hidden_size)		)
											self.parent.assertEqual(len(result.past_key_values		)		,					config.n_layer		)


				def     _A							(							self:   str		,					_lowerCamelCase:   Dict		,					_lowerCamelCase:   Dict		,					_lowerCamelCase:   Optional[int]		,					_lowerCamelCase:   Tuple		,					_lowerCamelCase:   Dict		,					*_lowerCamelCase:   str		):
											SCREAMING_SNAKE_CASE_   	=    CTRLLMHeadModel(__snake_case		)
											model.to(__snake_case		)
											model.eval()

											SCREAMING_SNAKE_CASE_   	=    model(__snake_case		,					token_type_ids=__snake_case		,					labels=__snake_case		)
											self.parent.assertEqual(result.loss.shape		,					()		)
											self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.seq_length, self.vocab_size)		)


				def     _A							(							self:   List[Any]		):
											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, '''head_mask''': head_mask}

											return config, inputs_dict





				def     _A							(							self:   int		,					_lowerCamelCase:   Optional[int]		,					_lowerCamelCase:   List[Any]		,					_lowerCamelCase:   int		,					_lowerCamelCase:   List[Any]		,					*_lowerCamelCase:   str		):
											SCREAMING_SNAKE_CASE_   	=    self.num_labels
											SCREAMING_SNAKE_CASE_   	=    CTRLForSequenceClassification(__snake_case		)
											model.to(__snake_case		)
											model.eval()
											SCREAMING_SNAKE_CASE_   	=    ids_tensor([self.batch_size]		,					self.type_sequence_label_size		)
											SCREAMING_SNAKE_CASE_   	=    model(__snake_case		,					token_type_ids=__snake_case		,					labels=__snake_case		)
											self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.num_labels)		)






@require_torch
class  __magic_name__						(  __SCREAMING_SNAKE_CASE    ,  __SCREAMING_SNAKE_CASE    ,  __SCREAMING_SNAKE_CASE    ,  unittest.TestCase):
				'''simple docstring'''







				SCREAMING_SNAKE_CASE__					:							Any	      =					(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
				SCREAMING_SNAKE_CASE__					:							List[Any]	      =					(CTRLLMHeadModel,) if is_torch_available() else ()
				SCREAMING_SNAKE_CASE__					:							int	      =					(
				    {
				        "feature-extraction": CTRLModel,
				        "text-classification": CTRLForSequenceClassification,
				        "text-generation": CTRLLMHeadModel,
				        "zero-shot": CTRLForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				SCREAMING_SNAKE_CASE__					:							Dict	      =					True
				SCREAMING_SNAKE_CASE__					:							Any	      =					False
				SCREAMING_SNAKE_CASE__					:							str	      =					False


				def     _A							(							self:   str		,					_lowerCamelCase:   int		,					_lowerCamelCase:   int		,					_lowerCamelCase:   str		,					_lowerCamelCase:   List[str]		,					_lowerCamelCase:   Optional[int]		):
											if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
																		# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
																		# `CTRLConfig` 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     _A							(							self:   str		):
											SCREAMING_SNAKE_CASE_   	=    CTRLModelTester(self		)
											SCREAMING_SNAKE_CASE_   	=    ConfigTester(self		,					config_class=__snake_case		,					n_embd=37		)


				def     _A							(							self:   str		):
											super().tearDown()
											# clean-up as much as possible GPU memory occupied by PyTorch
											gc.collect()
											torch.cuda.empty_cache()


				def     _A							(							self:   List[Any]		):
											self.config_tester.run_common_tests()


				def     _A							(							self:   List[str]		):
											SCREAMING_SNAKE_CASE_   	=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_ctrl_model(*__snake_case		)


				def     _A							(							self:   Union[str, Any]		):
											SCREAMING_SNAKE_CASE_   	=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_lm_head_model(*__snake_case		)


				@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''		)
				def     _A							(							self:   Union[str, Any]		):
											pass


				@slow
				def     _A							(							self:   Union[str, Any]		):
											for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE_   	=    CTRLModel.from_pretrained(__snake_case		)
																		self.assertIsNotNone(__snake_case		)





				@unittest.skip('''The model doesn\'t support left padding'''		)  # and it's not used enough to be worth fixing :)
				def     _A							(							self:   List[str]		):
											pass






@require_torch
class  __magic_name__						(  unittest.TestCase):
				'''simple docstring'''







				def     _A							(							self:   Dict		):
											super().tearDown()
											# clean-up as much as possible GPU memory occupied by PyTorch
											gc.collect()
											torch.cuda.empty_cache()





				@slow
				def     _A							(							self:   Union[str, Any]		):
											SCREAMING_SNAKE_CASE_   	=    CTRLLMHeadModel.from_pretrained('''ctrl'''		)
											model.to(__snake_case		)
											SCREAMING_SNAKE_CASE_   	=    torch.tensor(
											    [[1_18_59, 0, 16_11, 8]]		,					dtype=torch.long		,					device=__snake_case		)  # Legal the president is
											SCREAMING_SNAKE_CASE_   	=    [
											    1_18_59,
											    0,
											    16_11,
											    8,
											    5,
											    1_50,
											    2_64_49,
											    2,
											    19,
											    3_48,
											    4_69,
											    3,
											    25_95,
											    48,
											    2_07_40,
											    24_65_33,
											    24_65_33,
											    19,
											    30,
											    5,
											]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

											SCREAMING_SNAKE_CASE_   	=    model.generate(__snake_case		,					do_sample=__snake_case		)
											self.assertListEqual(output_ids[0].tolist()		,					__snake_case		)












import webbrowser
from sys import argv
from urllib.parse import parse_qs, quote

import requests
from bsa import BeautifulSoup
from fake_useragent import UserAgent

if __name__ == "__main__":
			__SCREAMING_SNAKE_CASE 						="""%20""".join(argv[1:]) if len(argv) > 1 else quote(str(input("""Search: """)))

			print("""Googling.....""")

			__SCREAMING_SNAKE_CASE 						=f"""https://www.google.com/search?q={query}&num=100"""

			__SCREAMING_SNAKE_CASE 						=requests.get(
			    url,
			    headers={"""User-Agent""": str(UserAgent().random)},
			)

			try:
						__SCREAMING_SNAKE_CASE 						=(
						    BeautifulSoup(res.text, """html.parser""")
						    .find("""div""", attrs={"""class""": """yuRUbf"""})
						    .find("""a""")
						    .get("""href""")
						)

			except AttributeError:
						__SCREAMING_SNAKE_CASE 						=parse_qs(
						    BeautifulSoup(res.text, """html.parser""")
						    .find("""div""", attrs={"""class""": """kCrYT"""})
						    .find("""a""")
						    .get("""href""")
						)["""url"""][0]

			webbrowser.open(link)


