




import unittest

from transformers import BertGenerationConfig, 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, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import BertGenerationDecoder, BertGenerationEncoder
class 							__snake_case	:



				def __init__(   self   :		Tuple		,						_UpperCAmelCase   :		str		,						_UpperCAmelCase   :		int=13		,						_UpperCAmelCase   :		Tuple=7		,						_UpperCAmelCase   :		str=True		,						_UpperCAmelCase   :		Any=True		,						_UpperCAmelCase   :		Tuple=99		,						_UpperCAmelCase   :		Optional[int]=32		,						_UpperCAmelCase   :		List[Any]=5		,						_UpperCAmelCase   :		Optional[Any]=4		,						_UpperCAmelCase   :		str=37		,						_UpperCAmelCase   :		Optional[int]="gelu"		,						_UpperCAmelCase   :		Dict=0.1		,						_UpperCAmelCase   :		Any=0.1		,						_UpperCAmelCase   :		Optional[Any]=50		,						_UpperCAmelCase   :		Optional[int]=0.02		,						_UpperCAmelCase   :		Tuple=True		,						_UpperCAmelCase   :		int=None		,						)	->     List[str]:





								'''simple docstring'''




								_lowerCAmelCase     :     List[Any]              =   parent
								_lowerCAmelCase     :     Any              =   batch_size
								_lowerCAmelCase     :     List[str]              =   seq_length
								_lowerCAmelCase     :     Dict              =   is_training
								_lowerCAmelCase     :     List[Any]              =   use_input_mask
								_lowerCAmelCase     :     Union[str, Any]              =   vocab_size
								_lowerCAmelCase     :     Tuple              =   hidden_size
								_lowerCAmelCase     :     Optional[Any]              =   num_hidden_layers
								_lowerCAmelCase     :     Union[str, Any]              =   num_attention_heads
								_lowerCAmelCase     :     int              =   intermediate_size
								_lowerCAmelCase     :     Any              =   hidden_act
								_lowerCAmelCase     :     List[str]              =   hidden_dropout_prob
								_lowerCAmelCase     :     Optional[Any]              =   attention_probs_dropout_prob
								_lowerCAmelCase     :     str              =   max_position_embeddings
								_lowerCAmelCase     :     Optional[Any]              =   initializer_range
								_lowerCAmelCase     :     str              =   use_labels
								_lowerCAmelCase     :     Union[str, Any]              =   scope



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





								'''simple docstring'''




								_lowerCAmelCase     :     Tuple              =   ids_tensor([self.batch_size, self.seq_length]		,						self.vocab_size   )

								_lowerCAmelCase     :     List[Any]              =   None
								if self.use_input_mask:
												_lowerCAmelCase     :     List[Any]              =   random_attention_mask([self.batch_size, self.seq_length]   )

								if self.use_labels:
												_lowerCAmelCase     :     Optional[Any]              =   ids_tensor([self.batch_size, self.seq_length]		,						self.vocab_size   )

								_lowerCAmelCase     :     Optional[Any]              =   self.get_config()

								return config, input_ids, input_mask, token_labels



				def    SCREAMING_SNAKE_CASE							(   self   :		str   )	->     int:





								'''simple docstring'''




								return BertGenerationConfig(
								    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		,						is_decoder=lowercase__		,						initializer_range=self.initializer_range		,						)



				def    SCREAMING_SNAKE_CASE							(   self   :		List[str]   )	->     int:





								'''simple docstring'''




								(
								    _lowerCAmelCase      
								)     :     List[str]              =   self.prepare_config_and_inputs()

								_lowerCAmelCase     :     Any              =   True
								_lowerCAmelCase     :     Dict              =   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
								_lowerCAmelCase     :     Optional[int]              =   ids_tensor([self.batch_size, self.seq_length]		,						vocab_size=2   )

								return (
								    config,
								    input_ids,
								    input_mask,
								    token_labels,
								    encoder_hidden_states,
								    encoder_attention_mask,
								)



				def    SCREAMING_SNAKE_CASE							(   self   :		List[str]		,						_UpperCAmelCase   :		Tuple		,						_UpperCAmelCase   :		Optional[int]		,						_UpperCAmelCase   :		Tuple		,						_UpperCAmelCase   :		str		,						**_UpperCAmelCase   :		List[str]		,						)	->     List[Any]:





								'''simple docstring'''




								_lowerCAmelCase     :     List[Any]              =   BertGenerationEncoder(config=lowercase__   )
								model.to(lowercase__   )
								model.eval()
								_lowerCAmelCase     :     List[str]              =   model(lowercase__		,						attention_mask=lowercase__   )
								_lowerCAmelCase     :     Union[str, Any]              =   model(lowercase__   )
								self.parent.assertEqual(result.last_hidden_state.shape		,						(self.batch_size, self.seq_length, self.hidden_size)   )



				def    SCREAMING_SNAKE_CASE							(   self   :		int		,						_UpperCAmelCase   :		List[str]		,						_UpperCAmelCase   :		Tuple		,						_UpperCAmelCase   :		List[Any]		,						_UpperCAmelCase   :		int		,						_UpperCAmelCase   :		str		,						_UpperCAmelCase   :		List[Any]		,						**_UpperCAmelCase   :		Union[str, Any]		,						)	->     Optional[int]:





								'''simple docstring'''




								_lowerCAmelCase     :     Union[str, Any]              =   True
								_lowerCAmelCase     :     Dict              =   BertGenerationEncoder(config=lowercase__   )
								model.to(lowercase__   )
								model.eval()
								_lowerCAmelCase     :     List[Any]              =   model(
								    lowercase__		,						attention_mask=lowercase__		,						encoder_hidden_states=lowercase__		,						encoder_attention_mask=lowercase__		,						)
								_lowerCAmelCase     :     Tuple              =   model(
								    lowercase__		,						attention_mask=lowercase__		,						encoder_hidden_states=lowercase__		,						)
								self.parent.assertEqual(result.last_hidden_state.shape		,						(self.batch_size, self.seq_length, self.hidden_size)   )



				def    SCREAMING_SNAKE_CASE							(   self   :		List[str]		,						_UpperCAmelCase   :		int		,						_UpperCAmelCase   :		List[Any]		,						_UpperCAmelCase   :		Union[str, Any]		,						_UpperCAmelCase   :		str		,						_UpperCAmelCase   :		List[Any]		,						_UpperCAmelCase   :		List[Any]		,						**_UpperCAmelCase   :		List[Any]		,						)	->     Optional[Any]:





								'''simple docstring'''




								_lowerCAmelCase     :     Any              =   True
								_lowerCAmelCase     :     Dict              =   True
								_lowerCAmelCase     :     Optional[Any]              =   BertGenerationDecoder(config=lowercase__   ).to(lowercase__   ).eval()

								# first forward pass
								_lowerCAmelCase     :     Optional[int]              =   model(
								    lowercase__		,						attention_mask=lowercase__		,						encoder_hidden_states=lowercase__		,						encoder_attention_mask=lowercase__		,						use_cache=lowercase__		,						)
								_lowerCAmelCase     :     List[str]              =   outputs.past_key_values

								# create hypothetical multiple next token and extent to next_input_ids
								_lowerCAmelCase     :     Optional[int]              =   ids_tensor((self.batch_size, 3)		,						config.vocab_size   )
								_lowerCAmelCase     :     Any              =   ids_tensor((self.batch_size, 3)		,						vocab_size=2   )

								# append to next input_ids and
								_lowerCAmelCase     :     str              =   torch.cat([input_ids, next_tokens]		,						dim=-1   )
								_lowerCAmelCase     :     List[Any]              =   torch.cat([input_mask, next_mask]		,						dim=-1   )

								_lowerCAmelCase     :     Optional[Any]              =   model(
								    lowercase__		,						attention_mask=lowercase__		,						encoder_hidden_states=lowercase__		,						encoder_attention_mask=lowercase__		,						output_hidden_states=lowercase__		,						)['''hidden_states'''][0]
								_lowerCAmelCase     :     Union[str, Any]              =   model(
								    lowercase__		,						attention_mask=lowercase__		,						encoder_hidden_states=lowercase__		,						encoder_attention_mask=lowercase__		,						past_key_values=lowercase__		,						output_hidden_states=lowercase__		,						)['''hidden_states'''][0]

								# select random slice
								_lowerCAmelCase     :     str              =   ids_tensor((1,)		,						output_from_past.shape[-1]   ).item()
								_lowerCAmelCase     :     List[str]              =   output_from_no_past[:, -3:, random_slice_idx].detach()
								_lowerCAmelCase     :     Any              =   output_from_past[:, :, random_slice_idx].detach()

								self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]   )

								# test that outputs are equal for slice
								self.parent.assertTrue(torch.allclose(lowercase__		,						lowercase__		,						atol=1E-3   )   )



				def    SCREAMING_SNAKE_CASE							(   self   :		Optional[Any]		,						_UpperCAmelCase   :		List[str]		,						_UpperCAmelCase   :		List[str]		,						_UpperCAmelCase   :		Optional[int]		,						_UpperCAmelCase   :		List[Any]		,						*_UpperCAmelCase   :		Union[str, Any]		,						)	->     Tuple:





								'''simple docstring'''




								_lowerCAmelCase     :     Optional[Any]              =   BertGenerationDecoder(lowercase__   )
								model.to(lowercase__   )
								model.eval()
								_lowerCAmelCase     :     List[str]              =   model(lowercase__		,						attention_mask=lowercase__		,						labels=lowercase__   )
								self.parent.assertEqual(result.logits.shape		,						(self.batch_size, self.seq_length, self.vocab_size)   )



				def    SCREAMING_SNAKE_CASE							(   self   :		str   )	->     Dict:





								'''simple docstring'''




								_lowerCAmelCase     :     str              =   self.prepare_config_and_inputs()
								_lowerCAmelCase     :     Any              =   {'''input_ids''': input_ids, '''attention_mask''': input_mask}
								return config, inputs_dict






