
'''simple docstring'''





import itertools
import os
import random
import tempfile
import unittest

import numpy as np

from transformers import TvltFeatureExtractor, is_datasets_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
      import torch

if is_datasets_available():
      from datasets import load_dataset

SCREAMING_SNAKE_CASE     =       random.Random()







def snake_case_     (					lowercase__   ,       lowercase__=1.0   ,       lowercase__=None   ,       lowercase__=None		):
  if rng is None:
    UpperCAmelCase__      :					List[str]				       =							global_rng

  UpperCAmelCase__      :					Optional[Any]				       =							[]
  for batch_idx in range(shape[0]		):
    values.append([]		)
    for _ in range(shape[1]		):
      values[-1].append(rng.random() * scale		)

  return values






class 							UpperCAmelCase_							(    unittest.TestCase				):

 '''simple docstring'''







 def __init__(					self		:  int			,				snake_case__		:  List[str]			,				snake_case__		:  List[str]=7			,				snake_case__		:  Any=4_00			,				snake_case__		:  int=20_00			,				snake_case__		:  Dict=20_48			,				snake_case__		:  Union[str, Any]=1_28			,				snake_case__		:  int=1			,				snake_case__		:  Union[str, Any]=5_12			,				snake_case__		:  int=30			,				snake_case__		:  str=4_41_00			,				):



   '''simple docstring'''




   UpperCAmelCase__      :					str				       =							parent
   UpperCAmelCase__      :					str				       =							batch_size
   UpperCAmelCase__      :					Optional[Any]				       =							min_seq_length
   UpperCAmelCase__      :					Dict				       =							max_seq_length
   UpperCAmelCase__      :					Optional[int]				       =							(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
   UpperCAmelCase__      :					Any				       =							spectrogram_length
   UpperCAmelCase__      :					Any				       =							feature_size
   UpperCAmelCase__      :					List[str]				       =							num_audio_channels
   UpperCAmelCase__      :					int				       =							hop_length
   UpperCAmelCase__      :					int				       =							chunk_length
   UpperCAmelCase__      :					Dict				       =							sampling_rate







 def 							UpperCamelCase  (					self		:  str ):



   '''simple docstring'''




   return {
       "spectrogram_length": self.spectrogram_length,
       "feature_size": self.feature_size,
       "num_audio_channels": self.num_audio_channels,
       "hop_length": self.hop_length,
       "chunk_length": self.chunk_length,
       "sampling_rate": self.sampling_rate,
   }







 def 							UpperCamelCase  (					self		:  Dict			,				snake_case__		:  int=False			,				snake_case__		:  str=False ):



   '''simple docstring'''




   def _flatten(snake_case__		:  Optional[int] ):
     return list(itertools.chain(*snake_case__ ) )

   if equal_length:
     UpperCAmelCase__      :					Dict				       =							[floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )]
   else:
     # make sure that inputs increase in size
     UpperCAmelCase__      :					Any				       =							[
         floats_list((x, self.feature_size) )
         for x in range(self.min_seq_length			,				self.max_seq_length			,				self.seq_length_diff )
     ]
   if numpify:
     UpperCAmelCase__      :					Tuple				       =							[np.asarray(snake_case__ ) for x in speech_inputs]
   return speech_inputs






