





'''simple docstring'''







import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
					from PIL import Image

					from transformers import CLIPImageProcessor, CLIPProcessor


@require_vision
class 					__a      (unittest.TestCase     ):

							def     UpperCAmelCase__	(							self							:     Tuple   )					->       Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  tempfile.mkdtemp()

													# fmt: off
													UpperCAmelCase_			:      Dict     						=  ['''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''lo''', '''l</w>''', '''w</w>''', '''r</w>''', '''t</w>''', '''low</w>''', '''er</w>''', '''lowest</w>''', '''newer</w>''', '''wider''', '''<unk>''', '''<|startoftext|>''', '''<|endoftext|>''']
													# fmt: on
													UpperCAmelCase_			:      List[Any]     						=  dict(zip(__magic_name__       ,     range(len(__magic_name__   )   )   )   )
													UpperCAmelCase_			:      List[Any]     						=  ['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''', '''''']
													UpperCAmelCase_			:      Tuple     						=  {'''unk_token''': '''<unk>'''}

													UpperCAmelCase_			:      List[str]     						=  os.path.join(self.tmpdirname       ,     VOCAB_FILES_NAMES['''vocab_file''']   )
													UpperCAmelCase_			:      int     						=  os.path.join(self.tmpdirname       ,     VOCAB_FILES_NAMES['''merges_file''']   )
													with open(self.vocab_file       ,     '''w'''       ,     encoding='''utf-8'''   ) as fp:
																			fp.write(json.dumps(__magic_name__   ) + '''\n'''   )
													with open(self.merges_file       ,     '''w'''       ,     encoding='''utf-8'''   ) as fp:
																			fp.write('''\n'''.join(__magic_name__   )   )

													UpperCAmelCase_			:      List[Any]     						=  {
													    '''do_resize''': True,
													    '''size''': 20,
													    '''do_center_crop''': True,
													    '''crop_size''': 18,
													    '''do_normalize''': True,
													    '''image_mean''': [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],
													    '''image_std''': [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],
													}
													UpperCAmelCase_			:      Union[str, Any]     						=  os.path.join(self.tmpdirname       ,     __magic_name__   )
													with open(self.image_processor_file       ,     '''w'''       ,     encoding='''utf-8'''   ) as fp:
																			json.dump(__magic_name__       ,     __magic_name__   )

							def     UpperCAmelCase__	(							self							:     Optional[int]       ,     **__magic_name__							:     Union[str, Any]   )					->       Optional[int]:







													"""simple docstring"""
													return CLIPTokenizer.from_pretrained(self.tmpdirname       ,     **__magic_name__   )

							def     UpperCAmelCase__	(							self							:     int       ,     **__magic_name__							:     Union[str, Any]   )					->       Optional[Any]:







													"""simple docstring"""
													return CLIPTokenizerFast.from_pretrained(self.tmpdirname       ,     **__magic_name__   )

							def     UpperCAmelCase__	(							self							:     List[Any]       ,     **__magic_name__							:     List[str]   )					->       int:







													"""simple docstring"""
													return CLIPImageProcessor.from_pretrained(self.tmpdirname       ,     **__magic_name__   )

							def     UpperCAmelCase__	(							self							:     int   )					->       str:







													"""simple docstring"""
													shutil.rmtree(self.tmpdirname   )

							def     UpperCAmelCase__	(							self							:     Tuple   )					->       int:







													"""simple docstring"""
													UpperCAmelCase_			:      List[str]     						=  [np.random.randint(2_55       ,     size=(3, 30, 4_00)       ,     dtype=np.uinta   )]

													UpperCAmelCase_			:      Tuple     						=  [Image.fromarray(np.moveaxis(__magic_name__       ,     0       ,     -1   )   ) for x in image_inputs]

													return image_inputs

							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  self.get_tokenizer()
													UpperCAmelCase_			:      Optional[int]     						=  self.get_rust_tokenizer()
													UpperCAmelCase_			:      Union[str, Any]     						=  self.get_image_processor()

													UpperCAmelCase_			:      Optional[int]     						=  CLIPProcessor(tokenizer=__magic_name__       ,     image_processor=__magic_name__   )
													processor_slow.save_pretrained(self.tmpdirname   )
													UpperCAmelCase_			:      int     						=  CLIPProcessor.from_pretrained(self.tmpdirname       ,     use_fast=__magic_name__   )

													UpperCAmelCase_			:      Union[str, Any]     						=  CLIPProcessor(tokenizer=__magic_name__       ,     image_processor=__magic_name__   )
													processor_fast.save_pretrained(self.tmpdirname   )
													UpperCAmelCase_			:      int     						=  CLIPProcessor.from_pretrained(self.tmpdirname   )

													self.assertEqual(processor_slow.tokenizer.get_vocab()       ,     tokenizer_slow.get_vocab()   )
													self.assertEqual(processor_fast.tokenizer.get_vocab()       ,     tokenizer_fast.get_vocab()   )
													self.assertEqual(tokenizer_slow.get_vocab()       ,     tokenizer_fast.get_vocab()   )
													self.assertIsInstance(processor_slow.tokenizer       ,     __magic_name__   )
													self.assertIsInstance(processor_fast.tokenizer       ,     __magic_name__   )

													self.assertEqual(processor_slow.image_processor.to_json_string()       ,     image_processor.to_json_string()   )
													self.assertEqual(processor_fast.image_processor.to_json_string()       ,     image_processor.to_json_string()   )
													self.assertIsInstance(processor_slow.image_processor       ,     __magic_name__   )
													self.assertIsInstance(processor_fast.image_processor       ,     __magic_name__   )

							def     UpperCAmelCase__	(							self							:     Any   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      Any     						=  CLIPProcessor(tokenizer=self.get_tokenizer()       ,     image_processor=self.get_image_processor()   )
													processor.save_pretrained(self.tmpdirname   )

													UpperCAmelCase_			:      Any     						=  self.get_tokenizer(bos_token='''(BOS)'''       ,     eos_token='''(EOS)'''   )
													UpperCAmelCase_			:      Union[str, Any]     						=  self.get_image_processor(do_normalize=__magic_name__       ,     padding_value=1.0   )

													UpperCAmelCase_			:      Any     						=  CLIPProcessor.from_pretrained(
													    self.tmpdirname       ,     bos_token='''(BOS)'''       ,     eos_token='''(EOS)'''       ,     do_normalize=__magic_name__       ,     padding_value=1.0   )

													self.assertEqual(processor.tokenizer.get_vocab()       ,     tokenizer_add_kwargs.get_vocab()   )
													self.assertIsInstance(processor.tokenizer       ,     __magic_name__   )

													self.assertEqual(processor.image_processor.to_json_string()       ,     image_processor_add_kwargs.to_json_string()   )
													self.assertIsInstance(processor.image_processor       ,     __magic_name__   )

							def     UpperCAmelCase__	(							self							:     List[str]   )					->       Any:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  self.get_image_processor()
													UpperCAmelCase_			:      Optional[int]     						=  self.get_tokenizer()

													UpperCAmelCase_			:      Tuple     						=  CLIPProcessor(tokenizer=__magic_name__       ,     image_processor=__magic_name__   )

													UpperCAmelCase_			:      List[Any]     						=  self.prepare_image_inputs()

													UpperCAmelCase_			:      Dict     						=  image_processor(__magic_name__       ,     return_tensors='''np'''   )
													UpperCAmelCase_			:      Dict     						=  processor(images=__magic_name__       ,     return_tensors='''np'''   )

													for key in input_image_proc.keys():
																			self.assertAlmostEqual(input_image_proc[key].sum()       ,     input_processor[key].sum()       ,     delta=1E-2   )

							def     UpperCAmelCase__	(							self							:     Optional[int]   )					->       Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      List[str]     						=  self.get_image_processor()
													UpperCAmelCase_			:      Optional[Any]     						=  self.get_tokenizer()

													UpperCAmelCase_			:      Any     						=  CLIPProcessor(tokenizer=__magic_name__       ,     image_processor=__magic_name__   )

													UpperCAmelCase_			:      int     						=  '''lower newer'''

													UpperCAmelCase_			:      str     						=  processor(text=__magic_name__   )

													UpperCAmelCase_			:      Union[str, Any]     						=  tokenizer(__magic_name__   )

													for key in encoded_tok.keys():
																			self.assertListEqual(encoded_tok[key]       ,     encoded_processor[key]   )

							def     UpperCAmelCase__	(							self							:     int   )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  self.get_image_processor()
													UpperCAmelCase_			:      Dict     						=  self.get_tokenizer()

													UpperCAmelCase_			:      List[str]     						=  CLIPProcessor(tokenizer=__magic_name__       ,     image_processor=__magic_name__   )

													UpperCAmelCase_			:      List[Any]     						=  '''lower newer'''
													UpperCAmelCase_			:      List[Any]     						=  self.prepare_image_inputs()

													UpperCAmelCase_			:      Optional[int]     						=  processor(text=__magic_name__       ,     images=__magic_name__   )

													self.assertListEqual(list(inputs.keys()   )       ,     ['''input_ids''', '''attention_mask''', '''pixel_values''']   )

													# test if it raises when no input is passed
													with pytest.raises(__magic_name__   ):
																			processor()

							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Any     						=  self.get_image_processor()
													UpperCAmelCase_			:      List[Any]     						=  self.get_tokenizer()

													UpperCAmelCase_			:      Optional[Any]     						=  CLIPProcessor(tokenizer=__magic_name__       ,     image_processor=__magic_name__   )

													UpperCAmelCase_			:      Optional[int]     						=  [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

													UpperCAmelCase_			:      Dict     						=  processor.batch_decode(__magic_name__   )
													UpperCAmelCase_			:      List[str]     						=  tokenizer.batch_decode(__magic_name__   )

													self.assertListEqual(__magic_name__       ,     __magic_name__   )



							def     UpperCAmelCase__	(							self							:     str   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  self.get_image_processor()
													UpperCAmelCase_			:      Optional[Any]     						=  self.get_tokenizer()

													UpperCAmelCase_			:      Tuple     						=  CLIPProcessor(tokenizer=__magic_name__       ,     image_processor=__magic_name__   )

													UpperCAmelCase_			:      List[str]     						=  '''lower newer'''
													UpperCAmelCase_			:      Any     						=  self.prepare_image_inputs()

													UpperCAmelCase_			:      Tuple     						=  processor(text=__magic_name__       ,     images=__magic_name__   )

													self.assertListEqual(list(inputs.keys()   )       ,     processor.model_input_names   )












'''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 					__a      (lowerCamelCase     ):
							__a					:   int									=       "dandelin/vilt-b32-finetuned-vqa"
							__a					:   Any									=       (
							    "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					:   Any									=       "image_qa"
							__a					:   str									=       AutoProcessor
							__a					:   Any									=       AutoModelForVisualQuestionAnswering

							__a					:   List[Any]									=       ["image", "text"]
							__a					:   int									=       ["text"]

							def __init__(							self							:     Tuple       ,     *__magic_name__							:     Any       ,     **__magic_name__							:     Any   )					->       Tuple:







													"""simple docstring"""
													requires_backends(self       ,     ['''vision''']   )
													super().__init__(*__magic_name__       ,     **__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Union[str, Any]       ,     __magic_name__							:     "Image"       ,     __magic_name__							:     str   )					->       Tuple:







													"""simple docstring"""
													return self.pre_processor(__magic_name__       ,     __magic_name__       ,     return_tensors='''pt'''   )

							def     UpperCAmelCase__	(							self							:     Any       ,     __magic_name__							:     List[str]   )					->       Optional[Any]:







													"""simple docstring"""
													with torch.no_grad():
																			return self.model(**__magic_name__   ).logits



							def     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     int   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  outputs.argmax(-1   ).item()
													return self.model.config.idalabel[idx]





