



import os

import pytest
import yaml

from datasets.features.features import Features, Value
from datasets.info import DatasetInfo, DatasetInfosDict






@pytest.mark.parametrize(
    """files"""					, [
        ["""full:README.md""", """dataset_infos.json"""],
        ["""empty:README.md""", """dataset_infos.json"""],
        ["""dataset_infos.json"""],
        ["""full:README.md"""],
    ]					, )
def        __lowerCAmelCase		(	__lowerCamelCase						:     Optional[Any]					, __lowerCamelCase						:     Optional[Any]						)	->    Tuple:
					__lowerCAmelCase						 =tmp_path_factory.mktemp("""dset_infos_dir"""						)
					if "full:README.md" in files:
										with open(dataset_infos_dir / """README.md"""					, """w"""						) as f:
															f.write("""---\ndataset_info:\n  dataset_size: 42\n---"""						)
					if "empty:README.md" in files:
										with open(dataset_infos_dir / """README.md"""					, """w"""						) as f:
															f.write(""""""						)
    # we want to support dataset_infos.json for backward compatibility
					if "dataset_infos.json" in files:
										with open(dataset_infos_dir / """dataset_infos.json"""					, """w"""						) as f:
															f.write("""{\"default\": {\"dataset_size\": 42}}"""						)
					__lowerCAmelCase						 =DatasetInfosDict.from_directory(__lowerCamelCase						)
					assert dataset_infos
					assert dataset_infos["default"].dataset_size == 42






@pytest.mark.parametrize(
    """dataset_info"""					, [
        DatasetInfo(),
        DatasetInfo(
            description="""foo"""					, features=Features({"""a""": Value("""int32"""						)}						)					, builder_name="""builder"""					, config_name="""config"""					, version="""1.0.0"""					, splits=[{"""name""": """train"""}]					, download_size=42					, ),
    ]					, )
def        __lowerCAmelCase		(	__lowerCamelCase						:     Optional[int]					, __lowerCamelCase						:     DatasetInfo						)	->    Dict:
					__lowerCAmelCase						 =str(__lowerCamelCase						)
					dataset_info.write_to_directory(__lowerCamelCase						)
					__lowerCAmelCase						 =DatasetInfo.from_directory(__lowerCamelCase						)
					assert dataset_info == reloaded
					assert os.path.exists(os.path.join(__lowerCamelCase					, """dataset_info.json"""						)						)






def        __lowerCAmelCase		(	)	->    Optional[int]:
					__lowerCAmelCase						 =DatasetInfo(
					    description="""foo"""					, citation="""bar"""					, homepage="""https://foo.bar"""					, license="""CC0"""					, features=Features({"""a""": Value("""int32"""						)}						)					, post_processed={}					, supervised_keys=()					, task_templates=[]					, builder_name="""builder"""					, config_name="""config"""					, version="""1.0.0"""					, splits=[{"""name""": """train""", """num_examples""": 42}]					, download_checksums={}					, download_size=1337					, post_processing_size=442					, dataset_size=1234					, size_in_bytes=1337 + 442 + 1234					, )
					__lowerCAmelCase						 =dataset_info._to_yaml_dict()
					assert sorted(__lowerCamelCase						) == sorted(DatasetInfo._INCLUDED_INFO_IN_YAML						)
					for key in DatasetInfo._INCLUDED_INFO_IN_YAML:
										assert key in dataset_info_yaml_dict
										assert isinstance(dataset_info_yaml_dict[key]					, (list, dict, int, str)						)
					__lowerCAmelCase						 =yaml.safe_dump(__lowerCamelCase						)
					__lowerCAmelCase						 =yaml.safe_load(__lowerCamelCase						)
					assert dataset_info_yaml_dict == reloaded






def        __lowerCAmelCase		(	)	->    Optional[int]:
					__lowerCAmelCase						 =DatasetInfo()
					__lowerCAmelCase						 =dataset_info._to_yaml_dict()
					assert dataset_info_yaml_dict == {}






@pytest.mark.parametrize(
    """dataset_infos_dict"""					, [
        DatasetInfosDict(),
        DatasetInfosDict({"""default""": DatasetInfo()}						),
        DatasetInfosDict({"""my_config_name""": DatasetInfo()}						),
        DatasetInfosDict(
            {
                """default""": DatasetInfo(
                    description="""foo"""					, features=Features({"""a""": Value("""int32"""						)}						)					, builder_name="""builder"""					, config_name="""config"""					, version="""1.0.0"""					, splits=[{"""name""": """train"""}]					, download_size=42					, )
            }						),
        DatasetInfosDict(
            {
                """v1""": DatasetInfo(dataset_size=42						),
                """v2""": DatasetInfo(dataset_size=1337						),
            }						),
    ]					, )
def        __lowerCAmelCase		(	__lowerCamelCase						:     Any					, __lowerCamelCase						:     DatasetInfosDict						)	->    Dict:
					__lowerCAmelCase						 =str(__lowerCamelCase						)
					dataset_infos_dict.write_to_directory(__lowerCamelCase						)
					__lowerCAmelCase						 =DatasetInfosDict.from_directory(__lowerCamelCase						)

					# the config_name of the dataset_infos_dict take over the attribute
					for config_name, dataset_info in dataset_infos_dict.items():
										__lowerCAmelCase						 =config_name
										# the yaml representation doesn't include fields like description or citation
										# so we just test that we can recover what we can from the yaml
										__lowerCAmelCase						 =DatasetInfo._from_yaml_dict(dataset_info._to_yaml_dict()						)
					assert dataset_infos_dict == reloaded

					if dataset_infos_dict:
										assert os.path.exists(os.path.join(__lowerCamelCase					, """README.md"""						)						)









