





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

from ..models.auto import AutoModelForVisionaSeq
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
	from PIL import Image




class  _A (					__lowercase		):
		lowercase__:   Optional[int]				     =   '''Salesforce/blip-image-captioning-base'''
		lowercase__:   Optional[Any]				     =   (
		    '''This is a tool that generates a description of an image. It takes an input named `image` which should be the '''
		    '''image to caption, and returns a text that contains the description in English.'''
		)
		lowercase__:   int				     =   '''image_captioner'''
		lowercase__:   Optional[Any]				     =   AutoModelForVisionaSeq

		lowercase__:   int				     =   ['''image''']
		lowercase__:   List[str]				     =   ['''text''']

		def __init__(   self					:   Tuple  ,      *__magic_name__					:   List[str]  ,      **__magic_name__					:   List[Any]			)					->    List[str]:


					"""simple docstring"""

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

		def 	lowercase__     (   self					:   Optional[int]  ,      __magic_name__					:   "Image"			)					->    Optional[Any]:


					"""simple docstring"""

					return self.pre_processor(images=__magic_name__  ,      return_tensors="""pt"""			)

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


					"""simple docstring"""

					return self.model.generate(**__magic_name__			)

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


					"""simple docstring"""

					return self.pre_processor.batch_decode(__magic_name__  ,      skip_special_tokens=__magic_name__			)[0].strip()






import shutil
import tempfile
import unittest

from transformers import ClapFeatureExtractor, ClapProcessor, RobertaTokenizer, RobertaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torchaudio

from .test_feature_extraction_clap import floats_list

@require_torchaudio
@require_sentencepiece
class     A__   (       unittest.TestCase      ):







    """simple docstring"""

    def 						a_     (			self       ):
         snake_case											=							'''laion/clap-htsat-unfused'''
         snake_case											=							tempfile.mkdtemp()
    def 						a_     (			self     ,   **__snake_case       ):
         return RobertaTokenizer.from_pretrained(self.checkpoint     ,   **__snake_case       )
    def 						a_     (			self     ,   **__snake_case       ):
         return ClapFeatureExtractor.from_pretrained(self.checkpoint     ,   **__snake_case       )
    def 						a_     (			self       ):
         shutil.rmtree(self.tmpdirname       )
    def 						a_     (			self       ):
         snake_case											=							self.get_tokenizer()
         snake_case											=							self.get_feature_extractor()

         snake_case											=							ClapProcessor(tokenizer=__snake_case     ,   feature_extractor=__snake_case       )

         processor.save_pretrained(self.tmpdirname       )
         snake_case											=							ClapProcessor.from_pretrained(self.tmpdirname       )

         self.assertEqual(processor.tokenizer.get_vocab()     ,   tokenizer.get_vocab()       )
         self.assertIsInstance(processor.tokenizer     ,   __snake_case       )

         self.assertEqual(processor.feature_extractor.to_json_string()     ,   feature_extractor.to_json_string()       )
         self.assertIsInstance(processor.feature_extractor     ,   __snake_case       )
    def 						a_     (			self       ):
         snake_case											=							ClapProcessor(tokenizer=self.get_tokenizer()     ,   feature_extractor=self.get_feature_extractor()       )
         processor.save_pretrained(self.tmpdirname       )

         snake_case											=							self.get_tokenizer(bos_token='''(BOS)'''     ,   eos_token='''(EOS)'''       )
         snake_case											=							self.get_feature_extractor(do_normalize=__snake_case     ,   padding_value=1.0       )

         snake_case											=							ClapProcessor.from_pretrained(
             self.tmpdirname     ,   bos_token='''(BOS)'''     ,   eos_token='''(EOS)'''     ,   do_normalize=__snake_case     ,   padding_value=1.0       )

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

         self.assertEqual(processor.feature_extractor.to_json_string()     ,   feature_extractor_add_kwargs.to_json_string()       )
         self.assertIsInstance(processor.feature_extractor     ,   __snake_case       )
    def 						a_     (			self       ):
         snake_case											=							self.get_feature_extractor()
         snake_case											=							self.get_tokenizer()

         snake_case											=							ClapProcessor(tokenizer=__snake_case     ,   feature_extractor=__snake_case       )

         snake_case											=							floats_list((3, 1_0_0_0)       )

         snake_case											=							feature_extractor(__snake_case     ,   return_tensors='''np'''       )
         snake_case											=							processor(audios=__snake_case     ,   return_tensors='''np'''       )

         for key in input_feat_extract.keys():
              self.assertAlmostEqual(input_feat_extract[key].sum()     ,   input_processor[key].sum()     ,   delta=1E-2       )
    def 						a_     (			self       ):
         snake_case											=							self.get_feature_extractor()
         snake_case											=							self.get_tokenizer()

         snake_case											=							ClapProcessor(tokenizer=__snake_case     ,   feature_extractor=__snake_case       )

         snake_case											=							'''This is a test string'''

         snake_case											=							processor(text=__snake_case       )

         snake_case											=							tokenizer(__snake_case       )

         for key in encoded_tok.keys():
              self.assertListEqual(encoded_tok[key]     ,   encoded_processor[key]       )
    def 						a_     (			self       ):
         snake_case											=							self.get_feature_extractor()
         snake_case											=							self.get_tokenizer()

         snake_case											=							ClapProcessor(tokenizer=__snake_case     ,   feature_extractor=__snake_case       )

         snake_case											=							[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

         snake_case											=							processor.batch_decode(__snake_case       )
         snake_case											=							tokenizer.batch_decode(__snake_case       )

         self.assertListEqual(__snake_case     ,   __snake_case       )




    def 						a_     (			self       ):
         snake_case											=							self.get_feature_extractor()
         snake_case											=							self.get_tokenizer()

         snake_case											=							ClapProcessor(tokenizer=__snake_case     ,   feature_extractor=__snake_case       )

         self.assertListEqual(
             processor.model_input_names[2:]     ,   feature_extractor.model_input_names     ,   msg='''`processor` and `feature_extractor` model input names do not match'''     ,   )




