


"""simple docstring"""





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

snake_case__		:     Union[str, Any]    				=				random.Random()


def      _snake_case						(						_snake_case				:			Optional[int] ,   _snake_case				:			List[str]=1.0 ,   _snake_case				:			int=None ,   _snake_case				:			List[str]=None			):
      if rng is None:
            lowerCAmelCase					:	str       =   global_rng

      lowerCAmelCase					:	List[Any]       =   []
      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			):






     def __init__(	self  :					Any					,				UpperCamelCase_  :					Dict					,				UpperCamelCase_  :					Any=7					,				UpperCamelCase_  :					int=4_0_0					,				UpperCamelCase_  :					int=2_0_0_0					,				UpperCamelCase_  :					str=1_0					,				UpperCamelCase_  :					str=1_6_0					,				UpperCamelCase_  :					Optional[int]=8					,				UpperCamelCase_  :					Any=0.0					,				UpperCamelCase_  :					List[Any]=4_0_0_0					,				UpperCamelCase_  :					str=False					,				UpperCamelCase_  :					str=True					,				):
           lowerCAmelCase					:	Any       =   parent
           lowerCAmelCase					:	List[Any]       =   batch_size
           lowerCAmelCase					:	List[Any]       =   min_seq_length
           lowerCAmelCase					:	Dict       =   max_seq_length
           lowerCAmelCase					:	Optional[Any]       =   (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
           lowerCAmelCase					:	Tuple       =   padding_value
           lowerCAmelCase					:	Tuple       =   sampling_rate
           lowerCAmelCase					:	str       =   return_attention_mask
           lowerCAmelCase					:	Any       =   do_normalize
           lowerCAmelCase					:	Union[str, Any]       =   feature_size
           lowerCAmelCase					:	List[Any]       =   chunk_length
           lowerCAmelCase					:	List[Any]       =   hop_length






     def      lowerCamelCase__     (	self  :					List[Any]				):
           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      lowerCamelCase__     (	self  :					Optional[Any]					,				UpperCamelCase_  :					str=False					,				UpperCamelCase_  :					Optional[Any]=False				):
           def _flatten(UpperCamelCase_  :					Optional[Any]				):
                 return list(itertools.chain(*_a				)				)

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



@require_torch
@require_torchaudio
class 		snake_case_(       UpperCamelCase__							,					unittest.TestCase			):
     __UpperCamelCase  =					WhisperFeatureExtractor if is_speech_available() else None






     def      lowerCamelCase__     (	self  :					List[str]				):
           lowerCAmelCase					:	Optional[int]       =   WhisperFeatureExtractionTester(self				)






     def      lowerCamelCase__     (	self  :					Optional[int]				):
           lowerCAmelCase					:	List[Any]       =   self.feature_extraction_class(**self.feat_extract_dict				)

           with tempfile.TemporaryDirectory() as tmpdirname:
                 lowerCAmelCase					:	List[str]       =   feat_extract_first.save_pretrained(_a				)[0]
                 check_json_file_has_correct_format(_a				)
                 lowerCAmelCase					:	Optional[int]       =   self.feature_extraction_class.from_pretrained(_a				)

           lowerCAmelCase					:	Tuple       =   feat_extract_first.to_dict()
           lowerCAmelCase					:	List[Any]       =   feat_extract_second.to_dict()
           lowerCAmelCase					:	List[Any]       =   feat_extract_first.mel_filters
           lowerCAmelCase					:	Union[str, Any]       =   feat_extract_second.mel_filters
           self.assertTrue(np.allclose(_a					,				_a				)				)
           self.assertEqual(_a					,				_a				)






     def      lowerCamelCase__     (	self  :					List[Any]				):
           lowerCAmelCase					:	Any       =   self.feature_extraction_class(**self.feat_extract_dict				)

           with tempfile.TemporaryDirectory() as tmpdirname:
                 lowerCAmelCase					:	Dict       =   os.path.join(_a					,				'''feat_extract.json'''				)
                 feat_extract_first.to_json_file(_a				)
                 lowerCAmelCase					:	Optional[int]       =   self.feature_extraction_class.from_json_file(_a				)

           lowerCAmelCase					:	str       =   feat_extract_first.to_dict()
           lowerCAmelCase					:	Any       =   feat_extract_second.to_dict()
           lowerCAmelCase					:	Union[str, Any]       =   feat_extract_first.mel_filters
           lowerCAmelCase					:	Union[str, Any]       =   feat_extract_second.mel_filters
           self.assertTrue(np.allclose(_a					,				_a				)				)
           self.assertEqual(_a					,				_a				)






     def      lowerCamelCase__     (	self  :					str				):
           # Tests that all call wrap to encode_plus and batch_encode_plus
           lowerCAmelCase					:	Optional[int]       =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
           # create three inputs of length 800, 1000, and 1200
           lowerCAmelCase					:	Optional[Any]       =   [floats_list((1, x)				)[0] for x in range(8_0_0					,				1_4_0_0					,				2_0_0				)]
           lowerCAmelCase					:	Any       =   [np.asarray(_a				) for speech_input in speech_inputs]

           # Test feature size
           lowerCAmelCase					:	Dict       =   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
           lowerCAmelCase					:	List[Any]       =   feature_extractor(speech_inputs[0]					,				return_tensors='''np'''				).input_features
           lowerCAmelCase					:	List[str]       =   feature_extractor(np_speech_inputs[0]					,				return_tensors='''np'''				).input_features
           self.assertTrue(np.allclose(_a					,				_a					,				atol=1E-3				)				)

           # Test batched
           lowerCAmelCase					:	Union[str, Any]       =   feature_extractor(_a					,				return_tensors='''np'''				).input_features
           lowerCAmelCase					:	Tuple       =   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.
           lowerCAmelCase					:	List[str]       =   [floats_list((1, x)				)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
           lowerCAmelCase					:	Any       =   np.asarray(_a				)
           lowerCAmelCase					:	Union[str, Any]       =   feature_extractor(_a					,				return_tensors='''np'''				).input_features
           lowerCAmelCase					:	int       =   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
           lowerCAmelCase					:	List[Any]       =   [floats_list((1, x)				)[0] for x in range(2_0_0					,				(feature_extractor.n_samples + 5_0_0)					,				2_0_0				)]
           lowerCAmelCase					:	Union[str, Any]       =   [np.asarray(_a				) for speech_input in speech_inputs]

           lowerCAmelCase					:	Tuple       =   [x[: feature_extractor.n_samples] for x in speech_inputs]
           lowerCAmelCase					:	Union[str, Any]       =   [np.asarray(_a				) for speech_input in speech_inputs_truncated]

           lowerCAmelCase					:	Optional[int]       =   feature_extractor(_a					,				return_tensors='''np'''				).input_features
           lowerCAmelCase					:	List[Any]       =   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      lowerCamelCase__     (	self  :					Union[str, Any]				):
           import torch

           lowerCAmelCase					:	Dict       =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
           lowerCAmelCase					:	Optional[int]       =   np.random.rand(1_0_0					,				3_2				).astype(np.floataa				)
           lowerCAmelCase					:	str       =   np_speech_inputs.tolist()

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






     def      lowerCamelCase__     (	self  :					Dict					,				UpperCamelCase_  :					int				):
           lowerCAmelCase					:	Optional[Any]       =   load_dataset('''hf-internal-testing/librispeech_asr_dummy'''					,				'''clean'''					,				split='''validation'''				)
           # automatic decoding with librispeech
           lowerCAmelCase					:	Optional[Any]       =   ds.sort('''id'''				).select(range(_a				)				)[:num_samples]["""audio"""]

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






     def      lowerCamelCase__     (	self  :					Union[str, Any]				):
           # fmt: off
           lowerCAmelCase					:	Dict       =   torch.tensor(
               [
                   0.1_193, -0.0_946, -0.1_098, -0.0_196, 0.0_225, -0.0_690, -0.1_736, 0.0_951,
                   0.0_971, -0.0_817, -0.0_702, 0.0_162, 0.0_260, 0.0_017, -0.0_192, -0.1_678,
                   0.0_709, -0.1_867, -0.0_655, -0.0_274, -0.0_234, -0.1_884, -0.0_516, -0.0_554,
                   -0.0_274, -0.1_425, -0.1_423, 0.0_837, 0.0_377, -0.0_854
               ]				)
           # fmt: on

           lowerCAmelCase					:	Dict       =   self._load_datasamples(1				)
           lowerCAmelCase					:	Optional[Any]       =   WhisperFeatureExtractor()
           lowerCAmelCase					:	Optional[Any]       =   feature_extractor(_a					,				return_tensors='''pt'''				).input_features
           self.assertEqual(input_features.shape					,				(1, 8_0, 3_0_0_0)				)
           self.assertTrue(torch.allclose(input_features[0, 0, :3_0]					,				_a					,				atol=1E-4				)				)






     def      lowerCamelCase__     (	self  :					str				):
           lowerCAmelCase					:	Dict       =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
           lowerCAmelCase					:	str       =   self._load_datasamples(1				)[0]
           lowerCAmelCase					:	Union[str, Any]       =   ((audio - audio.min()) / (audio.max() - audio.min())) * 6_5_5_3_5  # Rescale to [0, 65535] to show issue
           lowerCAmelCase					:	List[Any]       =   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				)				)