@require_torch
class 							__snake_case	(_UpperCamelCase    ,						_UpperCamelCase    ,						_UpperCamelCase    ,						unittest.TestCase		):
				lowerCAmelCase__      						=  (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
				lowerCAmelCase__      						=  (BertGenerationDecoder,) if is_torch_available() else ()
				lowerCAmelCase__      						=  (
				    {'''feature-extraction''': BertGenerationEncoder, '''text-generation''': BertGenerationDecoder}
				    if is_torch_available()
				    else {}
				)



				def    SCREAMING_SNAKE_CASE							(   self   :		Dict   )	->     Tuple:





								'''simple docstring'''




								_lowerCAmelCase     :     Union[str, Any]              =   BertGenerationEncoderTester(self   )
								_lowerCAmelCase     :     Optional[int]              =   ConfigTester(self		,						config_class=lowercase__		,						hidden_size=37   )



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





								'''simple docstring'''




								self.config_tester.run_common_tests()



				def    SCREAMING_SNAKE_CASE							(   self   :		str   )	->     Dict:





								'''simple docstring'''




								_lowerCAmelCase     :     List[Any]              =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowercase__   )



				def    SCREAMING_SNAKE_CASE							(   self   :		Tuple   )	->     Dict:





								'''simple docstring'''




								_lowerCAmelCase     :     Dict              =   self.model_tester.prepare_config_and_inputs()
								_lowerCAmelCase     :     int              =   '''bert'''
								self.model_tester.create_and_check_model(lowercase__		,						lowercase__		,						lowercase__		,						lowercase__   )



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





								'''simple docstring'''




								_lowerCAmelCase     :     Tuple              =   self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_model_as_decoder(*lowercase__   )



				def    SCREAMING_SNAKE_CASE							(   self   :		Any   )	->     Dict:





								'''simple docstring'''




								_lowerCAmelCase     :     int              =   self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_decoder_model_past_large_inputs(*lowercase__   )



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





								'''simple docstring'''




								(
								    _lowerCAmelCase      
								)     :     Tuple              =   self.model_tester.prepare_config_and_inputs_for_decoder()

								_lowerCAmelCase     :     int              =   None

								self.model_tester.create_and_check_model_as_decoder(
								    lowercase__		,						lowercase__		,						lowercase__		,						lowercase__		,						lowercase__		,						lowercase__		,						)



				def    SCREAMING_SNAKE_CASE							(   self   :		Tuple   )	->     Any:





								'''simple docstring'''




								_lowerCAmelCase     :     Optional[Any]              =   self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_for_causal_lm(*lowercase__   )



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





								'''simple docstring'''




								_lowerCAmelCase     :     str              =   BertGenerationEncoder.from_pretrained("""google/bert_for_seq_generation_L-24_bbc_encoder"""   )
								self.assertIsNotNone(lowercase__   )






