




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							:						List[Any]					       =  logging.get_logger(__name__)







@add_end_docstrings(UpperCamelCase  )
class     A(		UpperCamelCase  ):



 '''simple docstring'''


 def __init__(							self   :    Tuple		,				*A_   :    int		,				**A_   :    Union[str, Any]    )  ->      int:

       """simple docstring"""






       super().__init__(*A_		,				**A_    )
       self.check_model_type(A_    )

 def 							a__					(							self   :    Dict		,				A_   :    List[Any]=None		,				A_   :    int=None		,				A_   :    Optional[Any]=None		,				**A_   :    Optional[int]    )  ->      List[str]:

       """simple docstring"""






       lowerCamelCase_	,							lowerCamelCase_							=       {}, {}
       if padding is not None:
             lowerCamelCase_							=       padding
       if truncation is not None:
             lowerCamelCase_							=       truncation
       if top_k is not None:
             lowerCamelCase_							=       top_k
       return preprocess_params, {}, postprocess_params

 def __call__(							self   :    Optional[Any]		,				A_   :    Union["Image.Image", str]		,				A_   :    str = None		,				**A_   :    Any    )  ->      int:

       """simple docstring"""






       if isinstance(A_		,				(Image.Image, str)    ) and isinstance(A_		,				A_    ):
             lowerCamelCase_							=       {'image': image, 'question': question}
       else:
             lowerCamelCase_							=       image
       lowerCamelCase_							=       super().__call__(A_		,				**A_    )
       return results

 def 							a__					(							self   :    List[str]		,				A_   :    Any		,				A_   :    Dict=False		,				A_   :    Optional[int]=False    )  ->      str:

       """simple docstring"""






       lowerCamelCase_							=       load_image(inputs['image']    )
       lowerCamelCase_							=       self.tokenizer(
           inputs['question']		,				return_tensors=self.framework		,				padding=A_		,				truncation=A_    )
       lowerCamelCase_							=       self.image_processor(images=A_		,				return_tensors=self.framework    )
       model_inputs.update(A_    )
       return model_inputs

 def 							a__					(							self   :    Union[str, Any]		,				A_   :    Any    )  ->      Union[str, Any]:

       """simple docstring"""






       lowerCamelCase_							=       self.model(**A_    )
       return model_outputs





 def 							a__					(							self   :    Dict		,				A_   :    Union[str, Any]		,				A_   :    Dict=5    )  ->      Tuple:

       """simple docstring"""






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

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

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




"""simple docstring"""



import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin
class 						__UpperCAmelCase    (unittest.TestCase							,   __A     ):







			'''simple docstring'''





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[Any]								=			load_tool("""text-classification"""							)
										self.tool.setup()
										A__       :     Any								=			load_tool("""text-classification"""   ,      remote=snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     int								=			self.tool("""That's quite cool"""   ,      ["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Optional[Any]								=			self.remote_tool("""That's quite cool"""   ,      ["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     str								=			self.tool(text="""That's quite cool"""   ,      labels=["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     int								=			self.remote_tool(text="""That's quite cool"""   ,      labels=["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





