


'''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											=					True
except (ImportError, AttributeError):
		__snake_case											=					object


		def       a				(      *__a			, **__a       )  ->     Union[str, Any]:



							'''simple docstring'''






							pass







		__snake_case											=					False


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


def       a				(      __a       )  ->     Optional[Any]:



					'''simple docstring'''






					UpperCamelCase__       :Tuple							 =			pipeline(
					    task=args.task			, model=args.model if args.model else None			, config=args.config			, tokenizer=args.tokenizer			, device=args.device			, )
					return ServeCommand(__a			, args.host			, args.port			, args.workers       )
class        lowercase     (							A__ ):

		"""simple docstring"""

		_a   			=    42



class        lowercase     (							A__ ):

		"""simple docstring"""

		_a   			=    42
		_a   			=    42



class        lowercase     (							A__ ):

		"""simple docstring"""

		_a   			=    42



class        lowercase     (							A__ ):

		"""simple docstring"""

		_a   			=    42



class        lowercase     (							A__ ):

		"""simple docstring"""

		@staticmethod
		def    lowerCAmelCase__    (		UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__       :Dict							 =			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=8888   ,       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_							):

							'''simple docstring'''


							UpperCamelCase__       :Any							 =			pipeline

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




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							run(self._app   ,       host=self.host   ,       port=self.port   ,       workers=self.workers							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


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




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_ = Body(UpperCamelCase_   ,       embed=UpperCamelCase_							)   ,       UpperCamelCase_ = Body(UpperCamelCase_   ,       embed=UpperCamelCase_							)							):

							'''simple docstring'''


							try:
												UpperCamelCase__       :str							 =			self._pipeline.tokenizer.tokenize(UpperCamelCase_							)

												if return_ids:
																	UpperCamelCase__       :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=500   ,       detail={'''model''': '''''', '''error''': str(UpperCamelCase_							)}							)




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_ = Body(UpperCamelCase_   ,       embed=UpperCamelCase_							)   ,       UpperCamelCase_ = Body(UpperCamelCase_   ,       embed=UpperCamelCase_							)   ,       UpperCamelCase_ = Body(UpperCamelCase_   ,       embed=UpperCamelCase_							)   ,       ):

							'''simple docstring'''


							try:
												UpperCamelCase__       :Optional[int]							 =			self._pipeline.tokenizer.decode(UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							)
												return ServeDeTokenizeResult(model=''''''   ,       text=UpperCamelCase_							)
							except Exception as e:
												raise HTTPException(status_code=500   ,       detail={'''model''': '''''', '''error''': str(UpperCamelCase_							)}							)




		async def    lowerCAmelCase__    (		self   ,       UpperCamelCase_=Body(UpperCamelCase_   ,       embed=UpperCamelCase_							)							):

							'''simple docstring'''


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

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



'''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											=					logging.get_logger(__name__)

__snake_case											=					{
    '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/config.json''',
    '''distilbert-base-uncased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/config.json''',
    '''distilbert-base-cased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json''',
    '''distilbert-base-multilingual-cased''': (
        '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json'''
    ),
    '''distilbert-base-uncased-finetuned-sst-2-english''': (
        '''https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json'''
    ),
}
class        lowercase     (							A__ ):

		"""simple docstring"""

		_a   			=    'distilbert'
		_a   			=    {
		    'hidden_size': 'dim',
		    'num_attention_heads': 'n_heads',
		    'num_hidden_layers': 'n_layers',
		}




		def __init__(		self   ,       UpperCamelCase_=30522   ,       UpperCamelCase_=512   ,       UpperCamelCase_=False   ,       UpperCamelCase_=6   ,       UpperCamelCase_=12   ,       UpperCamelCase_=768   ,       UpperCamelCase_=4 * 768   ,       UpperCamelCase_=0.1   ,       UpperCamelCase_=0.1   ,       UpperCamelCase_="gelu"   ,       UpperCamelCase_=0.02   ,       UpperCamelCase_=0.1   ,       UpperCamelCase_=0.2   ,       UpperCamelCase_=0   ,       **UpperCamelCase_   ,       ):

							'''simple docstring'''


							UpperCamelCase__       :List[Any]							 =			vocab_size
							UpperCamelCase__       :Dict							 =			max_position_embeddings
							UpperCamelCase__       :str							 =			sinusoidal_pos_embds
							UpperCamelCase__       :Any							 =			n_layers
							UpperCamelCase__       :str							 =			n_heads
							UpperCamelCase__       :Tuple							 =			dim
							UpperCamelCase__       :str							 =			hidden_dim
							UpperCamelCase__       :Dict							 =			dropout
							UpperCamelCase__       :int							 =			attention_dropout
							UpperCamelCase__       :Optional[Any]							 =			activation
							UpperCamelCase__       :Optional[int]							 =			initializer_range
							UpperCamelCase__       :Union[str, Any]							 =			qa_dropout
							UpperCamelCase__       :Dict							 =			seq_classif_dropout
							super().__init__(**UpperCamelCase_   ,       pad_token_id=UpperCamelCase_							)



class        lowercase     (							A__ ):

		"""simple docstring"""

		@property
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


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