
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
    from transformers import TvltImageProcessor

if is_speech_available():
    from transformers import TvltFeatureExtractor

from transformers import TvltProcessor




@require_torch
class 					_lowerCamelCase			(   unittest.TestCase			):





     """simple docstring"""







     def  __SCREAMING_SNAKE_CASE      (				self	)	-> str:




           """simple docstring"""







           UpperCamelCase__     : List[str]											=					'''ZinengTang/tvlt-base'''
           UpperCamelCase__     : Any											=					tempfile.mkdtemp()






     def  __SCREAMING_SNAKE_CASE      (				self    ,    **__SCREAMING_SNAKE_CASE	)	-> List[str]:




           """simple docstring"""







           return TvltImageProcessor.from_pretrained(self.checkpoint    ,    **snake_case__	)






     def  __SCREAMING_SNAKE_CASE      (				self    ,    **__SCREAMING_SNAKE_CASE	)	-> int:




           """simple docstring"""







           return TvltFeatureExtractor.from_pretrained(self.checkpoint    ,    **snake_case__	)






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




           """simple docstring"""







           shutil.rmtree(self.tmpdirname	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Optional[int]:




           """simple docstring"""







           UpperCamelCase__     : List[str]											=					self.get_image_processor()
           UpperCamelCase__     : str											=					self.get_feature_extractor()

           UpperCamelCase__     : List[str]											=					TvltProcessor(image_processor=snake_case__    ,    feature_extractor=snake_case__	)
           processor.save_pretrained(self.tmpdirname	)
           UpperCamelCase__     : Tuple											=					TvltProcessor.from_pretrained(self.tmpdirname	)

           self.assertIsInstance(processor.feature_extractor    ,    snake_case__	)
           self.assertIsInstance(processor.image_processor    ,    snake_case__	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> int:




           """simple docstring"""







           UpperCamelCase__     : Dict											=					self.get_image_processor()
           UpperCamelCase__     : Optional[Any]											=					self.get_feature_extractor()

           UpperCamelCase__     : Optional[Any]											=					TvltProcessor(image_processor=snake_case__    ,    feature_extractor=snake_case__	)

           UpperCamelCase__     : int											=					np.ones([1_2_0_0_0]	)

           UpperCamelCase__     : Any											=					feature_extractor(snake_case__    ,    return_tensors='''np'''	)
           UpperCamelCase__     : str											=					processor(audio=snake_case__    ,    return_tensors='''np'''	)

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






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Optional[int]:




           """simple docstring"""







           UpperCamelCase__     : Tuple											=					self.get_image_processor()
           UpperCamelCase__     : Dict											=					self.get_feature_extractor()

           UpperCamelCase__     : int											=					TvltProcessor(image_processor=snake_case__    ,    feature_extractor=snake_case__	)

           UpperCamelCase__     : List[Any]											=					np.ones([3, 2_2_4, 2_2_4]	)

           UpperCamelCase__     : List[Any]											=					image_processor(snake_case__    ,    return_tensors='''np'''	)
           UpperCamelCase__     : Any											=					processor(images=snake_case__    ,    return_tensors='''np'''	)

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






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




           """simple docstring"""







           UpperCamelCase__     : int											=					self.get_image_processor()
           UpperCamelCase__     : List[str]											=					self.get_feature_extractor()

           UpperCamelCase__     : Optional[Any]											=					TvltProcessor(image_processor=snake_case__    ,    feature_extractor=snake_case__	)

           UpperCamelCase__     : Optional[Any]											=					np.ones([1_2_0_0_0]	)
           UpperCamelCase__     : Dict											=					np.ones([3, 2_2_4, 2_2_4]	)

           UpperCamelCase__     : Tuple											=					processor(audio=snake_case__    ,    images=snake_case__	)

           self.assertListEqual(list(inputs.keys()	)    ,    ['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask''']	)

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






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> str:




           """simple docstring"""







           UpperCamelCase__     : int											=					self.get_image_processor()
           UpperCamelCase__     : Tuple											=					self.get_feature_extractor()

           UpperCamelCase__     : List[Any]											=					TvltProcessor(image_processor=snake_case__    ,    feature_extractor=snake_case__	)

           self.assertListEqual(
               processor.model_input_names    ,    image_processor.model_input_names + feature_extractor.model_input_names    ,    msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match'''    ,    )





import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding




class   UpperCamelCase			(  SCREAMING_SNAKE_CASE ):
    __UpperCamelCase    =["image_processor", "tokenizer"]
    __UpperCamelCase    ="CLIPImageProcessor"
    __UpperCamelCase    =("CLIPTokenizer", "CLIPTokenizerFast")

    def __init__(      self			:   int				,     snake_case__			:   List[Any]=None				,     snake_case__			:   Any=None				,     **snake_case__			:   Any     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      None
     if "feature_extractor" in kwargs:
      warnings.warn(
          'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
          ' instead.'				,     snake_case__				,     )
      SCREAMING_SNAKE_CASE		    =      kwargs.pop('feature_extractor'     )

     SCREAMING_SNAKE_CASE		    =      image_processor if image_processor is not None else feature_extractor
     if image_processor is None:
      raise ValueError('You need to specify an `image_processor`.'     )
     if tokenizer is None:
      raise ValueError('You need to specify a `tokenizer`.'     )

     super().__init__(snake_case__				,     snake_case__     )

    def __call__(      self			:   List[Any]				,     snake_case__			:   Tuple=None				,     snake_case__			:   Optional[int]=None				,     snake_case__			:   List[str]=None				,     **snake_case__			:   Dict     ):

     """simple docstring"""






     if text is None and images is None:
      raise ValueError('You have to specify either text or images. Both cannot be none.'     )

     if text is not None:
      SCREAMING_SNAKE_CASE		    =      self.tokenizer(snake_case__				,     return_tensors=snake_case__				,     **snake_case__     )

     if images is not None:
      SCREAMING_SNAKE_CASE		    =      self.image_processor(snake_case__				,     return_tensors=snake_case__				,     **snake_case__     )

     if text is not None and images is not None:
      SCREAMING_SNAKE_CASE		    =      image_features.pixel_values
      return encoding
     elif text is not None:
      return encoding
     else:
      return BatchEncoding(data=dict(**snake_case__     )				,     tensor_type=snake_case__     )

    def UpperCamelCase      (      self			:   Union[str, Any]				,     *snake_case__			:   Optional[Any]				,     **snake_case__			:   Optional[int]     ):

     """simple docstring"""






     return self.tokenizer.batch_decode(*snake_case__				,     **snake_case__     )

    def UpperCamelCase      (      self			:   Dict				,     *snake_case__			:   Optional[Any]				,     **snake_case__			:   Dict     ):

     """simple docstring"""






     return self.tokenizer.decode(*snake_case__				,     **snake_case__     )

    @property
    def UpperCamelCase      (      self			:   Any     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      self.tokenizer.model_input_names
     SCREAMING_SNAKE_CASE		    =      self.image_processor.model_input_names
     return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names     )     )

    @property
    def UpperCamelCase      (      self			:   int     ):

     """simple docstring"""






     warnings.warn(
         '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'				,     snake_case__				,     )
     return self.image_processor_class




    @property
    def UpperCamelCase      (      self			:   Tuple     ):

     """simple docstring"""






     warnings.warn(
         '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'				,     snake_case__				,     )
     return self.image_processor

