




'''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

   __a						:      Any       =						True
except (ImportError, AttributeError):
   __a						:      Any       =						object





   def      __a     (       *_UpperCamelCase:   Union[str, Any] ,						**_UpperCamelCase:   List[str]   )						->			int:

        """simple docstring"""




        pass





   __a						:      int       =						False


__a						:      Any       =						logging.get_logger('''transformers-cli/serving''')





def      __a     (       _UpperCamelCase:   Namespace   )						->			List[str]:

     """simple docstring"""




     _snake_case							=				pipeline(
         task=args.task ,						model=args.model if args.model else None ,						config=args.config ,						tokenizer=args.tokenizer ,						device=args.device ,						)
     return ServeCommand(_UpperCamelCase ,						args.host ,						args.port ,						args.workers   )

class        _a    (						__lowerCAmelCase							):
     SCREAMING_SNAKE_CASE_							:   dict







class        _a    (						__lowerCAmelCase							):
     SCREAMING_SNAKE_CASE_							:   List[str]
     SCREAMING_SNAKE_CASE_							:   Optional[List[int]]







class        _a    (						__lowerCAmelCase							):
     SCREAMING_SNAKE_CASE_							:   str







class        _a    (						__lowerCAmelCase							):
     SCREAMING_SNAKE_CASE_							:   Any







class        _a    (						__lowerCAmelCase							):






     @staticmethod
     def     _lowercase     (			_SCREAMING_SNAKE_CASE   )							->      Tuple:
          _snake_case							=				parser.add_parser(
              "serve" ,help="CLI tool to run inference requests through REST and GraphQL endpoints."   )
          serve_parser.add_argument(
              "--task" ,type=_SCREAMING_SNAKE_CASE ,choices=get_supported_tasks() ,help="The task to run the pipeline on" ,)
          serve_parser.add_argument("--host" ,type=_SCREAMING_SNAKE_CASE ,default="localhost" ,help="Interface the server will listen on."   )
          serve_parser.add_argument("--port" ,type=_SCREAMING_SNAKE_CASE ,default=8_888 ,help="Port the serving will listen to."   )
          serve_parser.add_argument("--workers" ,type=_SCREAMING_SNAKE_CASE ,default=1 ,help="Number of http workers"   )
          serve_parser.add_argument("--model" ,type=_SCREAMING_SNAKE_CASE ,help="Model's name or path to stored model."   )
          serve_parser.add_argument("--config" ,type=_SCREAMING_SNAKE_CASE ,help="Model's config name or path to stored model."   )
          serve_parser.add_argument("--tokenizer" ,type=_SCREAMING_SNAKE_CASE ,help="Tokenizer name to use."   )
          serve_parser.add_argument(
              "--device" ,type=_SCREAMING_SNAKE_CASE ,default=-1 ,help="Indicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1)" ,)
          serve_parser.set_defaults(func=_SCREAMING_SNAKE_CASE   )






     def __init__(			self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   )							->      Union[str, Any]:
          _snake_case							=				pipeline

          _snake_case							=				host
          _snake_case							=				port
          _snake_case							=				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}"""   )
               _snake_case							=				FastAPI(
                   routes=[
                       APIRoute(
                           "/" ,self.model_info ,response_model=_SCREAMING_SNAKE_CASE ,response_class=_SCREAMING_SNAKE_CASE ,methods=["GET"] ,),
                       APIRoute(
                           "/tokenize" ,self.tokenize ,response_model=_SCREAMING_SNAKE_CASE ,response_class=_SCREAMING_SNAKE_CASE ,methods=["POST"] ,),
                       APIRoute(
                           "/detokenize" ,self.detokenize ,response_model=_SCREAMING_SNAKE_CASE ,response_class=_SCREAMING_SNAKE_CASE ,methods=["POST"] ,),
                       APIRoute(
                           "/forward" ,self.forward ,response_model=_SCREAMING_SNAKE_CASE ,response_class=_SCREAMING_SNAKE_CASE ,methods=["POST"] ,),
                   ] ,timeout=600 ,)






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






     def     _lowercase     (			self   )							->      int:
          return ServeModelInfoResult(infos=vars(self._pipeline.model.config   )   )






     def     _lowercase     (			self ,_SCREAMING_SNAKE_CASE = Body(_SCREAMING_SNAKE_CASE ,embed=_SCREAMING_SNAKE_CASE   ) ,_SCREAMING_SNAKE_CASE = Body(_SCREAMING_SNAKE_CASE ,embed=_SCREAMING_SNAKE_CASE   )   )							->      Union[str, Any]:
          try:
               _snake_case							=				self._pipeline.tokenizer.tokenize(_SCREAMING_SNAKE_CASE   )

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

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






     def     _lowercase     (			self ,_SCREAMING_SNAKE_CASE = Body(_SCREAMING_SNAKE_CASE ,embed=_SCREAMING_SNAKE_CASE   ) ,_SCREAMING_SNAKE_CASE = Body(_SCREAMING_SNAKE_CASE ,embed=_SCREAMING_SNAKE_CASE   ) ,_SCREAMING_SNAKE_CASE = Body(_SCREAMING_SNAKE_CASE ,embed=_SCREAMING_SNAKE_CASE   ) ,)							->      List[Any]:
          try:
               _snake_case							=				self._pipeline.tokenizer.decode(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   )
               return ServeDeTokenizeResult(model="" ,text=_SCREAMING_SNAKE_CASE   )
          except Exception as e:
               raise HTTPException(status_code=500 ,detail={"model": "", "error": str(_SCREAMING_SNAKE_CASE   )}   )






     async def     _lowercase     (			self ,_SCREAMING_SNAKE_CASE=Body(_SCREAMING_SNAKE_CASE ,embed=_SCREAMING_SNAKE_CASE   )   )							->      Optional[Any]:

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

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








'''simple docstring'''





import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase_						:      str       =						logging.get_logger(__name__)

UpperCamelCase_						:      Optional[Any]       =						{
    '''asapp/sew-tiny-100k''': '''https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json''',
    # See all SEW models at https://huggingface.co/models?filter=sew
}

class        _a    (						__lowerCAmelCase							):
     SCREAMING_SNAKE_CASE_							:   Dict       =						"""sew"""






     def __init__(			self ,_SCREAMING_SNAKE_CASE=32 ,_SCREAMING_SNAKE_CASE=768 ,_SCREAMING_SNAKE_CASE=12 ,_SCREAMING_SNAKE_CASE=12 ,_SCREAMING_SNAKE_CASE=3_072 ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE="gelu" ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.0 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.1 ,_SCREAMING_SNAKE_CASE=0.0_2 ,_SCREAMING_SNAKE_CASE=1e-5 ,_SCREAMING_SNAKE_CASE="group" ,_SCREAMING_SNAKE_CASE="gelu" ,_SCREAMING_SNAKE_CASE=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) ,_SCREAMING_SNAKE_CASE=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,_SCREAMING_SNAKE_CASE=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=128 ,_SCREAMING_SNAKE_CASE=16 ,_SCREAMING_SNAKE_CASE=True ,_SCREAMING_SNAKE_CASE=0.0_5 ,_SCREAMING_SNAKE_CASE=10 ,_SCREAMING_SNAKE_CASE=2 ,_SCREAMING_SNAKE_CASE=0.0 ,_SCREAMING_SNAKE_CASE=10 ,_SCREAMING_SNAKE_CASE=0 ,_SCREAMING_SNAKE_CASE="mean" ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=False ,_SCREAMING_SNAKE_CASE=256 ,_SCREAMING_SNAKE_CASE=0 ,_SCREAMING_SNAKE_CASE=1 ,_SCREAMING_SNAKE_CASE=2 ,**_SCREAMING_SNAKE_CASE ,)							->      str:
          super().__init__(**_SCREAMING_SNAKE_CASE ,pad_token_id=_SCREAMING_SNAKE_CASE ,bos_token_id=_SCREAMING_SNAKE_CASE ,eos_token_id=_SCREAMING_SNAKE_CASE   )
          _snake_case							=				hidden_size
          _snake_case							=				feat_extract_norm
          _snake_case							=				feat_extract_activation
          _snake_case							=				list(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				list(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				list(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				conv_bias
          _snake_case							=				num_conv_pos_embeddings
          _snake_case							=				num_conv_pos_embedding_groups
          _snake_case							=				len(self.conv_dim   )
          _snake_case							=				num_hidden_layers
          _snake_case							=				intermediate_size
          _snake_case							=				squeeze_factor
          _snake_case							=				hidden_act
          _snake_case							=				num_attention_heads
          _snake_case							=				hidden_dropout
          _snake_case							=				attention_dropout
          _snake_case							=				activation_dropout
          _snake_case							=				feat_proj_dropout
          _snake_case							=				final_dropout
          _snake_case							=				layerdrop
          _snake_case							=				layer_norm_eps
          _snake_case							=				initializer_range
          _snake_case							=				vocab_size

          if (
              (len(self.conv_stride   ) != self.num_feat_extract_layers)
              or (len(self.conv_kernel   ) != self.num_feat_extract_layers)
              or (len(self.conv_dim   ) != self.num_feat_extract_layers)
          ):
               raise ValueError(
                   "Configuration for convolutional layers is incorrect."
                   "It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,"
                   f"""but is `len(config.conv_dim) = {len(self.conv_dim   )}`, `len(config.conv_stride)"""
                   f"""= {len(self.conv_stride   )}`, `len(config.conv_kernel) = {len(self.conv_kernel   )}`."""   )

          # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
          _snake_case							=				apply_spec_augment
          _snake_case							=				mask_time_prob
          _snake_case							=				mask_time_length
          _snake_case							=				mask_time_min_masks
          _snake_case							=				mask_feature_prob
          _snake_case							=				mask_feature_length
          _snake_case							=				mask_feature_min_masks

          # ctc loss
          _snake_case							=				ctc_loss_reduction
          _snake_case							=				ctc_zero_infinity

          # sequence classification
          _snake_case							=				use_weighted_layer_sum
          _snake_case							=				classifier_proj_size






     @property
     def     _lowercase     (			self   )							->      Optional[Any]:
          return functools.reduce(operator.mul ,self.conv_stride ,1   )