"""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 ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer
from .base import PipelineTool



class 		snake_case_(       a__			):
     __UpperCamelCase  =					'''philschmid/bart-large-cnn-samsum'''
     __UpperCamelCase  =					(
         '''This is a tool that summarizes an English text. It takes an input `text` containing the text to summarize, '''
         '''and returns a summary of the text.'''
     )
     __UpperCamelCase  =					'''summarizer'''
     __UpperCamelCase  =					AutoTokenizer
     __UpperCamelCase  =					AutoModelForSeqaSeqLM

     __UpperCamelCase  =					['''text''']
     __UpperCamelCase  =					['''text''']






     def      lowerCamelCase__     (	self  :					Dict					,				UpperCamelCase_  :					int				):
           return self.pre_processor(UpperCamelCase_					,				return_tensors='''pt'''					,				truncation=UpperCamelCase_				)






     def      lowerCamelCase__     (	self  :					Optional[int]					,				UpperCamelCase_  :					str				):
           return self.model.generate(**UpperCamelCase_				)[0]






     def      lowerCamelCase__     (	self  :					Any					,				UpperCamelCase_  :					Tuple				):
           return self.pre_processor.decode(UpperCamelCase_					,				skip_special_tokens=UpperCamelCase_					,				clean_up_tokenization_spaces=UpperCamelCase_				)


