
"""simple docstring"""






from __future__ import annotations

import unittest

from transformers import EsmConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import numpy
					import tensorflow as tf

					from transformers.models.esm.modeling_tf_esm import (
					    TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TFEsmForMaskedLM,
					    TFEsmForSequenceClassification,
					    TFEsmForTokenClassification,
					    TFEsmModel,
					)

class 					__a     :



							'''simple docstring'''







							def __init__(     self  ,    _lowerCamelCase  ,    )			->							Optional[Any]:




										'''simple docstring'''
										__lowercase       =       parent
										__lowercase       =       13
										__lowercase       =       7
										__lowercase       =       True
										__lowercase       =       True
										__lowercase       =       True
										__lowercase       =       99
										__lowercase       =       32
										__lowercase       =       2
										__lowercase       =       4
										__lowercase       =       37
										__lowercase       =       "gelu"
										__lowercase       =       0.1
										__lowercase       =       0.1
										__lowercase       =       512
										__lowercase       =       16
										__lowercase       =       2
										__lowercase       =       0.02
										__lowercase       =       3
										__lowercase       =       4
										__lowercase       =       None
							def   SCREAMING_SNAKE_CASE						(     self       )			->							List[str]:




										'''simple docstring'''
										__lowercase       =       ids_tensor([self.batch_size, self.seq_length]  ,    self.vocab_size       )

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

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

										__lowercase       =       EsmConfig(
										    vocab_size=self.vocab_size  ,    hidden_size=self.hidden_size  ,    num_hidden_layers=self.num_hidden_layers  ,    pad_token_id=1  ,    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 config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Optional[Any]:




										'''simple docstring'''
										(
										    (
										    __lowercase
										)   ,					(
										    __lowercase
										)   ,					(
										    __lowercase
										)   ,					(
										    __lowercase
										)   ,					(
										    __lowercase
										)   ,					(
										    __lowercase
										)   ,					
										)       =       self.prepare_config_and_inputs()

										__lowercase       =       True
										__lowercase       =       floats_tensor([self.batch_size, self.seq_length, self.hidden_size]       )
										__lowercase       =       ids_tensor([self.batch_size, self.seq_length]  ,    vocab_size=2       )

										return (
										    config,
										    input_ids,
										    input_mask,
										    sequence_labels,
										    token_labels,
										    choice_labels,
										    encoder_hidden_states,
										    encoder_attention_mask,
										)
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase       )			->							Optional[Any]:




										'''simple docstring'''
										__lowercase       =       TFEsmModel(config=_lowerCamelCase       )
										__lowercase       =       {"input_ids": input_ids, "attention_mask": input_mask}
										__lowercase       =       model(_lowerCamelCase       )

										__lowercase       =       [input_ids, input_mask]
										__lowercase       =       model(_lowerCamelCase       )

										__lowercase       =       model(_lowerCamelCase       )

										self.parent.assertEqual(result.last_hidden_state.shape  ,    (self.batch_size, self.seq_length, self.hidden_size)       )
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    )			->							str:




										'''simple docstring'''
										__lowercase       =       True

										__lowercase       =       TFEsmModel(config=_lowerCamelCase       )
										__lowercase       =       {
										    "input_ids": input_ids,
										    "attention_mask": input_mask,
										    "encoder_hidden_states": encoder_hidden_states,
										    "encoder_attention_mask": encoder_attention_mask,
										}
										__lowercase       =       model(_lowerCamelCase       )

										__lowercase       =       [input_ids, input_mask]
										__lowercase       =       model(_lowerCamelCase  ,    encoder_hidden_states=_lowerCamelCase       )

										# Also check the case where encoder outputs are not passed
										__lowercase       =       model(_lowerCamelCase  ,    attention_mask=_lowerCamelCase       )

										self.parent.assertEqual(result.last_hidden_state.shape  ,    (self.batch_size, self.seq_length, self.hidden_size)       )
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase       )			->							Optional[Any]:




										'''simple docstring'''
										__lowercase       =       TFEsmForMaskedLM(config=_lowerCamelCase       )
										__lowercase       =       model([input_ids, input_mask]       )
										self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.seq_length, self.vocab_size)       )
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase       )			->							Optional[Any]:




										'''simple docstring'''
										__lowercase       =       self.num_labels
										__lowercase       =       TFEsmForTokenClassification(config=_lowerCamelCase       )
										__lowercase       =       {"input_ids": input_ids, "attention_mask": input_mask}
										__lowercase       =       model(_lowerCamelCase       )
										self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.seq_length, self.num_labels)       )





							def   SCREAMING_SNAKE_CASE						(     self       )			->							Optional[Any]:




										'''simple docstring'''
										__lowercase       =       self.prepare_config_and_inputs()
										(
										    (
										    __lowercase
										)   ,					(
										    __lowercase
										)   ,					(
										    __lowercase
										)   ,					(
										    __lowercase
										)   ,					(
										    __lowercase
										)   ,					(
										    __lowercase
										)   ,					
										)       =       config_and_inputs
										__lowercase       =       {"input_ids": input_ids, "attention_mask": input_mask}
										return config, inputs_dict





