

'''simple docstring'''






import tempfile
import unittest

import numpy as np

import transformers
from transformers import GPTaConfig, GPTaTokenizer, is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
      import jax
      import jax.numpy as jnp

      from transformers.modeling_flax_pytorch_utils import (
          convert_pytorch_state_dict_to_flax,
          load_flax_weights_in_pytorch_model,
      )
      from transformers.models.gpta.modeling_flax_gpta import FlaxGPTaLMHeadModel, FlaxGPTaModel

if is_torch_available():
      import torch
class      SCREAMING_SNAKE_CASE__      :




    def __init__( self    ,					lowercase__    ,					lowercase__=14    ,					lowercase__=7    ,					lowercase__=True    ,					lowercase__=True    ,					lowercase__=False    ,					lowercase__=True    ,					lowercase__=99    ,					lowercase__=32    ,					lowercase__=5    ,					lowercase__=4    ,					lowercase__=37    ,					lowercase__="gelu"    ,					lowercase__=0.1    ,					lowercase__=0.1    ,					lowercase__=512    ,					lowercase__=0.02    ,					):






          """simple docstring"""






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




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	ids_tensor([self.batch_size, self.seq_length]    ,					self.vocab_size      )

          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	None
          if self.use_input_mask:
                SCREAMING_SNAKE_CASE_				:      Tuple					=	random_attention_mask([self.batch_size, self.seq_length]      )

          SCREAMING_SNAKE_CASE_				:      Any					=	GPTaConfig(
              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    ,					use_cache=lowercase__    ,					bos_token_id=self.bos_token_id    ,					eos_token_id=self.eos_token_id    ,					pad_token_id=self.pad_token_id    ,					)

          return (config, input_ids, input_mask)




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_				:      int					=	self.prepare_config_and_inputs()
          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      Tuple					=	config_and_inputs
          SCREAMING_SNAKE_CASE_				:      Tuple					=	{"input_ids": input_ids, "attention_mask": attention_mask}
          return config, inputs_dict




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	self.prepare_config_and_inputs()

          SCREAMING_SNAKE_CASE_				:      str					=	floats_tensor([self.batch_size, self.seq_length, self.hidden_size]      )
          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	ids_tensor([self.batch_size, self.seq_length]    ,					vocab_size=2      )

          return (
              config,
              input_ids,
              attention_mask,
              encoder_hidden_states,
              encoder_attention_mask,
          )




    def   __lowerCamelCase				( self    ,					lowercase__    ,					lowercase__    ,					lowercase__    ,					lowercase__      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_				:      Dict					=	20
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	model_class_name(lowercase__      )

          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	model.init_cache(input_ids.shape[0]    ,					lowercase__      )
          SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	jnp.ones((input_ids.shape[0], max_decoder_length)    ,					dtype="i4"      )

          SCREAMING_SNAKE_CASE_				:      int					=	jnp.broadcast_to(
              jnp.arange(input_ids.shape[-1] - 1      )[None, :]    ,					(input_ids.shape[0], input_ids.shape[-1] - 1)      )
          SCREAMING_SNAKE_CASE_				:      List[Any]					=	model(
              input_ids[:, :-1]    ,					attention_mask=lowercase__    ,					past_key_values=lowercase__    ,					position_ids=lowercase__    ,					)

          SCREAMING_SNAKE_CASE_				:      Tuple					=	jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]    ,					dtype="i4"      )
          SCREAMING_SNAKE_CASE_				:      Dict					=	model(
              input_ids[:, -1:]    ,					attention_mask=lowercase__    ,					past_key_values=outputs_cache.past_key_values    ,					position_ids=lowercase__    ,					)

          SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	model(lowercase__      )

          SCREAMING_SNAKE_CASE_				:      Dict					=	np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])      )      )
          self.parent.assertTrue(diff < 1e-3    ,					msg=F"Max diff is {diff}"      )







    def   __lowerCamelCase				( self    ,					lowercase__    ,					lowercase__    ,					lowercase__    ,					lowercase__      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_				:      int					=	20
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	model_class_name(lowercase__      )

          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	jnp.concatenate(
              [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1])      )]    ,					axis=-1    ,					)

          SCREAMING_SNAKE_CASE_				:      str					=	model.init_cache(input_ids.shape[0]    ,					lowercase__      )
          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	jnp.broadcast_to(
              jnp.arange(input_ids.shape[-1] - 1      )[None, :]    ,					(input_ids.shape[0], input_ids.shape[-1] - 1)      )

          SCREAMING_SNAKE_CASE_				:      int					=	model(
              input_ids[:, :-1]    ,					attention_mask=lowercase__    ,					past_key_values=lowercase__    ,					position_ids=lowercase__    ,					)
          SCREAMING_SNAKE_CASE_				:      str					=	jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]    ,					dtype="i4"      )
          SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	model(
              input_ids[:, -1:]    ,					past_key_values=outputs_cache.past_key_values    ,					attention_mask=lowercase__    ,					position_ids=lowercase__    ,					)

          SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	model(lowercase__    ,					attention_mask=lowercase__      )

          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])      )      )
          self.parent.assertTrue(diff < 1e-3    ,					msg=F"Max diff is {diff}"      )






