





import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_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_speech_available():
    from transformers import WhisperFeatureExtractor

if is_torch_available():
    import torch

UpperCamelCase_												=	random.Random()






def 				_UpperCAmelCase			(    A				,							A=1.0				,							A=None				,							A=None     ):




   '''simple docstring'''

   if rng is None:
      UpperCAmelCase__						=global_rng

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

   return values

@require_torch
@require_torchaudio
class     snake_case_		(   unittest.TestCase							):






 '''simple docstring'''





 def __init__(  self,		A_,		A_=7,		A_=400,		A_=2000,		A_=10,		A_=160,		A_=8,		A_=0.0,		A_=4000,		A_=False,		A_=True,		)	->       List[str]:
    UpperCAmelCase__						=parent
    UpperCAmelCase__						=batch_size
    UpperCAmelCase__						=min_seq_length
    UpperCAmelCase__						=max_seq_length
    UpperCAmelCase__						=(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
    UpperCAmelCase__						=padding_value
    UpperCAmelCase__						=sampling_rate
    UpperCAmelCase__						=return_attention_mask
    UpperCAmelCase__						=do_normalize
    UpperCAmelCase__						=feature_size
    UpperCAmelCase__						=chunk_length
    UpperCAmelCase__						=hop_length

 def  __UpperCAmelCase						(  self		)	->       Optional[int]:
    return {
        "feature_size": self.feature_size,
        "hop_length": self.hop_length,
        "chunk_length": self.chunk_length,
        "padding_value": self.padding_value,
        "sampling_rate": self.sampling_rate,
        "return_attention_mask": self.return_attention_mask,
        "do_normalize": self.do_normalize,
    }






 def  __UpperCAmelCase						(  self,		A_=False,		A_=False		)	->       List[str]:
    def _flatten(A_		):
       return list(itertools.chain(*A_		)		)

    if equal_length:
       UpperCAmelCase__						=[floats_list((self.max_seq_length, self.feature_size)		) for _ in range(self.batch_size		)]
    else:
       # make sure that inputs increase in size
       UpperCAmelCase__						=[
           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__						=[np.asarray(A_		) for x in speech_inputs]
    return speech_inputs

@require_torch
@require_torchaudio
class     snake_case_		(   a,		unittest.TestCase							):






 '''simple docstring'''





 __UpperCamelCase       =      WhisperFeatureExtractor if is_speech_available() else None

 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    UpperCAmelCase__						=WhisperFeatureExtractionTester(self		)

 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    UpperCAmelCase__						=self.feature_extraction_class(**self.feat_extract_dict		)

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

    UpperCAmelCase__						=feat_extract_first.to_dict()
    UpperCAmelCase__						=feat_extract_second.to_dict()
    UpperCAmelCase__						=feat_extract_first.mel_filters
    UpperCAmelCase__						=feat_extract_second.mel_filters
    self.assertTrue(np.allclose(A_,		A_		)		)
    self.assertEqual(A_,		A_		)

 def  __UpperCAmelCase						(  self		)	->       Optional[int]:
    UpperCAmelCase__						=self.feature_extraction_class(**self.feat_extract_dict		)

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

    UpperCAmelCase__						=feat_extract_first.to_dict()
    UpperCAmelCase__						=feat_extract_second.to_dict()
    UpperCAmelCase__						=feat_extract_first.mel_filters
    UpperCAmelCase__						=feat_extract_second.mel_filters
    self.assertTrue(np.allclose(A_,		A_		)		)
    self.assertEqual(A_,		A_		)

 def  __UpperCAmelCase						(  self		)	->       List[str]:
    # Tests that all call wrap to encode_plus and batch_encode_plus
    UpperCAmelCase__						=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
    # create three inputs of length 800, 1000, and 1200
    UpperCAmelCase__						=[floats_list((1, x)		)[0] for x in range(800,		1400,		200		)]
    UpperCAmelCase__						=[np.asarray(A_		) for speech_input in speech_inputs]

    # Test feature size
    UpperCAmelCase__						=feature_extractor(A_,		padding="max_length",		return_tensors="np"		).input_features
    self.assertTrue(input_features.ndim == 3		)
    self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames		)
    self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size		)

    # Test not batched input
    UpperCAmelCase__						=feature_extractor(speech_inputs[0],		return_tensors="np"		).input_features
    UpperCAmelCase__						=feature_extractor(np_speech_inputs[0],		return_tensors="np"		).input_features
    self.assertTrue(np.allclose(A_,		A_,		atol=1E-3		)		)

    # Test batched
    UpperCAmelCase__						=feature_extractor(A_,		return_tensors="np"		).input_features
    UpperCAmelCase__						=feature_extractor(A_,		return_tensors="np"		).input_features
    for enc_seq_a, enc_seq_a in zip(A_,		A_		):
       self.assertTrue(np.allclose(A_,		A_,		atol=1E-3		)		)

    # Test 2-D numpy arrays are batched.
    UpperCAmelCase__						=[floats_list((1, x)		)[0] for x in (800, 800, 800)]
    UpperCAmelCase__						=np.asarray(A_		)
    UpperCAmelCase__						=feature_extractor(A_,		return_tensors="np"		).input_features
    UpperCAmelCase__						=feature_extractor(A_,		return_tensors="np"		).input_features
    for enc_seq_a, enc_seq_a in zip(A_,		A_		):
       self.assertTrue(np.allclose(A_,		A_,		atol=1E-3		)		)

    # Test truncation required
    UpperCAmelCase__						=[floats_list((1, x)		)[0] for x in range(200,		(feature_extractor.n_samples + 500),		200		)]
    UpperCAmelCase__						=[np.asarray(A_		) for speech_input in speech_inputs]

    UpperCAmelCase__						=[x[: feature_extractor.n_samples] for x in speech_inputs]
    UpperCAmelCase__						=[np.asarray(A_		) for speech_input in speech_inputs_truncated]

    UpperCAmelCase__						=feature_extractor(A_,		return_tensors="np"		).input_features
    UpperCAmelCase__						=feature_extractor(A_,		return_tensors="np"		).input_features
    for enc_seq_a, enc_seq_a in zip(A_,		A_		):
       self.assertTrue(np.allclose(A_,		A_,		atol=1E-3		)		)

 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    import torch

    UpperCAmelCase__						=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
    UpperCAmelCase__						=np.random.rand(100,		32		).astype(np.floataa		)
    UpperCAmelCase__						=np_speech_inputs.tolist()

    for inputs in [py_speech_inputs, np_speech_inputs]:
       UpperCAmelCase__						=feature_extractor.pad([{"input_features": inputs}],		return_tensors="np"		)
       self.assertTrue(np_processed.input_features.dtype == np.floataa		)
       UpperCAmelCase__						=feature_extractor.pad([{"input_features": inputs}],		return_tensors="pt"		)
       self.assertTrue(pt_processed.input_features.dtype == torch.floataa		)

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

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

 def  __UpperCAmelCase						(  self		)	->       List[str]:
    # fmt: off
    UpperCAmelCase__						=torch.tensor(
        [
            0.11_93, -0.09_46, -0.10_98, -0.01_96, 0.02_25, -0.06_90, -0.17_36, 0.09_51,
            0.09_71, -0.08_17, -0.07_02, 0.01_62, 0.02_60, 0.00_17, -0.01_92, -0.16_78,
            0.07_09, -0.18_67, -0.06_55, -0.02_74, -0.02_34, -0.18_84, -0.05_16, -0.05_54,
            -0.02_74, -0.14_25, -0.14_23, 0.08_37, 0.03_77, -0.08_54
        ]		)
    # fmt: on

    UpperCAmelCase__						=self._load_datasamples(1		)
    UpperCAmelCase__						=WhisperFeatureExtractor()
    UpperCAmelCase__						=feature_extractor(A_,		return_tensors="pt"		).input_features
    self.assertEqual(input_features.shape,		(1, 80, 3000)		)
    self.assertTrue(torch.allclose(input_features[0, 0, :30],		A_,		atol=1E-4		)		)






 def  __UpperCAmelCase						(  self		)	->       Optional[Any]:
    UpperCAmelCase__						=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
    UpperCAmelCase__						=self._load_datasamples(1		)[0]
    UpperCAmelCase__						=((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535  # Rescale to [0, 65535] to show issue
    UpperCAmelCase__						=feat_extract.zero_mean_unit_var_norm([audio],		attention_mask=A_		)[0]

    self.assertTrue(np.all(np.mean(A_		) < 1E-3		)		)
    self.assertTrue(np.all(np.abs(np.var(A_		) - 1		) < 1E-3		)		)










import os
import tempfile
import unittest
import uuid
from pathlib import Path

from transformers.testing_utils import get_tests_dir, require_soundfile, require_torch, require_vision
from transformers.tools.agent_types import AgentAudio, AgentImage, AgentText
from transformers.utils import is_soundfile_availble, is_torch_available, is_vision_available


if is_torch_available():
    import torch

if is_soundfile_availble():
    import soundfile as sf

if is_vision_available():
    from PIL import Image






def 				_UpperCAmelCase			(    A=""     ):




   '''simple docstring'''

   UpperCAmelCase__						=tempfile.mkdtemp()
   return os.path.join(A				,							str(uuid.uuida()     ) + suffix     )

@require_soundfile
@require_torch
class     snake_case_		(   unittest.TestCase							):






 '''simple docstring'''





 def  __UpperCAmelCase						(  self		)	->       str:
    UpperCAmelCase__						=torch.rand(12,		dtype=torch.floataa		) - 0.5
    UpperCAmelCase__						=AgentAudio(A_		)
    UpperCAmelCase__						=str(agent_type.to_string()		)

    # Ensure that the tensor and the agent_type's tensor are the same
    self.assertTrue(torch.allclose(A_,		agent_type.to_raw(),		atol=1E-4		)		)

    del agent_type

    # Ensure the path remains even after the object deletion
    self.assertTrue(os.path.exists(A_		)		)

    # Ensure that the file contains the same value as the original tensor
    UpperCAmelCase__			,							UpperCAmelCase__						=sf.read(A_		)
    self.assertTrue(torch.allclose(A_,		torch.tensor(A_		),		atol=1E-4		)		)






 def  __UpperCAmelCase						(  self		)	->       Optional[Any]:
    UpperCAmelCase__						=torch.rand(12,		dtype=torch.floataa		) - 0.5
    UpperCAmelCase__						=get_new_path(suffix=".wav"		)
    sf.write(A_,		A_,		1_6000		)

    UpperCAmelCase__						=AgentAudio(A_		)

    self.assertTrue(torch.allclose(A_,		agent_type.to_raw(),		atol=1E-4		)		)
    self.assertEqual(agent_type.to_string(),		A_		)

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






 '''simple docstring'''





 def  __UpperCAmelCase						(  self		)	->       int:
    UpperCAmelCase__						=torch.randint(0,		256,		(64, 64, 3)		)
    UpperCAmelCase__						=AgentImage(A_		)
    UpperCAmelCase__						=str(agent_type.to_string()		)

    # Ensure that the tensor and the agent_type's tensor are the same
    self.assertTrue(torch.allclose(A_,		agent_type._tensor,		atol=1E-4		)		)

    self.assertIsInstance(agent_type.to_raw(),		Image.Image		)

    # Ensure the path remains even after the object deletion
    del agent_type
    self.assertTrue(os.path.exists(A_		)		)

 def  __UpperCAmelCase						(  self		)	->       List[str]:
    UpperCAmelCase__						=Path(get_tests_dir("fixtures/tests_samples/COCO"		)		) / "000000039769.png"
    UpperCAmelCase__						=Image.open(A_		)
    UpperCAmelCase__						=AgentImage(A_		)

    self.assertTrue(path.samefile(agent_type.to_string()		)		)
    self.assertTrue(image == agent_type.to_raw()		)

    # Ensure the path remains even after the object deletion
    del agent_type
    self.assertTrue(os.path.exists(A_		)		)






 def  __UpperCAmelCase						(  self		)	->       Tuple:
    UpperCAmelCase__						=Path(get_tests_dir("fixtures/tests_samples/COCO"		)		) / "000000039769.png"
    UpperCAmelCase__						=Image.open(A_		)
    UpperCAmelCase__						=AgentImage(A_		)

    self.assertFalse(path.samefile(agent_type.to_string()		)		)
    self.assertTrue(image == agent_type.to_raw()		)

    # Ensure the path remains even after the object deletion
    del agent_type
    self.assertTrue(os.path.exists(A_		)		)

class     snake_case_		(   unittest.TestCase							):






 '''simple docstring'''





 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    UpperCAmelCase__						="Hey!"
    UpperCAmelCase__						=AgentText(A_		)

    self.assertEqual(A_,		agent_type.to_string()		)
    self.assertEqual(A_,		agent_type.to_raw()		)
    self.assertEqual(A_,		A_		)



