


import os
import tempfile
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
    import torch
    from torch import nn

    from transformers import (
        Adafactor,
        AdamW,
        get_constant_schedule,
        get_constant_schedule_with_warmup,
        get_cosine_schedule_with_warmup,
        get_cosine_with_hard_restarts_schedule_with_warmup,
        get_inverse_sqrt_schedule,
        get_linear_schedule_with_warmup,
        get_polynomial_decay_schedule_with_warmup,
    )

def    lowercase_	(	_A    :					Optional[Any]					,      _A    :					Any=10		):

      """simple docstring"""




      lowerCamelCase__	:       Dict					=						[]
      for _ in range(UpperCAmelCase__		):
            lrs.append(scheduler.get_lr()[0]		)
            scheduler.step()
      return lrs

def    lowercase_	(	_A    :					Tuple					,      _A    :					Tuple=10		):

      """simple docstring"""




      lowerCamelCase__	:       List[Any]					=						[]
      for step in range(UpperCAmelCase__		):
            lrs.append(scheduler.get_lr()[0]		)
            scheduler.step()
            if step == num_steps // 2:
                  with tempfile.TemporaryDirectory() as tmpdirname:
                        lowerCamelCase__	:       Optional[Any]					=						os.path.join(UpperCAmelCase__					,      "schedule.bin"		)
                        torch.save(scheduler.state_dict()					,      UpperCAmelCase__		)

                        lowerCamelCase__	:       str					=						torch.load(UpperCAmelCase__		)
                        scheduler.load_state_dict(UpperCAmelCase__		)
      return lrs







@require_torch
class     _lowercase				(							unittest.TestCase):


       """simple docstring"""




       def 		lowerCAmelCase  (     self							:    Any      ,   __lowerCamelCase							:    str      ,   __lowerCamelCase							:    Dict      ,   __lowerCamelCase							:    int ):


             '''simple docstring'''



             self.assertEqual(len(_A )      ,   len(_A ) )
             for a, b in zip(_A      ,   _A ):
                   self.assertAlmostEqual(_A      ,   _A      ,   delta=_A )





       def 		lowerCAmelCase  (     self							:    Tuple ):


             '''simple docstring'''



             lowerCamelCase__	:       Dict					=						torch.tensor([0.1, -0.2, -0.1]      ,   requires_grad=_A )
             lowerCamelCase__	:       Dict					=						torch.tensor([0.4, 0.2, -0.5] )
             lowerCamelCase__	:       Tuple					=						nn.MSELoss()
             # No warmup, constant schedule, no gradient clipping
             lowerCamelCase__	:       List[str]					=						AdamW(params=[w]      ,   lr=2E-1      ,   weight_decay=0.0 )
             for _ in range(100 ):
                   lowerCamelCase__	:       Union[str, Any]					=						criterion(_A      ,   _A )
                   loss.backward()
                   optimizer.step()
                   w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
                   w.grad.zero_()
             self.assertListAlmostEqual(w.tolist()      ,   [0.4, 0.2, -0.5]      ,   tol=1E-2 )





       def 		lowerCAmelCase  (     self							:    Tuple ):


             '''simple docstring'''



             lowerCamelCase__	:       Optional[int]					=						torch.tensor([0.1, -0.2, -0.1]      ,   requires_grad=_A )
             lowerCamelCase__	:       List[Any]					=						torch.tensor([0.4, 0.2, -0.5] )
             lowerCamelCase__	:       Any					=						nn.MSELoss()
             # No warmup, constant schedule, no gradient clipping
             lowerCamelCase__	:       Any					=						Adafactor(
                 params=[w]      ,   lr=1E-2      ,   eps=(1E-3_0, 1E-3)      ,   clip_threshold=1.0      ,   decay_rate=-0.8      ,   betaa=_A      ,   weight_decay=0.0      ,   relative_step=_A      ,   scale_parameter=_A      ,   warmup_init=_A      ,   )
             for _ in range(1000 ):
                   lowerCamelCase__	:       Union[str, Any]					=						criterion(_A      ,   _A )
                   loss.backward()
                   optimizer.step()
                   w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
                   w.grad.zero_()
             self.assertListAlmostEqual(w.tolist()      ,   [0.4, 0.2, -0.5]      ,   tol=1E-2 )







