




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

			a_        =       True
except (ImportError, AttributeError):
			a_        =       object







			def      __UpperCAmelCase     ( *__UpperCamelCase	,				**__UpperCamelCase	):
						pass


			a_        =       False


a_        =       logging.get_logger('transformers-cli/serving')







def      __UpperCAmelCase     ( __UpperCamelCase	):
			__lowercase			:  Optional[Any]								=       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   UpperCAmelCase_    (			snake_case		):
		UpperCamelCase							=42


class   UpperCAmelCase_    (			snake_case		):
		UpperCamelCase							=42
		UpperCamelCase							=42


class   UpperCAmelCase_    (			snake_case		):
		UpperCamelCase							=42


class   UpperCAmelCase_    (			snake_case		):
		UpperCamelCase							=42


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

					__lowercase			:  str								=       host
					__lowercase			:  Union[str, Any]								=       port
					__lowercase			:  Union[str, Any]								=       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}"""       )
								__lowercase			:  Tuple								=       FastAPI(
								    routes=[
								        APIRoute(
								            '''/'''				,						self.model_info				,						response_model=UpperCamelCase_				,						response_class=UpperCamelCase_				,						methods=['''GET''']				,						),
								        APIRoute(
								            '''/tokenize'''				,						self.tokenize				,						response_model=UpperCamelCase_				,						response_class=UpperCamelCase_				,						methods=['''POST''']				,						),
								        APIRoute(
								            '''/detokenize'''				,						self.detokenize				,						response_model=UpperCamelCase_				,						response_class=UpperCamelCase_				,						methods=['''POST''']				,						),
								        APIRoute(
								            '''/forward'''				,						self.forward				,						response_model=UpperCamelCase_				,						response_class=UpperCamelCase_				,						methods=['''POST''']				,						),
								    ]				,						timeout=6_00				,						)
		def 	_lowerCamelCase							(						self       )						->       Tuple:
					run(self._app				,						host=self.host				,						port=self.port				,						workers=self.workers       )
		def 	_lowerCamelCase							(						self       )						->       Any:
					return ServeModelInfoResult(infos=vars(self._pipeline.model.config       )       )
		def 	_lowerCamelCase							(						self				,						UpperCamelCase_ = Body(UpperCamelCase_				,						embed=UpperCamelCase_       )				,						UpperCamelCase_ = Body(UpperCamelCase_				,						embed=UpperCamelCase_       )       )						->       str:
					try:
								__lowercase			:  Optional[int]								=       self._pipeline.tokenizer.tokenize(UpperCamelCase_       )

								if return_ids:
											__lowercase			:  Optional[int]								=       self._pipeline.tokenizer.convert_tokens_to_ids(UpperCamelCase_       )
											return ServeTokenizeResult(tokens=UpperCamelCase_				,						tokens_ids=UpperCamelCase_       )
								else:
											return ServeTokenizeResult(tokens=UpperCamelCase_       )

					except Exception as e:
								raise HTTPException(status_code=5_00				,						detail={'''model''': '''''', '''error''': str(UpperCamelCase_       )}       )
		def 	_lowerCamelCase							(						self				,						UpperCamelCase_ = Body(UpperCamelCase_				,						embed=UpperCamelCase_       )				,						UpperCamelCase_ = Body(UpperCamelCase_				,						embed=UpperCamelCase_       )				,						UpperCamelCase_ = Body(UpperCamelCase_				,						embed=UpperCamelCase_       )				,						)						->       List[Any]:
					try:
								__lowercase			:  str								=       self._pipeline.tokenizer.decode(UpperCamelCase_				,						UpperCamelCase_				,						UpperCamelCase_       )
								return ServeDeTokenizeResult(model=''''''				,						text=UpperCamelCase_       )
					except Exception as e:
								raise HTTPException(status_code=5_00				,						detail={'''model''': '''''', '''error''': str(UpperCamelCase_       )}       )
		async def 	_lowerCamelCase							(						self				,						UpperCamelCase_=Body(UpperCamelCase_				,						embed=UpperCamelCase_       )       )						->       Dict:

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

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




from abc import ABC, abstractmethod
from argparse import ArgumentParser





class     lowerCAmelCase__     (				SCREAMING_SNAKE_CASE_			):


		'''simple docstring'''






		@staticmethod
		@abstractmethod
		def   _snake_case       (       snake_case__     :     ArgumentParser		)      ->	Tuple:
				raise NotImplementedError()



		@abstractmethod
		def   _snake_case       (       self     :     Optional[Any]		)      ->	Optional[Any]:
				raise NotImplementedError()