


# 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 argparse

from ...utils.dataclasses import (
    ComputeEnvironment,
    DistributedType,
    DynamoBackend,
    PrecisionType,
    SageMakerDistributedType,
)
from ..menu import BulletMenu


__UpperCamelCase    		=       [
    'EAGER',
    'AOT_EAGER',
    'INDUCTOR',
    'NVFUSER',
    'AOT_NVFUSER',
    'AOT_CUDAGRAPHS',
    'OFI',
    'FX2TRT',
    'ONNXRT',
    'IPEX',
]
def        UpperCamelCase_(				_A   :str				,     _A   :int=None				,     _A   :Union[str, Any]=None				,     _A   :str=None  )->		List[Any]:
			UpperCamelCase__					=       True
			while ask_again:
						UpperCamelCase__					=       input(_lowercase  )
						try:
									if default is not None and len(_lowercase  ) == 0:
												return default
									return convert_value(_lowercase  ) if convert_value is not None else result
						except Exception:
									if error_message is not None:
												print(_lowercase  )
def        UpperCamelCase_(				_A   :str				,     _A   :Optional[int]=[]				,     _A   :Optional[Any]=None				,     _A   :Any=0  )->		List[str]:
			UpperCamelCase__					=       BulletMenu(_lowercase				,     _lowercase  )
			UpperCamelCase__					=       menu.run(default_choice=_lowercase  )
			return convert_value(_lowercase  ) if convert_value is not None else result
def        UpperCamelCase_(				_A   :Dict  )->		List[Any]:
			UpperCamelCase__					=       int(_lowercase  )
			return ComputeEnvironment(["LOCAL_MACHINE", "AMAZON_SAGEMAKER"][value]  )
def        UpperCamelCase_(				_A   :List[Any]  )->		List[str]:
			UpperCamelCase__					=       int(_lowercase  )
			return DistributedType(["NO", "MULTI_CPU", "MULTI_XPU", "MULTI_GPU", "MULTI_NPU", "TPU"][value]  )
def        UpperCamelCase_(				_A   :str  )->		Optional[Any]:
			UpperCamelCase__					=       int(_lowercase  )
			return DynamoBackend(DYNAMO_BACKENDS[value]  ).value
def        UpperCamelCase_(				_A   :int  )->		Union[str, Any]:
			UpperCamelCase__					=       int(_lowercase  )
			return PrecisionType(["no", "fp16", "bf16", "fp8"][value]  )
def        UpperCamelCase_(				_A   :Optional[int]  )->		List[str]:
			UpperCamelCase__					=       int(_lowercase  )
			return SageMakerDistributedType(["NO", "DATA_PARALLEL", "MODEL_PARALLEL"][value]  )
def        UpperCamelCase_(				_A   :int  )->		List[str]:
			return {"yes": True, "no": False}[value.lower()]







class        lowerCamelCase__	(							argparse.RawDescriptionHelpFormatter      ):






			"""simple docstring"""






			def 				snake_case__				(							self      ,						snake_case      ,						snake_case      ,						snake_case      ,						snake_case     ):

						'''simple docstring'''


						UpperCamelCase__					=       super()._format_usage(UpperCamelCase__      ,						UpperCamelCase__      ,						UpperCamelCase__      ,						UpperCamelCase__     )
						UpperCamelCase__					=       usage.replace("<command> [<args>] "      ,						""     )
						return usage






import os
import textwrap

import pyarrow as pa
import pytest

from datasets import ClassLabel, Features, Image
from datasets.packaged_modules.csv.csv import Csv

from ..utils import require_pil
@pytest.fixture
def        UpperCamelCase_(				_A   :Union[str, Any]  )->		int:
			UpperCamelCase__					=       tmp_path / "file.csv"
			UpperCamelCase__					=       textwrap.dedent(
			    "\\n        header1,header2\n        1,2\n        10,20\n        "  )
			with open(_A				,     "w"  ) as f:
						f.write(_A  )
			return str(_A  )
@pytest.fixture
def        UpperCamelCase_(				_A   :int  )->		Union[str, Any]:
			UpperCamelCase__					=       tmp_path / "malformed_file.csv"
			UpperCamelCase__					=       textwrap.dedent(
			    "\\n        header1,header2\n        1,2\n        10,20,\n        "  )
			with open(_A				,     "w"  ) as f:
						f.write(_A  )
			return str(_A  )
