
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									=logging.get_logger(__name__)


@add_end_docstrings(_a)
class  __magic_name__						(     _a):





	def __init__(		self	: Any    ,*__SCREAMING_SNAKE_CASE	: Dict    ,**__SCREAMING_SNAKE_CASE	: List[Any]       ):
						super().__init__(*A_    ,**A_       )
						self.check_model_type(A_       )





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





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





	def 	_UpperCAmelCase						(		self	: Any    ,__SCREAMING_SNAKE_CASE	: Tuple    ,__SCREAMING_SNAKE_CASE	: Tuple=False    ,__SCREAMING_SNAKE_CASE	: List[str]=False       ):
						UpperCAmelCase				   =  load_image(inputs["image"]       )
						UpperCAmelCase				   =  self.tokenizer(
						    inputs["question"]    ,return_tensors=self.framework    ,padding=A_    ,truncation=A_       )
						UpperCAmelCase				   =  self.image_processor(images=A_    ,return_tensors=self.framework       )
						model_inputs.update(A_       )
						return model_inputs





	def 	_UpperCAmelCase						(		self	: Optional[int]    ,__SCREAMING_SNAKE_CASE	: Tuple       ):
						UpperCAmelCase				   =  self.model(**A_       )
						return model_outputs





	def 	_UpperCAmelCase						(		self	: Optional[Any]    ,__SCREAMING_SNAKE_CASE	: Optional[int]    ,__SCREAMING_SNAKE_CASE	: Optional[int]=5       ):
						if top_k > self.model.config.num_labels:
											UpperCAmelCase				   =  self.model.config.num_labels

						if self.framework == "pt":
											UpperCAmelCase				   =  model_outputs.logits.sigmoid()[0]
											UpperCAmelCase   ,     UpperCAmelCase				   =  probs.topk(A_       )
						else:
											raise ValueError(f'''Unsupported framework: {self.framework}'''       )

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









from typing import TYPE_CHECKING

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


UpperCamelCase_												=	{
    'configuration_owlvit': [
        'OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'OwlViTConfig',
        'OwlViTOnnxConfig',
        'OwlViTTextConfig',
        'OwlViTVisionConfig',
    ],
    'processing_owlvit': ['OwlViTProcessor'],
}


try:
    if not is_vision_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    UpperCamelCase_												=	['OwlViTFeatureExtractor']
    UpperCamelCase_												=	['OwlViTImageProcessor']

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    UpperCamelCase_												=	[
        'OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST',
        'OwlViTModel',
        'OwlViTPreTrainedModel',
        'OwlViTTextModel',
        'OwlViTVisionModel',
        'OwlViTForObjectDetection',
    ]

if TYPE_CHECKING:
    from .configuration_owlvit import (
        OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP,
        OwlViTConfig,
        OwlViTOnnxConfig,
        OwlViTTextConfig,
        OwlViTVisionConfig,
    )
    from .processing_owlvit import OwlViTProcessor

    try:
        if not is_vision_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .feature_extraction_owlvit import OwlViTFeatureExtractor
        from .image_processing_owlvit import OwlViTImageProcessor

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_owlvit import (
            OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
            OwlViTForObjectDetection,
            OwlViTModel,
            OwlViTPreTrainedModel,
            OwlViTTextModel,
            OwlViTVisionModel,
        )

else:
    import sys

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