@require_torch
class 							__snake_case	(unittest.TestCase		):



				@slow
				def    SCREAMING_SNAKE_CASE							(   self   :		Any   )	->     Any:





								'''simple docstring'''




								_lowerCAmelCase     :     List[str]              =   BertGenerationEncoder.from_pretrained("""google/bert_for_seq_generation_L-24_bbc_encoder"""   )
								_lowerCAmelCase     :     Dict              =   torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 1_0140, 102]]   )
								with torch.no_grad():
												_lowerCAmelCase     :     Any              =   model(lowercase__   )[0]
								_lowerCAmelCase     :     str              =   torch.Size([1, 8, 1024]   )
								self.assertEqual(output.shape		,						lowercase__   )
								_lowerCAmelCase     :     List[Any]              =   torch.tensor(
								    [[[0.1775, 0.0083, -0.0321], [1.6002, 0.1287, 0.3912], [2.1473, 0.5791, 0.6066]]]   )
								self.assertTrue(torch.allclose(output[:, :3, :3]		,						lowercase__		,						atol=1E-4   )   )






@require_torch
class 							__snake_case	(unittest.TestCase		):



				@slow
				def    SCREAMING_SNAKE_CASE							(   self   :		List[str]   )	->     Dict:





								'''simple docstring'''




								_lowerCAmelCase     :     Union[str, Any]              =   BertGenerationDecoder.from_pretrained("""google/bert_for_seq_generation_L-24_bbc_encoder"""   )
								_lowerCAmelCase     :     Any              =   torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 1_0140, 102]]   )
								with torch.no_grad():
												_lowerCAmelCase     :     int              =   model(lowercase__   )[0]
								_lowerCAmelCase     :     Optional[Any]              =   torch.Size([1, 8, 5_0358]   )
								self.assertEqual(output.shape		,						lowercase__   )
								_lowerCAmelCase     :     Tuple              =   torch.tensor(
								    [[[-0.5788, -2.5994, -3.7054], [0.0438, 4.7997, 1.8795], [1.5862, 6.6409, 4.4638]]]   )
								self.assertTrue(torch.allclose(output[:, :3, :3]		,						lowercase__		,						atol=1E-4   )   )





