






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






							def   a			(						*lowerCamelCase_		,   **lowerCamelCase_				):






											'''simple docstring'''
											pass




							A__	:			Dict			   = False


A__	:			Tuple			   = logging.get_logger('transformers-cli/serving')






def   a			(						lowerCamelCase_				):






				'''simple docstring'''
				lowercase__    =			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					(       A__	):

				"""simple docstring"""




				lowercase__											=       42



class       _UpperCAmelCase					(       A__	):

				"""simple docstring"""




				lowercase__											=       42
				lowercase__											=       42



class       _UpperCAmelCase					(       A__	):

				"""simple docstring"""




				lowercase__											=       42



class       _UpperCAmelCase					(       A__	):

				"""simple docstring"""




				lowercase__											=       42



class       _UpperCAmelCase					(       A__	):

				"""simple docstring"""




				@staticmethod
				def 				lowercase__   (     lowerCamelCase						:    ArgumentParser  ):

								'''simple docstring'''
								lowercase__    =			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						:    str,  lowerCamelCase						:    Pipeline,  lowerCamelCase						:    str,  lowerCamelCase						:    int,  lowerCamelCase						:    int  ):

								'''simple docstring'''
								lowercase__    =			pipeline

								lowercase__    =			host
								lowercase__    =			port
								lowercase__    =			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__    =			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 				lowercase__   (     self						:    Any  ):

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




				def 				lowercase__   (     self						:    Union[str, Any]  ):

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




				def 				lowercase__   (     self						:    Tuple,  lowerCamelCase						:    str = Body(lowerCamelCase,  embed=lowerCamelCase  ),  lowerCamelCase						:    bool = Body(lowerCamelCase,  embed=lowerCamelCase  )  ):

								'''simple docstring'''
								try:
												lowercase__    =			self._pipeline.tokenizer.tokenize(lowerCamelCase  )

												if return_ids:
																lowercase__    =			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 				lowercase__   (     self						:    str,  lowerCamelCase						:    List[int] = Body(lowerCamelCase,  embed=lowerCamelCase  ),  lowerCamelCase						:    bool = Body(lowerCamelCase,  embed=lowerCamelCase  ),  lowerCamelCase						:    bool = Body(lowerCamelCase,  embed=lowerCamelCase  ),  ):

								'''simple docstring'''
								try:
												lowercase__    =			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 				lowercase__   (     self						:    List[str],  lowerCamelCase						:    Optional[int]=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
												lowercase__    =			self._pipeline(lowerCamelCase  )
												return ServeForwardResult(output=lowerCamelCase  )
								except Exception as e:
												raise HTTPException(500,  {'''error''': str(lowerCamelCase  )}  )












from .configuration_bert_masked import MaskedBertConfig
from .modeling_bert_masked import (
    MaskedBertForMultipleChoice,
    MaskedBertForQuestionAnswering,
    MaskedBertForSequenceClassification,
    MaskedBertForTokenClassification,
    MaskedBertModel,
)
from .modules import *




