





from typing import List, Optional, Union

import numpy as np

from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function
from ....feature_extraction_sequence_utils import SequenceFeatureExtractor
from ....feature_extraction_utils import BatchFeature
from ....file_utils import PaddingStrategy, TensorType
from ....utils import logging


_snake_case  :   List[Any]						   =  logging.get_logger(__name__)



class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			["""input_features""", """attention_mask"""]






  def __init__(    self			:		Union[str, Any] ,  lowerCAmelCase_			:		Dict=8_0 ,  lowerCAmelCase_			:		Optional[int]=1_6_0_0_0 ,  lowerCAmelCase_			:		List[str]=0.0 ,  lowerCAmelCase_			:		Union[str, Any]=1_0 ,  lowerCAmelCase_			:		List[Any]=2_5 ,  lowerCAmelCase_			:		str="hamming_window" ,  lowerCAmelCase_			:		Any=3_27_68.0 ,  lowerCAmelCase_			:		Optional[Any]=0.97 ,  lowerCAmelCase_			:		int=1.0 ,  lowerCAmelCase_			:		List[Any]=True ,  lowerCAmelCase_			:		Dict=True ,  lowerCAmelCase_			:		Optional[int]=False ,  **lowerCAmelCase_			:		List[str] ,  )							->						Dict:
     super().__init__(feature_size=lowerCAmelCase_ ,  sampling_rate=lowerCAmelCase_ ,  padding_value=lowerCAmelCase_ ,  **lowerCAmelCase_					)

     __lowerCAmelCase							     =     feature_size
     __lowerCAmelCase							     =     sampling_rate
     __lowerCAmelCase							     =     padding_value
     __lowerCAmelCase							     =     hop_length
     __lowerCAmelCase							     =     win_length
     __lowerCAmelCase							     =     frame_signal_scale
     __lowerCAmelCase							     =     preemphasis_coeff
     __lowerCAmelCase							     =     mel_floor
     __lowerCAmelCase							     =     normalize_means
     __lowerCAmelCase							     =     normalize_vars
     __lowerCAmelCase							     =     win_function
     __lowerCAmelCase							     =     return_attention_mask

     __lowerCAmelCase							     =     win_length * sampling_rate // 1_0_0_0
     __lowerCAmelCase							     =     hop_length * sampling_rate // 1_0_0_0

     __lowerCAmelCase							     =     optimal_fft_length(self.sample_size					)
     __lowerCAmelCase							     =     (self.n_fft // 2) + 1






  def     lowercase		(    self			:		Optional[int] ,  lowerCAmelCase_			:		np.array					)							->						np.ndarray:
     if self.win_function == "hamming_window":
        __lowerCAmelCase							     =     window_function(window_length=self.sample_size ,  name=self.win_function ,  periodic=lowerCAmelCase_					)
     else:
        __lowerCAmelCase							     =     window_function(window_length=self.sample_size ,  name=self.win_function					)

     __lowerCAmelCase							     =     mel_filter_bank(
         num_frequency_bins=self.n_freqs ,  num_mel_filters=self.feature_size ,  min_frequency=0.0 ,  max_frequency=self.sampling_rate / 2.0 ,  sampling_rate=self.sampling_rate ,  )

     __lowerCAmelCase							     =     spectrogram(
         one_waveform * self.frame_signal_scale ,  window=lowerCAmelCase_ ,  frame_length=self.sample_size ,  hop_length=self.sample_stride ,  fft_length=self.n_fft ,  center=lowerCAmelCase_ ,  preemphasis=self.preemphasis_coeff ,  mel_filters=lowerCAmelCase_ ,  mel_floor=self.mel_floor ,  log_mel='log' ,  )
     return msfc_features.T






  def     lowercase		(    self			:		Any ,  lowerCAmelCase_			:		str ,  lowerCAmelCase_			:		Dict ,  lowerCAmelCase_			:		Dict					)							->						List[str]:
     # make sure we normalize float32 arrays
     if self.normalize_means:
        __lowerCAmelCase							     =     x[:input_length].mean(axis=0					)
        __lowerCAmelCase							     =     np.subtract(lowerCAmelCase_ ,  lowerCAmelCase_					)
     if self.normalize_vars:
        __lowerCAmelCase							     =     x[:input_length].std(axis=0					)
        __lowerCAmelCase							     =     np.divide(lowerCAmelCase_ ,  lowerCAmelCase_					)

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

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

     return x






  def     lowercase		(    self			:		Union[str, Any] ,  lowerCAmelCase_			:		List[np.ndarray] ,  lowerCAmelCase_			:		Optional[np.ndarray] = None					)							->						List[np.ndarray]:
     __lowerCAmelCase							     =     attention_mask.sum(-1					) if attention_mask is not None else [x.shape[0] for x in input_features]
     return [self._normalize_one(lowerCAmelCase_ ,  lowerCAmelCase_ ,  self.padding_value					) for x, n in zip(lowerCAmelCase_ ,  lowerCAmelCase_					)]






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

     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.'					)

     __lowerCAmelCase							     =     isinstance(lowerCAmelCase_ ,  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}"""					)
     __lowerCAmelCase							     =     is_batched_numpy or (
         isinstance(lowerCAmelCase_ ,  (list, tuple)					) and (isinstance(raw_speech[0] ,  (np.ndarray, tuple, list)					))
     )

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

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

     # extract fbank features
     __lowerCAmelCase							     =     [self._extract_mfsc_features(lowerCAmelCase_					) for one_waveform in raw_speech]

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

     __lowerCAmelCase							     =     self.pad(
         lowerCAmelCase_ ,  padding=lowerCAmelCase_ ,  max_length=lowerCAmelCase_ ,  truncation=lowerCAmelCase_ ,  pad_to_multiple_of=lowerCAmelCase_ ,  return_attention_mask=lowerCAmelCase_ ,  **lowerCAmelCase_ ,  )
     # make sure list is in array format
     __lowerCAmelCase							     =     padded_inputs.get('input_features'					)
     if isinstance(input_features[0] ,  lowerCAmelCase_					):
        __lowerCAmelCase							     =     [np.asarray(lowerCAmelCase_ ,  dtype=np.floataa					) for feature in input_features]

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

     if self.normalize_means or self.normalize_vars:
        __lowerCAmelCase							     =     (
            np.array(lowerCAmelCase_ ,  dtype=np.intaa					)
            if self._get_padding_strategies(lowerCAmelCase_ ,  max_length=lowerCAmelCase_					) is not PaddingStrategy.DO_NOT_PAD
            and padding
            else None
        )
        __lowerCAmelCase							     =     self.normalize(
            padded_inputs['input_features'] ,  attention_mask=lowerCAmelCase_					)

     if return_tensors is not None:
        __lowerCAmelCase							     =     padded_inputs.convert_to_tensors(lowerCAmelCase_					)

     return padded_inputs








from argparse import ArgumentParser, Namespace
from typing import Any, List, Optional

from ..pipelines import Pipeline, get_supported_tasks, pipeline
from ..utils import logging
from . import BaseTransformersCLICommand


try:
     from fastapi import Body, FastAPI, HTTPException
     from fastapi.routing import APIRoute
     from pydantic import BaseModel
     from starlette.responses import JSONResponse
     from uvicorn import run

     _snake_case  :   int						   =  True
except (ImportError, AttributeError):
     _snake_case  :   int						   =  object

     def   a_   (			*lowerCAmelCase_				:      List[str],     **lowerCAmelCase_				:      Optional[Any]     ):
        pass




     _snake_case  :   Union[str, Any]						   =  False


_snake_case  :   int						   =  logging.get_logger('transformers-cli/serving')

def   a_   (			lowerCAmelCase_				:      Namespace     ):
   __lowerCAmelCase							     =     pipeline(
       task=args.task,     model=args.model if args.model else None,     config=args.config,     tokenizer=args.tokenizer,     device=args.device,     )
   return ServeCommand(lowerCAmelCase_,     args.host,     args.port,     args.workers     )



class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			42





class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			42
  a_      =			42





class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			42





class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			42





class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  @staticmethod
  def     lowercase		(    lowerCAmelCase_			:		ArgumentParser					)							->						Union[str, Any]:
     __lowerCAmelCase							     =     parser.add_parser(
         'serve' ,  help='CLI tool to run inference requests through REST and GraphQL endpoints.'					)
     serve_parser.add_argument(
         '--task' ,  type=lowerCAmelCase_ ,  choices=get_supported_tasks() ,  help='The task to run the pipeline on' ,  )
     serve_parser.add_argument('--host' ,  type=lowerCAmelCase_ ,  default='localhost' ,  help='Interface the server will listen on.'					)
     serve_parser.add_argument('--port' ,  type=lowerCAmelCase_ ,  default=8_8_8_8 ,  help='Port the serving will listen to.'					)
     serve_parser.add_argument('--workers' ,  type=lowerCAmelCase_ ,  default=1 ,  help='Number of http workers'					)
     serve_parser.add_argument('--model' ,  type=lowerCAmelCase_ ,  help='Model\'s name or path to stored model.'					)
     serve_parser.add_argument('--config' ,  type=lowerCAmelCase_ ,  help='Model\'s config name or path to stored model.'					)
     serve_parser.add_argument('--tokenizer' ,  type=lowerCAmelCase_ ,  help='Tokenizer name to use.'					)
     serve_parser.add_argument(
         '--device' ,  type=lowerCAmelCase_ ,  default=-1 ,  help='Indicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1)' ,  )
     serve_parser.set_defaults(func=lowerCAmelCase_					)






  def __init__(    self			:		List[str] ,  lowerCAmelCase_			:		Pipeline ,  lowerCAmelCase_			:		str ,  lowerCAmelCase_			:		int ,  lowerCAmelCase_			:		int					)							->						str:
     __lowerCAmelCase							     =     pipeline

     __lowerCAmelCase							     =     host
     __lowerCAmelCase							     =     port
     __lowerCAmelCase							     =     workers

     if not _serve_dependencies_installed:
        raise RuntimeError(
            'Using serve command requires FastAPI and uvicorn. '
            'Please install transformers with [serving]: pip install "transformers[serving]".'
            'Or install FastAPI and uvicorn separately.'					)
     else:
        logger.info(f"""Serving model over {host}:{port}"""					)
        __lowerCAmelCase							     =     FastAPI(
            routes=[
                APIRoute(
                    '/' ,  self.model_info ,  response_model=lowerCAmelCase_ ,  response_class=lowerCAmelCase_ ,  methods=['GET'] ,  ),
                APIRoute(
                    '/tokenize' ,  self.tokenize ,  response_model=lowerCAmelCase_ ,  response_class=lowerCAmelCase_ ,  methods=['POST'] ,  ),
                APIRoute(
                    '/detokenize' ,  self.detokenize ,  response_model=lowerCAmelCase_ ,  response_class=lowerCAmelCase_ ,  methods=['POST'] ,  ),
                APIRoute(
                    '/forward' ,  self.forward ,  response_model=lowerCAmelCase_ ,  response_class=lowerCAmelCase_ ,  methods=['POST'] ,  ),
            ] ,  timeout=6_0_0 ,  )






  def     lowercase		(    self			:		Tuple					)							->						str:
     run(self._app ,  host=self.host ,  port=self.port ,  workers=self.workers					)






  def     lowercase		(    self			:		Any					)							->						List[str]:
     return ServeModelInfoResult(infos=vars(self._pipeline.model.config					)					)






  def     lowercase		(    self			:		int ,  lowerCAmelCase_			:		str = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					) ,  lowerCAmelCase_			:		bool = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					)					)							->						Dict:
     try:
        __lowerCAmelCase							     =     self._pipeline.tokenizer.tokenize(lowerCAmelCase_					)

        if return_ids:
           __lowerCAmelCase							     =     self._pipeline.tokenizer.convert_tokens_to_ids(lowerCAmelCase_					)
           return ServeTokenizeResult(tokens=lowerCAmelCase_ ,  tokens_ids=lowerCAmelCase_					)
        else:
           return ServeTokenizeResult(tokens=lowerCAmelCase_					)

     except Exception as e:
        raise HTTPException(status_code=5_0_0 ,  detail={'model': '', 'error': str(lowerCAmelCase_					)}					)






  def     lowercase		(    self			:		int ,  lowerCAmelCase_			:		List[int] = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					) ,  lowerCAmelCase_			:		bool = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					) ,  lowerCAmelCase_			:		bool = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					) ,  )							->						Union[str, Any]:
     try:
        __lowerCAmelCase							     =     self._pipeline.tokenizer.decode(lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_					)
        return ServeDeTokenizeResult(model='' ,  text=lowerCAmelCase_					)
     except Exception as e:
        raise HTTPException(status_code=5_0_0 ,  detail={'model': '', 'error': str(lowerCAmelCase_					)}					)






  async def     lowercase		(    self			:		List[Any] ,  lowerCAmelCase_			:		Union[str, Any]=Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					)					)							->						int:

     # Check we don't have empty string
     if len(lowerCAmelCase_					) == 0:
        return ServeForwardResult(output=[] ,  attention=[]					)

     try:
        # Forward through the model
        __lowerCAmelCase							     =     self._pipeline(lowerCAmelCase_					)
        return ServeForwardResult(output=lowerCAmelCase_					)
     except Exception as e:
        raise HTTPException(5_0_0 ,  {'error': str(lowerCAmelCase_					)}					)

