






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   lowerCamelCase__			(     *lowercase						,     **lowercase				):

     """simple docstring"""






     pass

   snake_case    				=		False


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


def   lowerCamelCase__			(     lowercase				):

  """simple docstring"""






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


class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


     UpperCamelCase_							:		dict





class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


     UpperCamelCase_							:		List[str]
     UpperCamelCase_							:		Optional[List[int]]





class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


     UpperCamelCase_							:		str





class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


     UpperCamelCase_							:		Any





class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


     @staticmethod
     def 				_A       (				UpperCAmelCase_       :	ArgumentParser      ):
       SCREAMING_SNAKE_CASE       :      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       :	List[str]					, UpperCAmelCase_       :	Pipeline					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	int      ):
       SCREAMING_SNAKE_CASE       :      Tuple							=    pipeline

       SCREAMING_SNAKE_CASE       :      List[Any]							=    host
       SCREAMING_SNAKE_CASE       :      Dict							=    port
       SCREAMING_SNAKE_CASE       :      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}'''      )
         SCREAMING_SNAKE_CASE       :      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=600					, )


     def 				_A       (				self       :	Tuple      ):
       run(self._app					, host=self.host					, port=self.port					, workers=self.workers      )


     def 				_A       (				self       :	Optional[int]      ):
       return ServeModelInfoResult(infos=vars(self._pipeline.model.config      )      )


     def 				_A       (				self       :	Optional[int]					, UpperCAmelCase_       :	str = Body(UpperCAmelCase_					, embed=UpperCAmelCase_      )					, UpperCAmelCase_       :	bool = Body(UpperCAmelCase_					, embed=UpperCAmelCase_      )      ):
       try:
         SCREAMING_SNAKE_CASE       :      Optional[int]							=    self._pipeline.tokenizer.tokenize(UpperCAmelCase_      )

         if return_ids:
           SCREAMING_SNAKE_CASE       :      Tuple							=    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 				_A       (				self       :	Dict					, UpperCAmelCase_       :	List[int] = Body(UpperCAmelCase_					, embed=UpperCAmelCase_      )					, UpperCAmelCase_       :	bool = Body(UpperCAmelCase_					, embed=UpperCAmelCase_      )					, UpperCAmelCase_       :	bool = Body(UpperCAmelCase_					, embed=UpperCAmelCase_      )					, ):
       try:
         SCREAMING_SNAKE_CASE       :      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 				_A       (				self       :	str					, UpperCAmelCase_       :	List[str]=Body(UpperCAmelCase_					, embed=UpperCAmelCase_      )      ):

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

       try:
         # Forward through the model
         SCREAMING_SNAKE_CASE       :      Tuple							=    self._pipeline(UpperCAmelCase_      )
         return ServeForwardResult(output=UpperCAmelCase_      )
       except Exception as e:
         raise HTTPException(500					, {"error": str(UpperCAmelCase_      )}      )














from typing import TYPE_CHECKING

from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


snake_case    				=		{
    """configuration_trajectory_transformer""": [
        """TRAJECTORY_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """TrajectoryTransformerConfig""",
    ],
}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   snake_case    				=		[
       """TRAJECTORY_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
       """TrajectoryTransformerModel""",
       """TrajectoryTransformerPreTrainedModel""",
       """load_tf_weights_in_trajectory_transformer""",
   ]


if TYPE_CHECKING:
   from .configuration_trajectory_transformer import (
       TRAJECTORY_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
       TrajectoryTransformerConfig,
   )

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_trajectory_transformer import (
          TRAJECTORY_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
          TrajectoryTransformerModel,
          TrajectoryTransformerPreTrainedModel,
          load_tf_weights_in_trajectory_transformer,
      )


else:
   import sys

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