import unittest

from transformers import is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
				from PIL import Image
else:






				class     __a			:



					@staticmethod
					def 		UpperCamelCase				(    *snake_case_				:     Any					,						**snake_case_				:     str)->				int:
										pass






@is_pipeline_test
@require_vision
class     __a			(  unittest.TestCase					):



	@require_torch
	def 		UpperCamelCase				(    self				:     Dict)->				List[str]:
						__lowerCAmelCase						 =pipeline(
						    model="""hf-internal-testing/tiny-random-clip-zero-shot-image-classification"""					,						)
						__lowerCAmelCase						 =Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
						__lowerCAmelCase						 =image_classifier(snake_case_					,						candidate_labels=["""a""", """b""", """c"""])

						# The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
						# python and torch versions.
						self.assertIn(
						    nested_simplify(snake_case_)					,						[
						        [{"""score""": 0.3_3_3, """label""": """a"""}, {"""score""": 0.3_3_3, """label""": """b"""}, {"""score""": 0.3_3_3, """label""": """c"""}],
						        [{"""score""": 0.3_3_3, """label""": """a"""}, {"""score""": 0.3_3_3, """label""": """c"""}, {"""score""": 0.3_3_3, """label""": """b"""}],
						    ]					,						)

						__lowerCAmelCase						 =image_classifier([image] * 5					,						candidate_labels=["""A""", """B""", """C"""]					,						batch_size=2)
						self.assertEqual(
						    nested_simplify(snake_case_)					,						[
						        [
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						        ],
						        [
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						        ],
						        [
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						        ],
						        [
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						        ],
						        [
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						        ],
						    ]					,						)



	@require_tf
	def 		UpperCamelCase				(    self				:     Dict)->				Optional[Any]:
						__lowerCAmelCase						 =pipeline(
						    model="""hf-internal-testing/tiny-random-clip-zero-shot-image-classification"""					,						framework="""tf""")
						__lowerCAmelCase						 =Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
						__lowerCAmelCase						 =image_classifier(snake_case_					,						candidate_labels=["""a""", """b""", """c"""])

						self.assertEqual(
						    nested_simplify(snake_case_)					,						[{"""score""": 0.3_3_3, """label""": """a"""}, {"""score""": 0.3_3_3, """label""": """b"""}, {"""score""": 0.3_3_3, """label""": """c"""}]					,						)

						__lowerCAmelCase						 =image_classifier([image] * 5					,						candidate_labels=["""A""", """B""", """C"""]					,						batch_size=2)
						self.assertEqual(
						    nested_simplify(snake_case_)					,						[
						        [
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						        ],
						        [
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						        ],
						        [
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						        ],
						        [
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						        ],
						        [
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						            {"""score""": 0.3_3_3, """label""": ANY(snake_case_)},
						        ],
						    ]					,						)



	@slow
	@require_torch
	def 		UpperCamelCase				(    self				:     Any)->				Dict:
						__lowerCAmelCase						 =pipeline(
						    task="""zero-shot-image-classification"""					,						model="""openai/clip-vit-base-patch32"""					,						)
						# This is an image of 2 cats with remotes and no planes
						__lowerCAmelCase						 =Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
						__lowerCAmelCase						 =image_classifier(snake_case_					,						candidate_labels=["""cat""", """plane""", """remote"""])

						self.assertEqual(
						    nested_simplify(snake_case_)					,						[
						        {"""score""": 0.5_1_1, """label""": """remote"""},
						        {"""score""": 0.4_8_5, """label""": """cat"""},
						        {"""score""": 0.0_0_4, """label""": """plane"""},
						    ]					,						)

						__lowerCAmelCase						 =image_classifier([image] * 5					,						candidate_labels=["""cat""", """plane""", """remote"""]					,						batch_size=2)
						self.assertEqual(
						    nested_simplify(snake_case_)					,						[
						        [
						            {"""score""": 0.5_1_1, """label""": """remote"""},
						            {"""score""": 0.4_8_5, """label""": """cat"""},
						            {"""score""": 0.0_0_4, """label""": """plane"""},
						        ],
						    ]
						    * 5					,						)



	@slow
	@require_tf
	def 		UpperCamelCase				(    self				:     Optional[int])->				int:
						__lowerCAmelCase						 =pipeline(
						    task="""zero-shot-image-classification"""					,						model="""openai/clip-vit-base-patch32"""					,						framework="""tf""")
						# This is an image of 2 cats with remotes and no planes
						__lowerCAmelCase						 =Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
						__lowerCAmelCase						 =image_classifier(snake_case_					,						candidate_labels=["""cat""", """plane""", """remote"""])
						self.assertEqual(
						    nested_simplify(snake_case_)					,						[
						        {"""score""": 0.5_1_1, """label""": """remote"""},
						        {"""score""": 0.4_8_5, """label""": """cat"""},
						        {"""score""": 0.0_0_4, """label""": """plane"""},
						    ]					,						)

						__lowerCAmelCase						 =image_classifier([image] * 5					,						candidate_labels=["""cat""", """plane""", """remote"""]					,						batch_size=2)
						self.assertEqual(
						    nested_simplify(snake_case_)					,						[
						        [
						            {"""score""": 0.5_1_1, """label""": """remote"""},
						            {"""score""": 0.4_8_5, """label""": """cat"""},
						            {"""score""": 0.0_0_4, """label""": """plane"""},
						        ],
						    ]
						    * 5					,						)




