
"""simple docstring"""







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

		_SCREAMING_SNAKE_CASE	       =      True
except (ImportError, AttributeError):
		_SCREAMING_SNAKE_CASE	       =      object
		def   __UpperCamelCase   (					*SCREAMING_SNAKE_CASE							,    **SCREAMING_SNAKE_CASE )  ->     Dict:







									"""simple docstring"""


									pass



		_SCREAMING_SNAKE_CASE	       =      False


_SCREAMING_SNAKE_CASE	       =      logging.get_logger("""transformers-cli/serving""")
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE )  ->     Tuple:







							"""simple docstring"""


							__snake_case     			=       pipeline(
							    task=args.task							,    model=args.model if args.model else None							,    config=args.config							,    tokenizer=args.tokenizer							,    device=args.device							,    )
							return ServeCommand(__SCREAMING_SNAKE_CASE							,    args.host							,    args.port							,    args.workers )







class 							__magic_name__     (		lowercase__   ):
					_SCREAMING_SNAKE_CASE       :					dict







class 							__magic_name__     (		lowercase__   ):
					_SCREAMING_SNAKE_CASE       :					List[str]
					_SCREAMING_SNAKE_CASE       :					Optional[List[int]]







class 							__magic_name__     (		lowercase__   ):
					_SCREAMING_SNAKE_CASE       :					str







class 							__magic_name__     (		lowercase__   ):
					_SCREAMING_SNAKE_CASE       :					Any







