


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    :						str     =							True
except (ImportError, AttributeError):
    __UpperCamelCase    :						Union[str, Any]     =							object





    def   _UpperCAmelCase       (     *UpperCAmelCase      :			Dict						,	**UpperCAmelCase      :			str      ):
          """simple docstring"""




          pass




    __UpperCamelCase    :						Union[str, Any]     =							False


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





def   _UpperCAmelCase       (     UpperCAmelCase      :			Namespace      ):
      """simple docstring"""




      __lowerCamelCase     :      List[Any]       						=						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 							_UpperCamelCase (						A						):

   '''simple docstring'''







   a_				:							dict



class 							_UpperCamelCase (						A						):

   '''simple docstring'''







   a_				:							List[str]
   a_				:							Optional[List[int]]



class 							_UpperCamelCase (						A						):

   '''simple docstring'''







   a_				:							str



class 							_UpperCamelCase (						A						):

   '''simple docstring'''







   a_				:							Any



class 							_UpperCamelCase (						A						):

   '''simple docstring'''







   @staticmethod
   def   _snake_case    (  _lowerCamelCase  : ArgumentParser							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						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  : Dict       ,  _lowerCamelCase  : Pipeline       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						pipeline

         __lowerCamelCase     :      List[str]       						=						host
         __lowerCamelCase     :      Optional[Any]       						=						port
         __lowerCamelCase     :      List[Any]       						=						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     :      Dict       						=						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   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



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

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



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

   def   _snake_case    (  self  : Any       ,  _lowerCamelCase  : str = Body(_lowerCamelCase       ,  embed=_lowerCamelCase							)       ,  _lowerCamelCase  : bool = Body(_lowerCamelCase       ,  embed=_lowerCamelCase							)							):






         '''simple docstring'''



         try:
               __lowerCamelCase     :      Optional[int]       						=						self._pipeline.tokenizer.tokenize(_lowerCamelCase							)

               if return_ids:
                     __lowerCamelCase     :      Optional[Any]       						=						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   _snake_case    (  self  : List[Any]       ,  _lowerCamelCase  : List[int] = Body(_lowerCamelCase       ,  embed=_lowerCamelCase							)       ,  _lowerCamelCase  : bool = Body(_lowerCamelCase       ,  embed=_lowerCamelCase							)       ,  _lowerCamelCase  : bool = Body(_lowerCamelCase       ,  embed=_lowerCamelCase							)       ,  ):






         '''simple docstring'''



         try:
               __lowerCamelCase     :      Tuple       						=						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   _snake_case    (  self  : Dict       ,  _lowerCamelCase  : Tuple=Body(_lowerCamelCase       ,  embed=_lowerCamelCase							)							):






         '''simple docstring'''



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

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







import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
    from PIL import Image

    from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
    import torch

if is_tf_available():
    import tensorflow as tf



@require_vision
@require_torchvision
class 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      Union[str, Any]       						=						SamImageProcessor()
         __lowerCamelCase     :      int       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : Tuple       ,  **_lowerCamelCase  : Any							):






         '''simple docstring'''



         return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowerCamelCase							).image_processor

   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

         __lowerCamelCase     :      Optional[int]       						=						[Image.fromarray(np.moveaxis(_lowerCamelCase       ,  0       ,  -1							)							) for x in image_inputs]

         return image_inputs

   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=self.get_image_processor()							)
         processor.save_pretrained(self.tmpdirname							)

         __lowerCamelCase     :      Optional[int]       						=						self.get_image_processor(do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         __lowerCamelCase     :      Optional[Any]       						=						SamProcessor.from_pretrained(self.tmpdirname       ,  do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         self.assertEqual(processor.image_processor.to_json_string()       ,  image_processor_add_kwargs.to_json_string()							)
         self.assertIsInstance(processor.image_processor       ,  _lowerCamelCase							)

   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						self.get_image_processor()

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      Optional[Any]       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      List[str]       						=						image_processor(_lowerCamelCase       ,  return_tensors="""np"""							)
         __lowerCamelCase     :      List[Any]       						=						processor(images=_lowerCamelCase       ,  return_tensors="""np"""							)

         input_feat_extract.pop("""original_sizes"""							)  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop("""reshaped_input_sizes"""							)  # pop original_sizes as it is popped in the processor

         for key in input_feat_extract.keys():
               self.assertAlmostEqual(input_feat_extract[key].sum()       ,  input_processor[key].sum()       ,  delta=1E-2							)


   @require_torch
   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						self.get_image_processor()

         __lowerCamelCase     :      Union[str, Any]       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      Union[str, Any]       						=						[torch.ones((1, 3, 5, 5)							)]

         __lowerCamelCase     :      Any       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      Any       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      Tuple       						=						processor.post_process_masks(_lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Union[str, Any]       						=						processor.post_process_masks(
             _lowerCamelCase       ,  torch.tensor(_lowerCamelCase							)       ,  torch.tensor(_lowerCamelCase							)							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         # should also work with np
         __lowerCamelCase     :      Any       						=						[np.ones((1, 3, 5, 5)							)]
         __lowerCamelCase     :      int       						=						processor.post_process_masks(_lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)							)

         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      List[str]       						=						[[1, 0], [0, 1]]
         with self.assertRaises(_lowerCamelCase							):
               __lowerCamelCase     :      Union[str, Any]       						=						processor.post_process_masks(_lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)							)



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

   '''simple docstring'''







   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[Any]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      int       						=						SamImageProcessor()
         __lowerCamelCase     :      Dict       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : List[str]       ,  **_lowerCamelCase  : List[Any]							):






         '''simple docstring'''



         return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowerCamelCase							).image_processor

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : List[str]							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

         __lowerCamelCase     :      str       						=						[Image.fromarray(np.moveaxis(_lowerCamelCase       ,  0       ,  -1							)							) for x in image_inputs]

         return image_inputs

   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						SamProcessor(image_processor=self.get_image_processor()							)
         processor.save_pretrained(self.tmpdirname							)

         __lowerCamelCase     :      Dict       						=						self.get_image_processor(do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         __lowerCamelCase     :      Tuple       						=						SamProcessor.from_pretrained(self.tmpdirname       ,  do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         self.assertEqual(processor.image_processor.to_json_string()       ,  image_processor_add_kwargs.to_json_string()							)
         self.assertIsInstance(processor.image_processor       ,  _lowerCamelCase							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						self.get_image_processor()

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      str       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      List[Any]       						=						image_processor(_lowerCamelCase       ,  return_tensors="""np"""							)
         __lowerCamelCase     :      Union[str, Any]       						=						processor(images=_lowerCamelCase       ,  return_tensors="""np"""							)

         input_feat_extract.pop("""original_sizes"""							)  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop("""reshaped_input_sizes"""							)  # pop reshaped_input_sizes as it is popped in the processor

         for key in input_feat_extract.keys():
               self.assertAlmostEqual(input_feat_extract[key].sum()       ,  input_processor[key].sum()       ,  delta=1E-2							)


   @require_tf
   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						self.get_image_processor()

         __lowerCamelCase     :      str       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      Any       						=						[tf.ones((1, 3, 5, 5)							)]

         __lowerCamelCase     :      Optional[Any]       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      List[str]       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      Optional[int]       						=						processor.post_process_masks(_lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase       ,  return_tensors="""tf"""							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Optional[Any]       						=						processor.post_process_masks(
             _lowerCamelCase       ,  tf.convert_to_tensor(_lowerCamelCase							)       ,  tf.convert_to_tensor(_lowerCamelCase							)       ,  return_tensors="""tf"""       ,  )
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         # should also work with np
         __lowerCamelCase     :      Tuple       						=						[np.ones((1, 3, 5, 5)							)]
         __lowerCamelCase     :      str       						=						processor.post_process_masks(
             _lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)       ,  return_tensors="""tf"""							)

         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Union[str, Any]       						=						[[1, 0], [0, 1]]
         with self.assertRaises(tf.errors.InvalidArgumentError							):
               __lowerCamelCase     :      Optional[Any]       						=						processor.post_process_masks(
                   _lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)       ,  return_tensors="""tf"""							)



@require_vision
@require_torchvision
class 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      Union[str, Any]       						=						SamImageProcessor()
         __lowerCamelCase     :      Dict       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : Optional[int]       ,  **_lowerCamelCase  : Tuple							):






         '''simple docstring'''



         return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowerCamelCase							).image_processor

   def   _snake_case    (  self  : List[str]							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      List[str]       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

         __lowerCamelCase     :      str       						=						[Image.fromarray(np.moveaxis(_lowerCamelCase       ,  0       ,  -1							)							) for x in image_inputs]

         return image_inputs

   @is_pt_tf_cross_test
   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						self.get_image_processor()

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      str       						=						np.random.randint(0       ,  2       ,  size=(1, 3, 5, 5)							).astype(np.floataa							)
         __lowerCamelCase     :      Tuple       						=						[tf.convert_to_tensor(_lowerCamelCase							)]
         __lowerCamelCase     :      List[str]       						=						[torch.tensor(_lowerCamelCase							)]

         __lowerCamelCase     :      Optional[int]       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      Tuple       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      int       						=						processor.post_process_masks(
             _lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase       ,  return_tensors="""tf"""							)
         __lowerCamelCase     :      Any       						=						processor.post_process_masks(
             _lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase       ,  return_tensors="""pt"""							)

         self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()							)							)


   @is_pt_tf_cross_test
   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						self.get_image_processor()

         __lowerCamelCase     :      int       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      Any       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      Any       						=						image_processor(_lowerCamelCase       ,  return_tensors="""pt"""							)["""pixel_values"""].numpy()
         __lowerCamelCase     :      Any       						=						processor(images=_lowerCamelCase       ,  return_tensors="""pt"""							)["""pixel_values"""].numpy()

         __lowerCamelCase     :      Dict       						=						image_processor(_lowerCamelCase       ,  return_tensors="""tf"""							)["""pixel_values"""].numpy()
         __lowerCamelCase     :      int       						=						processor(images=_lowerCamelCase       ,  return_tensors="""tf"""							)["""pixel_values"""].numpy()

         self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase							)							)
         self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase							)							)
         self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase							)							)



