





import unittest

from transformers import (
    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    PreTrainedTokenizer,
    is_vision_available,
)
from transformers.pipelines import ImageClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
	from PIL import Image
else:




	class 					UpperCamelCase_    :


								@staticmethod
								def        _snake_case					(							*__A      :Optional[Any]							,			**__A      :Any						)				->				str:



										"""simple docstring"""



										pass




@is_pipeline_test
@require_torch_or_tf
@require_vision
class 					UpperCamelCase_    (       unittest.TestCase      ):
							lowerCamelCase_          =   MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
							lowerCamelCase_          =   TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING


							def        _snake_case					(							self      :Union[str, Any]							,			__A      :str							,			__A      :Dict							,			__A      :List[str]						)				->				Optional[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= ImageClassificationPipeline(model=__A							,			image_processor=__A							,			top_k=2						)
									SCREAMING_SNAKE_CASE__								= [
									    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						),
									    """http://images.cocodataset.org/val2017/000000039769.jpg""",
									]
									return image_classifier, examples


							def        _snake_case					(							self      :List[Any]							,			__A      :Any							,			__A      :List[str]						)				->				Any:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= image_classifier("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)

									self.assertEqual(
									    __A							,			[
									        {"""score""": ANY(__A						), """label""": ANY(__A						)},
									        {"""score""": ANY(__A						), """label""": ANY(__A						)},
									    ]							,			)

									import datasets

									SCREAMING_SNAKE_CASE__								= datasets.load_dataset("""hf-internal-testing/fixtures_image_utils"""							,			"""image"""							,			split="""test"""						)

									# Accepts URL + PIL.Image + lists
									SCREAMING_SNAKE_CASE__								= image_classifier(
									    [
									        Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						),
									        """http://images.cocodataset.org/val2017/000000039769.jpg""",
									        # RGBA
									        dataset[0]["""file"""],
									        # LA
									        dataset[1]["""file"""],
									        # L
									        dataset[2]["""file"""],
									    ]						)
									self.assertEqual(
									    __A							,			[
									        [
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									        ],
									        [
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									        ],
									        [
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									        ],
									        [
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									        ],
									        [
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									        ],
									    ]							,			)


							@require_torch
							def        _snake_case					(							self      :str						)				->				Optional[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= """hf-internal-testing/tiny-random-vit"""
									SCREAMING_SNAKE_CASE__								= pipeline("""image-classification"""							,			model=__A						)

									SCREAMING_SNAKE_CASE__								= image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""						)
									self.assertEqual(
									    nested_simplify(__A							,			decimals=4						)							,			[{"""label""": """LABEL_1""", """score""": 0.5_7_4}, {"""label""": """LABEL_0""", """score""": 0.4_2_6}]							,			)

									SCREAMING_SNAKE_CASE__								= image_classifier(
									    [
									        """http://images.cocodataset.org/val2017/000000039769.jpg""",
									        """http://images.cocodataset.org/val2017/000000039769.jpg""",
									    ]							,			top_k=2							,			)
									self.assertEqual(
									    nested_simplify(__A							,			decimals=4						)							,			[
									        [{"""label""": """LABEL_1""", """score""": 0.5_7_4}, {"""label""": """LABEL_0""", """score""": 0.4_2_6}],
									        [{"""label""": """LABEL_1""", """score""": 0.5_7_4}, {"""label""": """LABEL_0""", """score""": 0.4_2_6}],
									    ]							,			)


							@require_tf
							def        _snake_case					(							self      :Any						)				->				Dict:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= """hf-internal-testing/tiny-random-vit"""
									SCREAMING_SNAKE_CASE__								= pipeline("""image-classification"""							,			model=__A							,			framework="""tf"""						)

									SCREAMING_SNAKE_CASE__								= image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""						)
									self.assertEqual(
									    nested_simplify(__A							,			decimals=4						)							,			[{"""label""": """LABEL_1""", """score""": 0.5_7_4}, {"""label""": """LABEL_0""", """score""": 0.4_2_6}]							,			)

									SCREAMING_SNAKE_CASE__								= image_classifier(
									    [
									        """http://images.cocodataset.org/val2017/000000039769.jpg""",
									        """http://images.cocodataset.org/val2017/000000039769.jpg""",
									    ]							,			top_k=2							,			)
									self.assertEqual(
									    nested_simplify(__A							,			decimals=4						)							,			[
									        [{"""label""": """LABEL_1""", """score""": 0.5_7_4}, {"""label""": """LABEL_0""", """score""": 0.4_2_6}],
									        [{"""label""": """LABEL_1""", """score""": 0.5_7_4}, {"""label""": """LABEL_0""", """score""": 0.4_2_6}],
									    ]							,			)


							def        _snake_case					(							self      :Dict						)				->				str:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= PreTrainedTokenizer()

									# Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
									SCREAMING_SNAKE_CASE__								= pipeline(
									    """image-classification"""							,			model="""hf-internal-testing/tiny-random-vit"""							,			tokenizer=__A						)

									self.assertIs(image_classifier.tokenizer							,			__A						)


							@slow
							@require_torch
							def        _snake_case					(							self      :Optional[int]						)				->				Optional[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= pipeline("""image-classification"""							,			model="""deepmind/vision-perceiver-conv"""						)
									SCREAMING_SNAKE_CASE__								= image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""						)
									self.assertEqual(
									    nested_simplify(__A							,			decimals=4						)							,			[
									        {"""score""": 0.4_3_8_5, """label""": """tabby, tabby cat"""},
									        {"""score""": 0.3_2_1, """label""": """tiger cat"""},
									        {"""score""": 0.0_5_0_2, """label""": """Egyptian cat"""},
									        {"""score""": 0.0_1_3_7, """label""": """crib, cot"""},
									        {"""score""": 0.0_0_7, """label""": """radiator"""},
									    ]							,			)

									SCREAMING_SNAKE_CASE__								= pipeline("""image-classification"""							,			model="""deepmind/vision-perceiver-fourier"""						)
									SCREAMING_SNAKE_CASE__								= image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""						)
									self.assertEqual(
									    nested_simplify(__A							,			decimals=4						)							,			[
									        {"""score""": 0.5_6_5_8, """label""": """tabby, tabby cat"""},
									        {"""score""": 0.1_3_0_9, """label""": """tiger cat"""},
									        {"""score""": 0.0_7_2_2, """label""": """Egyptian cat"""},
									        {"""score""": 0.0_7_0_7, """label""": """remote control, remote"""},
									        {"""score""": 0.0_0_8_2, """label""": """computer keyboard, keypad"""},
									    ]							,			)

									SCREAMING_SNAKE_CASE__								= pipeline("""image-classification"""							,			model="""deepmind/vision-perceiver-learned"""						)
									SCREAMING_SNAKE_CASE__								= image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""						)
									self.assertEqual(
									    nested_simplify(__A							,			decimals=4						)							,			[
									        {"""score""": 0.3_0_2_2, """label""": """tabby, tabby cat"""},
									        {"""score""": 0.2_3_6_2, """label""": """Egyptian cat"""},
									        {"""score""": 0.1_8_5_6, """label""": """tiger cat"""},
									        {"""score""": 0.0_3_2_4, """label""": """remote control, remote"""},
									        {"""score""": 0.0_0_9_6, """label""": """quilt, comforter, comfort, puff"""},
									    ]							,			)






