

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_lowerCamelCase    :       Optional[Any]        =							{
    "configuration_whisper": ["WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP", "WhisperConfig", "WhisperOnnxConfig"],
    "feature_extraction_whisper": ["WhisperFeatureExtractor"],
    "processing_whisper": ["WhisperProcessor"],
    "tokenization_whisper": ["WhisperTokenizer"],
}

try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_lowerCamelCase    :       List[str]        =							["WhisperTokenizerFast"]

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_lowerCamelCase    :       List[Any]        =							[
					    "WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "WhisperForConditionalGeneration",
					    "WhisperModel",
					    "WhisperPreTrainedModel",
					    "WhisperForAudioClassification",
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_lowerCamelCase    :       Tuple        =							[
					    "TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "TFWhisperForConditionalGeneration",
					    "TFWhisperModel",
					    "TFWhisperPreTrainedModel",
					]

try:
					if not is_flax_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_lowerCamelCase    :       List[str]        =							[
					    "FlaxWhisperForConditionalGeneration",
					    "FlaxWhisperModel",
					    "FlaxWhisperPreTrainedModel",
					    "FlaxWhisperForAudioClassification",
					]


if TYPE_CHECKING:
					from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig
					from .feature_extraction_whisper import WhisperFeatureExtractor
					from .processing_whisper import WhisperProcessor
					from .tokenization_whisper import WhisperTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_whisper_fast import WhisperTokenizerFast

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_whisper import (
										    WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST,
										    WhisperForAudioClassification,
										    WhisperForConditionalGeneration,
										    WhisperModel,
										    WhisperPreTrainedModel,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_whisper import (
										    TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFWhisperForConditionalGeneration,
										    TFWhisperModel,
										    TFWhisperPreTrainedModel,
										)

					try:
										if not is_flax_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_flax_whisper import (
										    FlaxWhisperForAudioClassification,
										    FlaxWhisperForConditionalGeneration,
										    FlaxWhisperModel,
										    FlaxWhisperPreTrainedModel,
										)

else:
					import sys

					_lowerCamelCase    :       int        =							_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)








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

_lowerCamelCase    :       Dict        =							logging.get_logger(__name__)





@add_end_docstrings(lowerCamelCase__						)
class  __UpperCAmelCase					(	lowerCamelCase__						):



   def __init__(							self							:					Optional[Any],		*__A							:					Tuple,		**__A							:					Tuple						):
     super().__init__(*__A,		**__A						)
     self.check_model_type(__A						)



   def 	__magic_name__   (							self							:					Union[str, Any],		__A							:					int=None,		__A							:					Tuple=None,		__A							:					Any=None,		**__A							:					Optional[int]						):
     UpperCAmelCase			,						UpperCAmelCase	:							List[Any]						=			{}, {}
     if padding is not None:
       UpperCAmelCase	:							Optional[int]						=			padding
     if truncation is not None:
       UpperCAmelCase	:							Optional[int]						=			truncation
     if top_k is not None:
       UpperCAmelCase	:							Tuple						=			top_k
     return preprocess_params, {}, postprocess_params



   def __call__(							self							:					Union[str, Any],		__A							:					Union["Image.Image", str],		__A							:					str = None,		**__A							:					Optional[int]						):
     if isinstance(__A,		(Image.Image, str)						) and isinstance(__A,		__A						):
       UpperCAmelCase	:							int						=			{'''image''': image, '''question''': question}
     else:
       UpperCAmelCase	:							str						=			image
     UpperCAmelCase	:							Union[str, Any]						=			super().__call__(__A,		**__A						)
     return results



   def 	__magic_name__   (							self							:					List[str],		__A							:					Union[str, Any],		__A							:					Tuple=False,		__A							:					List[Any]=False						):
     UpperCAmelCase	:							int						=			load_image(inputs['''image''']						)
     UpperCAmelCase	:							List[str]						=			self.tokenizer(
         inputs['''question'''],		return_tensors=self.framework,		padding=__A,		truncation=__A						)
     UpperCAmelCase	:							Union[str, Any]						=			self.image_processor(images=__A,		return_tensors=self.framework						)
     model_inputs.update(__A						)
     return model_inputs



   def 	__magic_name__   (							self							:					Optional[Any],		__A							:					List[Any]						):
     UpperCAmelCase	:							Optional[int]						=			self.model(**__A						)
     return model_outputs





   def 	__magic_name__   (							self							:					Any,		__A							:					List[str],		__A							:					Union[str, Any]=5						):
     if top_k > self.model.config.num_labels:
       UpperCAmelCase	:							Any						=			self.model.config.num_labels

     if self.framework == "pt":
       UpperCAmelCase	:							Any						=			model_outputs.logits.sigmoid()[0]
       UpperCAmelCase			,						UpperCAmelCase	:							Union[str, Any]						=			probs.topk(__A						)
     else:
       raise ValueError(F'''Unsupported framework: {self.framework}'''						)

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





