






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      :       int				  =   True
except (ImportError, AttributeError):
  __a      :       Optional[int]				  =   object



  def 			_SCREAMING_SNAKE_CASE						( *__lowercase   :			Tuple			,				**__lowercase   :			Optional[int]    )						->      List[str]:





      """simple docstring"""

      pass






  __a      :       str				  =   False


__a      :       str				  =   logging.get_logger("transformers-cli/serving")



def 			_SCREAMING_SNAKE_CASE						( __lowercase   :			Namespace    )						->      List[str]:





    """simple docstring"""

    __A         =	pipeline(
        task=args.task			,				model=args.model if args.model else None			,				config=args.config			,				tokenizer=args.tokenizer			,				device=args.device			,				)
    return ServeCommand(__lowercase			,				args.host			,				args.port			,				args.workers    )


class       __lowercase      (	lowercase_  ):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE   						=				42


class       __lowercase      (	lowercase_  ):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE   						=				42
     SCREAMING_SNAKE_CASE   						=				42


class       __lowercase      (	lowercase_  ):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE   						=				42


class       __lowercase      (	lowercase_  ):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE   						=				42


class       __lowercase      (	lowercase_  ):







     '''simple docstring'''




     @staticmethod
     def 		lowerCAmelCase_     (				UpperCamelCase_       :			ArgumentParser				):


         """simple docstring"""



         __A         =	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_888 ,	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       :			Dict ,	UpperCamelCase_       :			Pipeline ,	UpperCamelCase_       :			str ,	UpperCamelCase_       :			int ,	UpperCamelCase_       :			int				):


         """simple docstring"""



         __A         =	pipeline

         __A         =	host
         __A         =	port
         __A         =	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}"				)
             __A         =	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=600 ,	)




     def 		lowerCAmelCase_     (				self       :			int				):


         """simple docstring"""



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




     def 		lowerCAmelCase_     (				self       :			List[str]				):


         """simple docstring"""



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




     def 		lowerCAmelCase_     (				self       :			Dict ,	UpperCamelCase_       :			str = Body(UpperCamelCase_ ,	embed=UpperCamelCase_				) ,	UpperCamelCase_       :			bool = Body(UpperCamelCase_ ,	embed=UpperCamelCase_				)				):


         """simple docstring"""



         try:
             __A         =	self._pipeline.tokenizer.tokenize(UpperCamelCase_				)

             if return_ids:
                 __A         =	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=500 ,	detail={"""model""": """""", """error""": str(UpperCamelCase_				)}				)




     def 		lowerCAmelCase_     (				self       :			Optional[Any] ,	UpperCamelCase_       :			List[int] = Body(UpperCamelCase_ ,	embed=UpperCamelCase_				) ,	UpperCamelCase_       :			bool = Body(UpperCamelCase_ ,	embed=UpperCamelCase_				) ,	UpperCamelCase_       :			bool = Body(UpperCamelCase_ ,	embed=UpperCamelCase_				) ,	):


         """simple docstring"""



         try:
             __A         =	self._pipeline.tokenizer.decode(UpperCamelCase_ ,	UpperCamelCase_ ,	UpperCamelCase_				)
             return ServeDeTokenizeResult(model="""""" ,	text=UpperCamelCase_				)
         except Exception as e:
             raise HTTPException(status_code=500 ,	detail={"""model""": """""", """error""": str(UpperCamelCase_				)}				)





     async def 		lowerCAmelCase_     (				self       :			Any ,	UpperCamelCase_       :			List[Any]=Body(UpperCamelCase_ ,	embed=UpperCamelCase_				)				):


         """simple docstring"""



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

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












from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
  from PIL import Image

  from transformers import ViTImageProcessor


