



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

					snake_case_			:     Any  	=  True
except (ImportError, AttributeError):
					snake_case_			:     List[str]  	=  object






					def A__   (					*UpperCAmelCase_  ,  **UpperCAmelCase_							):
											pass

					snake_case_			:     int  	=  False


snake_case_			:     Any  	=  logging.get_logger('transformers-cli/serving')






def A__   (					UpperCAmelCase_							):
						_UpperCamelCase :		int			    =       pipeline(
						    task=args.task  ,  model=args.model if args.model else None  ,  config=args.config  ,  tokenizer=args.tokenizer  ,  device=args.device  ,  )
						return ServeCommand(UpperCAmelCase_  ,  args.host  ,  args.port  ,  args.workers							)






class 							lowercase__     (							lowercase       ):
	lowercase__      =      42






class 							lowercase__     (							lowercase       ):
	lowercase__      =      42
	lowercase__      =      42






class 							lowercase__     (							lowercase       ):
	lowercase__      =      42






class 							lowercase__     (							lowercase       ):
	lowercase__      =      42






class 							lowercase__     (							lowercase       ):





	@staticmethod
	def      UpperCamelCase_  (			lowerCamelCase__ :    ArgumentParser			):







							'''simple docstring'''
							_UpperCamelCase :		Optional[Any]			    =       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=8888			,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 :    Optional[Any]			,lowerCamelCase__ :    Pipeline			,lowerCamelCase__ :    str			,lowerCamelCase__ :    int			,lowerCamelCase__ :    int			):







							'''simple docstring'''
							_UpperCamelCase :		Dict			    =       pipeline

							_UpperCamelCase :		Tuple			    =       host
							_UpperCamelCase :		List[str]			    =       port
							_UpperCamelCase :		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}'			)
													_UpperCamelCase :		List[Any]			    =       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=600			,)





	def      UpperCamelCase_  (			self :    Any			):







							'''simple docstring'''
							run(self._app			,host=self.host			,port=self.port			,workers=self.workers			)





	def      UpperCamelCase_  (			self :    Dict			):







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





	def      UpperCamelCase_  (			self :    Any			,lowerCamelCase__ :    str = Body(lowerCamelCase__			,embed=lowerCamelCase__			)			,lowerCamelCase__ :    bool = Body(lowerCamelCase__			,embed=lowerCamelCase__			)			):







							'''simple docstring'''
							try:
													_UpperCamelCase :		Any			    =       self._pipeline.tokenizer.tokenize(lowerCamelCase__			)

													if return_ids:
																			_UpperCamelCase :		List[Any]			    =       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=500			,detail={'model': '', 'error': str(lowerCamelCase__			)}			)





	def      UpperCamelCase_  (			self :    Optional[int]			,lowerCamelCase__ :    List[int] = Body(lowerCamelCase__			,embed=lowerCamelCase__			)			,lowerCamelCase__ :    bool = Body(lowerCamelCase__			,embed=lowerCamelCase__			)			,lowerCamelCase__ :    bool = Body(lowerCamelCase__			,embed=lowerCamelCase__			)			,):







							'''simple docstring'''
							try:
													_UpperCamelCase :		Union[str, Any]			    =       self._pipeline.tokenizer.decode(lowerCamelCase__			,lowerCamelCase__			,lowerCamelCase__			)
													return ServeDeTokenizeResult(model=''			,text=lowerCamelCase__			)
							except Exception as e:
													raise HTTPException(status_code=500			,detail={'model': '', 'error': str(lowerCamelCase__			)}			)





	async def      UpperCamelCase_  (			self :    List[str]			,lowerCamelCase__ :    Any=Body(lowerCamelCase__			,embed=lowerCamelCase__			)			):







							'''simple docstring'''

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

							try:
													# Forward through the model
													_UpperCamelCase :		Union[str, Any]			    =       self._pipeline(lowerCamelCase__			)
													return ServeForwardResult(output=lowerCamelCase__			)
							except Exception as e:
													raise HTTPException(500			,{'error': str(lowerCamelCase__			)}			)







'''simple docstring'''
from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


snake_case_			:     Optional[int]  	=  logging.get_logger(__name__)

snake_case_			:     List[Any]  	=  {
    'facebook/xlm-roberta-xl': 'https://huggingface.co/facebook/xlm-roberta-xl/resolve/main/config.json',
    'facebook/xlm-roberta-xxl': 'https://huggingface.co/facebook/xlm-roberta-xxl/resolve/main/config.json',
    # See all XLM-RoBERTa-XL models at https://huggingface.co/models?filter=xlm-roberta-xl
}






class 							lowercase__     (							lowercase       ):
	lowercase__      =      """xlm-roberta-xl"""





	def __init__(			self :    Optional[int]			,lowerCamelCase__ :    Optional[Any]=250880			,lowerCamelCase__ :    Tuple=2560			,lowerCamelCase__ :    Union[str, Any]=36			,lowerCamelCase__ :    List[str]=32			,lowerCamelCase__ :    Optional[Any]=10240			,lowerCamelCase__ :    Tuple="gelu"			,lowerCamelCase__ :    int=0.1			,lowerCamelCase__ :    int=0.1			,lowerCamelCase__ :    Optional[int]=514			,lowerCamelCase__ :    List[str]=1			,lowerCamelCase__ :    Dict=0.0_2			,lowerCamelCase__ :    Any=1E-05			,lowerCamelCase__ :    Union[str, Any]=1			,lowerCamelCase__ :    str=0			,lowerCamelCase__ :    Tuple=2			,lowerCamelCase__ :    Union[str, Any]="absolute"			,lowerCamelCase__ :    Optional[Any]=True			,lowerCamelCase__ :    List[str]=None			,**lowerCamelCase__ :    Dict			,):







							'''simple docstring'''
							super().__init__(pad_token_id=lowerCamelCase__			,bos_token_id=lowerCamelCase__			,eos_token_id=lowerCamelCase__			,**lowerCamelCase__			)
							_UpperCamelCase :		Optional[int]			    =       vocab_size
							_UpperCamelCase :		Optional[Any]			    =       hidden_size
							_UpperCamelCase :		str			    =       num_hidden_layers
							_UpperCamelCase :		str			    =       num_attention_heads
							_UpperCamelCase :		Any			    =       hidden_act
							_UpperCamelCase :		Dict			    =       intermediate_size
							_UpperCamelCase :		Optional[int]			    =       hidden_dropout_prob
							_UpperCamelCase :		Any			    =       attention_probs_dropout_prob
							_UpperCamelCase :		List[Any]			    =       max_position_embeddings
							_UpperCamelCase :		str			    =       type_vocab_size
							_UpperCamelCase :		Optional[Any]			    =       initializer_range
							_UpperCamelCase :		Optional[int]			    =       layer_norm_eps
							_UpperCamelCase :		Optional[int]			    =       position_embedding_type
							_UpperCamelCase :		Optional[Any]			    =       use_cache
							_UpperCamelCase :		Optional[Any]			    =       classifier_dropout






class 							lowercase__     (							lowercase       ):





	@property
	def      UpperCamelCase_  (			self :    List[Any]			):







							'''simple docstring'''
							if self.task == "multiple-choice":
													_UpperCamelCase :		Union[str, Any]			    =       {0: 'batch', 1: 'choice', 2: 'sequence'}
							else:
													_UpperCamelCase :		Optional[Any]			    =       {0: 'batch', 1: 'sequence'}
							return OrderedDict(
							    [
							        ('input_ids', dynamic_axis),
							        ('attention_mask', dynamic_axis),
							    ]			)


