


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	: Optional[int]  				=True
except (ImportError, AttributeError):
						__lowerCAmelCase	: Optional[Any]  				=object


						def        _UpperCamelCase							(      *lowercase__    ,       **lowercase__      ):
												pass
						__lowerCAmelCase	: Union[str, Any]  				=False


__lowerCAmelCase	: Union[str, Any]  				=logging.get_logger('transformers-cli/serving')


def        _UpperCamelCase							(      lowercase__      ):
						__SCREAMING_SNAKE_CASE						:     List[str]					=		pipeline(
						    task=args.task    ,       model=args.model if args.model else None    ,       config=args.config    ,       tokenizer=args.tokenizer    ,       device=args.device    ,       )
						return ServeCommand(lowercase__    ,       args.host    ,       args.port    ,       args.workers      )
class        _lowercase      (			A__       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			dict




class        _lowercase      (			A__       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			List[str]
				SCREAMING_SNAKE_CASE__				:			Optional[List[int]]




class        _lowercase      (			A__       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			str




class        _lowercase      (			A__       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			Any




class        _lowercase      (			A__       ):






				'''simple docstring'''



				@staticmethod
				def 		__magic_name__(       lowerCAmelCase__			:ArgumentParser	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		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_888			,			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			:int			,			lowerCAmelCase__			:Pipeline			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:int	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     str					=		pipeline

										__SCREAMING_SNAKE_CASE						:     Dict					=		host
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		port
										__SCREAMING_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}'''	)
																__SCREAMING_SNAKE_CASE						:     Optional[int]					=		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 		__magic_name__(       self			:Union[str, Any]	)		->			Optional[int]:
										run(self._app			,			host=self.host			,			port=self.port			,			workers=self.workers	)


				def 		__magic_name__(       self			:Dict	)		->			Any:
										return ServeModelInfoResult(infos=vars(self._pipeline.model.config	)	)


				def 		__magic_name__(       self			:str			,			lowerCAmelCase__			:str = Body(lowerCAmelCase__			,			embed=lowerCAmelCase__	)			,			lowerCAmelCase__			:bool = Body(lowerCAmelCase__			,			embed=lowerCAmelCase__	)	)		->			Any:
										try:
																__SCREAMING_SNAKE_CASE						:     int					=		self._pipeline.tokenizer.tokenize(lowerCAmelCase__	)

																if return_ids:
																						__SCREAMING_SNAKE_CASE						:     List[str]					=		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 		__magic_name__(       self			:List[str]			,			lowerCAmelCase__			:List[int] = Body(lowerCAmelCase__			,			embed=lowerCAmelCase__	)			,			lowerCAmelCase__			:bool = Body(lowerCAmelCase__			,			embed=lowerCAmelCase__	)			,			lowerCAmelCase__			:bool = Body(lowerCAmelCase__			,			embed=lowerCAmelCase__	)			,			)		->			List[Any]:
										try:
																__SCREAMING_SNAKE_CASE						:     int					=		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 		__magic_name__(       self			:Dict			,			lowerCAmelCase__			:str=Body(lowerCAmelCase__			,			embed=lowerCAmelCase__	)	)		->			Optional[int]:

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

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




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__lowerCAmelCase	: Any  				={'configuration_vit_msn': ['VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViTMSNConfig']}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__lowerCAmelCase	: int  				=[
						    'VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'ViTMSNModel',
						    'ViTMSNForImageClassification',
						    'ViTMSNPreTrainedModel',
						]

if TYPE_CHECKING:
						from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_vit_msn import (
												    VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST,
												    ViTMSNForImageClassification,
												    ViTMSNModel,
												    ViTMSNPreTrainedModel,
												)

else:
						import sys

						__lowerCAmelCase	: Union[str, Any]  				=_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