from __future__ import annotations

import requests

lowerCAmelCase       		=    set(
    """approved_at_utc approved_by author_flair_background_color
author_flair_css_class author_flair_richtext author_flair_template_id author_fullname
author_premium can_mod_post category clicked content_categories created_utc downs
edited gilded gildings hidden hide_score is_created_from_ads_ui is_meta
is_original_content is_reddit_media_domain is_video link_flair_css_class
link_flair_richtext link_flair_text link_flair_text_color media_embed mod_reason_title
name permalink pwls quarantine saved score secure_media secure_media_embed selftext
subreddit subreddit_name_prefixed subreddit_type thumbnail title top_awarded_type
total_awards_received ups upvote_ratio url user_reports""".split()
)




def     __SCREAMING_SNAKE_CASE							(       lowercase_      ,       lowercase_ = 1      ,       lowercase_ = "new"      ,       lowercase_ = None      )		->     dict:




						'''simple docstring'''







						__UpperCAmelCase			:							Tuple	    =	wanted_data or []
						if invalid_search_terms := ", ".join(sorted(set(lowercase_      ) - valid_terms      )      ):
												__UpperCAmelCase			:							List[Any]	    =	f"Invalid search term: {invalid_search_terms}"
												raise ValueError(lowercase_      )
						__UpperCAmelCase			:							Optional[int]	    =	requests.get(
						    f"https://reddit.com/r/{subreddit}/{age}.json?limit={limit}"      ,       headers={'''User-agent''': '''A random string'''}      ,       )
						if response.status_code == 429:
												raise requests.HTTPError

						__UpperCAmelCase			:							List[str]	    =	response.json()
						if not wanted_data:
												return {id_: data["data"]["children"][id_] for id_ in range(lowercase_      )}

						__UpperCAmelCase			:							List[Any]	    =	{}
						for id_ in range(lowercase_      ):
												__UpperCAmelCase			:							str	    =	{
												    item: data['''data''']['''children'''][id_]['''data'''][item] for item in wanted_data
												}
						return data_dict


if __name__ == "__main__":
				# If you get Error 429, that means you are rate limited.Try after some time
				print(get_subreddit_data("""learnpython""", wanted_data=["""title""", """url""", """selftext"""]))

