





from typing import Optional, Union

import torch
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_mobilenet_va import MobileNetVaConfig


_snake_case  :   int						   =  logging.get_logger(__name__)


# General docstring
_snake_case  :   int						   =  'MobileNetV1Config'

# Base docstring
_snake_case  :   Optional[int]						   =  'google/mobilenet_v1_1.0_224'
_snake_case  :   List[str]						   =  [1, 1024, 7, 7]

# Image classification docstring
_snake_case  :   Optional[Any]						   =  'google/mobilenet_v1_1.0_224'
_snake_case  :   List[str]						   =  'tabby, tabby cat'


_snake_case  :   List[Any]						   =  [
    'google/mobilenet_v1_1.0_224',
    'google/mobilenet_v1_0.75_192',
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
]

def   a_   (			lowerCAmelCase_				:      Union[str, Any],     lowerCAmelCase_				:      Tuple,     lowerCAmelCase_				:      Optional[Any]=None     ):
   __lowerCAmelCase							     =     {}

   if isinstance(lowerCAmelCase_,     lowerCAmelCase_     ):
      __lowerCAmelCase							     =     model.mobilenet_va
   else:
      __lowerCAmelCase							     =     model

   __lowerCAmelCase							     =     'MobilenetV1/Conv2d_0/'
   __lowerCAmelCase							     =     backbone.conv_stem.convolution.weight
   __lowerCAmelCase							     =     backbone.conv_stem.normalization.bias
   __lowerCAmelCase							     =     backbone.conv_stem.normalization.weight
   __lowerCAmelCase							     =     backbone.conv_stem.normalization.running_mean
   __lowerCAmelCase							     =     backbone.conv_stem.normalization.running_var

   for i in range(13     ):
      __lowerCAmelCase							     =     i + 1
      __lowerCAmelCase							     =     i * 2

      __lowerCAmelCase							     =     backbone.layer[pt_index]
      __lowerCAmelCase							     =     F"""MobilenetV1/Conv2d_{tf_index}_depthwise/"""
      __lowerCAmelCase							     =     pointer.convolution.weight
      __lowerCAmelCase							     =     pointer.normalization.bias
      __lowerCAmelCase							     =     pointer.normalization.weight
      __lowerCAmelCase							     =     pointer.normalization.running_mean
      __lowerCAmelCase							     =     pointer.normalization.running_var

      __lowerCAmelCase							     =     backbone.layer[pt_index + 1]
      __lowerCAmelCase							     =     F"""MobilenetV1/Conv2d_{tf_index}_pointwise/"""
      __lowerCAmelCase							     =     pointer.convolution.weight
      __lowerCAmelCase							     =     pointer.normalization.bias
      __lowerCAmelCase							     =     pointer.normalization.weight
      __lowerCAmelCase							     =     pointer.normalization.running_mean
      __lowerCAmelCase							     =     pointer.normalization.running_var

   if isinstance(lowerCAmelCase_,     lowerCAmelCase_     ):
      __lowerCAmelCase							     =     'MobilenetV1/Logits/Conv2d_1c_1x1/'
      __lowerCAmelCase							     =     model.classifier.weight
      __lowerCAmelCase							     =     model.classifier.bias

   return tf_to_pt_map

