

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




   def   _A  (  *UpperCamelCase_					:	Tuple,      **UpperCamelCase_					:	Tuple)     ->						Dict:
      '''simple docstring'''


      pass

   _a							=							False


_a							=							logging.get_logger('transformers-cli/serving')




def   _A  (  UpperCamelCase_					:	Namespace)     ->						Any:
   '''simple docstring'''


   __lowercase								=						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  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 __UpperCAmelCase     : dict







class  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 __UpperCAmelCase     : List[str]
 __UpperCAmelCase     : Optional[List[int]]







class  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 __UpperCAmelCase     : str







class  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 __UpperCAmelCase     : Any







class  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 @staticmethod
 def  _lowercase			(      UpperCAmelCase__							:      ArgumentParser     ):
    __lowercase								=						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=8_8_8_8, 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							:      str, UpperCAmelCase__							:      Pipeline, UpperCAmelCase__							:      str, UpperCAmelCase__							:      int, UpperCAmelCase__							:      int     ):
    __lowercase								=						pipeline

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


 def  _lowercase			(      self							:      int     ):
    run(self._app, host=self.host, port=self.port, workers=self.workers     )


 def  _lowercase			(      self							:      Any     ):
    return ServeModelInfoResult(infos=vars(self._pipeline.model.config     )     )


 def  _lowercase			(      self							:      Dict, UpperCAmelCase__							:      str = Body(UpperCAmelCase__, embed=UpperCAmelCase__     ), UpperCAmelCase__							:      bool = Body(UpperCAmelCase__, embed=UpperCAmelCase__     )     ):
    try:
       __lowercase								=						self._pipeline.tokenizer.tokenize(UpperCAmelCase__     )

       if return_ids:
          __lowercase								=						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=5_0_0, detail={"model": "", "error": str(UpperCAmelCase__     )}     )


 def  _lowercase			(      self							:      Tuple, UpperCAmelCase__							:      List[int] = Body(UpperCAmelCase__, embed=UpperCAmelCase__     ), UpperCAmelCase__							:      bool = Body(UpperCAmelCase__, embed=UpperCAmelCase__     ), UpperCAmelCase__							:      bool = Body(UpperCAmelCase__, embed=UpperCAmelCase__     ), ):
    try:
       __lowercase								=						self._pipeline.tokenizer.decode(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__     )
       return ServeDeTokenizeResult(model="", text=UpperCAmelCase__     )
    except Exception as e:
       raise HTTPException(status_code=5_0_0, detail={"model": "", "error": str(UpperCAmelCase__     )}     )



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







"""simple docstring"""





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


_a							=							logging.get_logger(__name__)

_a							=							{
    'MIT/ast-finetuned-audioset-10-10-0.4593': (
        'https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593/resolve/main/config.json'
    ),
}




class  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 __UpperCAmelCase     : Any       		=     "audio-spectrogram-transformer"


 def __init__(      self							:      Optional[Any], UpperCAmelCase__							:      Dict=7_6_8, UpperCAmelCase__							:      Optional[Any]=1_2, UpperCAmelCase__							:      Optional[int]=1_2, UpperCAmelCase__							:      List[str]=3_0_7_2, UpperCAmelCase__							:      List[str]="gelu", UpperCAmelCase__							:      Dict=0.0, UpperCAmelCase__							:      str=0.0, UpperCAmelCase__							:      Any=0.02, UpperCAmelCase__							:      Any=1E-12, UpperCAmelCase__							:      Dict=1_6, UpperCAmelCase__							:      Optional[int]=True, UpperCAmelCase__							:      Union[str, Any]=1_0, UpperCAmelCase__							:      List[Any]=1_0, UpperCAmelCase__							:      Optional[int]=1_0_2_4, UpperCAmelCase__							:      List[str]=1_2_8, **UpperCAmelCase__							:      List[Any], ):
    super().__init__(**UpperCAmelCase__     )

    __lowercase								=						hidden_size
    __lowercase								=						num_hidden_layers
    __lowercase								=						num_attention_heads
    __lowercase								=						intermediate_size
    __lowercase								=						hidden_act
    __lowercase								=						hidden_dropout_prob
    __lowercase								=						attention_probs_dropout_prob
    __lowercase								=						initializer_range
    __lowercase								=						layer_norm_eps
    __lowercase								=						patch_size
    __lowercase								=						qkv_bias
    __lowercase								=						frequency_stride
    __lowercase								=						time_stride
    __lowercase								=						max_length
    __lowercase								=						num_mel_bins




