




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_ : Union[str, Any]    		=       True
except (ImportError, AttributeError):
    snake_case_ : str    		=       object


    def    A						(*__A   :    Tuple   ,				**__A   :    List[str]       )   ->      List[str]:







         """simple docstring"""


         pass


    snake_case_ : List[str]    		=       False


snake_case_ : Tuple    		=       logging.get_logger("transformers-cli/serving")


def    A						(__A   :    Namespace       )   ->      Optional[int]:







     """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(__A   ,				args.host   ,				args.port   ,				args.workers       )







class __snake_case			(			a		):
   UpperCAmelCase__  :    dict







class __snake_case			(			a		):
   UpperCAmelCase__  :    List[str]
   UpperCAmelCase__  :    Optional[List[int]]







class __snake_case			(			a		):
   UpperCAmelCase__  :    str







class __snake_case			(			a		):
   UpperCAmelCase__  :    Any







class __snake_case			(			a		):







   @staticmethod
   def lowerCamelCase						(      _snake_case		:						ArgumentParser):

        """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=_snake_case					,						choices=get_supported_tasks()					,						help='''The task to run the pipeline on'''					,						)
        serve_parser.add_argument('''--host'''					,						type=_snake_case					,						default='''localhost'''					,						help='''Interface the server will listen on.''')
        serve_parser.add_argument('''--port'''					,						type=_snake_case					,						default=8888					,						help='''Port the serving will listen to.''')
        serve_parser.add_argument('''--workers'''					,						type=_snake_case					,						default=1					,						help='''Number of http workers''')
        serve_parser.add_argument('''--model'''					,						type=_snake_case					,						help='''Model\'s name or path to stored model.''')
        serve_parser.add_argument('''--config'''					,						type=_snake_case					,						help='''Model\'s config name or path to stored model.''')
        serve_parser.add_argument('''--tokenizer'''					,						type=_snake_case					,						help='''Tokenizer name to use.''')
        serve_parser.add_argument(
            '''--device'''					,						type=_snake_case					,						default=-1					,						help='''Indicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1)'''					,						)
        serve_parser.set_defaults(func=_snake_case)







   def __init__(      self		:						Tuple					,						_snake_case		:						Pipeline					,						_snake_case		:						str					,						_snake_case		:						int					,						_snake_case		:						int):

        """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=_snake_case					,						response_class=_snake_case					,						methods=['''GET''']					,						),
                     APIRoute(
                         '''/tokenize'''					,						self.tokenize					,						response_model=_snake_case					,						response_class=_snake_case					,						methods=['''POST''']					,						),
                     APIRoute(
                         '''/detokenize'''					,						self.detokenize					,						response_model=_snake_case					,						response_class=_snake_case					,						methods=['''POST''']					,						),
                     APIRoute(
                         '''/forward'''					,						self.forward					,						response_model=_snake_case					,						response_class=_snake_case					,						methods=['''POST''']					,						),
                 ]					,						timeout=600					,						)







   def lowerCamelCase						(      self		:						Tuple):

        """simple docstring"""


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







   def lowerCamelCase						(      self		:						List[str]):

        """simple docstring"""


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







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

        """simple docstring"""


        try:
             UpperCAmelCase_          =							self._pipeline.tokenizer.tokenize(_snake_case)

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

        except Exception as e:
             raise HTTPException(status_code=500					,						detail={'''model''': '''''', '''error''': str(_snake_case)})







   def lowerCamelCase						(      self		:						Tuple					,						_snake_case		:						List[int] = Body(_snake_case					,						embed=_snake_case)					,						_snake_case		:						bool = Body(_snake_case					,						embed=_snake_case)					,						_snake_case		:						bool = Body(_snake_case					,						embed=_snake_case)					,						):

        """simple docstring"""


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







   async def lowerCamelCase						(      self		:						List[Any]					,						_snake_case		:						Optional[int]=Body(_snake_case					,						embed=_snake_case)):

        """simple docstring"""


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

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













import os
import sys
import tempfile

import torch

from .state import AcceleratorState
from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment


def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase						,	_UpperCAmelCase=()						,	_UpperCAmelCase=None						,	_UpperCAmelCase="no"						,	_UpperCAmelCase="29500" )			->     Tuple:
    lowerCamelCase__    :      Dict					 =		False
    lowerCamelCase__    :      Dict					 =		False
    if any(key.startswith('KAGGLE' ) for key in os.environ.keys() ):
        lowerCamelCase__    :      Optional[Any]					 =		True
    elif "IPython" in sys.modules:
        lowerCamelCase__    :      Optional[Any]					 =		'google.colab' in str(sys.modules['IPython'].get_ipython() )

    try:
        lowerCamelCase__    :      List[str]					 =		PrecisionType(mixed_precision.lower() )
    except ValueError:
        raise ValueError(
            F"""Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}.""" )

    if (in_colab or in_kaggle) and (os.environ.get('TPU_NAME'						,	_UpperCAmelCase ) is not None):
        # TPU launch
        import torch_xla.distributed.xla_multiprocessing as xmp

        if len(AcceleratorState._shared_state ) > 0:
            raise ValueError(
                'To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside '
                'your training function. Restart your notebook and make sure no cells initializes an '
                '`Accelerator`.' )
        if num_processes is None:
            lowerCamelCase__    :      Optional[Any]					 =		8

        lowerCamelCase__    :      List[str]					 =		PrepareForLaunch(_UpperCAmelCase						,	distributed_type='TPU' )
        print(F"""Launching a training on {num_processes} TPU cores.""" )
        xmp.spawn(_UpperCAmelCase						,	args=_UpperCAmelCase						,	nprocs=_UpperCAmelCase						,	start_method='fork' )
    elif in_colab:
        # No need for a distributed launch otherwise as it's either CPU or one GPU.
        if torch.cuda.is_available():
            print('Launching training on one GPU.' )
        else:
            print('Launching training on one CPU.' )
        function(*_UpperCAmelCase )
    else:
        if num_processes is None:
            raise ValueError(
                'You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call.' )

        if num_processes > 1:
            # Multi-GPU launch
            from torch.multiprocessing import start_processes
            from torch.multiprocessing.spawn import ProcessRaisedException

            if len(AcceleratorState._shared_state ) > 0:
                raise ValueError(
                    'To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized '
                    'inside your training function. Restart your notebook and make sure no cells initializes an '
                    '`Accelerator`.' )

            if torch.cuda.is_initialized():
                raise ValueError(
                    'To launch a multi-GPU training from your notebook, you need to avoid running any instruction '
                    'using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA '
                    'function.' )

            # torch.distributed will expect a few environment variable to be here. We set the ones common to each
            # process here (the other ones will be set be the launcher).
            with patch_environment(
                world_size=_UpperCAmelCase						,	master_addr='127.0.01'						,	master_port=_UpperCAmelCase						,	mixed_precision=_UpperCAmelCase ):
                lowerCamelCase__    :      Optional[int]					 =		PrepareForLaunch(_UpperCAmelCase						,	distributed_type='MULTI_GPU' )
                print(F"""Launching training on {num_processes} GPUs.""" )
                try:
                    start_processes(_UpperCAmelCase						,	args=_UpperCAmelCase						,	nprocs=_UpperCAmelCase						,	start_method='fork' )
                except ProcessRaisedException as e:
                    if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]:
                        raise RuntimeError(
                            'CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. '
                            'This likely stems from an outside import causing issues once the `notebook_launcher()` is called. '
                            'Please review your imports and test them when running the `notebook_launcher()` to identify '
                            'which one is problematic.' ) from e

        else:
            # No need for a distributed launch otherwise as it's either CPU, GPU or MPS.
            if is_mps_available():
                lowerCamelCase__    :      int					 =		'1'
                print('Launching training on MPS.' )
            elif torch.cuda.is_available():
                print('Launching training on one GPU.' )
            else:
                print('Launching training on CPU.' )
            function(*_UpperCAmelCase )


def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase						,	_UpperCAmelCase=()						,	_UpperCAmelCase=2 )			->     Optional[Any]:
    from torch.multiprocessing import start_processes

    with tempfile.NamedTemporaryFile() as tmp_file:
        # torch.distributed will expect a few environment variable to be here. We set the ones common to each
        # process here (the other ones will be set be the launcher).
        with patch_environment(
            world_size=_UpperCAmelCase						,	master_addr='127.0.01'						,	master_port='29500'						,	accelerate_mixed_precision='no'						,	accelerate_debug_rdv_file=tmp_file.name						,	accelerate_use_cpu='yes'						,	):
            lowerCamelCase__    :      Optional[Any]					 =		PrepareForLaunch(_UpperCAmelCase						,	debug=_UpperCAmelCase )
            start_processes(_UpperCAmelCase						,	args=_UpperCAmelCase						,	nprocs=_UpperCAmelCase						,	start_method='fork' )





