



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

__snake_case						:Optional[Any]						=					logging.get_logger(__name__)

@add_end_docstrings(snake_case__   )
class    _A	(     snake_case__   ):



       def __init__(		self							:					Union[str, Any]					,		*__SCREAMING_SNAKE_CASE							:					Dict					,		**__SCREAMING_SNAKE_CASE							:					Optional[int]):


           '''simple docstring'''

           super().__init__(*UpperCAmelCase_					,		**UpperCAmelCase_)
           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 		_lowerCamelCase      (		self							:					Dict					,		__SCREAMING_SNAKE_CASE							:					List[Any]=None):


           '''simple docstring'''

           __a							=    {}
           if top_k is not None:
               __a							=    top_k
           return {}, {}, postprocess_params



       def __call__(		self							:					Optional[int]					,		__SCREAMING_SNAKE_CASE							:					Union[str, List[str], "Image.Image", List["Image.Image"]]					,		**__SCREAMING_SNAKE_CASE							:					int):


           '''simple docstring'''

           return super().__call__(UpperCAmelCase_					,		**UpperCAmelCase_)



       def 		_lowerCamelCase      (		self							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					Tuple):


           '''simple docstring'''

           __a							=    load_image(UpperCAmelCase_)
           __a							=    self.image_processor(images=UpperCAmelCase_					,		return_tensors=self.framework)
           return model_inputs



       def 		_lowerCamelCase      (		self							:					Union[str, Any]					,		__SCREAMING_SNAKE_CASE							:					str):


           '''simple docstring'''

           __a							=    self.model(**UpperCAmelCase_)
           return model_outputs





       def 		_lowerCamelCase      (		self							:					Union[str, Any]					,		__SCREAMING_SNAKE_CASE							:					Tuple					,		__SCREAMING_SNAKE_CASE							:					List[Any]=5):


           '''simple docstring'''

           if top_k > self.model.config.num_labels:
               __a							=    self.model.config.num_labels

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

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










# Lint as: python3
import itertools
import os
import re


__snake_case						:int						=					re.compile(r'''([A-Z]+)([A-Z][a-z])''')
__snake_case						:List[Any]						=					re.compile(r'''([a-z\d])([A-Z])''')

__snake_case						:Dict						=					re.compile(r'''(?<!_)_(?!_)''')
__snake_case						:Optional[int]						=					re.compile(r'''(_{2,})''')

__snake_case						:Optional[Any]						=					r'''^\w+(\.\w+)*$'''

__snake_case						:Union[str, Any]						=					r'''<>:/\|?*'''





def 					__snake_case     (      _UpperCAmelCase    ):
    __a							=    _uppercase_uppercase_re.sub(R'''\1_\2'''       ,			_UpperCAmelCase    )
    __a							=    _lowercase_uppercase_re.sub(R'''\1_\2'''       ,			_UpperCAmelCase    )
    return name.lower()





def 					__snake_case     (      _UpperCAmelCase    ):
    __a							=    _single_underscore_re.split(_UpperCAmelCase    )
    __a							=    [_multiple_underscores_re.split(_UpperCAmelCase    ) for n in name]
    return "".join(n.capitalize() for n in itertools.chain.from_iterable(_UpperCAmelCase    ) if n != ''''''    )





def 					__snake_case     (      _UpperCAmelCase    ):
    if os.path.basename(_UpperCAmelCase    ) != name:
        raise ValueError(f'Should be a dataset name, not a path: {name}'    )
    return camelcase_to_snakecase(_UpperCAmelCase    )





def 					__snake_case     (      _UpperCAmelCase       ,			_UpperCAmelCase    ):
    if os.path.basename(_UpperCAmelCase    ) != name:
        raise ValueError(f'Should be a dataset name, not a path: {name}'    )
    if not re.match(_split_re       ,			_UpperCAmelCase    ):
        raise ValueError(f'Split name should match \'{_split_re}\'\' but got \'{split}\'.'    )
    return f'{filename_prefix_for_name(_UpperCAmelCase    )}-{split}'





def 					__snake_case     (      _UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase=None    ):
    __a							=    filename_prefix_for_split(_UpperCAmelCase       ,			_UpperCAmelCase    )
    if filetype_suffix:
        prefix += f'.{filetype_suffix}'
    __a							=    os.path.join(_UpperCAmelCase       ,			_UpperCAmelCase    )
    return f'{filepath}*'





def 					__snake_case     (      _UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase=None       ,			_UpperCAmelCase=None    ):
    __a							=    filename_prefix_for_split(_UpperCAmelCase       ,			_UpperCAmelCase    )
    __a							=    os.path.join(_UpperCAmelCase       ,			_UpperCAmelCase    )

    if shard_lengths:
        __a							=    len(_UpperCAmelCase    )
        __a							=    [f'{prefix}-{shard_id:05d}-of-{num_shards:05d}' for shard_id in range(_UpperCAmelCase    )]
        if filetype_suffix:
            __a							=    [filename + f'.{filetype_suffix}' for filename in filenames]
        return filenames
    else:
        __a							=    prefix
        if filetype_suffix:
            filename += f'.{filetype_suffix}'
        return [filename]