@require_flax
class      SCREAMING_SNAKE_CASE__      (    _UpperCAmelCase,_UpperCAmelCase,unittest.TestCase				):
    _A         =							(FlaxGPTaModel, FlaxGPTaLMHeadModel) if is_flax_available() else ()
    _A         =							(FlaxGPTaLMHeadModel,) if is_flax_available() else ()




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_				:      Dict					=	FlaxGPTaModelTester(self      )




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          for model_class_name in self.all_model_classes:
                SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      Any					=	self.model_tester.prepare_config_and_inputs()
                self.model_tester.check_use_cache_forward(lowercase__    ,					lowercase__    ,					lowercase__    ,					lowercase__      )




    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          for model_class_name in self.all_model_classes:
                SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	self.model_tester.prepare_config_and_inputs()
                self.model_tester.check_use_cache_forward_with_attn_mask(
                    lowercase__    ,					lowercase__    ,					lowercase__    ,					lowercase__      )




    @slow
    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_				:      int					=	GPTaTokenizer.from_pretrained("gpt2"    ,					pad_token="</s>"    ,					padding_side="left"      )
          SCREAMING_SNAKE_CASE_				:      Tuple					=	tokenizer(["Hello this is a long string", "Hey"]    ,					return_tensors="np"    ,					padding=lowercase__    ,					truncation=lowercase__      )

          SCREAMING_SNAKE_CASE_				:      str					=	FlaxGPTaLMHeadModel.from_pretrained("gpt2"      )
          SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	False
          SCREAMING_SNAKE_CASE_				:      str					=	model.config.eos_token_id

          SCREAMING_SNAKE_CASE_				:      List[str]					=	jax.jit(model.generate      )

          SCREAMING_SNAKE_CASE_				:      Optional[int]					=	jit_generate(inputs["input_ids"]    ,					attention_mask=inputs["attention_mask"]      ).sequences

          SCREAMING_SNAKE_CASE_				:      Tuple					=	tokenizer.batch_decode(lowercase__    ,					skip_special_tokens=lowercase__      )

          SCREAMING_SNAKE_CASE_				:      Tuple					=	[
              "Hello this is a long string of words. I'm going to start with the first one.\n",
              "Hey, I'm not sure if I'm going to be able to do",
          ]

          self.assertListEqual(lowercase__    ,					lowercase__      )




    @is_pt_flax_cross_test
    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      Tuple					=	self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                with self.subTest(model_class.__name__      ):
                      # prepare inputs
                      SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	self._prepare_for_class(lowercase__    ,					lowercase__      )
                      SCREAMING_SNAKE_CASE_				:      List[str]					=	{k: torch.tensor(v.tolist()      ) for k, v in prepared_inputs_dict.items()}

                      # load corresponding PyTorch class
                      SCREAMING_SNAKE_CASE_				:      int					=	model_class.__name__[4:]  # Skip the "Flax" at the beginning
                      SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	getattr(lowercase__    ,					lowercase__      )

                      SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      int					=	pt_inputs["input_ids"].shape
                      SCREAMING_SNAKE_CASE_				:      Optional[int]					=	np.random.randint(0    ,					seq_length - 1    ,					size=(batch_size,)      )
                      for batch_idx, start_index in enumerate(lowercase__      ):
                            SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	0
                            SCREAMING_SNAKE_CASE_				:      Any					=	1
                            SCREAMING_SNAKE_CASE_				:      str					=	0
                            SCREAMING_SNAKE_CASE_				:      int					=	1
                      SCREAMING_SNAKE_CASE_				:      List[str]					=	pt_model_class(lowercase__      ).eval()
                      SCREAMING_SNAKE_CASE_				:      Tuple					=	model_class(lowercase__    ,					dtype=jnp.floataa      )

                      SCREAMING_SNAKE_CASE_				:      Tuple					=	convert_pytorch_state_dict_to_flax(pt_model.state_dict()    ,					lowercase__      )
                      SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	fx_state

                      with torch.no_grad():
                            SCREAMING_SNAKE_CASE_				:      int					=	pt_model(**lowercase__      ).to_tuple()

                      SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	fx_model(**lowercase__      ).to_tuple()
                      self.assertEqual(len(lowercase__      )    ,					len(lowercase__      )    ,					"Output lengths differ between Flax and PyTorch"      )
                      for fx_output, pt_output in zip(lowercase__    ,					lowercase__      ):
                            self.assert_almost_equals(fx_output[:, -1]    ,					pt_output[:, -1].numpy()    ,					4e-2      )

                      with tempfile.TemporaryDirectory() as tmpdirname:
                            pt_model.save_pretrained(lowercase__      )
                            SCREAMING_SNAKE_CASE_				:      Tuple					=	model_class.from_pretrained(lowercase__    ,					from_pt=lowercase__      )

                      SCREAMING_SNAKE_CASE_				:      List[str]					=	fx_model_loaded(**lowercase__      ).to_tuple()
                      self.assertEqual(
                          len(lowercase__      )    ,					len(lowercase__      )    ,					"Output lengths differ between Flax and PyTorch"      )
                      for fx_output_loaded, pt_output in zip(lowercase__    ,					lowercase__      ):
                            self.assert_almost_equals(fx_output_loaded[:, -1]    ,					pt_output[:, -1].numpy()    ,					4e-2      )




    @is_pt_flax_cross_test
    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      str					=	self.model_tester.prepare_config_and_inputs_for_common()
          for model_class in self.all_model_classes:
                with self.subTest(model_class.__name__      ):
                      # prepare inputs
                      SCREAMING_SNAKE_CASE_				:      List[Any]					=	self._prepare_for_class(lowercase__    ,					lowercase__      )
                      SCREAMING_SNAKE_CASE_				:      List[Any]					=	{k: torch.tensor(v.tolist()      ) for k, v in prepared_inputs_dict.items()}

                      # load corresponding PyTorch class
                      SCREAMING_SNAKE_CASE_				:      List[Any]					=	model_class.__name__[4:]  # Skip the "Flax" at the beginning
                      SCREAMING_SNAKE_CASE_				:      Any					=	getattr(lowercase__    ,					lowercase__      )

                      SCREAMING_SNAKE_CASE_				:      int					=	pt_model_class(lowercase__      ).eval()
                      SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	model_class(lowercase__    ,					dtype=jnp.floataa      )

                      SCREAMING_SNAKE_CASE_				:      Tuple					=	load_flax_weights_in_pytorch_model(lowercase__    ,					fx_model.params      )
                      SCREAMING_SNAKE_CASE_,						SCREAMING_SNAKE_CASE_				:      Optional[Any]					=	pt_inputs["input_ids"].shape
                      SCREAMING_SNAKE_CASE_				:      List[str]					=	np.random.randint(0    ,					seq_length - 1    ,					size=(batch_size,)      )
                      for batch_idx, start_index in enumerate(lowercase__      ):
                            SCREAMING_SNAKE_CASE_				:      List[Any]					=	0
                            SCREAMING_SNAKE_CASE_				:      Optional[int]					=	1
                            SCREAMING_SNAKE_CASE_				:      str					=	0
                            SCREAMING_SNAKE_CASE_				:      str					=	1

                      # make sure weights are tied in PyTorch
                      pt_model.tie_weights()

                      with torch.no_grad():
                            SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	pt_model(**lowercase__      ).to_tuple()

                      SCREAMING_SNAKE_CASE_				:      List[str]					=	fx_model(**lowercase__      ).to_tuple()
                      self.assertEqual(len(lowercase__      )    ,					len(lowercase__      )    ,					"Output lengths differ between Flax and PyTorch"      )
                      for fx_output, pt_output in zip(lowercase__    ,					lowercase__      ):
                            self.assert_almost_equals(fx_output[:, -1]    ,					pt_output[:, -1].numpy()    ,					4e-2      )

                      with tempfile.TemporaryDirectory() as tmpdirname:
                            fx_model.save_pretrained(lowercase__      )
                            SCREAMING_SNAKE_CASE_				:      str					=	pt_model_class.from_pretrained(lowercase__    ,					from_flax=lowercase__      )

                      with torch.no_grad():
                            SCREAMING_SNAKE_CASE_				:      int					=	pt_model_loaded(**lowercase__      ).to_tuple()

                      self.assertEqual(
                          len(lowercase__      )    ,					len(lowercase__      )    ,					"Output lengths differ between Flax and PyTorch"      )
                      for fx_output, pt_output in zip(lowercase__    ,					lowercase__      ):
                            self.assert_almost_equals(fx_output[:, -1]    ,					pt_output[:, -1].numpy()    ,					4e-2      )







    @slow
    def   __lowerCamelCase				( self      ):






          """simple docstring"""






          for model_class_name in self.all_model_classes:
                SCREAMING_SNAKE_CASE_				:      Union[str, Any]					=	model_class_name.from_pretrained("gpt2"    ,					from_pt=lowercase__      )
                SCREAMING_SNAKE_CASE_				:      Any					=	model(np.ones((1, 1)      )      )
                self.assertIsNotNone(lowercase__      )




