





from typing import Optional

import numpy as np
import torch
from torch import nn
from transformers import GPTaConfig, GPTaLMHeadModel
from transformers.modeling_utils import ModuleUtilsMixin

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin



class    _SCREAMING_SNAKE_CASE						(	__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ):







 '''simple docstring'''
 lowercase_		=			[R"h\.\d+\.attn\.bias", R"h\.\d+\.attn\.masked_bias"]

 @register_to_config
 def __init__(self				:   str       , UpperCAmelCase_				:   int       , UpperCAmelCase_				:   int       , UpperCAmelCase_				:   Optional[int] = None       , UpperCAmelCase_				:   int = 50_257       , UpperCAmelCase_				:   int = 1_024       , UpperCAmelCase_				:   int = 768       , UpperCAmelCase_				:   int = 12       , UpperCAmelCase_				:   int = 12       , UpperCAmelCase_				:   Optional[int] = None       , UpperCAmelCase_				:   str = "gelu_new"       , UpperCAmelCase_				:   float = 0.1       , UpperCAmelCase_				:   float = 0.1       , UpperCAmelCase_				:   float = 0.1       , UpperCAmelCase_				:   float = 1E-5       , UpperCAmelCase_				:   float = 0.02       , UpperCAmelCase_				:   bool = True       , UpperCAmelCase_				:   bool = True       , UpperCAmelCase_				:   bool = False       , UpperCAmelCase_				:   bool = False       , )       ->str:



   '''simple docstring'''



   super().__init__()

   lowerCamelCase__:      List[str]			  =prefix_length

   if prefix_inner_dim != n_embd and prefix_hidden_dim is None:
     raise ValueError(
         F"""`prefix_hidden_dim` cannot be `None` when `prefix_inner_dim`: {prefix_hidden_dim} and"""
         F""" `n_embd`: {n_embd} are not equal.""")

   lowerCamelCase__:      List[str]			  =prefix_inner_dim
   lowerCamelCase__:      Union[str, Any]			  =prefix_hidden_dim

   lowerCamelCase__:      Tuple			  =(
       nn.Linear(self.prefix_inner_dim       , self.prefix_hidden_dim)
       if self.prefix_hidden_dim is not None
       else nn.Identity()
   )
   lowerCamelCase__:      Any			  =(
       nn.Linear(self.prefix_hidden_dim       , UpperCAmelCase_) if self.prefix_hidden_dim is not None else nn.Identity()
   )

   lowerCamelCase__:      Dict			  =GPTaConfig(
       vocab_size=UpperCAmelCase_       , n_positions=UpperCAmelCase_       , n_embd=UpperCAmelCase_       , n_layer=UpperCAmelCase_       , n_head=UpperCAmelCase_       , n_inner=UpperCAmelCase_       , activation_function=UpperCAmelCase_       , resid_pdrop=UpperCAmelCase_       , embd_pdrop=UpperCAmelCase_       , attn_pdrop=UpperCAmelCase_       , layer_norm_epsilon=UpperCAmelCase_       , initializer_range=UpperCAmelCase_       , scale_attn_weights=UpperCAmelCase_       , use_cache=UpperCAmelCase_       , scale_attn_by_inverse_layer_idx=UpperCAmelCase_       , reorder_and_upcast_attn=UpperCAmelCase_       , )
   lowerCamelCase__:      int			  =GPTaLMHeadModel(UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_					(self				:   Dict       , UpperCAmelCase_				:   torch.Tensor       , UpperCAmelCase_				:   torch.Tensor       , UpperCAmelCase_				:   Optional[torch.Tensor] = None       , UpperCAmelCase_				:   Optional[torch.Tensor] = None       , )       ->Optional[Any]:



   '''simple docstring'''



   lowerCamelCase__:      str			  =self.transformer.transformer.wte(UpperCAmelCase_)
   lowerCamelCase__:      List[str]			  =self.encode_prefix(UpperCAmelCase_)
   lowerCamelCase__:      Optional[int]			  =self.decode_prefix(UpperCAmelCase_)
   lowerCamelCase__:      List[str]			  =torch.cat((prefix_embeds, embedding_text)       , dim=1)

   if labels is not None:
     lowerCamelCase__:      List[Any]			  =self.get_dummy_token(input_ids.shape[0]       , input_ids.device)
     lowerCamelCase__:      List[Any]			  =torch.cat((dummy_token, input_ids)       , dim=1)
   lowerCamelCase__:      Tuple			  =self.transformer(inputs_embeds=UpperCAmelCase_       , labels=UpperCAmelCase_       , attention_mask=UpperCAmelCase_)
   if self.prefix_hidden_dim is not None:
     return out, hidden
   else:
     return out

 def 							SCREAMING_SNAKE_CASE_					(self				:   Optional[int]       , UpperCAmelCase_				:   int       , UpperCAmelCase_				:   torch.device)       ->torch.Tensor:



   '''simple docstring'''



   return torch.zeros(UpperCAmelCase_       , self.prefix_length       , dtype=torch.intaa       , device=UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_					(self				:   str       , UpperCAmelCase_				:   Any)       ->Any:



   '''simple docstring'''



   return self.encode_prefix(UpperCAmelCase_)

 @torch.no_grad()
 def 							SCREAMING_SNAKE_CASE_					(self				:   int       , UpperCAmelCase_				:   Any       , UpperCAmelCase_				:   str       , UpperCAmelCase_				:   Union[str, Any])       ->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:      Optional[Any]			  =torch.split(UpperCAmelCase_       , 1       , dim=0)
   lowerCamelCase__:      Dict			  =[]
   lowerCamelCase__:      List[Any]			  =[]
   for feature in features:
     lowerCamelCase__:      Tuple			  =self.decode_prefix(feature.to(UpperCAmelCase_))  # back to the clip feature
     # Only support beam search for now
     lowerCamelCase__  ,					lowerCamelCase__:      Tuple			  =self.generate_beam(
         input_embeds=UpperCAmelCase_       , device=UpperCAmelCase_       , eos_token_id=UpperCAmelCase_)
     generated_tokens.append(output_tokens[0])
     generated_seq_lengths.append(seq_lengths[0])
   lowerCamelCase__:      Union[str, Any]			  =torch.stack(UpperCAmelCase_)
   lowerCamelCase__:      List[Any]			  =torch.stack(UpperCAmelCase_)
   return generated_tokens, generated_seq_lengths







 @torch.no_grad()
 def 							SCREAMING_SNAKE_CASE_					(self				:   str       , UpperCAmelCase_				:   List[str]=None       , UpperCAmelCase_				:   List[str]=None       , UpperCAmelCase_				:   Optional[int]=None       , UpperCAmelCase_				:   int = 5       , UpperCAmelCase_				:   int = 67       , UpperCAmelCase_				:   float = 1.0       , UpperCAmelCase_				:   Optional[int] = None       , )       ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:      List[str]			  =eos_token_id
   lowerCamelCase__:      str			  =None
   lowerCamelCase__:      Optional[Any]			  =None
   lowerCamelCase__:      Any			  =torch.ones(UpperCAmelCase_       , device=UpperCAmelCase_       , dtype=torch.int)
   lowerCamelCase__:      Any			  =torch.zeros(UpperCAmelCase_       , device=UpperCAmelCase_       , dtype=torch.bool)

   if input_embeds is not None:
     lowerCamelCase__:      Union[str, Any]			  =input_embeds
   else:
     lowerCamelCase__:      Optional[int]			  =self.transformer.transformer.wte(UpperCAmelCase_)

   for i in range(UpperCAmelCase_):
     lowerCamelCase__:      Dict			  =self.transformer(inputs_embeds=UpperCAmelCase_)
     lowerCamelCase__:      Optional[Any]			  =outputs.logits
     lowerCamelCase__:      Dict			  =logits[:, -1, :] / (temperature if temperature > 0 else 1.0)
     lowerCamelCase__:      Dict			  =logits.softmax(-1).log()

     if scores is None:
       lowerCamelCase__  ,					lowerCamelCase__:      str			  =logits.topk(UpperCAmelCase_       , -1)
       lowerCamelCase__:      List[str]			  =generated.expand(UpperCAmelCase_       , *generated.shape[1:])
       lowerCamelCase__  ,					lowerCamelCase__:      List[str]			  =next_tokens.permute(1       , 0), scores.squeeze(0)
       if tokens is None:
         lowerCamelCase__:      Optional[int]			  =next_tokens
       else:
         lowerCamelCase__:      Optional[Any]			  =tokens.expand(UpperCAmelCase_       , *tokens.shape[1:])
         lowerCamelCase__:      List[str]			  =torch.cat((tokens, next_tokens)       , dim=1)
     else:
       lowerCamelCase__:      Any			  =-float(np.inf)
       lowerCamelCase__:      Optional[Any]			  =0
       lowerCamelCase__:      Optional[int]			  =scores[:, None] + logits
       seq_lengths[~is_stopped] += 1
       lowerCamelCase__:      int			  =scores_sum / seq_lengths[:, None]
       lowerCamelCase__  ,					lowerCamelCase__:      Any			  =scores_sum_average.view(-1).topk(UpperCAmelCase_       , -1)
       lowerCamelCase__:      List[Any]			  =next_tokens // scores_sum.shape[1]
       lowerCamelCase__:      str			  =seq_lengths[next_tokens_source]
       lowerCamelCase__:      Optional[int]			  =next_tokens % scores_sum.shape[1]
       lowerCamelCase__:      Optional[Any]			  =next_tokens.unsqueeze(1)
       lowerCamelCase__:      Dict			  =tokens[next_tokens_source]
       lowerCamelCase__:      Union[str, Any]			  =torch.cat((tokens, next_tokens)       , dim=1)
       lowerCamelCase__:      List[Any]			  =generated[next_tokens_source]
       lowerCamelCase__:      List[str]			  =scores_sum_average * seq_lengths
       lowerCamelCase__:      Tuple			  =is_stopped[next_tokens_source]

     lowerCamelCase__:      Dict			  =self.transformer.transformer.wte(next_tokens.squeeze()).view(generated.shape[0]       , 1       , -1)
     lowerCamelCase__:      List[str]			  =torch.cat((generated, next_token_embed)       , dim=1)
     lowerCamelCase__:      List[Any]			  =is_stopped + next_tokens.eq(UpperCAmelCase_).squeeze()
     if is_stopped.all():
       break

   lowerCamelCase__:      Dict			  =scores / seq_lengths
   lowerCamelCase__:      Dict			  =scores.argsort(descending=UpperCAmelCase_)
   # tokens tensors are already padded to max_seq_length
   lowerCamelCase__:      Tuple			  =[tokens[i] for i in order]
   lowerCamelCase__:      List[Any]			  =torch.stack(UpperCAmelCase_       , dim=0)
   lowerCamelCase__:      Optional[Any]			  =torch.tensor([seq_lengths[i] for i in order]       , dtype=seq_lengths.dtype)
   return output_texts, seq_lengths




"""simple docstring"""





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__":
							__UpperCamelCase  :      Tuple							      =		'''%20'''.join(argv[1:]) if len(argv) > 1 else quote(str(input('''Search: ''')))

							print('''Googling.....''')

							__UpperCamelCase  :      Optional[int]							      =		F'''https://www.google.com/search?q={query}&num=100'''

							__UpperCamelCase  :      Optional[Any]							      =		requests.get(
							    url,
							    headers={'''User-Agent''': str(UserAgent().random)},
							)

							try:
														__UpperCamelCase  :      Union[str, Any]							      =		(
														    BeautifulSoup(res.text, '''html.parser''')
														    .find('''div''', attrs={'''class''': '''yuRUbf'''})
														    .find('''a''')
														    .get('''href''')
														)

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

							webbrowser.open(link)




