


"""simple docstring"""

# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
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  lowerCamelCase__    (		_a     ):
      a			:  Optional[Any]           =		"""dandelin/vilt-b32-finetuned-vqa"""
      a			:  Dict           =		(
          """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			:  int           =		"""image_qa"""
      a			:  Any           =		AutoProcessor
      a			:  int           =		AutoModelForVisualQuestionAnswering

      a			:  Union[str, Any]           =		["""image""", """text"""]
      a			:  List[Any]           =		["""text"""]
      def __init__(		self						:						str   ,							*A_						:						Optional[int]   ,							**A_						:						Optional[int]				):






       '''simple docstring'''


       requires_backends(self   ,							["""vision"""]				)
       super().__init__(*A_   ,							**A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]   ,							A_						:						"Image"   ,							A_						:						str				):






       '''simple docstring'''


       return self.pre_processor(A_   ,							A_   ,							return_tensors="""pt"""				)
      def SCREAMING_SNAKE_CASE_						(		self						:						str   ,							A_						:						int				):






       '''simple docstring'''


       with torch.no_grad():
        return self.model(**A_				).logits






      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]   ,							A_						:						Tuple				):






       '''simple docstring'''


       __lowercase						=						outputs.argmax(-1				).item()
       return self.model.config.idalabel[idx]




"""simple docstring"""

import unittest

import numpy as np

from transformers.testing_utils import require_flax, require_tf, require_torch
from transformers.utils import (
    expand_dims,
    flatten_dict,
    is_flax_available,
    is_tf_available,
    is_torch_available,
    reshape,
    squeeze,
    transpose,
)


if is_flax_available():
       import jax.numpy as jnp

if is_tf_available():
       import tensorflow as tf

if is_torch_available():
       import torch





class  lowerCamelCase__    (		unittest.TestCase     ):
      def SCREAMING_SNAKE_CASE_						(		self						:						Any				):






       '''simple docstring'''


       __lowercase						=						{
           """task_specific_params""": {
               """summarization""": {"""length_penalty""": 1.0, """max_length""": 1_2_8, """min_length""": 1_2, """num_beams""": 4},
               """summarization_cnn""": {"""length_penalty""": 2.0, """max_length""": 1_4_2, """min_length""": 5_6, """num_beams""": 4},
               """summarization_xsum""": {"""length_penalty""": 1.0, """max_length""": 6_2, """min_length""": 1_1, """num_beams""": 6},
           }
       }
       __lowercase						=						{
           """task_specific_params.summarization.length_penalty""": 1.0,
           """task_specific_params.summarization.max_length""": 1_2_8,
           """task_specific_params.summarization.min_length""": 1_2,
           """task_specific_params.summarization.num_beams""": 4,
           """task_specific_params.summarization_cnn.length_penalty""": 2.0,
           """task_specific_params.summarization_cnn.max_length""": 1_4_2,
           """task_specific_params.summarization_cnn.min_length""": 5_6,
           """task_specific_params.summarization_cnn.num_beams""": 4,
           """task_specific_params.summarization_xsum.length_penalty""": 1.0,
           """task_specific_params.summarization_xsum.max_length""": 6_2,
           """task_specific_params.summarization_xsum.min_length""": 1_1,
           """task_specific_params.summarization_xsum.num_beams""": 6,
       }

       self.assertEqual(flatten_dict(A_				)   ,							A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       self.assertTrue(np.allclose(transpose(A_				)   ,							x.transpose()				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       self.assertTrue(np.allclose(transpose(A_   ,							axes=(1, 2, 0)				)   ,							x.transpose((1, 2, 0)				)				)				)
      @require_torch
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						torch.tensor(A_				)
       self.assertTrue(np.allclose(transpose(A_				)   ,							transpose(A_				).numpy()				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       __lowercase						=						torch.tensor(A_				)
       self.assertTrue(np.allclose(transpose(A_   ,							axes=(1, 2, 0)				)   ,							transpose(A_   ,							axes=(1, 2, 0)				).numpy()				)				)
      @require_tf
      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						tf.constant(A_				)
       self.assertTrue(np.allclose(transpose(A_				)   ,							transpose(A_				).numpy()				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       __lowercase						=						tf.constant(A_				)
       self.assertTrue(np.allclose(transpose(A_   ,							axes=(1, 2, 0)				)   ,							transpose(A_   ,							axes=(1, 2, 0)				).numpy()				)				)
      @require_flax
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[int]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						jnp.array(A_				)
       self.assertTrue(np.allclose(transpose(A_				)   ,							np.asarray(transpose(A_				)				)				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       __lowercase						=						jnp.array(A_				)
       self.assertTrue(np.allclose(transpose(A_   ,							axes=(1, 2, 0)				)   ,							np.asarray(transpose(A_   ,							axes=(1, 2, 0)				)				)				)				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       self.assertTrue(np.allclose(reshape(A_   ,							(4, 3)				)   ,							np.reshape(A_   ,							(4, 3)				)				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       self.assertTrue(np.allclose(reshape(A_   ,							(1_2, 5)				)   ,							np.reshape(A_   ,							(1_2, 5)				)				)				)
      @require_torch
      def SCREAMING_SNAKE_CASE_						(		self						:						Dict				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						torch.tensor(A_				)
       self.assertTrue(np.allclose(reshape(A_   ,							(4, 3)				)   ,							reshape(A_   ,							(4, 3)				).numpy()				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       __lowercase						=						torch.tensor(A_				)
       self.assertTrue(np.allclose(reshape(A_   ,							(1_2, 5)				)   ,							reshape(A_   ,							(1_2, 5)				).numpy()				)				)
      @require_tf
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[int]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						tf.constant(A_				)
       self.assertTrue(np.allclose(reshape(A_   ,							(4, 3)				)   ,							reshape(A_   ,							(4, 3)				).numpy()				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       __lowercase						=						tf.constant(A_				)
       self.assertTrue(np.allclose(reshape(A_   ,							(1_2, 5)				)   ,							reshape(A_   ,							(1_2, 5)				).numpy()				)				)
      @require_flax
      def SCREAMING_SNAKE_CASE_						(		self						:						str				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						jnp.array(A_				)
       self.assertTrue(np.allclose(reshape(A_   ,							(4, 3)				)   ,							np.asarray(reshape(A_   ,							(4, 3)				)				)				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       __lowercase						=						jnp.array(A_				)
       self.assertTrue(np.allclose(reshape(A_   ,							(1_2, 5)				)   ,							np.asarray(reshape(A_   ,							(1_2, 5)				)				)				)				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Dict				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(1   ,							3   ,							4				)
       self.assertTrue(np.allclose(squeeze(A_				)   ,							np.squeeze(A_				)				)				)

       __lowercase						=						np.random.randn(1   ,							4   ,							1   ,							5				)
       self.assertTrue(np.allclose(squeeze(A_   ,							axis=2				)   ,							np.squeeze(A_   ,							axis=2				)				)				)
      @require_torch
      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(1   ,							3   ,							4				)
       __lowercase						=						torch.tensor(A_				)
       self.assertTrue(np.allclose(squeeze(A_				)   ,							squeeze(A_				).numpy()				)				)

       __lowercase						=						np.random.randn(1   ,							4   ,							1   ,							5				)
       __lowercase						=						torch.tensor(A_				)
       self.assertTrue(np.allclose(squeeze(A_   ,							axis=2				)   ,							squeeze(A_   ,							axis=2				).numpy()				)				)
      @require_tf
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(1   ,							3   ,							4				)
       __lowercase						=						tf.constant(A_				)
       self.assertTrue(np.allclose(squeeze(A_				)   ,							squeeze(A_				).numpy()				)				)

       __lowercase						=						np.random.randn(1   ,							4   ,							1   ,							5				)
       __lowercase						=						tf.constant(A_				)
       self.assertTrue(np.allclose(squeeze(A_   ,							axis=2				)   ,							squeeze(A_   ,							axis=2				).numpy()				)				)
      @require_flax
      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(1   ,							3   ,							4				)
       __lowercase						=						jnp.array(A_				)
       self.assertTrue(np.allclose(squeeze(A_				)   ,							np.asarray(squeeze(A_				)				)				)				)

       __lowercase						=						np.random.randn(1   ,							4   ,							1   ,							5				)
       __lowercase						=						jnp.array(A_				)
       self.assertTrue(np.allclose(squeeze(A_   ,							axis=2				)   ,							np.asarray(squeeze(A_   ,							axis=2				)				)				)				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       self.assertTrue(np.allclose(expand_dims(A_   ,							axis=1				)   ,							np.expand_dims(A_   ,							axis=1				)				)				)
      @require_torch
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[int]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						torch.tensor(A_				)
       self.assertTrue(np.allclose(expand_dims(A_   ,							axis=1				)   ,							expand_dims(A_   ,							axis=1				).numpy()				)				)
      @require_tf
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[Any]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						tf.constant(A_				)
       self.assertTrue(np.allclose(expand_dims(A_   ,							axis=1				)   ,							expand_dims(A_   ,							axis=1				).numpy()				)				)






      @require_flax
      def SCREAMING_SNAKE_CASE_						(		self						:						List[str]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						jnp.array(A_				)
       self.assertTrue(np.allclose(expand_dims(A_   ,							axis=1				)   ,							np.asarray(expand_dims(A_   ,							axis=1				)				)				)				)