def   a_   (			lowerCAmelCase_				:      str,     lowerCAmelCase_				:      Tuple,     lowerCAmelCase_				:      str     ):
   try:
      import numpy as np
      import tensorflow as tf
   except ImportError:
      logger.error(
          'Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see '
          'https://www.tensorflow.org/install/ for installation instructions.'     )
      raise

   # Load weights from TF model
   __lowerCAmelCase							     =     tf.train.list_variables(lowerCAmelCase_     )
   __lowerCAmelCase							     =     {}
   for name, shape in init_vars:
      logger.info(F"""Loading TF weight {name} with shape {shape}"""     )
      __lowerCAmelCase							     =     tf.train.load_variable(lowerCAmelCase_,     lowerCAmelCase_     )
      __lowerCAmelCase							     =     array

   # Build TF to PyTorch weights loading map
   __lowerCAmelCase							     =     _build_tf_to_pytorch_map(lowerCAmelCase_,     lowerCAmelCase_,     lowerCAmelCase_     )

   for name, pointer in tf_to_pt_map.items():
      logger.info(F"""Importing {name}"""     )
      if name not in tf_weights:
         logger.info(F"""{name} not in tf pre-trained weights, skipping"""     )
         continue

      __lowerCAmelCase							     =     tf_weights[name]

      if "depthwise_weights" in name:
         logger.info('Transposing depthwise'     )
         __lowerCAmelCase							     =     np.transpose(lowerCAmelCase_,     (2, 3, 0, 1)     )
      elif "weights" in name:
         logger.info('Transposing'     )
         if len(pointer.shape     ) == 2:  # copying into linear layer
            __lowerCAmelCase							     =     array.squeeze().transpose()
         else:
            __lowerCAmelCase							     =     np.transpose(lowerCAmelCase_,     (3, 2, 0, 1)     )

      if pointer.shape != array.shape:
         raise ValueError(F"""Pointer shape {pointer.shape} and array shape {array.shape} mismatched"""     )

      logger.info(F"""Initialize PyTorch weight {name} {array.shape}"""     )
      __lowerCAmelCase							     =     torch.from_numpy(lowerCAmelCase_     )

      tf_weights.pop(lowerCAmelCase_,     lowerCAmelCase_     )
      tf_weights.pop(name + '/RMSProp',     lowerCAmelCase_     )
      tf_weights.pop(name + '/RMSProp_1',     lowerCAmelCase_     )
      tf_weights.pop(name + '/ExponentialMovingAverage',     lowerCAmelCase_     )

   logger.info(F"""Weights not copied to PyTorch model: {", ".join(tf_weights.keys()     )}"""     )
   return model

def   a_   (			lowerCAmelCase_				:      torch.Tensor,     lowerCAmelCase_				:      nn.Convad     ):
   __lowerCAmelCase     ,     __lowerCAmelCase							     =     features.shape[-2:]
   __lowerCAmelCase     ,     __lowerCAmelCase							     =     conv_layer.stride
   __lowerCAmelCase     ,     __lowerCAmelCase							     =     conv_layer.kernel_size

   if in_height % stride_height == 0:
      __lowerCAmelCase							     =     max(kernel_height - stride_height,     0     )
   else:
      __lowerCAmelCase							     =     max(kernel_height - (in_height % stride_height),     0     )

   if in_width % stride_width == 0:
      __lowerCAmelCase							     =     max(kernel_width - stride_width,     0     )
   else:
      __lowerCAmelCase							     =     max(kernel_width - (in_width % stride_width),     0     )

   __lowerCAmelCase							     =     pad_along_width // 2
   __lowerCAmelCase							     =     pad_along_width - pad_left
   __lowerCAmelCase							     =     pad_along_height // 2
   __lowerCAmelCase							     =     pad_along_height - pad_top

   __lowerCAmelCase							     =     (pad_left, pad_right, pad_top, pad_bottom)
   return nn.functional.pad(lowerCAmelCase_,     lowerCAmelCase_,     'constant',     0.0     )



