


'''simple docstring'''
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

__SCREAMING_SNAKE_CASE					:				List[Any]									=		logging.get_logger(__name__)






@add_end_docstrings(lowercase						)
class 					__lowerCAmelCase   (   lowercase						):

						"""simple docstring"""






						def __init__(  self							:      Tuple ,  *lowerCAmelCase							:      List[str] ,  **lowerCAmelCase							:      List[str]	):
													super().__init__(*lowerCAmelCase ,  **lowerCAmelCase	)
													self.check_model_type(lowerCAmelCase	)






						def 				_UpperCAmelCase       (  self							:      Tuple ,  lowerCAmelCase							:      Optional[int]=None ,  lowerCAmelCase							:      Any=None ,  lowerCAmelCase							:      Tuple=None ,  **lowerCAmelCase							:      Optional[Any]	):
													A_    ,					A_			    =		{}, {}
													if padding is not None:
																				A_			    =		padding
													if truncation is not None:
																				A_			    =		truncation
													if top_k is not None:
																				A_			    =		top_k
													return preprocess_params, {}, postprocess_params






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






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






						def 				_UpperCAmelCase       (  self							:      int ,  lowerCAmelCase							:      Union[str, Any]	):
													A_			    =		self.model(**lowerCAmelCase	)
													return model_outputs






						def 				_UpperCAmelCase       (  self							:      Any ,  lowerCAmelCase							:      Tuple ,  lowerCAmelCase							:      Tuple=5	):
													if top_k > self.model.config.num_labels:
																				A_			    =		self.model.config.num_labels

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

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









'''simple docstring'''
import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin






class 					__lowerCAmelCase   (   unittest.TestCase				,			lowercase						):

						"""simple docstring"""






						def 				_UpperCAmelCase       (  self							:      List[str]	):
													A_			    =		load_tool("text-classification"	)
													self.tool.setup()
													A_			    =		load_tool("text-classification" ,  remote=lowerCAmelCase	)






						def 				_UpperCAmelCase       (  self							:      Union[str, Any]	):
													A_			    =		self.tool("That's quite cool" ,  ["positive", "negative"]	)
													self.assertEqual(lowerCAmelCase ,  "positive"	)






						def 				_UpperCAmelCase       (  self							:      str	):
													A_			    =		self.remote_tool("That's quite cool" ,  ["positive", "negative"]	)
													self.assertEqual(lowerCAmelCase ,  "positive"	)






						def 				_UpperCAmelCase       (  self							:      str	):
													A_			    =		self.tool(text="That's quite cool" ,  labels=["positive", "negative"]	)
													self.assertEqual(lowerCAmelCase ,  "positive"	)






						def 				_UpperCAmelCase       (  self							:      List[Any]	):
													A_			    =		self.remote_tool(text="That's quite cool" ,  labels=["positive", "negative"]	)
													self.assertEqual(lowerCAmelCase ,  "positive"	)





