
# 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    SCREAMING_SNAKE_CASE__     (	UpperCAmelCase					):





	'''simple docstring'''



	_UpperCAmelCase       :		Dict       =					"dandelin/vilt-b32-finetuned-vqa"
	_UpperCAmelCase       :		Optional[int]       =					(
	    "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."
	)
	_UpperCAmelCase       :		List[str]       =					"image_qa"
	_UpperCAmelCase       :		Dict       =					AutoProcessor
	_UpperCAmelCase       :		Dict       =					AutoModelForVisualQuestionAnswering

	_UpperCAmelCase       :		Optional[int]       =					["image", "text"]
	_UpperCAmelCase       :		Tuple       =					["text"]



	def __init__(  self	:    Optional[int]			,  *lowercase	:    int			,  **lowercase	:    Union[str, Any]	):


				'''simple docstring'''


				requires_backends(self			,  ['vision']	)
				super().__init__(*lowercase			,  **lowercase	)



	def  A							(  self	:    Optional[int]			,  lowercase	:    "Image"			,  lowercase	:    str	):


				'''simple docstring'''


				return self.pre_processor(lowercase			,  lowercase			,  return_tensors='pt'	)



	def  A							(  self	:    str			,  lowercase	:    Tuple	):


				'''simple docstring'''


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





	def  A							(  self	:    Optional[int]			,  lowercase	:    List[str]	):


				'''simple docstring'''


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

import tempfile
import unittest

from transformers import AutoModelForSeqaSeqLM, AutoTokenizer
from transformers.testing_utils import (
    is_torch_available,
    require_optimum,
    require_torch,
    slow,
)


if is_torch_available():
			import torch




@require_torch
@require_optimum
@slow
class    SCREAMING_SNAKE_CASE__     (	unittest.TestCase					):





	'''simple docstring'''



	def  A							(  self	:    Optional[int]	):


				'''simple docstring'''


				_snake_case		=			'hf-internal-testing/tiny-random-t5'
				_snake_case		=			AutoTokenizer.from_pretrained(lowercase	)
				_snake_case		=			AutoModelForSeqaSeqLM.from_pretrained(lowercase	)

				_snake_case		=			tokenizer('This is me'			,  return_tensors='pt'	)

				_snake_case		=			model.to_bettertransformer()

				self.assertTrue(any('BetterTransformer' in mod.__class__.__name__ for _, mod in model.named_modules()	)	)

				_snake_case		=			model.generate(**lowercase	)

				_snake_case		=			model.reverse_bettertransformer()

				self.assertFalse(any('BetterTransformer' in mod.__class__.__name__ for _, mod in model.named_modules()	)	)

				with tempfile.TemporaryDirectory() as tmpdirname:
							model.save_pretrained(lowercase	)

							_snake_case		=			AutoModelForSeqaSeqLM.from_pretrained(lowercase	)

							self.assertFalse(
							    any('BetterTransformer' in mod.__class__.__name__ for _, mod in model_reloaded.named_modules()	)	)

							_snake_case		=			model_reloaded.generate(**lowercase	)
							self.assertTrue(torch.allclose(lowercase			,  lowercase	)	)





	def  A							(  self	:    List[Any]	):


				'''simple docstring'''


				_snake_case		=			'hf-internal-testing/tiny-random-t5'
				_snake_case		=			AutoModelForSeqaSeqLM.from_pretrained(lowercase	)

				_snake_case		=			model.to_bettertransformer()

				with tempfile.TemporaryDirectory() as tmpdirname:
							with self.assertRaises(lowercase	):
										model.save_pretrained(lowercase	)

							_snake_case		=			model.reverse_bettertransformer()
							model.save_pretrained(lowercase	)