class     _UpperCAmelCase   (   nn.Module   ):

  """simple docstring"""


  def __init__(    self			:		Tuple ,  lowerCAmelCase_			:		MobileNetVaConfig ,  lowerCAmelCase_			:		int ,  lowerCAmelCase_			:		int ,  lowerCAmelCase_			:		int ,  lowerCAmelCase_			:		Optional[int] = 1 ,  lowerCAmelCase_			:		Optional[int] = 1 ,  lowerCAmelCase_			:		bool = False ,  lowerCAmelCase_			:		Optional[bool] = True ,  lowerCAmelCase_			:		Optional[bool or str] = True ,  )							->						None:
     super().__init__()
     __lowerCAmelCase							     =     config

     if in_channels % groups != 0:
        raise ValueError(f"""Input channels ({in_channels}) are not divisible by {groups} groups."""					)
     if out_channels % groups != 0:
        raise ValueError(f"""Output channels ({out_channels}) are not divisible by {groups} groups."""					)

     __lowerCAmelCase							     =     0 if config.tf_padding else int((kernel_size - 1) / 2					)

     __lowerCAmelCase							     =     nn.Convad(
         in_channels=lowerCAmelCase_ ,  out_channels=lowerCAmelCase_ ,  kernel_size=lowerCAmelCase_ ,  stride=lowerCAmelCase_ ,  padding=lowerCAmelCase_ ,  groups=lowerCAmelCase_ ,  bias=lowerCAmelCase_ ,  padding_mode='zeros' ,  )

     if use_normalization:
        __lowerCAmelCase							     =     nn.BatchNormad(
            num_features=lowerCAmelCase_ ,  eps=config.layer_norm_eps ,  momentum=0.99_97 ,  affine=lowerCAmelCase_ ,  track_running_stats=lowerCAmelCase_ ,  )
     else:
        __lowerCAmelCase							     =     None

     if use_activation:
        if isinstance(lowerCAmelCase_ ,  lowerCAmelCase_					):
           __lowerCAmelCase							     =     ACTaFN[use_activation]
        elif isinstance(config.hidden_act ,  lowerCAmelCase_					):
           __lowerCAmelCase							     =     ACTaFN[config.hidden_act]
        else:
           __lowerCAmelCase							     =     config.hidden_act
     else:
        __lowerCAmelCase							     =     None






  def     lowercase		(    self			:		Dict ,  lowerCAmelCase_			:		torch.Tensor					)							->						torch.Tensor:
     if self.config.tf_padding:
        __lowerCAmelCase							     =     apply_tf_padding(lowerCAmelCase_ ,  self.convolution					)
     __lowerCAmelCase							     =     self.convolution(lowerCAmelCase_					)
     if self.normalization is not None:
        __lowerCAmelCase							     =     self.normalization(lowerCAmelCase_					)
     if self.activation is not None:
        __lowerCAmelCase							     =     self.activation(lowerCAmelCase_					)
     return features





class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			MobileNetVaConfig
  a_      =			load_tf_weights_in_mobilenet_va
  a_      =			"""mobilenet_v1"""
  a_      =			"""pixel_values"""
  a_      =			False






  def     lowercase		(    self			:		Optional[int] ,  lowerCAmelCase_			:		Union[nn.Linear, nn.Convad]					)							->						None:
     if isinstance(lowerCAmelCase_ ,  (nn.Linear, nn.Convad)					):
        module.weight.data.normal_(mean=0.0 ,  std=self.config.initializer_range					)
        if module.bias is not None:
           module.bias.data.zero_()
     elif isinstance(lowerCAmelCase_ ,  nn.BatchNormad					):
        module.bias.data.zero_()
        module.weight.data.fill_(1.0					)





_snake_case  :   List[Any]						   =  R'\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'

_snake_case  :   Optional[Any]						   =  R'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`MobileNetV1ImageProcessor.__call__`] for details.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n'



@add_start_docstrings(
    """The bare MobileNetV1 model outputting raw hidden-states without any specific head on top."""		,   _UpperCamelCase		,   )
