
"""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 				lowerCamelCase__		(      *__snake_case,					**__snake_case     )							->		Any:


									"""simple docstring"""





									pass
				_a														=		False


_a														=		logging.get_logger("""transformers-cli/serving""")



def 				lowerCamelCase__		(      __snake_case     )							->		List[Any]:


					"""simple docstring"""





					_UpperCamelCase										=  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    _UpperCAmelCase(   __lowercase     ):
		lowercase__      =				42






class    _UpperCAmelCase(   __lowercase     ):
		lowercase__      =				42
		lowercase__      =				42






class    _UpperCAmelCase(   __lowercase     ):
		lowercase__      =				42






class    _UpperCAmelCase(   __lowercase     ):
		lowercase__      =				42






class    _UpperCAmelCase(   __lowercase     ):



		@staticmethod
		def 						UpperCAmelCase							( __a)	->	Dict:






							'''simple docstring'''
							_UpperCamelCase										=  parser.add_parser(
							    '''serve'''    ,		help='''CLI tool to run inference requests through REST and GraphQL endpoints.''')
							serve_parser.add_argument(
							    '''--task'''    ,		type=__a    ,		choices=get_supported_tasks()    ,		help='''The task to run the pipeline on'''    ,		)
							serve_parser.add_argument('''--host'''    ,		type=__a    ,		default='''localhost'''    ,		help='''Interface the server will listen on.''')
							serve_parser.add_argument('''--port'''    ,		type=__a    ,		default=88_88    ,		help='''Port the serving will listen to.''')
							serve_parser.add_argument('''--workers'''    ,		type=__a    ,		default=1    ,		help='''Number of http workers''')
							serve_parser.add_argument('''--model'''    ,		type=__a    ,		help='''Model\'s name or path to stored model.''')
							serve_parser.add_argument('''--config'''    ,		type=__a    ,		help='''Model\'s config name or path to stored model.''')
							serve_parser.add_argument('''--tokenizer'''    ,		type=__a    ,		help='''Tokenizer name to use.''')
							serve_parser.add_argument(
							    '''--device'''    ,		type=__a    ,		default=-1    ,		help='''Indicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1)'''    ,		)
							serve_parser.set_defaults(func=__a)



		def __init__( self    ,		__a    ,		__a    ,		__a    ,		__a)	->	Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase										=  pipeline

							_UpperCamelCase										=  host
							_UpperCamelCase										=  port
							_UpperCamelCase										=  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										=  FastAPI(
												    routes=[
												        APIRoute(
												            '''/'''    ,		self.model_info    ,		response_model=__a    ,		response_class=__a    ,		methods=['''GET''']    ,		),
												        APIRoute(
												            '''/tokenize'''    ,		self.tokenize    ,		response_model=__a    ,		response_class=__a    ,		methods=['''POST''']    ,		),
												        APIRoute(
												            '''/detokenize'''    ,		self.detokenize    ,		response_model=__a    ,		response_class=__a    ,		methods=['''POST''']    ,		),
												        APIRoute(
												            '''/forward'''    ,		self.forward    ,		response_model=__a    ,		response_class=__a    ,		methods=['''POST''']    ,		),
												    ]    ,		timeout=6_00    ,		)



		def 						UpperCAmelCase							( self)	->	Dict:






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



		def 						UpperCAmelCase							( self)	->	Tuple:






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



		def 						UpperCAmelCase							( self    ,		__a = Body(__a    ,		embed=__a)    ,		__a = Body(__a    ,		embed=__a))	->	Any:






							'''simple docstring'''

							try:
												_UpperCamelCase										=  self._pipeline.tokenizer.tokenize(__a)

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

							except Exception as e:
												raise HTTPException(status_code=5_00    ,		detail={'''model''': '''''', '''error''': str(__a)})



		def 						UpperCAmelCase							( self    ,		__a = Body(__a    ,		embed=__a)    ,		__a = Body(__a    ,		embed=__a)    ,		__a = Body(__a    ,		embed=__a)    ,		)	->	List[Any]:






							'''simple docstring'''

							try:
												_UpperCamelCase										=  self._pipeline.tokenizer.decode(__a    ,		__a    ,		__a)
												return ServeDeTokenizeResult(model=''''''    ,		text=__a)
							except Exception as e:
												raise HTTPException(status_code=5_00    ,		detail={'''model''': '''''', '''error''': str(__a)})




		async def 						UpperCAmelCase							( self    ,		__a=Body(__a    ,		embed=__a))	->	Optional[int]:






							'''simple docstring'''

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

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








"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_torch_available,
)


_a														=		{
    """configuration_trocr""": ["""TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TrOCRConfig"""],
    """processing_trocr""": ["""TrOCRProcessor"""],
}


try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_a														=		[
				    """TROCR_PRETRAINED_MODEL_ARCHIVE_LIST""",
				    """TrOCRForCausalLM""",
				    """TrOCRPreTrainedModel""",
				]


if TYPE_CHECKING:
				from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig
				from .processing_trocr import TrOCRProcessor

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel

else:
				import sys

				_a														=		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)






