





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

UpperCAmelCase_           =	logging.get_logger(__name__)




@add_end_docstrings(A__    )
class 			__UpperCamelCase    (							A__    ):





     def __init__(     self				,							*_UpperCamelCase				,							**_UpperCamelCase					):
        super().__init__(*_UpperCamelCase				,							**_UpperCamelCase					)
        self.check_model_type(_UpperCamelCase					)





     def 		UpperCamelCase(     self				,							_UpperCamelCase=None				,							_UpperCamelCase=None				,							_UpperCamelCase=None				,							**_UpperCamelCase					):
        _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				,							_UpperCamelCase				,							_UpperCamelCase = None				,							**_UpperCamelCase					):
        if isinstance(_UpperCamelCase				,							(Image.Image, str)					) and isinstance(_UpperCamelCase				,							_UpperCamelCase					):
           _UpperCAmelCase           =   {'''image''': image, '''question''': question}
        else:
           _UpperCAmelCase           =   image
        _UpperCAmelCase           =   super().__call__(_UpperCamelCase				,							**_UpperCamelCase					)
        return results





     def 		UpperCamelCase(     self				,							_UpperCamelCase				,							_UpperCamelCase=False				,							_UpperCamelCase=False					):
        _UpperCAmelCase           =   load_image(inputs['''image''']					)
        _UpperCAmelCase           =   self.tokenizer(
            inputs['''question''']				,							return_tensors=self.framework				,							padding=_UpperCamelCase				,							truncation=_UpperCamelCase					)
        _UpperCAmelCase           =   self.image_processor(images=_UpperCamelCase				,							return_tensors=self.framework					)
        model_inputs.update(_UpperCamelCase					)
        return model_inputs





     def 		UpperCamelCase(     self				,							_UpperCamelCase					):
        _UpperCAmelCase           =   self.model(**_UpperCamelCase					)
        return model_outputs





     def 		UpperCamelCase(     self				,							_UpperCamelCase				,							_UpperCamelCase=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(_UpperCamelCase					)
        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(_UpperCamelCase				,							_UpperCamelCase					)]






from ..utils import DummyObject, requires_backends




class 			__UpperCamelCase    (							metaclass=A__    ):
     __A	:  str							  =   ["""torch""", """scipy"""]





     def __init__(     self				,							*_UpperCamelCase				,							**_UpperCamelCase					):
        requires_backends(self				,							['''torch''', '''scipy''']					)





     @classmethod
     def 		UpperCamelCase(     cls				,							*_UpperCamelCase				,							**_UpperCamelCase					):
        requires_backends(cls				,							['''torch''', '''scipy''']					)





     @classmethod
     def 		UpperCamelCase(     cls				,							*_UpperCamelCase				,							**_UpperCamelCase					):
        requires_backends(cls				,							['''torch''', '''scipy''']					)