class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  def __init__(    self			:		List[Any] ,  lowerCAmelCase_			:		MobileNetVaConfig ,  lowerCAmelCase_			:		bool = True					)							->						Optional[Any]:
     super().__init__(lowerCAmelCase_					)
     __lowerCAmelCase							     =     config

     __lowerCAmelCase							     =     3_2
     __lowerCAmelCase							     =     max(int(depth * config.depth_multiplier					) ,  config.min_depth					)

     __lowerCAmelCase							     =     MobileNetVaConvLayer(
         lowerCAmelCase_ ,  in_channels=config.num_channels ,  out_channels=lowerCAmelCase_ ,  kernel_size=3 ,  stride=2 ,  )

     __lowerCAmelCase							     =     [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

     __lowerCAmelCase							     =     nn.ModuleList()
     for i in range(1_3					):
        __lowerCAmelCase							     =     out_channels

        if strides[i] == 2 or i == 0:
           depth *= 2
           __lowerCAmelCase							     =     max(int(depth * config.depth_multiplier					) ,  config.min_depth					)

        self.layer.append(
            MobileNetVaConvLayer(
                lowerCAmelCase_ ,  in_channels=lowerCAmelCase_ ,  out_channels=lowerCAmelCase_ ,  kernel_size=3 ,  stride=strides[i] ,  groups=lowerCAmelCase_ ,  )					)

        self.layer.append(
            MobileNetVaConvLayer(
                lowerCAmelCase_ ,  in_channels=lowerCAmelCase_ ,  out_channels=lowerCAmelCase_ ,  kernel_size=1 ,  )					)

     __lowerCAmelCase							     =     nn.AdaptiveAvgPoolad((1, 1)					) if add_pooling_layer else None

     # Initialize weights and apply final processing
     self.post_init()






  def     lowercase		(    self			:		List[str] ,  lowerCAmelCase_			:		Optional[Any]					)							->						Dict:
     raise NotImplementedError






  @add_start_docstrings_to_model_forward(lowerCAmelCase_					)
  @add_code_sample_docstrings(
      checkpoint=_CHECKPOINT_FOR_DOC ,  output_type=lowerCAmelCase_ ,  config_class=_CONFIG_FOR_DOC ,  modality='vision' ,  expected_output=_EXPECTED_OUTPUT_SHAPE ,  )
  def     lowercase		(    self			:		Any ,  lowerCAmelCase_			:		Optional[torch.Tensor] = None ,  lowerCAmelCase_			:		Optional[bool] = None ,  lowerCAmelCase_			:		Optional[bool] = None ,  )							->						Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:
     __lowerCAmelCase							     =     (
         output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
     )
     __lowerCAmelCase							     =     return_dict if return_dict is not None else self.config.use_return_dict

     if pixel_values is None:
        raise ValueError('You have to specify pixel_values'					)

     __lowerCAmelCase							     =     self.conv_stem(lowerCAmelCase_					)

     __lowerCAmelCase							     =     () if output_hidden_states else None

     for i, layer_module in enumerate(self.layer					):
        __lowerCAmelCase							     =     layer_module(lowerCAmelCase_					)

        if output_hidden_states:
           __lowerCAmelCase							     =     all_hidden_states + (hidden_states,)

     __lowerCAmelCase							     =     hidden_states

     if self.pooler is not None:
        __lowerCAmelCase							     =     torch.flatten(self.pooler(lowerCAmelCase_					) ,  start_dim=1					)
     else:
        __lowerCAmelCase							     =     None

     if not return_dict:
        return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None					)

     return BaseModelOutputWithPoolingAndNoAttention(
         last_hidden_state=lowerCAmelCase_ ,  pooler_output=lowerCAmelCase_ ,  hidden_states=lowerCAmelCase_ ,  )





@add_start_docstrings(
    """
    MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    """		,   _UpperCamelCase		,   )
