





# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os

from ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES
from ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType
from ...utils.imports import is_botoa_available
from .config_args import SageMakerConfig
from .config_utils import (
    DYNAMO_BACKENDS,
    _ask_field,
    _ask_options,
    _convert_dynamo_backend,
    _convert_mixed_precision,
    _convert_sagemaker_distributed_mode,
    _convert_yes_no_to_bool,
)


if is_botoa_available():
    import botoa  # noqa: F401






def lowerCamelCase__   (							snake_case_     :  int	)				->    Any:
   __snake_case								=       botoa.client('''iam'''	)

   __snake_case								=       {
       '''Version''': '''2012-10-17''',
       '''Statement''': [
           {'''Effect''': '''Allow''', '''Principal''': {'''Service''': '''sagemaker.amazonaws.com'''}, '''Action''': '''sts:AssumeRole'''}
       ],
   }
   try:
      # create the role, associated with the chosen trust policy
      iam_client.create_role(
          RoleName=snake_case_	,				AssumeRolePolicyDocument=json.dumps(snake_case_	,				indent=2	)	)
      __snake_case								=       {
          '''Version''': '''2012-10-17''',
          '''Statement''': [
              {
                  '''Effect''': '''Allow''',
                  '''Action''': [
                      '''sagemaker:*''',
                      '''ecr:GetDownloadUrlForLayer''',
                      '''ecr:BatchGetImage''',
                      '''ecr:BatchCheckLayerAvailability''',
                      '''ecr:GetAuthorizationToken''',
                      '''cloudwatch:PutMetricData''',
                      '''cloudwatch:GetMetricData''',
                      '''cloudwatch:GetMetricStatistics''',
                      '''cloudwatch:ListMetrics''',
                      '''logs:CreateLogGroup''',
                      '''logs:CreateLogStream''',
                      '''logs:DescribeLogStreams''',
                      '''logs:PutLogEvents''',
                      '''logs:GetLogEvents''',
                      '''s3:CreateBucket''',
                      '''s3:ListBucket''',
                      '''s3:GetBucketLocation''',
                      '''s3:GetObject''',
                      '''s3:PutObject''',
                  ],
                  '''Resource''': '''*''',
              }
          ],
      }
      # attach policy to role
      iam_client.put_role_policy(
          RoleName=snake_case_	,				PolicyName=f"""{role_name}_policy_permission"""	,				PolicyDocument=json.dumps(snake_case_	,				indent=2	)	,				)
   except iam_client.exceptions.EntityAlreadyExistsException:
      print(f"""role {role_name} already exists. Using existing one"""	)






def lowerCamelCase__   (							snake_case_     :  Tuple	)				->    Any:
   __snake_case								=       botoa.client('''iam'''	)
   return iam_client.get_role(RoleName=snake_case_	)["Role"]["Arn"]