@require_torch
@require_torchaudio
class 							UpperCAmelCase_							(    A					,				unittest.TestCase				):

 '''simple docstring'''
 lowercase_					:    Dict				=							TvltFeatureExtractor







 def 							UpperCamelCase  (					self		:  List[Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[int]				       =							TvltFeatureExtractionTester(self )







 def 							UpperCamelCase  (					self		:  Optional[int] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Any				       =							self.feature_extraction_class(**self.feat_extract_dict )
   self.assertTrue(hasattr(snake_case__			,				"spectrogram_length" ) )
   self.assertTrue(hasattr(snake_case__			,				"feature_size" ) )
   self.assertTrue(hasattr(snake_case__			,				"num_audio_channels" ) )
   self.assertTrue(hasattr(snake_case__			,				"hop_length" ) )
   self.assertTrue(hasattr(snake_case__			,				"chunk_length" ) )
   self.assertTrue(hasattr(snake_case__			,				"sampling_rate" ) )







 def 							UpperCamelCase  (					self		:  int ):



   '''simple docstring'''




   UpperCAmelCase__      :					Dict				       =							self.feature_extraction_class(**self.feat_extract_dict )

   with tempfile.TemporaryDirectory() as tmpdirname:
     UpperCAmelCase__      :					int				       =							feat_extract_first.save_pretrained(snake_case__ )[0]
     check_json_file_has_correct_format(snake_case__ )
     UpperCAmelCase__      :					List[Any]				       =							self.feature_extraction_class.from_pretrained(snake_case__ )

   UpperCAmelCase__      :					List[str]				       =							feat_extract_first.to_dict()
   UpperCAmelCase__      :					List[str]				       =							feat_extract_second.to_dict()
   UpperCAmelCase__      :					Dict				       =							dict_first.pop("mel_filters" )
   UpperCAmelCase__      :					List[str]				       =							dict_second.pop("mel_filters" )
   self.assertTrue(np.allclose(snake_case__			,				snake_case__ ) )
   self.assertEqual(snake_case__			,				snake_case__ )







 def 							UpperCamelCase  (					self		:  Union[str, Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Union[str, Any]				       =							self.feature_extraction_class(**self.feat_extract_dict )

   with tempfile.TemporaryDirectory() as tmpdirname:
     UpperCAmelCase__      :					Any				       =							os.path.join(snake_case__			,				"feat_extract.json" )
     feat_extract_first.to_json_file(snake_case__ )
     UpperCAmelCase__      :					str				       =							self.feature_extraction_class.from_json_file(snake_case__ )

   UpperCAmelCase__      :					Union[str, Any]				       =							feat_extract_first.to_dict()
   UpperCAmelCase__      :					Dict				       =							feat_extract_second.to_dict()
   UpperCAmelCase__      :					Union[str, Any]				       =							dict_first.pop("mel_filters" )
   UpperCAmelCase__      :					int				       =							dict_second.pop("mel_filters" )
   self.assertTrue(np.allclose(snake_case__			,				snake_case__ ) )
   self.assertEqual(snake_case__			,				snake_case__ )







 def 							UpperCamelCase  (					self		:  List[str] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Any				       =							self.feature_extraction_class(**self.feat_extract_dict )

   # create three inputs of length 800, 1000, and 1200
   UpperCAmelCase__      :					List[Any]				       =							[floats_list((1, x) )[0] for x in range(8_00			,				14_00			,				2_00 )]
   UpperCAmelCase__      :					List[str]				       =							[np.asarray(snake_case__ ) for speech_input in speech_inputs]

   # Test not batched input
   UpperCAmelCase__      :					List[str]				       =							feature_extractor(np_speech_inputs[0]			,				return_tensors="np"			,				sampling_rate=4_41_00 ).audio_values

   self.assertTrue(encoded_audios.ndim == 4 )
   self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size )
   self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length )
   self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels )

   # Test batched
   UpperCAmelCase__      :					List[Any]				       =							feature_extractor(snake_case__			,				return_tensors="np"			,				sampling_rate=4_41_00 ).audio_values

   self.assertTrue(encoded_audios.ndim == 4 )
   self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size )
   self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length )
   self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels )

   # Test audio masking
   UpperCAmelCase__      :					Any				       =							feature_extractor(
       snake_case__			,				return_tensors="np"			,				sampling_rate=4_41_00			,				mask_audio=snake_case__ ).audio_values

   self.assertTrue(encoded_audios.ndim == 4 )
   self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size )
   self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length )
   self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels )

   # Test 2-D numpy arrays are batched.
   UpperCAmelCase__      :					Optional[int]				       =							[floats_list((1, x) )[0] for x in (8_00, 8_00, 8_00)]
   UpperCAmelCase__      :					int				       =							np.asarray(snake_case__ )
   UpperCAmelCase__      :					Optional[int]				       =							feature_extractor(snake_case__			,				return_tensors="np"			,				sampling_rate=4_41_00 ).audio_values
   self.assertTrue(encoded_audios.ndim == 4 )
   self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size )
   self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length )
   self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels )







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



   '''simple docstring'''




   UpperCAmelCase__      :					List[Any]				       =							load_dataset("hf-internal-testing/librispeech_asr_dummy"			,				"clean"			,				split="validation" )
   # automatic decoding with librispeech
   UpperCAmelCase__      :					Tuple				       =							ds.sort("id" ).select(range(snake_case__ ) )[:num_samples]["audio"]

   return [x["array"] for x in speech_samples]







 def 							UpperCamelCase  (					self		:  List[Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Dict				       =							self._load_datasamples(1 )
   UpperCAmelCase__      :					List[str]				       =							TvltFeatureExtractor()
   UpperCAmelCase__      :					List[str]				       =							feature_extractor(snake_case__			,				return_tensors="pt" ).audio_values

   self.assertEquals(audio_values.shape			,				(1, 1, 1_92, 1_28) )

   UpperCAmelCase__      :					Optional[int]				       =							torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]] )
   self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]			,				snake_case__			,				atol=1e-4 ) )