class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  def __init__(    self			:		List[Any] ,  lowerCAmelCase_			:		MobileNetVaConfig					)							->						None:
     super().__init__(lowerCAmelCase_					)

     __lowerCAmelCase							     =     config.num_labels
     __lowerCAmelCase							     =     MobileNetVaModel(lowerCAmelCase_					)

     __lowerCAmelCase							     =     self.mobilenet_va.layer[-1].convolution.out_channels

     # Classifier head
     __lowerCAmelCase							     =     nn.Dropout(config.classifier_dropout_prob ,  inplace=lowerCAmelCase_					)
     __lowerCAmelCase							     =     nn.Linear(lowerCAmelCase_ ,  config.num_labels					) if config.num_labels > 0 else nn.Identity()

     # Initialize weights and apply final processing
     self.post_init()






  @add_start_docstrings_to_model_forward(lowerCAmelCase_					)
  @add_code_sample_docstrings(
      checkpoint=_IMAGE_CLASS_CHECKPOINT ,  output_type=lowerCAmelCase_ ,  config_class=_CONFIG_FOR_DOC ,  expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT ,  )
  def     lowercase		(    self			:		str ,  lowerCAmelCase_			:		Optional[torch.Tensor] = None ,  lowerCAmelCase_			:		Optional[bool] = None ,  lowerCAmelCase_			:		Optional[torch.Tensor] = None ,  lowerCAmelCase_			:		Optional[bool] = None ,  )							->						Union[tuple, ImageClassifierOutputWithNoAttention]:
     __lowerCAmelCase							     =     return_dict if return_dict is not None else self.config.use_return_dict

     __lowerCAmelCase							     =     self.mobilenet_va(lowerCAmelCase_ ,  output_hidden_states=lowerCAmelCase_ ,  return_dict=lowerCAmelCase_					)

     __lowerCAmelCase							     =     outputs.pooler_output if return_dict else outputs[1]

     __lowerCAmelCase							     =     self.classifier(self.dropout(lowerCAmelCase_					)					)

     __lowerCAmelCase							     =     None
     if labels is not None:
        if self.config.problem_type is None:
           if self.num_labels == 1:
              __lowerCAmelCase							     =     'regression'
           elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
              __lowerCAmelCase							     =     'single_label_classification'
           else:
              __lowerCAmelCase							     =     'multi_label_classification'

        if self.config.problem_type == "regression":
           __lowerCAmelCase							     =     MSELoss()
           if self.num_labels == 1:
              __lowerCAmelCase							     =     loss_fct(logits.squeeze() ,  labels.squeeze()					)
           else:
              __lowerCAmelCase							     =     loss_fct(lowerCAmelCase_ ,  lowerCAmelCase_					)
        elif self.config.problem_type == "single_label_classification":
           __lowerCAmelCase							     =     CrossEntropyLoss()
           __lowerCAmelCase							     =     loss_fct(logits.view(-1 ,  self.num_labels					) ,  labels.view(-1					)					)
        elif self.config.problem_type == "multi_label_classification":
           __lowerCAmelCase							     =     BCEWithLogitsLoss()
           __lowerCAmelCase							     =     loss_fct(lowerCAmelCase_ ,  lowerCAmelCase_					)

     if not return_dict:
        __lowerCAmelCase							     =     (logits,) + outputs[2:]
        return ((loss,) + output) if loss is not None else output

     return ImageClassifierOutputWithNoAttention(
         loss=lowerCAmelCase_ ,  logits=lowerCAmelCase_ ,  hidden_states=outputs.hidden_states ,  )








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

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




     _snake_case  :   Union[str, Any]						   =  False


_snake_case  :   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					)							->						Union[str, Any]:
     __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			:		List[str] ,  lowerCAmelCase_			:		Pipeline ,  lowerCAmelCase_			:		str ,  lowerCAmelCase_			:		int ,  lowerCAmelCase_			:		int					)							->						str:
     __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			:		Tuple					)							->						str:
     run(self._app ,  host=self.host ,  port=self.port ,  workers=self.workers					)






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






  def     lowercase		(    self			:		int ,  lowerCAmelCase_			:		str = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					) ,  lowerCAmelCase_			:		bool = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					)					)							->						Dict:
     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			:		int ,  lowerCAmelCase_			:		List[int] = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					) ,  lowerCAmelCase_			:		bool = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					) ,  lowerCAmelCase_			:		bool = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					) ,  )							->						Union[str, 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			:		List[Any] ,  lowerCAmelCase_			:		Union[str, Any]=Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					)					)							->						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_					)}					)