class 							__magic_name__     (		lowercase__   ):





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





					def __init__(				self   :							Optional[int]							,						snake_case_   :							int							,						snake_case_   :							Tuple							,						snake_case_   :							Any							,						snake_case_   :							List[Any]    ):
												__snake_case     			=       pipeline

												__snake_case     			=       host
												__snake_case     			=       port
												__snake_case     			=       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}'''    )
																			__snake_case     			=       FastAPI(
																			    routes=[
																			        APIRoute(
																			            "/"							,						self.model_info							,						response_model=snake_case_							,						response_class=snake_case_							,						methods=["GET"]							,						),
																			        APIRoute(
																			            "/tokenize"							,						self.tokenize							,						response_model=snake_case_							,						response_class=snake_case_							,						methods=["POST"]							,						),
																			        APIRoute(
																			            "/detokenize"							,						self.detokenize							,						response_model=snake_case_							,						response_class=snake_case_							,						methods=["POST"]							,						),
																			        APIRoute(
																			            "/forward"							,						self.forward							,						response_model=snake_case_							,						response_class=snake_case_							,						methods=["POST"]							,						),
																			    ]							,						timeout=600							,						)





					def 							lowerCAmelCase      (				self   :							str    ):
												run(self._app							,						host=self.host							,						port=self.port							,						workers=self.workers    )





					def 							lowerCAmelCase      (				self   :							List[Any]    ):
												return ServeModelInfoResult(infos=vars(self._pipeline.model.config    )    )





					def 							lowerCAmelCase      (				self   :							Any							,						snake_case_   :							str = Body(snake_case_							,						embed=snake_case_    )							,						snake_case_   :							Any = Body(snake_case_							,						embed=snake_case_    )    ):
												try:
																			__snake_case     			=       self._pipeline.tokenizer.tokenize(snake_case_    )

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

												except Exception as e:
																			raise HTTPException(status_code=500							,						detail={"model": "", "error": str(snake_case_    )}    )





					def 							lowerCAmelCase      (				self   :							Tuple							,						snake_case_   :							Dict = Body(snake_case_							,						embed=snake_case_    )							,						snake_case_   :							Any = Body(snake_case_							,						embed=snake_case_    )							,						snake_case_   :							Tuple = Body(snake_case_							,						embed=snake_case_    )							,						):
												try:
																			__snake_case     			=       self._pipeline.tokenizer.decode(snake_case_							,						snake_case_							,						snake_case_    )
																			return ServeDeTokenizeResult(model=""							,						text=snake_case_    )
												except Exception as e:
																			raise HTTPException(status_code=500							,						detail={"model": "", "error": str(snake_case_    )}    )





					async def 							lowerCAmelCase      (				self   :							int							,						snake_case_   :							Any=Body(snake_case_							,						embed=snake_case_    )    ):

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

												try:
																			# Forward through the model
																			__snake_case     			=       self._pipeline(snake_case_    )
																			return ServeForwardResult(output=snake_case_    )
												except Exception as e:
																			raise HTTPException(500							,						{"error": str(snake_case_    )}    )


"""simple docstring"""







import math

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE	       =      logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE	       =      {
    """facebook/data2vec-base-960h""": """https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json""",
    # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio
}







class 							__magic_name__     (		lowercase__   ):
					_SCREAMING_SNAKE_CASE       :					Any     							=   'data2vec-audio'





					def __init__(				self   :							Any							,						snake_case_   :							Optional[Any]=32							,						snake_case_   :							Tuple=768							,						snake_case_   :							Optional[int]=12							,						snake_case_   :							str=12							,						snake_case_   :							int=3072							,						snake_case_   :							Tuple="gelu"							,						snake_case_   :							Optional[Any]=0.1							,						snake_case_   :							str=0.1							,						snake_case_   :							int=0.1							,						snake_case_   :							int=0.0							,						snake_case_   :							Optional[int]=0.1							,						snake_case_   :							Dict=0.1							,						snake_case_   :							Dict=0.02							,						snake_case_   :							Optional[int]=1e-5							,						snake_case_   :							Optional[Any]="gelu"							,						snake_case_   :							Tuple=(512, 512, 512, 512, 512, 512, 512)							,						snake_case_   :							int=(5, 2, 2, 2, 2, 2, 2)							,						snake_case_   :							Union[str, Any]=(10, 3, 3, 3, 3, 2, 2)							,						snake_case_   :							Optional[int]=False							,						snake_case_   :							int=16							,						snake_case_   :							List[Any]=19							,						snake_case_   :							str=5							,						snake_case_   :							List[Any]=0.05							,						snake_case_   :							Dict=10							,						snake_case_   :							Dict=2							,						snake_case_   :							Dict=0.0							,						snake_case_   :							Union[str, Any]=10							,						snake_case_   :							Optional[Any]=0							,						snake_case_   :							Optional[Any]="sum"							,						snake_case_   :							List[str]=False							,						snake_case_   :							List[Any]=False							,						snake_case_   :							Optional[int]=256							,						snake_case_   :							int=(512, 512, 512, 512, 1500)							,						snake_case_   :							List[Any]=(5, 3, 3, 1, 1)							,						snake_case_   :							Any=(1, 2, 3, 1, 1)							,						snake_case_   :							List[Any]=512							,						snake_case_   :							Optional[int]=0							,						snake_case_   :							Dict=1							,						snake_case_   :							List[str]=2							,						snake_case_   :							Optional[Any]=False							,						snake_case_   :							Optional[Any]=3							,						snake_case_   :							Tuple=2							,						snake_case_   :							Any=3							,						snake_case_   :							Union[str, Any]=None							,						**snake_case_   :							int							,						):
												super().__init__(**snake_case_							,						pad_token_id=snake_case_							,						bos_token_id=snake_case_							,						eos_token_id=snake_case_    )
												__snake_case     			=       hidden_size
												__snake_case     			=       feat_extract_activation
												__snake_case     			=       list(snake_case_    )
												__snake_case     			=       list(snake_case_    )
												__snake_case     			=       list(snake_case_    )
												__snake_case     			=       conv_bias
												__snake_case     			=       num_conv_pos_embeddings
												__snake_case     			=       num_conv_pos_embedding_groups
												__snake_case     			=       conv_pos_kernel_size
												__snake_case     			=       len(self.conv_dim    )
												__snake_case     			=       num_hidden_layers
												__snake_case     			=       intermediate_size
												__snake_case     			=       hidden_act
												__snake_case     			=       num_attention_heads
												__snake_case     			=       hidden_dropout
												__snake_case     			=       attention_dropout
												__snake_case     			=       activation_dropout
												__snake_case     			=       feat_proj_dropout
												__snake_case     			=       final_dropout
												__snake_case     			=       layerdrop
												__snake_case     			=       layer_norm_eps
												__snake_case     			=       initializer_range
												__snake_case     			=       vocab_size
												__snake_case     			=       use_weighted_layer_sum

												if (
												    (len(self.conv_stride    ) != self.num_feat_extract_layers)
												    or (len(self.conv_kernel    ) != self.num_feat_extract_layers)
												    or (len(self.conv_dim    ) != self.num_feat_extract_layers)
												):
																			raise ValueError(
																			    "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
																			    " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
																			    F''' {len(self.conv_dim    )}`, `len(config.conv_stride) = {len(self.conv_stride    )}`,'''
																			    F''' `len(config.conv_kernel) = {len(self.conv_kernel    )}`.'''    )

												# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
												__snake_case     			=       mask_time_prob
												__snake_case     			=       mask_time_length
												__snake_case     			=       mask_time_min_masks
												__snake_case     			=       mask_feature_prob
												__snake_case     			=       mask_feature_length
												__snake_case     			=       mask_feature_min_masks

												# ctc loss
												__snake_case     			=       ctc_loss_reduction
												__snake_case     			=       ctc_zero_infinity

												# adapter
												__snake_case     			=       add_adapter
												__snake_case     			=       adapter_kernel_size
												__snake_case     			=       adapter_stride
												__snake_case     			=       num_adapter_layers
												__snake_case     			=       output_hidden_size or hidden_size

												# SequenceClassification-specific parameter. Feel free to ignore for other classes.
												__snake_case     			=       classifier_proj_size

												# XVector-specific parameters. Feel free to ignore for other classes.
												__snake_case     			=       list(snake_case_    )
												__snake_case     			=       list(snake_case_    )
												__snake_case     			=       list(snake_case_    )
												__snake_case     			=       xvector_output_dim





					@property
					def 							lowerCAmelCase      (				self   :							List[str]    ):
												return math.prod(self.conv_stride    )