'''simple docstring'''





import json
import os
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import ImageGPTImageProcessor






class 							UpperCAmelCase_							(    unittest.TestCase				):

 '''simple docstring'''







 def __init__(					self		:  Optional[int]			,				snake_case__		:  int			,				snake_case__		:  Optional[Any]=7			,				snake_case__		:  Tuple=3			,				snake_case__		:  Optional[Any]=18			,				snake_case__		:  Dict=30			,				snake_case__		:  Optional[int]=4_00			,				snake_case__		:  str=True			,				snake_case__		:  str=None			,				snake_case__		:  Optional[int]=True			,				):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[int]				       =							size if size is not None else {"height": 18, "width": 18}
   UpperCAmelCase__      :					List[str]				       =							parent
   UpperCAmelCase__      :					Union[str, Any]				       =							batch_size
   UpperCAmelCase__      :					List[str]				       =							num_channels
   UpperCAmelCase__      :					List[Any]				       =							image_size
   UpperCAmelCase__      :					Tuple				       =							min_resolution
   UpperCAmelCase__      :					Optional[int]				       =							max_resolution
   UpperCAmelCase__      :					Any				       =							do_resize
   UpperCAmelCase__      :					Any				       =							size
   UpperCAmelCase__      :					int				       =							do_normalize







 def 							UpperCamelCase  (					self		:  List[str] ):



   '''simple docstring'''




   return {
       # here we create 2 clusters for the sake of simplicity
       "clusters": np.asarray(
           [
               [0.8866443634033203, 0.6618829369544983, 0.3891746401786804],
               [-0.6042559146881104, -0.02295008860528469, 0.5423797369003296],
           ] ),
       "do_resize": self.do_resize,
       "size": self.size,
       "do_normalize": self.do_normalize,
   }






