



from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


lowerCAmelCase_           = logging.get_logger(__name__)
class      UpperCamelCase (				snake_case__ ):







		"""simple docstring"""



		snake_case    =   ["input_features", "attention_mask"]
		def __init__(  self		:       str						,_SCREAMING_SNAKE_CASE		:       Optional[Any]=8_0						,_SCREAMING_SNAKE_CASE		:       Union[str, Any]=1_6_0_0_0						,_SCREAMING_SNAKE_CASE		:       Dict=8_0						,_SCREAMING_SNAKE_CASE		:       Optional[int]=0.0						,_SCREAMING_SNAKE_CASE		:       int=True						,_SCREAMING_SNAKE_CASE		:       List[Any]=True						,_SCREAMING_SNAKE_CASE		:       str=True						,**_SCREAMING_SNAKE_CASE		:       Optional[Any]						,)    ->   Dict:


						'''simple docstring'''
						super().__init__(feature_size=_SCREAMING_SNAKE_CASE						,sampling_rate=_SCREAMING_SNAKE_CASE						,padding_value=_SCREAMING_SNAKE_CASE						,**_SCREAMING_SNAKE_CASE		)
						A	 =      num_mel_bins
						A	 =      do_ceptral_normalize
						A	 =      normalize_means
						A	 =      normalize_vars
						A	 =      True
		def       A(  self		:       Optional[int]						,_SCREAMING_SNAKE_CASE		:       np.ndarray						,)    ->   np.ndarray:


						'''simple docstring'''
						A	 =      waveform * (2**1_5)  # Kaldi compliance: 16-bit signed integers
						A	 =      torch.from_numpy(_SCREAMING_SNAKE_CASE		).unsqueeze(0		)
						A	 =      ta_kaldi.fbank(_SCREAMING_SNAKE_CASE						,num_mel_bins=self.num_mel_bins						,sample_frequency=self.sampling_rate		)
						return features.numpy()
		@staticmethod
		def       A(  _SCREAMING_SNAKE_CASE		:       np.ndarray						,_SCREAMING_SNAKE_CASE		:       int						,_SCREAMING_SNAKE_CASE		:       Optional[bool] = True						,_SCREAMING_SNAKE_CASE		:       Optional[bool] = True						,_SCREAMING_SNAKE_CASE		:       float = 0.0						,)    ->   np.ndarray:


						'''simple docstring'''
						# make sure we normalize float32 arrays
						if normalize_means:
										A	 =      x[:input_length].mean(axis=0		)
										A	 =      np.subtract(_SCREAMING_SNAKE_CASE						,_SCREAMING_SNAKE_CASE		)
						if normalize_vars:
										A	 =      x[:input_length].std(axis=0		)
										A	 =      np.divide(_SCREAMING_SNAKE_CASE						,_SCREAMING_SNAKE_CASE		)

						if input_length < x.shape[0]:
										A	 =      padding_value

						# make sure array is in float32
						A	 =      x.astype(np.floataa		)

						return x
		def       A(  self		:       Any						,_SCREAMING_SNAKE_CASE		:       List[np.ndarray]						,_SCREAMING_SNAKE_CASE		:       Optional[np.ndarray] = None		)    ->   List[np.ndarray]:


						'''simple docstring'''
						A	 =      attention_mask.sum(-1		) if attention_mask is not None else [x.shape[0] for x in input_features]
						return [
						    self.utterance_cmvn(_SCREAMING_SNAKE_CASE						,_SCREAMING_SNAKE_CASE						,self.normalize_means						,self.normalize_vars						,self.padding_value		)
						    for x, n in zip(_SCREAMING_SNAKE_CASE						,_SCREAMING_SNAKE_CASE		)
						]







		def __call__(  self		:       List[str]						,_SCREAMING_SNAKE_CASE		:       Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]						,_SCREAMING_SNAKE_CASE		:       Union[bool, str, PaddingStrategy] = False						,_SCREAMING_SNAKE_CASE		:       Optional[int] = None						,_SCREAMING_SNAKE_CASE		:       bool = False						,_SCREAMING_SNAKE_CASE		:       Optional[int] = None						,_SCREAMING_SNAKE_CASE		:       Optional[Union[str, TensorType]] = None						,_SCREAMING_SNAKE_CASE		:       Optional[int] = None						,_SCREAMING_SNAKE_CASE		:       Optional[bool] = None						,**_SCREAMING_SNAKE_CASE		:       Dict						,)    ->   BatchFeature:


						'''simple docstring'''

						if sampling_rate is not None:
										if sampling_rate != self.sampling_rate:
														raise ValueError(
														    f'The model corresponding to this feature extractor: {self} was trained using a sampling rate of'
														    f' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with'
														    f' {self.sampling_rate} and not {sampling_rate}.'		)
						else:
										logger.warning(
										    'It is strongly recommended to pass the `sampling_rate` argument to this function. '
										    'Failing to do so can result in silent errors that might be hard to debug.'		)

						A	 =      isinstance(_SCREAMING_SNAKE_CASE						,np.ndarray		) and len(raw_speech.shape		) > 1
						if is_batched_numpy and len(raw_speech.shape		) > 2:
										raise ValueError(f'Only mono-channel audio is supported for input to {self}'		)
						A	 =      is_batched_numpy or (
						    isinstance(_SCREAMING_SNAKE_CASE						,(list, tuple)		) and (isinstance(raw_speech[0]						,(np.ndarray, tuple, list)		))
						)

						if is_batched:
										A	 =      [np.asarray(_SCREAMING_SNAKE_CASE						,dtype=np.floataa		) for speech in raw_speech]
						elif not is_batched and not isinstance(_SCREAMING_SNAKE_CASE						,np.ndarray		):
										A	 =      np.asarray(_SCREAMING_SNAKE_CASE						,dtype=np.floataa		)
						elif isinstance(_SCREAMING_SNAKE_CASE						,np.ndarray		) and raw_speech.dtype is np.dtype(np.floataa		):
										A	 =      raw_speech.astype(np.floataa		)

						# always return batch
						if not is_batched:
										A	 =      [raw_speech]

						# extract fbank features
						A	 =      [self._extract_fbank_features(_SCREAMING_SNAKE_CASE		) for waveform in raw_speech]

						# convert into correct format for padding
						A	 =      BatchFeature({'input_features': features}		)

						A	 =      self.pad(
						    _SCREAMING_SNAKE_CASE						,padding=_SCREAMING_SNAKE_CASE						,max_length=_SCREAMING_SNAKE_CASE						,truncation=_SCREAMING_SNAKE_CASE						,pad_to_multiple_of=_SCREAMING_SNAKE_CASE						,return_attention_mask=_SCREAMING_SNAKE_CASE						,**_SCREAMING_SNAKE_CASE						,)

						# make sure list is in array format
						A	 =      padded_inputs.get('input_features'		)
						if isinstance(input_features[0]						,_SCREAMING_SNAKE_CASE		):
										A	 =      [np.asarray(_SCREAMING_SNAKE_CASE						,dtype=np.floataa		) for feature in input_features]

						A	 =      padded_inputs.get('attention_mask'		)
						if attention_mask is not None:
										A	 =      [np.asarray(_SCREAMING_SNAKE_CASE						,dtype=np.intaa		) for array in attention_mask]

						# Utterance-level cepstral mean and variance normalization
						if self.do_ceptral_normalize:
										A	 =      (
										    np.array(_SCREAMING_SNAKE_CASE						,dtype=np.intaa		)
										    if self._get_padding_strategies(_SCREAMING_SNAKE_CASE						,max_length=_SCREAMING_SNAKE_CASE		) is not PaddingStrategy.DO_NOT_PAD
										    else None
										)
										A	 =      self.normalize(
										    padded_inputs['input_features']						,attention_mask=_SCREAMING_SNAKE_CASE		)

						if return_tensors is not None:
										A	 =      padded_inputs.convert_to_tensors(_SCREAMING_SNAKE_CASE		)

						return padded_inputs













