





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  :    Tuple						    =  True
except (ImportError, AttributeError):
     _snake_case  :    Any						    =  object

     def   a_    ( *lowerCAmelCase_      :			Optional[Any],	**lowerCAmelCase_      :			int	):
        pass




     _snake_case  :    Any						    =  False


_snake_case  :    Optional[int]						    =  logging.get_logger('transformers-cli/serving')

def   a_    ( lowerCAmelCase_      :			Namespace	):
   __lowerCAmelCase								=	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    (    _UpperCamelCase    ):

  """simple docstring"""


  a_        = 42





class 				_UpperCAmelCase    (    _UpperCamelCase    ):

  """simple docstring"""


  a_        = 42
  a_        = 42





class 				_UpperCAmelCase    (    _UpperCamelCase    ):

  """simple docstring"""


  a_        = 42





class 				_UpperCAmelCase    (    _UpperCamelCase    ):

  """simple docstring"""


  a_        = 42





class 				_UpperCAmelCase    (    _UpperCamelCase    ):

  """simple docstring"""


  @staticmethod
  def 				lowercase		(				lowerCAmelCase_ :		ArgumentParser     )							->						Tuple:
     __lowerCAmelCase								=	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_8_8_8					,  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 :		Optional[int]					,  lowerCAmelCase_ :		Pipeline					,  lowerCAmelCase_ :		str					,  lowerCAmelCase_ :		int					,  lowerCAmelCase_ :		int     )							->						Optional[int]:
     __lowerCAmelCase								=	pipeline

     __lowerCAmelCase								=	host
     __lowerCAmelCase								=	port
     __lowerCAmelCase								=	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}"""     )
        __lowerCAmelCase								=	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=6_0_0					,  )






  def 				lowercase		(				self :		Optional[Any]     )							->						Optional[Any]:
     run(self._app					,  host=self.host					,  port=self.port					,  workers=self.workers     )






  def 				lowercase		(				self :		int     )							->						List[str]:
     return ServeModelInfoResult(infos=vars(self._pipeline.model.config     )     )






  def 				lowercase		(				self :		Union[str, Any]					,  lowerCAmelCase_ :		str = Body(lowerCAmelCase_					,  embed=lowerCAmelCase_     )					,  lowerCAmelCase_ :		bool = Body(lowerCAmelCase_					,  embed=lowerCAmelCase_     )     )							->						Any:
     try:
        __lowerCAmelCase								=	self._pipeline.tokenizer.tokenize(lowerCAmelCase_     )

        if return_ids:
           __lowerCAmelCase								=	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=5_0_0					,  detail={'model': '', 'error': str(lowerCAmelCase_     )}     )






  def 				lowercase		(				self :		Optional[Any]					,  lowerCAmelCase_ :		List[int] = Body(lowerCAmelCase_					,  embed=lowerCAmelCase_     )					,  lowerCAmelCase_ :		bool = Body(lowerCAmelCase_					,  embed=lowerCAmelCase_     )					,  lowerCAmelCase_ :		bool = Body(lowerCAmelCase_					,  embed=lowerCAmelCase_     )					,  )							->						Optional[Any]:
     try:
        __lowerCAmelCase								=	self._pipeline.tokenizer.decode(lowerCAmelCase_					,  lowerCAmelCase_					,  lowerCAmelCase_     )
        return ServeDeTokenizeResult(model=''					,  text=lowerCAmelCase_     )
     except Exception as e:
        raise HTTPException(status_code=5_0_0					,  detail={'model': '', 'error': str(lowerCAmelCase_     )}     )






  async def 				lowercase		(				self :		Tuple					,  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
        __lowerCAmelCase								=	self._pipeline(lowerCAmelCase_     )
        return ServeForwardResult(output=lowerCAmelCase_     )
     except Exception as e:
        raise HTTPException(5_0_0					,  {'error': str(lowerCAmelCase_     )}     )








from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


_snake_case  :    Union[str, Any]						    =  {
    'configuration_layoutlmv3': [
        'LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'LayoutLMv3Config',
        'LayoutLMv3OnnxConfig',
    ],
    'processing_layoutlmv3': ['LayoutLMv3Processor'],
    'tokenization_layoutlmv3': ['LayoutLMv3Tokenizer'],
}

try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _snake_case  :    Union[str, Any]						    =  ['LayoutLMv3TokenizerFast']

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _snake_case  :    List[str]						    =  [
         'LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST',
         'LayoutLMv3ForQuestionAnswering',
         'LayoutLMv3ForSequenceClassification',
         'LayoutLMv3ForTokenClassification',
         'LayoutLMv3Model',
         'LayoutLMv3PreTrainedModel',
     ]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _snake_case  :    Optional[Any]						    =  [
         'TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST',
         'TFLayoutLMv3ForQuestionAnswering',
         'TFLayoutLMv3ForSequenceClassification',
         'TFLayoutLMv3ForTokenClassification',
         'TFLayoutLMv3Model',
         'TFLayoutLMv3PreTrainedModel',
     ]

try:
     if not is_vision_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _snake_case  :    Tuple						    =  ['LayoutLMv3FeatureExtractor']
     _snake_case  :    str						    =  ['LayoutLMv3ImageProcessor']


if TYPE_CHECKING:
     from .configuration_layoutlmva import (
         LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP,
         LayoutLMvaConfig,
         LayoutLMvaOnnxConfig,
     )
     from .processing_layoutlmva import LayoutLMvaProcessor
     from .tokenization_layoutlmva import LayoutLMvaTokenizer

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_layoutlmva import (
              LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
              LayoutLMvaForQuestionAnswering,
              LayoutLMvaForSequenceClassification,
              LayoutLMvaForTokenClassification,
              LayoutLMvaModel,
              LayoutLMvaPreTrainedModel,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_layoutlmva import (
              TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
              TFLayoutLMvaForQuestionAnswering,
              TFLayoutLMvaForSequenceClassification,
              TFLayoutLMvaForTokenClassification,
              TFLayoutLMvaModel,
              TFLayoutLMvaPreTrainedModel,
          )

     try:
          if not is_vision_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor
          from .image_processing_layoutlmva import LayoutLMvaImageProcessor

else:
     import sys

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