@require_tf
class 					__a     ( __a					,   __a					,   unittest.TestCase       ):



							'''simple docstring'''







							_lowerCamelCase					:			List[str]          =   (
							    (
							        TFEsmModel,
							        TFEsmForMaskedLM,
							        TFEsmForSequenceClassification,
							        TFEsmForTokenClassification,
							    )
							    if is_tf_available()
							    else ()
							)
							_lowerCamelCase					:			Optional[int]          =   (
							    {
							        """feature-extraction""": TFEsmModel,
							        """fill-mask""": TFEsmForMaskedLM,
							        """text-classification""": TFEsmForSequenceClassification,
							        """token-classification""": TFEsmForTokenClassification,
							        """zero-shot""": TFEsmForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							_lowerCamelCase					:			int          =   False
							_lowerCamelCase					:			Optional[int]          =   False
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Dict:




										'''simple docstring'''
										__lowercase       =       TFEsmModelTester(self       )
										__lowercase       =       ConfigTester(self  ,    config_class=_lowerCamelCase  ,    hidden_size=37       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							str:




										'''simple docstring'''
										self.config_tester.run_common_tests()
							def   SCREAMING_SNAKE_CASE						(     self       )			->							List[str]:




										'''simple docstring'''
										__lowercase       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_lowerCamelCase       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Optional[Any]:




										'''simple docstring'''
										__lowercase       =       self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_model_as_decoder(*_lowerCamelCase       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Any:




										'''simple docstring'''
										__lowercase       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*_lowerCamelCase       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							List[Any]:




										'''simple docstring'''
										__lowercase       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*_lowerCamelCase       )
							@slow
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Any:




										'''simple docstring'''
										for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__lowercase       =       TFEsmModel.from_pretrained(_lowerCamelCase       )
													self.assertIsNotNone(_lowerCamelCase       )
							@unittest.skip("Protein models do not support embedding resizing."       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Optional[Any]:




										'''simple docstring'''
										pass
							@unittest.skip("Protein models do not support embedding resizing."       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							int:




										'''simple docstring'''
										pass





							def   SCREAMING_SNAKE_CASE						(     self       )			->							Dict:




										'''simple docstring'''
										__lowercase   ,					__lowercase       =       self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													__lowercase       =       model_class(_lowerCamelCase       )
													assert isinstance(model.get_input_embeddings()  ,    tf.keras.layers.Layer       )
													if model_class is TFEsmForMaskedLM:
																# Output embedding test differs from the main test because they're a matrix, not a layer
																__lowercase       =       model.get_bias()
																assert isinstance(_lowerCamelCase  ,    _lowerCamelCase       )
																for k, v in name.items():
																			assert isinstance(_lowerCamelCase  ,    tf.Variable       )
													else:
																__lowercase       =       model.get_output_embeddings()
																assert x is None
																__lowercase       =       model.get_bias()
																assert name is None





@require_tf
class 					__a     ( unittest.TestCase       ):



							'''simple docstring'''







							@slow
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Union[str, Any]:




										'''simple docstring'''
										__lowercase       =       TFEsmForMaskedLM.from_pretrained("facebook/esm2_t6_8M_UR50D"       )

										__lowercase       =       tf.constant([[0, 1, 2, 3, 4, 5]]       )
										__lowercase       =       model(_lowerCamelCase       )[0]
										__lowercase       =       [1, 6, 33]
										self.assertEqual(list(output.numpy().shape       )  ,    _lowerCamelCase       )
										# compare the actual values for a slice.
										__lowercase       =       tf.constant(
										    [
										        [
										            [8.92_1518, -10.58_9814, -6.467_1307],
										            [-6.396_7156, -13.91_1377, -1.121_1915],
										            [-7.78_1247, -13.95_1557, -3.74_0592],
										        ]
										    ]       )
										self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()  ,    expected_slice.numpy()  ,    atol=1e-2       )       )





							@slow
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Optional[int]:




										'''simple docstring'''
										__lowercase       =       TFEsmModel.from_pretrained("facebook/esm2_t6_8M_UR50D"       )

										__lowercase       =       tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]       )
										__lowercase       =       model(_lowerCamelCase       )[0]
										# compare the actual values for a slice.
										__lowercase       =       tf.constant(
										    [
										        [
										            [0.1444_3092, 0.5412_5327, 0.324_7739],
										            [0.3034_0484, 0.0052_6676, 0.3107_7722],
										            [0.3227_8043, -0.2498_7096, 0.341_4628],
										        ]
										    ]       )
										self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()  ,    expected_slice.numpy()  ,    atol=1e-4       )       )