import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging


if TYPE_CHECKING:
					from transformers.pipelines.conversational import Conversation


lowerCAmelCase_           = logging.get_logger(__name__)

lowerCAmelCase_           = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'}

lowerCAmelCase_           = {
    'tokenizer_file': {
        'EleutherAI/gpt-neox-20b': 'https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json',
    },
}

lowerCAmelCase_           = {
    'gpt-neox-20b': 2_048,
}
class      UpperCamelCase (				snake_case__ ):







		"""simple docstring"""



		snake_case    =   VOCAB_FILES_NAMES
		snake_case    =   PRETRAINED_VOCAB_FILES_MAP
		snake_case    =   PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		snake_case    =   ["input_ids", "attention_mask"]
		def __init__(  self		:       str						,_SCREAMING_SNAKE_CASE		:       Union[str, Any]=None						,_SCREAMING_SNAKE_CASE		:       Union[str, Any]=None						,_SCREAMING_SNAKE_CASE		:       Union[str, Any]=None						,_SCREAMING_SNAKE_CASE		:       Dict="<|endoftext|>"						,_SCREAMING_SNAKE_CASE		:       int="<|endoftext|>"						,_SCREAMING_SNAKE_CASE		:       Optional[int]="<|endoftext|>"						,_SCREAMING_SNAKE_CASE		:       List[Any]=False						,**_SCREAMING_SNAKE_CASE		:       Union[str, Any]						,)    ->   int:


						'''simple docstring'''
						super().__init__(
						    _SCREAMING_SNAKE_CASE						,_SCREAMING_SNAKE_CASE						,tokenizer_file=_SCREAMING_SNAKE_CASE						,unk_token=_SCREAMING_SNAKE_CASE						,bos_token=_SCREAMING_SNAKE_CASE						,eos_token=_SCREAMING_SNAKE_CASE						,add_prefix_space=_SCREAMING_SNAKE_CASE						,**_SCREAMING_SNAKE_CASE						,)

						A	 =      json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()		)
						if pre_tok_state.get('add_prefix_space'						,_SCREAMING_SNAKE_CASE		) != add_prefix_space:
										A	 =      getattr(_SCREAMING_SNAKE_CASE						,pre_tok_state.pop('type'		)		)
										A	 =      add_prefix_space
										A	 =      pre_tok_class(**_SCREAMING_SNAKE_CASE		)

						A	 =      add_prefix_space
		def       A(  self		:       Optional[int]						,_SCREAMING_SNAKE_CASE		:       str						,_SCREAMING_SNAKE_CASE		:       Optional[str] = None		)    ->   Tuple[str]:


						'''simple docstring'''
						A	 =      self._tokenizer.model.save(_SCREAMING_SNAKE_CASE						,name=_SCREAMING_SNAKE_CASE		)
						return tuple(_SCREAMING_SNAKE_CASE		)







		def       A(  self		:       List[Any]						,_SCREAMING_SNAKE_CASE		:       "Conversation"		)    ->   List[int]:


						'''simple docstring'''
						A	 =      []
						for is_user, text in conversation.iter_texts():
										input_ids.extend(self.encode(_SCREAMING_SNAKE_CASE						,add_special_tokens=_SCREAMING_SNAKE_CASE		) + [self.eos_token_id]		)

						if len(_SCREAMING_SNAKE_CASE		) > self.model_max_length:
										A	 =      input_ids[-self.model_max_length :]
						return input_ids