def lowerCamelCase__   (							)				->    Optional[int]:
   __snake_case								=       _ask_options(
       '''How do you want to authorize?'''	,				['''AWS Profile''', '''Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) ''']	,				snake_case_	,				)
   __snake_case								=       None
   if credentials_configuration == 0:
      __snake_case								=       _ask_field('''Enter your AWS Profile name: [default] '''	,				default='''default'''	)
      __snake_case								=       aws_profile
   else:
      print(
          '''Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,'''
          '''`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`'''	)
      __snake_case								=       _ask_field('''AWS Access Key ID: '''	)
      __snake_case								=       aws_access_key_id

      __snake_case								=       _ask_field('''AWS Secret Access Key: '''	)
      __snake_case								=       aws_secret_access_key

   __snake_case								=       _ask_field('''Enter your AWS Region: [us-east-1]'''	,				default='''us-east-1'''	)
   __snake_case								=       aws_region

   __snake_case								=       _ask_options(
       '''Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?'''	,				['''Provide IAM Role name''', '''Create new IAM role using credentials''']	,				snake_case_	,				)
   if role_management == 0:
      __snake_case								=       _ask_field('''Enter your IAM role name: '''	)
   else:
      __snake_case								=       '''accelerate_sagemaker_execution_role'''
      print(f"""Accelerate will create an iam role \"{iam_role_name}\" using the provided credentials"""	)
      _create_iam_role_for_sagemaker(snake_case_	)

   __snake_case								=       _ask_field(
       '''Do you want to use custom Docker image? [yes/NO]: '''	,				_convert_yes_no_to_bool	,				default=snake_case_	,				error_message='''Please enter yes or no.'''	,				)
   __snake_case								=       None
   if is_custom_docker_image:
      __snake_case								=       _ask_field('''Enter your Docker image: '''	,				lambda snake_case_	: str(snake_case_	).lower()	)

   __snake_case								=       _ask_field(
       '''Do you want to provide SageMaker input channels with data locations? [yes/NO]: '''	,				_convert_yes_no_to_bool	,				default=snake_case_	,				error_message='''Please enter yes or no.'''	,				)
   __snake_case								=       None
   if is_sagemaker_inputs_enabled:
      __snake_case								=       _ask_field(
          '''Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): '''	,				lambda snake_case_	: str(snake_case_	).lower()	,				)

   __snake_case								=       _ask_field(
       '''Do you want to enable SageMaker metrics? [yes/NO]: '''	,				_convert_yes_no_to_bool	,				default=snake_case_	,				error_message='''Please enter yes or no.'''	,				)
   __snake_case								=       None
   if is_sagemaker_metrics_enabled:
      __snake_case								=       _ask_field(
          '''Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): '''	,				lambda snake_case_	: str(snake_case_	).lower()	,				)

   __snake_case								=       _ask_options(
       '''What is the distributed mode?'''	,				['''No distributed training''', '''Data parallelism''']	,				_convert_sagemaker_distributed_mode	,				)
   __snake_case								=       {}
   __snake_case								=       _ask_field(
       '''Do you wish to optimize your script with torch dynamo?[yes/NO]:'''	,				_convert_yes_no_to_bool	,				default=snake_case_	,				error_message='''Please enter yes or no.'''	,				)
   if use_dynamo:
      __snake_case								=       '''dynamo_'''
      __snake_case								=       _ask_options(
          '''Which dynamo backend would you like to use?'''	,				[x.lower() for x in DYNAMO_BACKENDS]	,				_convert_dynamo_backend	,				default=2	,				)
      __snake_case								=       _ask_field(
          '''Do you want to customize the defaults sent to torch.compile? [yes/NO]: '''	,				_convert_yes_no_to_bool	,				default=snake_case_	,				error_message='''Please enter yes or no.'''	,				)

      if use_custom_options:
         __snake_case								=       _ask_options(
             '''Which mode do you want to use?'''	,				snake_case_	,				lambda snake_case_	: TORCH_DYNAMO_MODES[int(snake_case_	)]	,				default='''default'''	,				)
         __snake_case								=       _ask_field(
             '''Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: '''	,				_convert_yes_no_to_bool	,				default=snake_case_	,				error_message='''Please enter yes or no.'''	,				)
         __snake_case								=       _ask_field(
             '''Do you want to enable dynamic shape tracing? [yes/NO]: '''	,				_convert_yes_no_to_bool	,				default=snake_case_	,				error_message='''Please enter yes or no.'''	,				)
   __snake_case								=       '''Which EC2 instance type you want to use for your training?'''
   if distributed_type != SageMakerDistributedType.NO:
      __snake_case								=       _ask_options(
          snake_case_	,				snake_case_	,				lambda snake_case_	: SAGEMAKER_PARALLEL_EC2_INSTANCES[int(snake_case_	)]	)
   else:
      eca_instance_query += "? [ml.p3.2xlarge]:"
      __snake_case								=       _ask_field(snake_case_	,				lambda snake_case_	: str(snake_case_	).lower()	,				default='''ml.p3.2xlarge'''	)

   __snake_case								=       1
   if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL):
      __snake_case								=       _ask_field(
          '''How many machines do you want use? [1]: '''	,				snake_case_	,				default=1	,				)

   __snake_case								=       _ask_options(
       '''Do you wish to use FP16 or BF16 (mixed precision)?'''	,				['''no''', '''fp16''', '''bf16''', '''fp8''']	,				_convert_mixed_precision	,				)

   if use_dynamo and mixed_precision == "no":
      print(
          '''Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts.'''	)

   return SageMakerConfig(
       image_uri=snake_case_	,				compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER	,				distributed_type=snake_case_	,				use_cpu=snake_case_	,				dynamo_config=snake_case_	,				eca_instance_type=snake_case_	,				profile=snake_case_	,				region=snake_case_	,				iam_role_name=snake_case_	,				mixed_precision=snake_case_	,				num_machines=snake_case_	,				sagemaker_inputs_file=snake_case_	,				sagemaker_metrics_file=snake_case_	,				)








from __future__ import annotations

snake_case_           =      'Muhammad Umer Farooq'
snake_case_           =      'MIT'
snake_case_           =      '1.0.0'
snake_case_           =      'Muhammad Umer Farooq'
snake_case_           =      'contact@muhammadumerfarooq.me'
snake_case_           =      'Alpha'

import re
from html.parser import HTMLParser
from urllib import parse

import requests

class 		SCREAMING_SNAKE_CASE__    (	_UpperCAmelCase  ):


      def __init__(self	:				Dict    ,      a__	:				str				):





         """simple docstring"""



         super().__init__()
         __snake_case								=       []
         __snake_case								=       domain


      def        a			(self	:				Tuple    ,      a__	:				str    ,      a__	:				list[tuple[str, str | None]]				):





         """simple docstring"""



         if tag == "a":
            # Check the list of defined attributes.
            for name, value in attrs:
               # If href is defined, and not empty nor # print it.
               if name == "href" and value != "#" and value != "":
                  # If not already in urls.
                  if value not in self.urls:
                     __snake_case								=       parse.urljoin(self.domain    ,      a__				)
                     self.urls.append(a__				)






def lowerCamelCase__   (							snake_case_     :  str	)				->    str:
   return ".".join(get_sub_domain_name(snake_case_	).split('''.'''	)[-2:]	)






def lowerCamelCase__   (							snake_case_     :  str	)				->    str:
   return parse.urlparse(snake_case_	).netloc






def lowerCamelCase__   (							snake_case_     :  str = "https://github.com"	)				->    list[str]:
   __snake_case								=       get_domain_name(snake_case_	)

   # Initialize the parser
   __snake_case								=       Parser(snake_case_	)

   try:
      # Open URL
      __snake_case								=       requests.get(snake_case_	)

      # pass the raw HTML to the parser to get links
      parser.feed(r.text	)

      # Get links and loop through
      __snake_case								=       set()
      for link in parser.urls:
         # open URL.
         # read = requests.get(link)
         try:
            __snake_case								=       requests.get(snake_case_	)
            # Get the valid email.
            __snake_case								=       re.findall('''[a-zA-Z0-9]+@''' + domain	,				read.text	)
            # If not in list then append it.
            for email in emails:
               valid_emails.add(snake_case_	)
         except ValueError:
            pass
   except ValueError:
      raise SystemExit(1	)

   # Finally return a sorted list of email addresses with no duplicates.
   return sorted(snake_case_	)


if __name__ == "__main__":
    snake_case_           =      emails_from_url('https://github.com')
    print(F'{len(emails)} emails found:')
    print('\n'.join(sorted(emails)))