class       __lowercase      :







     '''simple docstring'''




     def __init__(				self       :			Tuple ,	UpperCamelCase_       :			Tuple ,	UpperCamelCase_       :			Union[str, Any]=13 ,	UpperCamelCase_       :			Optional[Any]=30 ,	UpperCamelCase_       :			Optional[int]=2 ,	UpperCamelCase_       :			Optional[Any]=3 ,	UpperCamelCase_       :			int=True ,	UpperCamelCase_       :			str=True ,	UpperCamelCase_       :			str=32 ,	UpperCamelCase_       :			Tuple=2 ,	UpperCamelCase_       :			List[Any]=4 ,	UpperCamelCase_       :			List[str]=37 ,	UpperCamelCase_       :			Any="gelu" ,	UpperCamelCase_       :			Any=0.1 ,	UpperCamelCase_       :			Optional[Any]=0.1 ,	UpperCamelCase_       :			Any=10 ,	UpperCamelCase_       :			str=0.02 ,	UpperCamelCase_       :			Dict=3 ,	UpperCamelCase_       :			int=None ,	):


         """simple docstring"""



         __A         =	parent
         __A         =	batch_size
         __A         =	image_size
         __A         =	patch_size
         __A         =	num_channels
         __A         =	is_training
         __A         =	use_labels
         __A         =	hidden_size
         __A         =	num_hidden_layers
         __A         =	num_attention_heads
         __A         =	intermediate_size
         __A         =	hidden_act
         __A         =	hidden_dropout_prob
         __A         =	attention_probs_dropout_prob
         __A         =	type_sequence_label_size
         __A         =	initializer_range
         __A         =	scope

         # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
         __A         =	(image_size // patch_size) ** 2
         __A         =	num_patches + 1




     def 		lowerCAmelCase_     (				self       :			Optional[Any]				):


         """simple docstring"""



         __A         =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

         __A         =	None
         if self.use_labels:
             __A         =	ids_tensor([self.batch_size] ,	self.type_sequence_label_size				)

         __A         =	self.get_config()

         return config, pixel_values, labels




     def 		lowerCAmelCase_     (				self       :			int				):


         """simple docstring"""



         return ViTConfig(
             image_size=self.image_size ,	patch_size=self.patch_size ,	num_channels=self.num_channels ,	hidden_size=self.hidden_size ,	num_hidden_layers=self.num_hidden_layers ,	num_attention_heads=self.num_attention_heads ,	intermediate_size=self.intermediate_size ,	hidden_act=self.hidden_act ,	hidden_dropout_prob=self.hidden_dropout_prob ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob ,	is_decoder=UpperCamelCase_ ,	initializer_range=self.initializer_range ,	)




     def 		lowerCAmelCase_     (				self       :			Tuple ,	UpperCamelCase_       :			int ,	UpperCamelCase_       :			List[str] ,	UpperCamelCase_       :			Optional[int]				):


         """simple docstring"""



         __A         =	TFViTModel(config=UpperCamelCase_				)
         __A         =	model(UpperCamelCase_ ,	training=UpperCamelCase_				)
         self.parent.assertEqual(result.last_hidden_state.shape ,	(self.batch_size, self.seq_length, self.hidden_size)				)

         # Test with an image with different size than the one specified in config.
         __A         =	self.image_size // 2
         __A         =	pixel_values[:, :, :image_size, :image_size]
         __A         =	model(UpperCamelCase_ ,	interpolate_pos_encoding=UpperCamelCase_ ,	training=UpperCamelCase_				)
         __A         =	(image_size // self.patch_size) ** 2 + 1
         self.parent.assertEqual(result.last_hidden_state.shape ,	(self.batch_size, seq_length, self.hidden_size)				)




     def 		lowerCAmelCase_     (				self       :			Tuple ,	UpperCamelCase_       :			Union[str, Any] ,	UpperCamelCase_       :			Optional[Any] ,	UpperCamelCase_       :			List[Any]				):


         """simple docstring"""



         __A         =	self.type_sequence_label_size
         __A         =	TFViTForImageClassification(UpperCamelCase_				)
         __A         =	model(UpperCamelCase_ ,	labels=UpperCamelCase_ ,	training=UpperCamelCase_				)
         self.parent.assertEqual(result.logits.shape ,	(self.batch_size, self.type_sequence_label_size)				)

         # Test with an image with different size than the one specified in config.
         __A         =	self.image_size // 2
         __A         =	pixel_values[:, :, :image_size, :image_size]
         __A         =	model(UpperCamelCase_ ,	interpolate_pos_encoding=UpperCamelCase_ ,	training=UpperCamelCase_				)
         self.parent.assertEqual(result.logits.shape ,	(self.batch_size, self.type_sequence_label_size)				)

         # test greyscale images
         __A         =	1
         __A         =	TFViTForImageClassification(UpperCamelCase_				)
         __A         =	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
         __A         =	model(UpperCamelCase_				)
         self.parent.assertEqual(result.logits.shape ,	(self.batch_size, self.type_sequence_label_size)				)





     def 		lowerCAmelCase_     (				self       :			Optional[int]				):


         """simple docstring"""



         __A         =	self.prepare_config_and_inputs()
         __A    ,	__A    ,	__A         =	config_and_inputs
         __A         =	{"""pixel_values""": pixel_values}
         return config, inputs_dict


@require_tf
class       __lowercase      (	lowercase_			,      lowercase_			,      unittest.TestCase  ):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE   						=				(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
     SCREAMING_SNAKE_CASE   						=				(
         {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
         if is_tf_available()
         else {}
     )

     SCREAMING_SNAKE_CASE   						=				False
     SCREAMING_SNAKE_CASE   						=				False
     SCREAMING_SNAKE_CASE   						=				False




     def 		lowerCAmelCase_     (				self       :			List[Any]				):


         """simple docstring"""



         __A         =	TFViTModelTester(self				)
         __A         =	ConfigTester(self ,	config_class=UpperCamelCase_ ,	has_text_modality=UpperCamelCase_ ,	hidden_size=37				)




     def 		lowerCAmelCase_     (				self       :			Tuple				):


         """simple docstring"""



         self.config_tester.run_common_tests()




     @unittest.skip(reason="""ViT does not use inputs_embeds"""				)
     def 		lowerCAmelCase_     (				self       :			Dict				):


         """simple docstring"""



         pass




     @unittest.skip(reason="""ViT does not use inputs_embeds"""				)
     def 		lowerCAmelCase_     (				self       :			Any				):


         """simple docstring"""



         pass




     def 		lowerCAmelCase_     (				self       :			Tuple				):


         """simple docstring"""



         __A    ,	__A         =	self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             __A         =	model_class(UpperCamelCase_				)
             self.assertIsInstance(model.get_input_embeddings() ,	(tf.keras.layers.Layer)				)
             __A         =	model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(UpperCamelCase_ ,	tf.keras.layers.Layer				)				)




     def 		lowerCAmelCase_     (				self       :			List[str]				):


         """simple docstring"""



         __A    ,	__A         =	self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             __A         =	model_class(UpperCamelCase_				)
             __A         =	inspect.signature(model.call				)
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             __A         =	[*signature.parameters.keys()]

             __A         =	["""pixel_values"""]
             self.assertListEqual(arg_names[:1] ,	UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			Optional[Any]				):


         """simple docstring"""



         __A         =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			str				):


         """simple docstring"""



         __A         =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_				)





     @slow
     def 		lowerCAmelCase_     (				self       :			Any				):


         """simple docstring"""



         __A         =	TFViTModel.from_pretrained("""google/vit-base-patch16-224"""				)
         self.assertIsNotNone(UpperCamelCase_				)



def 			_SCREAMING_SNAKE_CASE						( )						->      Union[str, Any]:





    """simple docstring"""

    __A         =	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""    )
    return image






@require_tf
@require_vision
class       __lowercase      (	unittest.TestCase  ):







     '''simple docstring'''




     @cached_property
     def 		lowerCAmelCase_     (				self       :			List[Any]				):


         """simple docstring"""



         return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224"""				) if is_vision_available() else None





     @slow
     def 		lowerCAmelCase_     (				self       :			Optional[Any]				):


         """simple docstring"""



         __A         =	TFViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""				)

         __A         =	self.default_image_processor
         __A         =	prepare_img()
         __A         =	image_processor(images=UpperCamelCase_ ,	return_tensors="""tf"""				)

         # forward pass
         __A         =	model(**UpperCamelCase_				)

         # verify the logits
         __A         =	tf.TensorShape((1, 1_000)				)
         self.assertEqual(outputs.logits.shape ,	UpperCamelCase_				)

         __A         =	tf.constant([-0.2744, 0.8215, -0.0836]				)

         tf.debugging.assert_near(outputs.logits[0, :3] ,	UpperCamelCase_ ,	atol=1e-4				)