@require_torch
@require_vision
class 							UpperCAmelCase_							(    A					,				unittest.TestCase				):

 '''simple docstring'''
 lowercase_					:    Any				=							ImageGPTImageProcessor if is_vision_available() else None







 def 							UpperCamelCase  (					self		:  Dict ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[str]				       =							ImageGPTImageProcessingTester(self )







 @property
 def 							UpperCamelCase  (					self		:  List[Any] ):



   '''simple docstring'''




   return self.image_processor_tester.prepare_image_processor_dict()







 def 							UpperCamelCase  (					self		:  Union[str, Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[str]				       =							self.image_processing_class(**self.image_processor_dict )
   self.assertTrue(hasattr(snake_case__			,				"clusters" ) )
   self.assertTrue(hasattr(snake_case__			,				"do_resize" ) )
   self.assertTrue(hasattr(snake_case__			,				"size" ) )
   self.assertTrue(hasattr(snake_case__			,				"do_normalize" ) )







 def 							UpperCamelCase  (					self		:  Tuple ):



   '''simple docstring'''




   UpperCAmelCase__      :					str				       =							self.image_processing_class.from_dict(self.image_processor_dict )
   self.assertEqual(image_processor.size			,				{"height": 18, "width": 18} )

   UpperCAmelCase__      :					Union[str, Any]				       =							self.image_processing_class.from_dict(self.image_processor_dict			,				size=42 )
   self.assertEqual(image_processor.size			,				{"height": 42, "width": 42} )







 def 							UpperCamelCase  (					self		:  Any ):



   '''simple docstring'''




   UpperCAmelCase__      :					int				       =							self.image_processing_class(**self.image_processor_dict )
   UpperCAmelCase__      :					List[str]				       =							json.loads(image_processor.to_json_string() )
   for key, value in self.image_processor_dict.items():
     if key == "clusters":
       self.assertTrue(np.array_equal(snake_case__			,				obj[key] ) )
     else:
       self.assertEqual(obj[key]			,				snake_case__ )







 def 							UpperCamelCase  (					self		:  Union[str, Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Dict				       =							self.image_processing_class(**self.image_processor_dict )

   with tempfile.TemporaryDirectory() as tmpdirname:
     UpperCAmelCase__      :					Union[str, Any]				       =							os.path.join(snake_case__			,				"image_processor.json" )
     image_processor_first.to_json_file(snake_case__ )
     UpperCAmelCase__      :					Any				       =							self.image_processing_class.from_json_file(snake_case__ ).to_dict()

   UpperCAmelCase__      :					Union[str, Any]				       =							image_processor_first.to_dict()
   for key, value in image_processor_first.items():
     if key == "clusters":
       self.assertTrue(np.array_equal(snake_case__			,				image_processor_second[key] ) )
     else:
       self.assertEqual(image_processor_first[key]			,				snake_case__ )







 def 							UpperCamelCase  (					self		:  List[str] ):



   '''simple docstring'''




   UpperCAmelCase__      :					int				       =							self.image_processing_class(**self.image_processor_dict )

   with tempfile.TemporaryDirectory() as tmpdirname:
     image_processor_first.save_pretrained(snake_case__ )
     UpperCAmelCase__      :					List[str]				       =							self.image_processing_class.from_pretrained(snake_case__ ).to_dict()

   UpperCAmelCase__      :					Tuple				       =							image_processor_first.to_dict()
   for key, value in image_processor_first.items():
     if key == "clusters":
       self.assertTrue(np.array_equal(snake_case__			,				image_processor_second[key] ) )
     else:
       self.assertEqual(image_processor_first[key]			,				snake_case__ )







 @unittest.skip("ImageGPT requires clusters at initialization" )
 def 							UpperCamelCase  (					self		:  List[Any] ):



   '''simple docstring'''




   pass







def snake_case_     (					):
  UpperCAmelCase__      :					List[str]				       =							load_dataset("hf-internal-testing/fixtures_image_utils"   ,       split="test"		)

  UpperCAmelCase__      :					Tuple				       =							Image.open(dataset[4]["file"]		)
  UpperCAmelCase__      :					List[Any]				       =							Image.open(dataset[5]["file"]		)

  UpperCAmelCase__      :					Optional[Any]				       =							[imagea, imagea]

  return images






@require_vision
@require_torch
class 							UpperCAmelCase_							(    unittest.TestCase				):

 '''simple docstring'''







 @slow
 def 							UpperCamelCase  (					self		:  str ):



   '''simple docstring'''




   UpperCAmelCase__      :					Dict				       =							ImageGPTImageProcessor.from_pretrained("openai/imagegpt-small" )

   UpperCAmelCase__      :					Union[str, Any]				       =							prepare_images()

   # test non-batched
   UpperCAmelCase__      :					Optional[Any]				       =							image_processing(images[0]			,				return_tensors="pt" )

   self.assertIsInstance(encoding.input_ids			,				torch.LongTensor )
   self.assertEqual(encoding.input_ids.shape			,				(1, 10_24) )

   UpperCAmelCase__      :					Union[str, Any]				       =							[3_06, 1_91, 1_91]
   self.assertEqual(encoding.input_ids[0, :3].tolist()			,				snake_case__ )

   # test batched
   UpperCAmelCase__      :					List[Any]				       =							image_processing(snake_case__			,				return_tensors="pt" )

   self.assertIsInstance(encoding.input_ids			,				torch.LongTensor )
   self.assertEqual(encoding.input_ids.shape			,				(2, 10_24) )

   UpperCAmelCase__      :					List[str]				       =							[3_03, 13, 13]
   self.assertEqual(encoding.input_ids[1, -3:].tolist()			,				snake_case__ )