from typing import TYPE_CHECKING

from ..utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available


_lowerCamelCase							=     {
    'configuration_utils': ['GenerationConfig'],
    'streamers': ['TextIteratorStreamer', 'TextStreamer'],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase							=     [
	    'Constraint',
	    'ConstraintListState',
	    'DisjunctiveConstraint',
	    'PhrasalConstraint',
	]
	_lowerCamelCase							=     [
	    'BeamHypotheses',
	    'BeamScorer',
	    'BeamSearchScorer',
	    'ConstrainedBeamSearchScorer',
	]
	_lowerCamelCase							=     [
	    'EpsilonLogitsWarper',
	    'EtaLogitsWarper',
	    'ForcedBOSTokenLogitsProcessor',
	    'ForcedEOSTokenLogitsProcessor',
	    'HammingDiversityLogitsProcessor',
	    'InfNanRemoveLogitsProcessor',
	    'LogitsProcessor',
	    'LogitsProcessorList',
	    'LogitsWarper',
	    'MinLengthLogitsProcessor',
	    'MinNewTokensLengthLogitsProcessor',
	    'NoBadWordsLogitsProcessor',
	    'NoRepeatNGramLogitsProcessor',
	    'PrefixConstrainedLogitsProcessor',
	    'RepetitionPenaltyLogitsProcessor',
	    'SequenceBiasLogitsProcessor',
	    'EncoderRepetitionPenaltyLogitsProcessor',
	    'TemperatureLogitsWarper',
	    'TopKLogitsWarper',
	    'TopPLogitsWarper',
	    'TypicalLogitsWarper',
	    'EncoderNoRepeatNGramLogitsProcessor',
	    'ExponentialDecayLengthPenalty',
	    'LogitNormalization',
	]
	_lowerCamelCase							=     [
	    'MaxNewTokensCriteria',
	    'MaxLengthCriteria',
	    'MaxTimeCriteria',
	    'StoppingCriteria',
	    'StoppingCriteriaList',
	    'validate_stopping_criteria',
	]
	_lowerCamelCase							=     [
	    'GenerationMixin',
	    'top_k_top_p_filtering',
	    'GreedySearchEncoderDecoderOutput',
	    'GreedySearchDecoderOnlyOutput',
	    'SampleEncoderDecoderOutput',
	    'SampleDecoderOnlyOutput',
	    'BeamSearchEncoderDecoderOutput',
	    'BeamSearchDecoderOnlyOutput',
	    'BeamSampleEncoderDecoderOutput',
	    'BeamSampleDecoderOnlyOutput',
	    'ContrastiveSearchEncoderDecoderOutput',
	    'ContrastiveSearchDecoderOnlyOutput',
	]

try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase							=     [
	    'TFForcedBOSTokenLogitsProcessor',
	    'TFForcedEOSTokenLogitsProcessor',
	    'TFLogitsProcessor',
	    'TFLogitsProcessorList',
	    'TFLogitsWarper',
	    'TFMinLengthLogitsProcessor',
	    'TFNoBadWordsLogitsProcessor',
	    'TFNoRepeatNGramLogitsProcessor',
	    'TFRepetitionPenaltyLogitsProcessor',
	    'TFTemperatureLogitsWarper',
	    'TFTopKLogitsWarper',
	    'TFTopPLogitsWarper',
	    'TFForceTokensLogitsProcessor',
	    'TFSuppressTokensAtBeginLogitsProcessor',
	    'TFSuppressTokensLogitsProcessor',
	]
	_lowerCamelCase							=     [
	    'TFGenerationMixin',
	    'tf_top_k_top_p_filtering',
	    'TFGreedySearchDecoderOnlyOutput',
	    'TFGreedySearchEncoderDecoderOutput',
	    'TFSampleEncoderDecoderOutput',
	    'TFSampleDecoderOnlyOutput',
	    'TFBeamSearchEncoderDecoderOutput',
	    'TFBeamSearchDecoderOnlyOutput',
	    'TFBeamSampleEncoderDecoderOutput',
	    'TFBeamSampleDecoderOnlyOutput',
	    'TFContrastiveSearchEncoderDecoderOutput',
	    'TFContrastiveSearchDecoderOnlyOutput',
	]

try:
	if not is_flax_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase							=     [
	    'FlaxForcedBOSTokenLogitsProcessor',
	    'FlaxForcedEOSTokenLogitsProcessor',
	    'FlaxLogitsProcessor',
	    'FlaxLogitsProcessorList',
	    'FlaxLogitsWarper',
	    'FlaxMinLengthLogitsProcessor',
	    'FlaxTemperatureLogitsWarper',
	    'FlaxTopKLogitsWarper',
	    'FlaxTopPLogitsWarper',
	]
	_lowerCamelCase							=     [
	    'FlaxGenerationMixin',
	    'FlaxGreedySearchOutput',
	    'FlaxSampleOutput',
	    'FlaxBeamSearchOutput',
	]

if TYPE_CHECKING:
	from .configuration_utils import GenerationConfig
	from .streamers import TextIteratorStreamer, TextStreamer

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .beam_constraints import Constraint, ConstraintListState, DisjunctiveConstraint, PhrasalConstraint
		from .beam_search import BeamHypotheses, BeamScorer, BeamSearchScorer, ConstrainedBeamSearchScorer
		from .logits_process import (
		    EncoderNoRepeatNGramLogitsProcessor,
		    EncoderRepetitionPenaltyLogitsProcessor,
		    EpsilonLogitsWarper,
		    EtaLogitsWarper,
		    ExponentialDecayLengthPenalty,
		    ForcedBOSTokenLogitsProcessor,
		    ForcedEOSTokenLogitsProcessor,
		    HammingDiversityLogitsProcessor,
		    InfNanRemoveLogitsProcessor,
		    LogitNormalization,
		    LogitsProcessor,
		    LogitsProcessorList,
		    LogitsWarper,
		    MinLengthLogitsProcessor,
		    MinNewTokensLengthLogitsProcessor,
		    NoBadWordsLogitsProcessor,
		    NoRepeatNGramLogitsProcessor,
		    PrefixConstrainedLogitsProcessor,
		    RepetitionPenaltyLogitsProcessor,
		    SequenceBiasLogitsProcessor,
		    TemperatureLogitsWarper,
		    TopKLogitsWarper,
		    TopPLogitsWarper,
		    TypicalLogitsWarper,
		)
		from .stopping_criteria import (
		    MaxLengthCriteria,
		    MaxNewTokensCriteria,
		    MaxTimeCriteria,
		    StoppingCriteria,
		    StoppingCriteriaList,
		    validate_stopping_criteria,
		)
		from .utils import (
		    BeamSampleDecoderOnlyOutput,
		    BeamSampleEncoderDecoderOutput,
		    BeamSearchDecoderOnlyOutput,
		    BeamSearchEncoderDecoderOutput,
		    ContrastiveSearchDecoderOnlyOutput,
		    ContrastiveSearchEncoderDecoderOutput,
		    GenerationMixin,
		    GreedySearchDecoderOnlyOutput,
		    GreedySearchEncoderDecoderOutput,
		    SampleDecoderOnlyOutput,
		    SampleEncoderDecoderOutput,
		    top_k_top_p_filtering,
		)

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tf_logits_process import (
		    TFForcedBOSTokenLogitsProcessor,
		    TFForcedEOSTokenLogitsProcessor,
		    TFForceTokensLogitsProcessor,
		    TFLogitsProcessor,
		    TFLogitsProcessorList,
		    TFLogitsWarper,
		    TFMinLengthLogitsProcessor,
		    TFNoBadWordsLogitsProcessor,
		    TFNoRepeatNGramLogitsProcessor,
		    TFRepetitionPenaltyLogitsProcessor,
		    TFSuppressTokensAtBeginLogitsProcessor,
		    TFSuppressTokensLogitsProcessor,
		    TFTemperatureLogitsWarper,
		    TFTopKLogitsWarper,
		    TFTopPLogitsWarper,
		)
		from .tf_utils import (
		    TFBeamSampleDecoderOnlyOutput,
		    TFBeamSampleEncoderDecoderOutput,
		    TFBeamSearchDecoderOnlyOutput,
		    TFBeamSearchEncoderDecoderOutput,
		    TFContrastiveSearchDecoderOnlyOutput,
		    TFContrastiveSearchEncoderDecoderOutput,
		    TFGenerationMixin,
		    TFGreedySearchDecoderOnlyOutput,
		    TFGreedySearchEncoderDecoderOutput,
		    TFSampleDecoderOnlyOutput,
		    TFSampleEncoderDecoderOutput,
		    tf_top_k_top_p_filtering,
		)

	try:
		if not is_flax_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .flax_logits_process import (
		    FlaxForcedBOSTokenLogitsProcessor,
		    FlaxForcedEOSTokenLogitsProcessor,
		    FlaxLogitsProcessor,
		    FlaxLogitsProcessorList,
		    FlaxLogitsWarper,
		    FlaxMinLengthLogitsProcessor,
		    FlaxTemperatureLogitsWarper,
		    FlaxTopKLogitsWarper,
		    FlaxTopPLogitsWarper,
		)
		from .flax_utils import FlaxBeamSearchOutput, FlaxGenerationMixin, FlaxGreedySearchOutput, FlaxSampleOutput
else:
	import sys

	_lowerCamelCase							=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)