





import unittest

from datasets import load_dataset

from transformers.pipelines import pipeline
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_torch, slow







@is_pipeline_test
@require_torch
class   _UpperCamelCase  (       unittest.TestCase  ):







					'''simple docstring'''

					@require_torch
					def        SCREAMING_SNAKE_CASE							(						self    :  str		):






						'''simple docstring'''







						UpperCAmelCase_								=     pipeline(
						    task="""zero-shot-audio-classification""" ,					model="""hf-internal-testing/tiny-clap-htsat-unfused"""		)
						UpperCAmelCase_								=     load_dataset("""ashraq/esc50"""		)
						UpperCAmelCase_								=     dataset['''train''']['''audio'''][-1]['''array''']
						UpperCAmelCase_								=     audio_classifier(__lowercase ,					candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""]		)
						self.assertEqual(
						    nested_simplify(__lowercase		) ,					[{"""score""": 0.501, """label""": """Sound of a dog"""}, {"""score""": 0.499, """label""": """Sound of vaccum cleaner"""}] ,					)





					@unittest.skip("""No models are available in TF"""		)
					def        SCREAMING_SNAKE_CASE							(						self    :  Tuple		):






						'''simple docstring'''







						pass





					@slow
					@require_torch
					def        SCREAMING_SNAKE_CASE							(						self    :  int		):






						'''simple docstring'''







						UpperCAmelCase_								=     pipeline(
						    task="""zero-shot-audio-classification""" ,					model="""laion/clap-htsat-unfused""" ,					)
						# This is an audio of a dog
						UpperCAmelCase_								=     load_dataset("""ashraq/esc50"""		)
						UpperCAmelCase_								=     dataset['''train''']['''audio'''][-1]['''array''']
						UpperCAmelCase_								=     audio_classifier(__lowercase ,					candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""]		)

						self.assertEqual(
						    nested_simplify(__lowercase		) ,					[
						        {"""score""": 0.999, """label""": """Sound of a dog"""},
						        {"""score""": 0.001, """label""": """Sound of vaccum cleaner"""},
						    ] ,					)

						UpperCAmelCase_								=     audio_classifier([audio] * 5 ,					candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""]		)
						self.assertEqual(
						    nested_simplify(__lowercase		) ,					[
						        [
						            {"""score""": 0.999, """label""": """Sound of a dog"""},
						            {"""score""": 0.001, """label""": """Sound of vaccum cleaner"""},
						        ],
						    ]
						    * 5 ,					)
						UpperCAmelCase_								=     audio_classifier(
						    [audio] * 5 ,					candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""] ,					batch_size=5		)
						self.assertEqual(
						    nested_simplify(__lowercase		) ,					[
						        [
						            {"""score""": 0.999, """label""": """Sound of a dog"""},
						            {"""score""": 0.001, """label""": """Sound of vaccum cleaner"""},
						        ],
						    ]
						    * 5 ,					)





					@unittest.skip("""No models are available in TF"""		)
					def        SCREAMING_SNAKE_CASE							(						self    :  Dict		):






						'''simple docstring'''







						pass









import pytest

from datasets import Dataset, DatasetDict, Features, NamedSplit, Value
from datasets.io.text import TextDatasetReader

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases




def    A_(			A				,      A   ):
	assert isinstance(A				,      A   )
	assert dataset.num_rows == 4
	assert dataset.num_columns == 1
	assert dataset.column_names == ["text"]
	for feature, expected_dtype in expected_features.items():
		assert dataset.features[feature].dtype == expected_dtype




@pytest.mark.parametrize("""keep_in_memory"""				,      [False, True]   )
def    A_(			A				,      A				,      A   ):
	UpperCAmelCase_								=     tmp_path / """cache"""
	UpperCAmelCase_								=     {"""text""": """string"""}
	with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
		UpperCAmelCase_								=     TextDatasetReader(A				,      cache_dir=A				,      keep_in_memory=A   ).read()
	_check_text_dataset(A				,      A   )




@pytest.mark.parametrize(
    """features"""				,      [
        None,
        {"""text""": """string"""},
        {"""text""": """int32"""},
        {"""text""": """float32"""},
    ]				,      )