'''simple docstring'''






import requests
from bsa import BeautifulSoup


def  __lowerCamelCase      (				SCREAMING_SNAKE_CASE_					:     int  ,  SCREAMING_SNAKE_CASE_					:     str   )							->       str:
      """simple docstring"""

      SCREAMING_SNAKE_CASE_				:      List[str]					=	(
          "https://www.horoscope.com/us/horoscopes/general/"
          F"horoscope-general-daily-{day}.aspx?sign={zodiac_sign}"
      )
      SCREAMING_SNAKE_CASE_				:      Dict					=	BeautifulSoup(requests.get(SCREAMING_SNAKE_CASE_   ).content  ,  "html.parser"   )
      return soup.find("div"  ,  class_="main-horoscope"   ).p.text


if __name__ == "__main__":
      print('Daily Horoscope. \n')
      print(
          'enter your Zodiac sign number:\n',
          '1. Aries\n',
          '2. Taurus\n',
          '3. Gemini\n',
          '4. Cancer\n',
          '5. Leo\n',
          '6. Virgo\n',
          '7. Libra\n',
          '8. Scorpio\n',
          '9. Sagittarius\n',
          '10. Capricorn\n',
          '11. Aquarius\n',
          '12. Pisces\n',
      )
      snake_case_					  =	int(input('number> ').strip())
      print('choose some day:\n', 'yesterday\n', 'today\n', 'tomorrow\n')
      snake_case_					  =	input('enter the day> ')
      snake_case_					  =	horoscope(zodiac_sign, day)
      print(horoscope_text)

