


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

    __lowerCamelCase      :Optional[Any]	    =		True
except (ImportError, AttributeError):
    __lowerCamelCase      :List[Any]	    =		object




    def   snake_case		(     *UpperCamelCase__       :						List[Any]			,				**UpperCamelCase__       :						Any    )   ->      Any:
          pass






    __lowerCamelCase      :Dict	    =		False


__lowerCamelCase      :Union[str, Any]	    =		logging.get_logger('transformers-cli/serving')




def   snake_case		(     UpperCamelCase__       :						Namespace    )   ->      int:
      lowerCamelCase					:					Dict        =	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__	(						__lowercase):


     """simple docstring"""
     snake_case__	:					dict


class 			A__	(						__lowercase):


     """simple docstring"""
     snake_case__	:					List[str]
     snake_case__	:					Optional[List[int]]


class 			A__	(						__lowercase):


     """simple docstring"""
     snake_case__	:					str


class 			A__	(						__lowercase):


     """simple docstring"""
     snake_case__	:					Any


class 			A__	(						__lowercase):


     """simple docstring"""

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

     def __init__(	self:						Union[str, Any]     ,		__a:						Pipeline     ,		__a:						str     ,		__a:						int     ,		__a:						int	)->    Optional[Any]:
           lowerCamelCase					:					List[str]        =	pipeline

           lowerCamelCase					:					int        =	host
           lowerCamelCase					:					Tuple        =	port
           lowerCamelCase					:					Dict        =	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					:					List[Any]        =	FastAPI(
                     routes=[
                         APIRoute(
                             """/"""     ,		self.model_info     ,		response_model=__a     ,		response_class=__a     ,		methods=["""GET"""]     ,		),
                         APIRoute(
                             """/tokenize"""     ,		self.tokenize     ,		response_model=__a     ,		response_class=__a     ,		methods=["""POST"""]     ,		),
                         APIRoute(
                             """/detokenize"""     ,		self.detokenize     ,		response_model=__a     ,		response_class=__a     ,		methods=["""POST"""]     ,		),
                         APIRoute(
                             """/forward"""     ,		self.forward     ,		response_model=__a     ,		response_class=__a     ,		methods=["""POST"""]     ,		),
                     ]     ,		timeout=600     ,		)

     def 		a__						(	self:						Dict	)->    List[str]:
           run(self._app     ,		host=self.host     ,		port=self.port     ,		workers=self.workers	)

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

     def 		a__						(	self:						Tuple     ,		__a:						str = Body(__a     ,		embed=__a	)     ,		__a:						bool = Body(__a     ,		embed=__a	)	)->    Optional[int]:
           try:
                 lowerCamelCase					:					int        =	self._pipeline.tokenizer.tokenize(__a	)

                 if return_ids:
                       lowerCamelCase					:					Dict        =	self._pipeline.tokenizer.convert_tokens_to_ids(__a	)
                       return ServeTokenizeResult(tokens=__a     ,		tokens_ids=__a	)
                 else:
                       return ServeTokenizeResult(tokens=__a	)

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

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







     async def 		a__						(	self:						Any     ,		__a:						Union[str, Any]=Body(__a     ,		embed=__a	)	)->    Any:

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

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








"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


__lowerCamelCase      :List[str]	    =		{
    'configuration_owlvit': [
        'OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'OwlViTConfig',
        'OwlViTOnnxConfig',
        'OwlViTTextConfig',
        'OwlViTVisionConfig',
    ],
    'processing_owlvit': ['OwlViTProcessor'],
}


try:
    if not is_vision_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __lowerCamelCase      :Optional[int]	    =		['OwlViTFeatureExtractor']
    __lowerCamelCase      :List[str]	    =		['OwlViTImageProcessor']

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __lowerCamelCase      :Optional[Any]	    =		[
        'OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST',
        'OwlViTModel',
        'OwlViTPreTrainedModel',
        'OwlViTTextModel',
        'OwlViTVisionModel',
        'OwlViTForObjectDetection',
    ]

if TYPE_CHECKING:
    from .configuration_owlvit import (
        OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP,
        OwlViTConfig,
        OwlViTOnnxConfig,
        OwlViTTextConfig,
        OwlViTVisionConfig,
    )
    from .processing_owlvit import OwlViTProcessor

    try:
        if not is_vision_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .feature_extraction_owlvit import OwlViTFeatureExtractor
        from .image_processing_owlvit import OwlViTImageProcessor

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_owlvit import (
            OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
            OwlViTForObjectDetection,
            OwlViTModel,
            OwlViTPreTrainedModel,
            OwlViTTextModel,
            OwlViTVisionModel,
        )

else:
    import sys

    __lowerCamelCase      :Dict	    =		_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




