




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

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


						def 					_a							(					*lowerCAmelCase    ,						**lowerCAmelCase )->	List[str]:
								pass




						SCREAMING_SNAKE_CASE:     int									= False


SCREAMING_SNAKE_CASE:     List[str]									= logging.get_logger('''transformers-cli/serving''')


def 					_a							(					lowerCAmelCase )->	int:
		SCREAMING_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(lowerCAmelCase    ,						args.host    ,						args.port    ,						args.workers )



class    lowercase_		(SCREAMING_SNAKE_CASE__ ):
						lowerCAmelCase__			      =42



class    lowercase_		(SCREAMING_SNAKE_CASE__ ):
						lowerCAmelCase__			      =42
						lowerCAmelCase__			      =42



class    lowercase_		(SCREAMING_SNAKE_CASE__ ):
						lowerCAmelCase__			      =42



class    lowercase_		(SCREAMING_SNAKE_CASE__ ):
						lowerCAmelCase__			      =42



class    lowercase_		(SCREAMING_SNAKE_CASE__ ):



						@staticmethod
						def 	__a					(			snake_case__	:   ArgumentParser  ):

								"""simple docstring"""

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



						def __init__(			self	:   List[Any]					,							snake_case__	:   Pipeline					,							snake_case__	:   str					,							snake_case__	:   int					,							snake_case__	:   int  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							pipeline

								SCREAMING_SNAKE_CASE_					      =							host
								SCREAMING_SNAKE_CASE_					      =							port
								SCREAMING_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}'''  )
										SCREAMING_SNAKE_CASE_					      =							FastAPI(
										    routes=[
										        APIRoute(
										            '/'					,							self.model_info					,							response_model=snake_case__					,							response_class=snake_case__					,							methods=['GET']					,							),
										        APIRoute(
										            '/tokenize'					,							self.tokenize					,							response_model=snake_case__					,							response_class=snake_case__					,							methods=['POST']					,							),
										        APIRoute(
										            '/detokenize'					,							self.detokenize					,							response_model=snake_case__					,							response_class=snake_case__					,							methods=['POST']					,							),
										        APIRoute(
										            '/forward'					,							self.forward					,							response_model=snake_case__					,							response_class=snake_case__					,							methods=['POST']					,							),
										    ]					,							timeout=6_00					,							)



						def 	__a					(			self	:   Optional[Any]  ):

								"""simple docstring"""

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



						def 	__a					(			self	:   Tuple  ):

								"""simple docstring"""

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



						def 	__a					(			self	:   List[Any]					,							snake_case__	:   str = Body(snake_case__					,							embed=snake_case__  )					,							snake_case__	:   bool = Body(snake_case__					,							embed=snake_case__  )  ):

								"""simple docstring"""

								try:
										SCREAMING_SNAKE_CASE_					      =							self._pipeline.tokenizer.tokenize(snake_case__  )

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

								except Exception as e:
										raise HTTPException(status_code=5_00					,							detail={'model': '', 'error': str(snake_case__  )}  )



						def 	__a					(			self	:   Tuple					,							snake_case__	:   List[int] = Body(snake_case__					,							embed=snake_case__  )					,							snake_case__	:   bool = Body(snake_case__					,							embed=snake_case__  )					,							snake_case__	:   bool = Body(snake_case__					,							embed=snake_case__  )					,							):

								"""simple docstring"""

								try:
										SCREAMING_SNAKE_CASE_					      =							self._pipeline.tokenizer.decode(snake_case__					,							snake_case__					,							snake_case__  )
										return ServeDeTokenizeResult(model=''					,							text=snake_case__  )
								except Exception as e:
										raise HTTPException(status_code=5_00					,							detail={'model': '', 'error': str(snake_case__  )}  )






						async def 	__a					(			self	:   Any					,							snake_case__	:   List[str]=Body(snake_case__					,							embed=snake_case__  )  ):

								"""simple docstring"""

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

								try:
										# Forward through the model
										SCREAMING_SNAKE_CASE_					      =							self._pipeline(snake_case__  )
										return ServeForwardResult(output=snake_case__  )
								except Exception as e:
										raise HTTPException(5_00					,							{'error': str(snake_case__  )}  )





import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
						from PIL import Image
else:



						class    lowercase_		:



												@staticmethod
												def 	__a					(			*snake_case__	:   List[Any]					,							**snake_case__	:   List[Any]  ):

														"""simple docstring"""

														pass



@is_pipeline_test
@require_vision
@require_torch
class    lowercase_		(unittest.TestCase ):
						lowerCAmelCase__			      =MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING



						def 	__a					(			self	:   Any					,							snake_case__	:   List[str]					,							snake_case__	:   int					,							snake_case__	:   Optional[int]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							pipeline(
								    'zero-shot-object-detection'					,							model='hf-internal-testing/tiny-random-owlvit-object-detection'  )

								SCREAMING_SNAKE_CASE_					      =							[
								    {
								        'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
								        'candidate_labels': ['cat', 'remote', 'couch'],
								    }
								]
								return object_detector, examples



						def 	__a					(			self	:   List[Any]					,							snake_case__	:   Union[str, Any]					,							snake_case__	:   Union[str, Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							object_detector(examples[0]					,							threshold=0.0  )

								SCREAMING_SNAKE_CASE_					      =							len(snake_case__  )
								self.assertGreater(snake_case__					,							0  )
								self.assertEqual(
								    snake_case__					,							[
								        {
								            'score': ANY(snake_case__  ),
								            'label': ANY(snake_case__  ),
								            'box': {'xmin': ANY(snake_case__  ), 'ymin': ANY(snake_case__  ), 'xmax': ANY(snake_case__  ), 'ymax': ANY(snake_case__  )},
								        }
								        for i in range(snake_case__  )
								    ]					,							)



						@require_tf
						@unittest.skip('Zero Shot Object Detection not implemented in TF'  )
						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								pass



						@require_torch
						def 	__a					(			self	:   Tuple  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							pipeline(
								    'zero-shot-object-detection'					,							model='hf-internal-testing/tiny-random-owlvit-object-detection'  )

								SCREAMING_SNAKE_CASE_					      =							object_detector(
								    './tests/fixtures/tests_samples/COCO/000000039769.png'					,							candidate_labels=['cat', 'remote', 'couch']					,							threshold=0.64					,							)

								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        {'score': 0.72_35, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.72_18, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.71_84, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.67_48, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.66_56, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.66_14, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.64_56, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								        {'score': 0.6_42, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
								        {'score': 0.64_19, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								    ]					,							)

								SCREAMING_SNAKE_CASE_					      =							object_detector(
								    [
								        {
								            'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
								            'candidate_labels': ['cat', 'remote', 'couch'],
								        }
								    ]					,							threshold=0.64					,							)

								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        [
								            {'score': 0.72_35, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.72_18, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.71_84, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.67_48, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.66_56, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.66_14, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.64_56, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								            {'score': 0.6_42, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
								            {'score': 0.64_19, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								        ]
								    ]					,							)



						@require_torch
						@slow
						def 	__a					(			self	:   Optional[Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							pipeline('zero-shot-object-detection'  )

								SCREAMING_SNAKE_CASE_					      =							object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,							candidate_labels=['cat', 'remote', 'couch']					,							)
								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								        {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								        {'score': 0.14_74, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								        {'score': 0.12_08, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								    ]					,							)

								SCREAMING_SNAKE_CASE_					      =							object_detector(
								    [
								        {
								            'image': 'http://images.cocodataset.org/val2017/000000039769.jpg',
								            'candidate_labels': ['cat', 'remote', 'couch'],
								        },
								        {
								            'image': 'http://images.cocodataset.org/val2017/000000039769.jpg',
								            'candidate_labels': ['cat', 'remote', 'couch'],
								        },
								    ]					,							)
								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        [
								            {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								            {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								            {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								            {'score': 0.14_74, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								            {'score': 0.12_08, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								        ],
								        [
								            {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								            {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								            {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								            {'score': 0.14_74, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								            {'score': 0.12_08, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								        ],
								    ]					,							)



						@require_tf
						@unittest.skip('Zero Shot Object Detection not implemented in TF'  )
						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								pass



						@require_torch
						@slow
						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							0.2
								SCREAMING_SNAKE_CASE_					      =							pipeline('zero-shot-object-detection'  )

								SCREAMING_SNAKE_CASE_					      =							object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,							candidate_labels=['cat', 'remote', 'couch']					,							threshold=snake_case__					,							)
								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								        {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								    ]					,							)






						@require_torch
						@slow
						def 	__a					(			self	:   Tuple  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							2
								SCREAMING_SNAKE_CASE_					      =							pipeline('zero-shot-object-detection'  )

								SCREAMING_SNAKE_CASE_					      =							object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,							candidate_labels=['cat', 'remote', 'couch']					,							top_k=snake_case__					,							)
								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								    ]					,							)