@require_torch
class     _lowercase				(							unittest.TestCase):


       """simple docstring"""




       A__      		=    nn.Linear(50							,						50) if is_torch_available() else None
       A__      		=    AdamW(m.parameters()							,						lr=10.0) if is_torch_available() else None
       A__      		=    10





       def 		lowerCAmelCase  (     self							:    List[Any]      ,   __lowerCamelCase							:    Any      ,   __lowerCamelCase							:    Dict      ,   __lowerCamelCase							:    Any      ,   __lowerCamelCase							:    Union[str, Any]=None ):


             '''simple docstring'''



             self.assertEqual(len(_A )      ,   len(_A ) )
             for a, b in zip(_A      ,   _A ):
                   self.assertAlmostEqual(_A      ,   _A      ,   delta=_A      ,   msg=_A )





       def 		lowerCAmelCase  (     self							:    Union[str, Any] ):


             '''simple docstring'''



             lowerCamelCase__	:       Optional[int]					=						{"num_warmup_steps": 2, "num_training_steps": 10}
             # schedulers doct format
             # function: (sched_args_dict, expected_learning_rates)
             lowerCamelCase__	:       int					=						{
                 get_constant_schedule: ({}, [1_0.0] * self.num_steps),
                 get_constant_schedule_with_warmup: (
                     {"num_warmup_steps": 4},
                     [0.0, 2.5, 5.0, 7.5, 1_0.0, 1_0.0, 1_0.0, 1_0.0, 1_0.0, 1_0.0],
                 ),
                 get_linear_schedule_with_warmup: (
                     {**common_kwargs},
                     [0.0, 5.0, 1_0.0, 8.7_5, 7.5, 6.2_5, 5.0, 3.7_5, 2.5, 1.2_5],
                 ),
                 get_cosine_schedule_with_warmup: (
                     {**common_kwargs},
                     [0.0, 5.0, 1_0.0, 9.6_1, 8.5_3, 6.9_1, 5.0, 3.0_8, 1.4_6, 0.3_8],
                 ),
                 get_cosine_with_hard_restarts_schedule_with_warmup: (
                     {**common_kwargs, "num_cycles": 2},
                     [0.0, 5.0, 1_0.0, 8.5_3, 5.0, 1.4_6, 1_0.0, 8.5_3, 5.0, 1.4_6],
                 ),
                 get_polynomial_decay_schedule_with_warmup: (
                     {**common_kwargs, "power": 2.0, "lr_end": 1E-7},
                     [0.0, 5.0, 1_0.0, 7.6_5_6, 5.6_2_5, 3.9_0_6, 2.5, 1.4_0_6, 0.6_2_5, 0.1_5_6],
                 ),
                 get_inverse_sqrt_schedule: (
                     {"num_warmup_steps": 2},
                     [0.0, 5.0, 1_0.0, 8.1_6_5, 7.0_7_1, 6.3_2_5, 5.7_7_4, 5.3_4_5, 5.0, 4.7_1_4],
                 ),
             }

             for scheduler_func, data in scheds.items():
                   lowerCamelCase__					,     lowerCamelCase__	:       Tuple					=						data

                   lowerCamelCase__	:       Union[str, Any]					=						scheduler_func(self.optimizer      ,   **_A )
                   self.assertEqual(len([scheduler.get_lr()[0]] )      ,   1 )
                   lowerCamelCase__	:       Optional[int]					=						unwrap_schedule(_A      ,   self.num_steps )
                   self.assertListAlmostEqual(
                       _A      ,   _A      ,   tol=1E-2      ,   msg=f"failed for {scheduler_func} in normal scheduler"      ,   )

                   lowerCamelCase__	:       str					=						scheduler_func(self.optimizer      ,   **_A )
                   if scheduler_func.__name__ != "get_constant_schedule":
                         LambdaScheduleWrapper.wrap_scheduler(_A )  # wrap to test picklability of the schedule
                   lowerCamelCase__	:       List[Any]					=						unwrap_and_save_reload_schedule(_A      ,   self.num_steps )
                   self.assertListEqual(_A      ,   _A      ,   msg=f"failed for {scheduler_func} in save and reload" )







class     _lowercase				:


       """simple docstring"""




       def __init__(     self							:    Union[str, Any]      ,   __lowerCamelCase							:    Optional[Any] ):


             '''simple docstring'''



             lowerCamelCase__	:       Tuple					=						fn





       def __call__(     self							:    Tuple      ,   *__lowerCamelCase							:    str      ,   **__lowerCamelCase							:    Dict ):


             '''simple docstring'''



             return self.fn(*_A      ,   **_A )





       @classmethod
       def 		lowerCAmelCase  (     self							:    Tuple      ,   __lowerCamelCase							:    Optional[Any] ):


             '''simple docstring'''



             lowerCamelCase__	:       List[str]					=						list(map(self      ,   scheduler.lr_lambdas ) )






import sys
import webbrowser

import requests
from bsa import BeautifulSoup
from fake_useragent import UserAgent

if __name__ == "__main__":
    print("Googling.....")
    A   :					str	=      "https://www.google.com/search?q=" + " ".join(sys.argv[1:])
    A   :					Optional[int]	=      requests.get(url, headers={"UserAgent": UserAgent().random})
    # res.raise_for_status()
    with open("project1a.html", "wb") as out_file:  # only for knowing the class
        for data in res.iter_content(10000):
            out_file.write(data)
    A   :					int	=      BeautifulSoup(res.text, "html.parser")
    A   :					Any	=      list(soup.select(".eZt8xd"))[:5]

    print(len(links))
    for link in links:
        if link.text == "Maps":
            webbrowser.open(link.get("href"))
        else:
            webbrowser.open(f'https://google.com{link.get("href")}')


