





from typing import TYPE_CHECKING

import torch

from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
						from PIL import Image

class SCREAMING_SNAKE_CASE_ (					UpperCamelCase__   ):





					"""simple docstring"""


					A__				 =    """dandelin/vilt-b32-finetuned-vqa"""
					A__				 =    (
					    """This is a tool that answers a question about an image. It takes an input named `image` which should be the """
					    """image containing the information, as well as a `question` which should be the question in English. It """
					    """returns a text that is the answer to the question."""
					)
					A__				 =    """image_qa"""
					A__				 =    AutoProcessor
					A__				 =    AutoModelForVisualQuestionAnswering

					A__				 =    ["""image""", """text"""]
					A__				 =    ["""text"""]





					def __init__( self	,  *_lowerCAmelCase	,  **_lowerCAmelCase	):
										requires_backends(self	,  ["vision"]	)
										super().__init__(*_lowerCAmelCase	,  **_lowerCAmelCase	)





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										return self.pre_processor(_lowerCAmelCase	,  _lowerCAmelCase	,  return_tensors="pt"	)





					def 					__magic_name__ ( self	,  _lowerCAmelCase	):
										with torch.no_grad():
															return self.model(**_lowerCAmelCase	).logits






					def 					__magic_name__ ( self	,  _lowerCAmelCase	):
										lowerCamelCase__       =       outputs.argmax(-1	).item()
										return self.model.config.idalabel[idx]












import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin

class SCREAMING_SNAKE_CASE_ (					unittest.TestCase							,       lowercase_   ):





					"""simple docstring"""


					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       load_tool("text-classification"	)
										self.tool.setup()
										lowerCamelCase__       =       load_tool("text-classification"	,  remote=_lowerCAmelCase	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.tool("That's quite cool"	,  ["positive", "negative"]	)
										self.assertEqual(_lowerCAmelCase	,  "positive"	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.remote_tool("That's quite cool"	,  ["positive", "negative"]	)
										self.assertEqual(_lowerCAmelCase	,  "positive"	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.tool(text="That's quite cool"	,  labels=["positive", "negative"]	)
										self.assertEqual(_lowerCAmelCase	,  "positive"	)






					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.remote_tool(text="That's quite cool"	,  labels=["positive", "negative"]	)
										self.assertEqual(_lowerCAmelCase	,  "positive"	)