@pytest.fixture
def        UpperCamelCase_(				_A   :List[Any]				,     _A   :Dict  )->		Optional[Any]:
			UpperCamelCase__					=       tmp_path / "csv_with_image.csv"
			UpperCamelCase__					=       textwrap.dedent(
			    F'''\
        image
        {image_file}
        '''  )
			with open(_A				,     "w"  ) as f:
						f.write(_A  )
			return str(_A  )
@pytest.fixture
def        UpperCamelCase_(				_A   :List[Any]  )->		Any:
			UpperCamelCase__					=       tmp_path / "csv_with_label.csv"
			UpperCamelCase__					=       textwrap.dedent(
			    "\\n        label\n        good\n        bad\n        good\n        "  )
			with open(_A				,     "w"  ) as f:
						f.write(_A  )
			return str(_A  )
@pytest.fixture
def        UpperCamelCase_(				_A   :Any  )->		int:
			UpperCamelCase__					=       tmp_path / "csv_with_int_list.csv"
			UpperCamelCase__					=       textwrap.dedent(
			    "\\n        int_list\n        1 2 3\n        4 5 6\n        7 8 9\n        "  )
			with open(_A				,     "w"  ) as f:
						f.write(_A  )
			return str(_A  )
def        UpperCamelCase_(				_A   :Optional[Any]				,     _A   :Optional[Any]				,     _A   :List[str]  )->		Union[str, Any]:
			UpperCamelCase__					=       Csv()
			UpperCamelCase__					=       csv._generate_tables([[csv_file, malformed_csv_file]]  )
			with pytest.raises(_A				,     match="Error tokenizing data"  ):
						for _ in generator:
									pass
			assert any(
			    record.levelname == "ERROR"
			    and "Failed to read file" in record.message
			    and os.path.basename(_A  ) in record.message
			    for record in caplog.records  )
@require_pil
def        UpperCamelCase_(				_A   :Any  )->		int:
			with open(_A				,     encoding="utf-8"  ) as f:
						UpperCamelCase__					=       f.read().splitlines()[1]
			UpperCamelCase__					=       Csv(encoding="utf-8"				,     features=Features({"image": Image()}  )  )
			UpperCamelCase__					=       csv._generate_tables([[csv_file_with_image]]  )
			UpperCamelCase__					=       pa.concat_tables([table for _, table in generator]  )
			assert pa_table.schema.field("image"  ).type == Image()()
			UpperCamelCase__					=       pa_table.to_pydict()["image"]
			assert generated_content == [{"path": image_file, "bytes": None}]
def        UpperCamelCase_(				_A   :Any  )->		Dict:
			with open(_A				,     encoding="utf-8"  ) as f:
						UpperCamelCase__					=       f.read().splitlines()[1:]
			UpperCamelCase__					=       Csv(encoding="utf-8"				,     features=Features({"label": ClassLabel(names=["good", "bad"]  )}  )  )
			UpperCamelCase__					=       csv._generate_tables([[csv_file_with_label]]  )
			UpperCamelCase__					=       pa.concat_tables([table for _, table in generator]  )
			assert pa_table.schema.field("label"  ).type == ClassLabel(names=["good", "bad"]  )()
			UpperCamelCase__					=       pa_table.to_pydict()["label"]
			assert generated_content == [ClassLabel(names=["good", "bad"]  ).straint(_A  ) for label in labels]







def        UpperCamelCase_(				_A   :Dict  )->		Optional[Any]:
			UpperCamelCase__					=       Csv(encoding="utf-8"				,     sep=","				,     converters={"int_list": lambda _A  : [int(_A  ) for i in x.split()]}  )
			UpperCamelCase__					=       csv._generate_tables([[csv_file_with_int_list]]  )
			UpperCamelCase__					=       pa.concat_tables([table for _, table in generator]  )
			assert pa.types.is_list(pa_table.schema.field("int_list"  ).type  )
			UpperCamelCase__					=       pa_table.to_pydict()["int_list"]
			assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]


