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

      UpperCamelCase__											=					True
except (ImportError, AttributeError):
      UpperCamelCase__											=					object




      def    lowerCAmelCase_ (     *__A,  **__A							)     ->				List[Any]:







            '''simple docstring'''


            pass







      UpperCamelCase__											=					False


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




def    lowerCAmelCase_ (     __A							)     ->				Dict:







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





class      A							(				UpperCAmelCase_     ):
   __UpperCAmelCase : dict





class      A							(				UpperCAmelCase_     ):
   __UpperCAmelCase : List[str]
   __UpperCAmelCase : Optional[List[int]]





class      A							(				UpperCAmelCase_     ):
   __UpperCAmelCase : str





class      A							(				UpperCAmelCase_     ):
   __UpperCAmelCase : Any





class      A							(				UpperCAmelCase_     ):


   @staticmethod
   def 			lowercase_      (__UpperCAmelCase     :						ArgumentParser		)						->				int:
         """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=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     :						Optional[int]       ,  __UpperCAmelCase     :						Pipeline       ,  __UpperCAmelCase     :						str       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						int		)						->				Union[str, Any]:
         """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=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		)						->				Tuple:
         """simple docstring"""



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


   def 			lowercase_      (self     :						List[str]		)						->				int:
         """simple docstring"""



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


   def 			lowercase_      (self     :						Dict       ,  __UpperCAmelCase     :						str = Body(lowerCAmelCase__       ,  embed=lowerCAmelCase__		)       ,  __UpperCAmelCase     :						bool = Body(lowerCAmelCase__       ,  embed=lowerCAmelCase__		)		)						->				int:
         """simple docstring"""



         try:
               UpperCAmelCase__								=  self._pipeline.tokenizer.tokenize(lowerCAmelCase__		)

               if return_ids:
                     UpperCAmelCase__								=  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     :						str       ,  __UpperCAmelCase     :						List[int] = Body(lowerCAmelCase__       ,  embed=lowerCAmelCase__		)       ,  __UpperCAmelCase     :						bool = Body(lowerCAmelCase__       ,  embed=lowerCAmelCase__		)       ,  __UpperCAmelCase     :						bool = Body(lowerCAmelCase__       ,  embed=lowerCAmelCase__		)       ,  )						->				List[str]:
         """simple docstring"""



         try:
               UpperCAmelCase__								=  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[str]       ,  __UpperCAmelCase     :						Optional[int]=Body(lowerCAmelCase__       ,  embed=lowerCAmelCase__		)		)						->				List[str]:
         """simple docstring"""



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

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






from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
						if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.25.0""")):
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline
else:
						from .pipeline_unclip import UnCLIPPipeline
						from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline
						from .text_proj import UnCLIPTextProjModel






