

'''simple docstring'''


from typing import List, Optional, Union

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


lowerCAmelCase__						:       int										=						logging.get_logger(__name__)

lowerCAmelCase__						:       Union[str, Any]										=						{
    """huggingface/time-series-transformer-tourism-monthly""": (
        """https://huggingface.co/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json"""
    ),
    # See all TimeSeriesTransformer models at https://huggingface.co/models?filter=time_series_transformer
}




class     a			(				__a       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    "time_series_transformer"
						__UpperCAmelCase			     =    {
						    "hidden_size": "d_model",
						    "num_attention_heads": "encoder_attention_heads",
						    "num_hidden_layers": "encoder_layers",
						}







						def __init__(							self					:      str			,			snake_case_					:      Union[str, Any] = None			,			snake_case_					:      Any = None			,			snake_case_					:      Optional[int] = "student_t"			,			snake_case_					:      str = "nll"			,			snake_case_					:      Union[str, Any] = 1			,			snake_case_					:      Dict = [1, 2, 3, 4, 5, 6, 7]			,			snake_case_					:      Union[str, Any] = "mean"			,			snake_case_					:      List[str] = 0			,			snake_case_					:      List[str] = 0			,			snake_case_					:      Optional[int] = 0			,			snake_case_					:      Any = 0			,			snake_case_					:      int = None			,			snake_case_					:      List[str] = None			,			snake_case_					:      List[str] = 3_2			,			snake_case_					:      List[Any] = 3_2			,			snake_case_					:      Tuple = 2			,			snake_case_					:      Dict = 2			,			snake_case_					:      List[Any] = 2			,			snake_case_					:      List[Any] = 2			,			snake_case_					:      List[Any] = True			,			snake_case_					:      Any = "gelu"			,			snake_case_					:      Optional[int] = 6_4			,			snake_case_					:      Tuple = 0.1			,			snake_case_					:      int = 0.1			,			snake_case_					:      Tuple = 0.1			,			snake_case_					:      Optional[int] = 0.1			,			snake_case_					:      Optional[Any] = 0.1			,			snake_case_					:      Union[str, Any] = 1_0_0			,			snake_case_					:      List[str] = 0.0_2			,			snake_case_					:      Union[str, Any]=True			,			**snake_case_					:      str			,			):



								'''simple docstring'''



								snake_case__						:       Tuple       						=  prediction_length
								snake_case__						:       Optional[Any]       						=  context_length or prediction_length
								snake_case__						:       str       						=  distribution_output
								snake_case__						:       int       						=  loss
								snake_case__						:       Optional[Any]       						=  input_size
								snake_case__						:       Any       						=  num_time_features
								snake_case__						:       Optional[Any]       						=  lags_sequence
								snake_case__						:       List[str]       						=  scaling
								snake_case__						:       Union[str, Any]       						=  num_dynamic_real_features
								snake_case__						:       Any       						=  num_static_real_features
								snake_case__						:       Tuple       						=  num_static_categorical_features
								if cardinality and num_static_categorical_features > 0:
										if len(lowerCAmelCase_  ) != num_static_categorical_features:
												raise ValueError(
												    '''The cardinality should be a list of the same length as `num_static_categorical_features`'''  )
										snake_case__						:       Dict       						=  cardinality
								else:
										snake_case__						:       Optional[Any]       						=  [0]
								if embedding_dimension and num_static_categorical_features > 0:
										if len(lowerCAmelCase_  ) != num_static_categorical_features:
												raise ValueError(
												    '''The embedding dimension should be a list of the same length as `num_static_categorical_features`'''  )
										snake_case__						:       Tuple       						=  embedding_dimension
								else:
										snake_case__						:       str       						=  [min(5_0			,			(cat + 1) // 2  ) for cat in self.cardinality]
								snake_case__						:       Optional[int]       						=  num_parallel_samples

								# Transformer architecture configuration
								snake_case__						:       str       						=  input_size * len(lowerCAmelCase_  ) + self._number_of_features
								snake_case__						:       Tuple       						=  d_model
								snake_case__						:       Optional[Any]       						=  encoder_attention_heads
								snake_case__						:       List[str]       						=  decoder_attention_heads
								snake_case__						:       int       						=  encoder_ffn_dim
								snake_case__						:       int       						=  decoder_ffn_dim
								snake_case__						:       Dict       						=  encoder_layers
								snake_case__						:       Dict       						=  decoder_layers

								snake_case__						:       Tuple       						=  dropout
								snake_case__						:       str       						=  attention_dropout
								snake_case__						:       List[str]       						=  activation_dropout
								snake_case__						:       Tuple       						=  encoder_layerdrop
								snake_case__						:       Union[str, Any]       						=  decoder_layerdrop

								snake_case__						:       Optional[Any]       						=  activation_function
								snake_case__						:       Dict       						=  init_std

								snake_case__						:       Tuple       						=  use_cache

								super().__init__(is_encoder_decoder=lowerCAmelCase_			,			**lowerCAmelCase_  )







						@property
						def       __magic_name__							(							self					:      Any  ):



								'''simple docstring'''



								return (
								    sum(self.embedding_dimension  )
								    + self.num_dynamic_real_features
								    + self.num_time_features
								    + self.num_static_real_features
								    + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
								)






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

					lowerCAmelCase__						:       Any										=						True
except (ImportError, AttributeError):
					lowerCAmelCase__						:       Dict										=						object




					def 	_a       (				*__lowerCAmelCase		:      List[str]   ,							**__lowerCAmelCase		:      Tuple     ):

							"""simple docstring"""

							pass





					lowerCAmelCase__						:       str										=						False


lowerCAmelCase__						:       List[str]										=						logging.get_logger("""transformers-cli/serving""")




def 	_a       (				__lowerCAmelCase		:      Namespace     ):

		"""simple docstring"""

		snake_case__						:       Union[str, Any]       						=  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     a			(				SCREAMING_SNAKE_CASE       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    42





class     a			(				SCREAMING_SNAKE_CASE       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    42
						__UpperCAmelCase			     =    42





class     a			(				SCREAMING_SNAKE_CASE       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    42





class     a			(				SCREAMING_SNAKE_CASE       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    42





class     a			(				SCREAMING_SNAKE_CASE       ):






						"""simple docstring"""




						@staticmethod
						def       __magic_name__							(							snake_case_					:      ArgumentParser  ):



								'''simple docstring'''



								snake_case__						:       Optional[Any]       						=  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=8_8_8_8			,			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					:      Union[str, Any]			,			snake_case_					:      Pipeline			,			snake_case_					:      str			,			snake_case_					:      int			,			snake_case_					:      int  ):



								'''simple docstring'''



								snake_case__						:       Any       						=  pipeline

								snake_case__						:       Tuple       						=  host
								snake_case__						:       Optional[Any]       						=  port
								snake_case__						:       Tuple       						=  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__						:       str       						=  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=6_0_0			,			)







						def       __magic_name__							(							self					:      str  ):



								'''simple docstring'''



								run(self._app			,			host=self.host			,			port=self.port			,			workers=self.workers  )







						def       __magic_name__							(							self					:      Optional[Any]  ):



								'''simple docstring'''



								return ServeModelInfoResult(infos=vars(self._pipeline.model.config  )  )







						def       __magic_name__							(							self					:      List[str]			,			snake_case_					:      str = Body(snake_case_			,			embed=snake_case_  )			,			snake_case_					:      bool = Body(snake_case_			,			embed=snake_case_  )  ):



								'''simple docstring'''



								try:
										snake_case__						:       Optional[Any]       						=  self._pipeline.tokenizer.tokenize(snake_case_  )

										if return_ids:
												snake_case__						:       Optional[int]       						=  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=5_0_0			,			detail={'''model''': '''''', '''error''': str(snake_case_  )}  )







						def       __magic_name__							(							self					:      List[Any]			,			snake_case_					:      List[int] = Body(snake_case_			,			embed=snake_case_  )			,			snake_case_					:      bool = Body(snake_case_			,			embed=snake_case_  )			,			snake_case_					:      bool = Body(snake_case_			,			embed=snake_case_  )			,			):



								'''simple docstring'''



								try:
										snake_case__						:       Optional[int]       						=  self._pipeline.tokenizer.decode(snake_case_			,			snake_case_			,			snake_case_  )
										return ServeDeTokenizeResult(model=''''''			,			text=snake_case_  )
								except Exception as e:
										raise HTTPException(status_code=5_0_0			,			detail={'''model''': '''''', '''error''': str(snake_case_  )}  )







						async def       __magic_name__							(							self					:      Tuple			,			snake_case_					:      List[str]=Body(snake_case_			,			embed=snake_case_  )  ):



								'''simple docstring'''



								if len(snake_case_  ) == 0:
										return ServeForwardResult(output=[]			,			attention=[]  )

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