"""simple docstring"""






import json
import os
import re
import sys
import urllib.request

import requests
from bsa import BeautifulSoup

_lowercase							=				{
    '''User-Agent''': '''Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'''
    ''' (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582'''
}



def      lowerCAmelCase__			(		__magic_name__ = "dhaka"			,   __magic_name__ = 5      )						->int:
			__lowercase       =       min(__magic_name__			,   5_0      )  # Prevent abuse!
			__lowercase       =       {
			    "q": query,
			    "tbm": "isch",
			    "hl": "en",
			    "ijn": "0",
			}

			__lowercase       =       requests.get("https://www.google.com/search"			,   params=__magic_name__			,   headers=__magic_name__      )
			__lowercase       =       BeautifulSoup(html.text			,   "html.parser"      )
			__lowercase       =       "".join(
			    re.findall(R"AF_initDataCallback\(([^<]+)\);"			,   str(soup.select("script"      )      )      )      )

			__lowercase       =       json.dumps(__magic_name__      )
			__lowercase       =       json.loads(__magic_name__      )

			__lowercase       =       re.findall(
			    R"\[\"GRID_STATE0\",null,\[\[1,\[0,\".*?\",(.*),\"All\","			,   __magic_name__			,   )
			if not matched_google_image_data:
						return 0

			__lowercase       =       re.sub(
			    R"\[\"(https\:\/\/encrypted-tbn0\.gstatic\.com\/images\?.*?)\",\d+,\d+\]"			,   ""			,   str(__magic_name__      )			,   )

			__lowercase       =       re.findall(
			    R"(?:'|,),\[\"(https:|http.*?)\",\d+,\d+\]"			,   __magic_name__			,   )
			for index, fixed_full_res_image in enumerate(__magic_name__      ):
						if index >= max_images:
									return index
						__lowercase       =       bytes(__magic_name__			,   "ascii"      ).decode(
						    "unicode-escape"      )
						__lowercase       =       bytes(__magic_name__			,   "ascii"      ).decode(
						    "unicode-escape"      )
						__lowercase       =       urllib.request.build_opener()
						__lowercase       =       [
						    (
						        "User-Agent",
						        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
						        " (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582",
						    )
						]
						urllib.request.install_opener(__magic_name__      )
						__lowercase       =       F'''query_{query.replace(' '			,   '_'      )}'''
						if not os.path.exists(__magic_name__      ):
									os.makedirs(__magic_name__      )
						urllib.request.urlretrieve(  # noqa: S310
						    __magic_name__			,   F'''{path_name}/original_size_img_{index}.jpg'''      )
			return index


if __name__ == "__main__":
					try:
										_lowercase							=				download_images_from_google_query(sys.argv[1])
										print(F"{image_count} images were downloaded to disk.")
					except IndexError:
										print('''Please provide a search term.''')
										raise





