

from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
	from PIL import Image

	from ..image_utils import load_image

if is_tf_available():
	import tensorflow as tf

	from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
	from ..tf_utils import stable_softmax

if is_torch_available():
	from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

__A     :					int     = logging.get_logger(__name__)


@add_end_docstrings(lowercase__)
class        _SCREAMING_SNAKE_CASE	(   lowercase__):







						def __init__(       self			,		*_SCREAMING_SNAKE_CASE			,		**_SCREAMING_SNAKE_CASE				)->				List[Any]:
													super().__init__(*SCREAMING_SNAKE_CASE__			,		**SCREAMING_SNAKE_CASE__				)
													requires_backends(self			,		"""vision"""				)
													self.check_model_type(
													    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
													    if self.framework == """tf"""
													    else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING				)







						def     _snake_case							(       self			,		_SCREAMING_SNAKE_CASE=None				)->				List[Any]:
													lowerCamelCase_				     ={}
													if top_k is not None:
																				lowerCamelCase_				     =top_k
													return {}, {}, postprocess_params







						def __call__(       self			,		_SCREAMING_SNAKE_CASE			,		**_SCREAMING_SNAKE_CASE				)->				Tuple:
													return super().__call__(SCREAMING_SNAKE_CASE__			,		**SCREAMING_SNAKE_CASE__				)







						def     _snake_case							(       self			,		_SCREAMING_SNAKE_CASE				)->				Union[str, Any]:
													lowerCamelCase_				     =load_image(SCREAMING_SNAKE_CASE__				)
													lowerCamelCase_				     =self.image_processor(images=SCREAMING_SNAKE_CASE__			,		return_tensors=self.framework				)
													return model_inputs







						def     _snake_case							(       self			,		_SCREAMING_SNAKE_CASE				)->				Dict:
													lowerCamelCase_				     =self.model(**SCREAMING_SNAKE_CASE__				)
													return model_outputs







						def     _snake_case							(       self			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE=5				)->				Any:
													if top_k > self.model.config.num_labels:
																				lowerCamelCase_				     =self.model.config.num_labels

													if self.framework == "pt":
																				lowerCamelCase_				     =model_outputs.logits.softmax(-1				)[0]
																				lowerCamelCase_				     =probs.topk(SCREAMING_SNAKE_CASE__				)
													elif self.framework == "tf":
																				lowerCamelCase_				     =stable_softmax(model_outputs.logits			,		axis=-1				)[0]
																				lowerCamelCase_				     =tf.math.top_k(SCREAMING_SNAKE_CASE__			,		k=SCREAMING_SNAKE_CASE__				)
																				lowerCamelCase_				     =topk.values.numpy(), topk.indices.numpy()
													else:
																				raise ValueError(f'Unsupported framework: {self.framework}'				)

													lowerCamelCase_				     =scores.tolist()
													lowerCamelCase_				     =ids.tolist()
													return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(SCREAMING_SNAKE_CASE__			,		SCREAMING_SNAKE_CASE__				)]




import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class  SCREAMING_SNAKE_CASE__  (				lowercase__			):
      snake_case__				:     Tuple        =						['''image_processor''', '''tokenizer''']
      snake_case__				:     Union[str, Any]        =						'''CLIPImageProcessor'''
      snake_case__				:     Dict        =						('''CLIPTokenizer''', '''CLIPTokenizerFast''')
      def __init__(       self				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=None     ,							SCREAMING_SNAKE_CASE__				:       List[str]=None     ,							**SCREAMING_SNAKE_CASE__				:       int     )		->			Any:
             a_					:		List[Any]							=  None
             if "feature_extractor" in kwargs:
                    warnings.warn(
                        'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                        ' instead.'     ,							SCREAMING_SNAKE_CASE__     ,							)
                    a_					:		Tuple							=  kwargs.pop('feature_extractor'     )

             a_					:		Tuple							=  image_processor if image_processor is not None else feature_extractor
             if image_processor is None:
                    raise ValueError('You need to specify an `image_processor`.'     )
             if tokenizer is None:
                    raise ValueError('You need to specify a `tokenizer`.'     )

             super().__init__(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )
      def __call__(       self				:       List[Any]     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]=None     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=None     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=None     ,							**SCREAMING_SNAKE_CASE__				:       str     )		->			Optional[Any]:

             if text is None and images is None:
                    raise ValueError('You have to specify either text or images. Both cannot be none.'     )

             if text is not None:
                    a_					:		List[str]							=  self.tokenizer(SCREAMING_SNAKE_CASE__     ,							return_tensors=SCREAMING_SNAKE_CASE__     ,							**SCREAMING_SNAKE_CASE__     )

             if images is not None:
                    a_					:		Dict							=  self.image_processor(SCREAMING_SNAKE_CASE__     ,							return_tensors=SCREAMING_SNAKE_CASE__     ,							**SCREAMING_SNAKE_CASE__     )

             if text is not None and images is not None:
                    a_					:		Dict							=  image_features.pixel_values
                    return encoding
             elif text is not None:
                    return encoding
             else:
                    return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE__     )     ,							tensor_type=SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     ,							*SCREAMING_SNAKE_CASE__				:       List[Any]     ,							**SCREAMING_SNAKE_CASE__				:       Optional[int]     )		->			List[Any]:
             return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE__     ,							**SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     ,							*SCREAMING_SNAKE_CASE__				:       List[Any]     ,							**SCREAMING_SNAKE_CASE__				:       Dict     )		->			Optional[Any]:
             return self.tokenizer.decode(*SCREAMING_SNAKE_CASE__     ,							**SCREAMING_SNAKE_CASE__     )
      @property
      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     )		->			Any:
             a_					:		str							=  self.tokenizer.model_input_names
             a_					:		Tuple							=  self.image_processor.model_input_names
             return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names     )     )
      @property
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			str:
             warnings.warn(
                 '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'     ,							SCREAMING_SNAKE_CASE__     ,							)
             return self.image_processor_class

      @property
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			Tuple:
             warnings.warn(
                 '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'     ,							SCREAMING_SNAKE_CASE__     ,							)
             return self.image_processor