def    A_(			A				,      A				,      A   ):
	UpperCAmelCase_								=     tmp_path / """cache"""
	UpperCAmelCase_								=     {"""text""": """string"""}
	UpperCAmelCase_								=     features.copy() if features else default_expected_features
	UpperCAmelCase_								=     (
	    Features({feature: Value(A   ) for feature, dtype in features.items()}   ) if features is not None else None
	)
	UpperCAmelCase_								=     TextDatasetReader(A				,      features=A				,      cache_dir=A   ).read()
	_check_text_dataset(A				,      A   )




@pytest.mark.parametrize("""split"""				,      [None, NamedSplit("""train"""   ), """train""", """test"""]   )
def    A_(			A				,      A				,      A   ):
	UpperCAmelCase_								=     tmp_path / """cache"""
	UpperCAmelCase_								=     {"""text""": """string"""}
	UpperCAmelCase_								=     TextDatasetReader(A				,      cache_dir=A				,      split=A   ).read()
	_check_text_dataset(A				,      A   )
	assert dataset.split == split if split else "train"




@pytest.mark.parametrize("""path_type"""				,      [str, list]   )
def    A_(			A				,      A				,      A   ):
	if issubclass(A				,      A   ):
		UpperCAmelCase_								=     text_path
	elif issubclass(A				,      A   ):
		UpperCAmelCase_								=     [text_path]
	UpperCAmelCase_								=     tmp_path / """cache"""
	UpperCAmelCase_								=     {"""text""": """string"""}
	UpperCAmelCase_								=     TextDatasetReader(A				,      cache_dir=A   ).read()
	_check_text_dataset(A				,      A   )




def    A_(			A				,      A				,      A=("train",)   ):
	assert isinstance(A				,      A   )
	for split in splits:
		UpperCAmelCase_								=     dataset_dict[split]
		assert dataset.num_rows == 4
		assert dataset.num_columns == 1
		assert dataset.column_names == ["text"]
		for feature, expected_dtype in expected_features.items():
			assert dataset.features[feature].dtype == expected_dtype




@pytest.mark.parametrize("""keep_in_memory"""				,      [False, True]   )
def    A_(			A				,      A				,      A   ):
	UpperCAmelCase_								=     tmp_path / """cache"""
	UpperCAmelCase_								=     {"""text""": """string"""}
	with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
		UpperCAmelCase_								=     TextDatasetReader({"""train""": text_path}				,      cache_dir=A				,      keep_in_memory=A   ).read()
	_check_text_datasetdict(A				,      A   )




@pytest.mark.parametrize(
    """features"""				,      [
        None,
        {"""text""": """string"""},
        {"""text""": """int32"""},
        {"""text""": """float32"""},
    ]				,      )
def    A_(			A				,      A				,      A   ):
	UpperCAmelCase_								=     tmp_path / """cache"""
	# CSV file loses col_1 string dtype information: default now is "int64" instead of "string"
	UpperCAmelCase_								=     {"""text""": """string"""}
	UpperCAmelCase_								=     features.copy() if features else default_expected_features
	UpperCAmelCase_								=     (
	    Features({feature: Value(A   ) for feature, dtype in features.items()}   ) if features is not None else None
	)
	UpperCAmelCase_								=     TextDatasetReader({"""train""": text_path}				,      features=A				,      cache_dir=A   ).read()
	_check_text_datasetdict(A				,      A   )




@pytest.mark.parametrize("""split"""				,      [None, NamedSplit("""train"""   ), """train""", """test"""]   )
def    A_(			A				,      A				,      A   ):
	if split:
		UpperCAmelCase_								=     {split: text_path}
	else:
		UpperCAmelCase_								=     """train"""
		UpperCAmelCase_								=     {"""train""": text_path, """test""": text_path}
	UpperCAmelCase_								=     tmp_path / """cache"""
	UpperCAmelCase_								=     {"""text""": """string"""}
	UpperCAmelCase_								=     TextDatasetReader(A				,      cache_dir=A   ).read()
	_check_text_datasetdict(A				,      A				,      splits=list(path.keys()   )   )
	assert all(dataset[split].split == split for split in path.keys()   )


