from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
    from PIL import Image

    from ..image_utils import load_image

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

__lowercase											=      logging.get_logger(__name__)


@add_end_docstrings(UpperCAmelCase_				)
class 			lowerCamelCase_  (     UpperCAmelCase_				):






    '''simple docstring'''






    def __init__(			self				,				*__lowercase				,				**__lowercase) ->			Tuple:
        super().__init__(*__lowercase				,				**__lowercase)
        self.check_model_type(__lowercase)







    def     UpperCamelCase__     (			self				,				__lowercase=None				,				__lowercase=None				,				__lowercase=None				,				**__lowercase) ->			Dict:
        __UpperCamelCase						, __UpperCamelCase  :int    						=	{}, {}
        if padding is not None:
            __UpperCamelCase  :Union[str, Any]    						=	padding
        if truncation is not None:
            __UpperCamelCase  :List[str]    						=	truncation
        if top_k is not None:
            __UpperCamelCase  :int    						=	top_k
        return preprocess_params, {}, postprocess_params







    def __call__(			self				,				__lowercase				,				__lowercase = None				,				**__lowercase) ->			int:
        if isinstance(__lowercase				,				(Image.Image, str)) and isinstance(__lowercase				,				__lowercase):
            __UpperCamelCase  :Tuple    						=	{'''image''': image, '''question''': question}
        else:
            __UpperCamelCase  :Dict    						=	image
        __UpperCamelCase  :List[Any]    						=	super().__call__(__lowercase				,				**__lowercase)
        return results







    def     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase=False				,				__lowercase=False) ->			Optional[int]:
        __UpperCamelCase  :Dict    						=	load_image(inputs['''image'''])
        __UpperCamelCase  :List[str]    						=	self.tokenizer(
            inputs['''question''']				,				return_tensors=self.framework				,				padding=__lowercase				,				truncation=__lowercase)
        __UpperCamelCase  :Dict    						=	self.image_processor(images=__lowercase				,				return_tensors=self.framework)
        model_inputs.update(__lowercase)
        return model_inputs







    def     UpperCamelCase__     (			self				,				__lowercase) ->			List[str]:
        __UpperCamelCase  :Tuple    						=	self.model(**__lowercase)
        return model_outputs







    def     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase=5) ->			List[str]:
        if top_k > self.model.config.num_labels:
            __UpperCamelCase  :List[Any]    						=	self.model.config.num_labels

        if self.framework == "pt":
            __UpperCamelCase  :List[Any]    						=	model_outputs.logits.sigmoid()[0]
            __UpperCamelCase						, __UpperCamelCase  :int    						=	probs.topk(__lowercase)
        else:
            raise ValueError(f"""Unsupported framework: {self.framework}""")

        __UpperCamelCase  :Tuple    						=	scores.tolist()
        __UpperCamelCase  :str    						=	ids.tolist()
        return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(__lowercase				,				__lowercase)]






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_speech_available, is_torch_available


__lowercase											=      {
    '''configuration_audio_spectrogram_transformer''': [
        '''AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''ASTConfig''',
    ]
}

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __lowercase											=      [
        '''AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
        '''ASTForAudioClassification''',
        '''ASTModel''',
        '''ASTPreTrainedModel''',
    ]

try:
    if not is_speech_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __lowercase											=      ['''ASTFeatureExtractor''']

if TYPE_CHECKING:
    from .configuration_audio_spectrogram_transformer import (
        AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
        ASTConfig,
    )

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_audio_spectrogram_transformer import (
            AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
            ASTForAudioClassification,
            ASTModel,
            ASTPreTrainedModel,
        )

    try:
        if not is_speech_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .feature_extraction_audio_spectrogram_transformer import ASTFeatureExtractor


else:
    import sys

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





