






import unittest

import numpy as np
import requests

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
  import torch

  from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
  SCREAMING_SNAKE_CASE 							=						False

if is_vision_available():
  from PIL import Image

  from transformers import PixaStructImageProcessor
class 							A_    (						unittest.TestCase			):






 '''simple docstring'''




 def __init__(	self							,  _A							,  _A=7							,  _A=3							,  _A=18							,  _A=30							,  _A=400							,  _A=None							,  _A=True							,  _A=True							,  _A=None							,  ) ->		List[str]:

     """simple docstring"""



     _UpperCAmelCase				:      Union[str, Any]			  =				size if size is not None else {'''height''': 20, '''width''': 20}
     _UpperCAmelCase				:      Any			  =				parent
     _UpperCAmelCase				:      List[Any]			  =				batch_size
     _UpperCAmelCase				:      Optional[int]			  =				num_channels
     _UpperCAmelCase				:      Dict			  =				image_size
     _UpperCAmelCase				:      Dict			  =				min_resolution
     _UpperCAmelCase				:      Optional[int]			  =				max_resolution
     _UpperCAmelCase				:      Union[str, Any]			  =				size
     _UpperCAmelCase				:      Union[str, Any]			  =				do_normalize
     _UpperCAmelCase				:      Any			  =				do_convert_rgb
     _UpperCAmelCase				:      Tuple			  =				[512, 1024, 2048, 4096]
     _UpperCAmelCase				:      List[str]			  =				patch_size if patch_size is not None else {'''height''': 16, '''width''': 16}


 def  snake_case__				(	self) ->		List[Any]:

     """simple docstring"""



     return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}


 def  snake_case__				(	self) ->		Optional[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      Tuple			  =				'''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg'''
     _UpperCAmelCase				:      Tuple			  =				Image.open(requests.get(__lowerCAmelCase							,  stream=__lowerCAmelCase).raw).convert('''RGB''')
     return raw_image



@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11 ,   reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`." ,   )
@require_torch
@require_vision
class 							A_    (						__lowercase ,   unittest.TestCase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			List[Any] =					PixaStructImageProcessor if is_vision_available() else None


 def  snake_case__				(	self) ->		List[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[int]			  =				PixaStructImageProcessingTester(self)


 @property
 def  snake_case__				(	self) ->		List[Any]:

     """simple docstring"""



     return self.image_processor_tester.prepare_image_processor_dict()


 def  snake_case__				(	self) ->		str:

     """simple docstring"""



     _UpperCAmelCase				:      Union[str, Any]			  =				self.image_processing_class(**self.image_processor_dict)
     self.assertTrue(hasattr(__lowerCAmelCase							,  '''do_normalize'''))
     self.assertTrue(hasattr(__lowerCAmelCase							,  '''do_convert_rgb'''))


 def  snake_case__				(	self) ->		Optional[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      Union[str, Any]			  =				self.image_processor_tester.prepare_dummy_image()

     _UpperCAmelCase				:      str			  =				self.image_processing_class(**self.image_processor_dict)
     _UpperCAmelCase				:      int			  =				2048

     _UpperCAmelCase				:      List[Any]			  =				image_processor(__lowerCAmelCase							,  return_tensors='''pt'''							,  max_patches=__lowerCAmelCase)
     self.assertTrue(torch.allclose(inputs.flattened_patches.mean()							,  torch.tensor(0.0606)							,  atol=1e-3							,  rtol=1e-3))


 def  snake_case__				(	self) ->		str:

     """simple docstring"""



     _UpperCAmelCase				:      int			  =				self.image_processing_class(**self.image_processor_dict)
     # create random PIL images
     _UpperCAmelCase				:      str			  =				prepare_image_inputs(self.image_processor_tester							,  equal_resolution=__lowerCAmelCase)
     for image in image_inputs:
         self.assertIsInstance(__lowerCAmelCase							,  Image.Image)

     # Test not batched input
     _UpperCAmelCase				:      str			  =				(
         (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
         * self.image_processor_tester.num_channels
     ) + 2

     for max_patch in self.image_processor_tester.max_patches:
         # Test not batched input
         _UpperCAmelCase				:      Union[str, Any]			  =				image_processor(
             image_inputs[0]							,  return_tensors='''pt'''							,  max_patches=__lowerCAmelCase).flattened_patches
         self.assertEqual(
             encoded_images.shape							,  (1, max_patch, expected_hidden_dim)							,  )

         # Test batched
         _UpperCAmelCase				:      List[str]			  =				image_processor(
             __lowerCAmelCase							,  return_tensors='''pt'''							,  max_patches=__lowerCAmelCase).flattened_patches
         self.assertEqual(
             encoded_images.shape							,  (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,  )


 def  snake_case__				(	self) ->		List[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      Tuple			  =				self.image_processing_class(**self.image_processor_dict)
     # create random PIL images
     _UpperCAmelCase				:      List[Any]			  =				prepare_image_inputs(self.image_processor_tester							,  equal_resolution=__lowerCAmelCase)
     for image in image_inputs:
         self.assertIsInstance(__lowerCAmelCase							,  Image.Image)

     # Test not batched input
     _UpperCAmelCase				:      List[Any]			  =				(
         (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
         * self.image_processor_tester.num_channels
     ) + 2

     _UpperCAmelCase				:      str			  =				True

     for max_patch in self.image_processor_tester.max_patches:
         # Test not batched input
         with self.assertRaises(__lowerCAmelCase):
             _UpperCAmelCase				:      Optional[Any]			  =				image_processor(
                 image_inputs[0]							,  return_tensors='''pt'''							,  max_patches=__lowerCAmelCase).flattened_patches

         _UpperCAmelCase				:      int			  =				'''Hello'''

         _UpperCAmelCase				:      Dict			  =				image_processor(
             image_inputs[0]							,  return_tensors='''pt'''							,  max_patches=__lowerCAmelCase							,  header_text=__lowerCAmelCase).flattened_patches
         self.assertEqual(
             encoded_images.shape							,  (1, max_patch, expected_hidden_dim)							,  )

         # Test batched
         _UpperCAmelCase				:      Union[str, Any]			  =				image_processor(
             __lowerCAmelCase							,  return_tensors='''pt'''							,  max_patches=__lowerCAmelCase							,  header_text=__lowerCAmelCase).flattened_patches
         self.assertEqual(
             encoded_images.shape							,  (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,  )


 def  snake_case__				(	self) ->		Optional[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[Any]			  =				self.image_processing_class(**self.image_processor_dict)
     # create random numpy tensors
     _UpperCAmelCase				:      Optional[int]			  =				prepare_image_inputs(self.image_processor_tester							,  equal_resolution=__lowerCAmelCase							,  numpify=__lowerCAmelCase)
     for image in image_inputs:
         self.assertIsInstance(__lowerCAmelCase							,  np.ndarray)

     _UpperCAmelCase				:      Dict			  =				(
         (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
         * self.image_processor_tester.num_channels
     ) + 2

     for max_patch in self.image_processor_tester.max_patches:
         # Test not batched input
         _UpperCAmelCase				:      Optional[Any]			  =				image_processor(
             image_inputs[0]							,  return_tensors='''pt'''							,  max_patches=__lowerCAmelCase).flattened_patches
         self.assertEqual(
             encoded_images.shape							,  (1, max_patch, expected_hidden_dim)							,  )

         # Test batched
         _UpperCAmelCase				:      Union[str, Any]			  =				image_processor(
             __lowerCAmelCase							,  return_tensors='''pt'''							,  max_patches=__lowerCAmelCase).flattened_patches
         self.assertEqual(
             encoded_images.shape							,  (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,  )


 def  snake_case__				(	self) ->		Any:

     """simple docstring"""



     _UpperCAmelCase				:      List[Any]			  =				self.image_processing_class(**self.image_processor_dict)
     # create random PyTorch tensors
     _UpperCAmelCase				:      Tuple			  =				prepare_image_inputs(self.image_processor_tester							,  equal_resolution=__lowerCAmelCase							,  torchify=__lowerCAmelCase)
     for image in image_inputs:
         self.assertIsInstance(__lowerCAmelCase							,  torch.Tensor)

     # Test not batched input
     _UpperCAmelCase				:      List[Any]			  =				(
         (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
         * self.image_processor_tester.num_channels
     ) + 2

     for max_patch in self.image_processor_tester.max_patches:
         # Test not batched input
         _UpperCAmelCase				:      Any			  =				image_processor(
             image_inputs[0]							,  return_tensors='''pt'''							,  max_patches=__lowerCAmelCase).flattened_patches
         self.assertEqual(
             encoded_images.shape							,  (1, max_patch, expected_hidden_dim)							,  )

         # Test batched
         _UpperCAmelCase				:      int			  =				image_processor(
             __lowerCAmelCase							,  return_tensors='''pt'''							,  max_patches=__lowerCAmelCase).flattened_patches
         self.assertEqual(
             encoded_images.shape							,  (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,  )



@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11 ,   reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`." ,   )
@require_torch
@require_vision
class 							A_    (						__lowercase ,   unittest.TestCase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			Tuple =					PixaStructImageProcessor if is_vision_available() else None


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

     """simple docstring"""



     _UpperCAmelCase				:      List[str]			  =				PixaStructImageProcessingTester(self							,  num_channels=4)
     _UpperCAmelCase				:      Tuple			  =				3


 @property
 def  snake_case__				(	self) ->		List[Any]:

     """simple docstring"""



     return self.image_processor_tester.prepare_image_processor_dict()


 def  snake_case__				(	self) ->		Any:

     """simple docstring"""



     _UpperCAmelCase				:      Union[str, Any]			  =				self.image_processing_class(**self.image_processor_dict)
     self.assertTrue(hasattr(__lowerCAmelCase							,  '''do_normalize'''))
     self.assertTrue(hasattr(__lowerCAmelCase							,  '''do_convert_rgb'''))


 def  snake_case__				(	self) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      Any			  =				self.image_processing_class(**self.image_processor_dict)
     # create random PIL images
     _UpperCAmelCase				:      List[str]			  =				prepare_image_inputs(self.image_processor_tester							,  equal_resolution=__lowerCAmelCase)
     for image in image_inputs:
         self.assertIsInstance(__lowerCAmelCase							,  Image.Image)

     # Test not batched input
     _UpperCAmelCase				:      int			  =				(
         (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
         * (self.image_processor_tester.num_channels - 1)
     ) + 2

     for max_patch in self.image_processor_tester.max_patches:
         # Test not batched input
         _UpperCAmelCase				:      int			  =				image_processor(
             image_inputs[0]							,  return_tensors='''pt'''							,  max_patches=__lowerCAmelCase).flattened_patches
         self.assertEqual(
             encoded_images.shape							,  (1, max_patch, expected_hidden_dim)							,  )

         # Test batched
         _UpperCAmelCase				:      Dict			  =				image_processor(
             __lowerCAmelCase							,  return_tensors='''pt'''							,  max_patches=__lowerCAmelCase).flattened_patches
         self.assertEqual(
             encoded_images.shape							,  (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,  )








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 							=						True
except (ImportError, AttributeError):
  SCREAMING_SNAKE_CASE 							=						object

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


  SCREAMING_SNAKE_CASE 							=						False


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

def      _lowerCamelCase	(						__A    :							Namespace )      ->					Tuple:
    _UpperCAmelCase				:      Any			  =				pipeline(
        task=args.task		,		model=args.model if args.model else None		,		config=args.config		,		tokenizer=args.tokenizer		,		device=args.device		,		)
    return ServeCommand(__A		,		args.host		,		args.port		,		args.workers )
class 							A_    (						__lowercase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			dict



class 							A_    (						__lowercase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			List[str]
 _SCREAMING_SNAKE_CASE							:			Optional[List[int]]



class 							A_    (						__lowercase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			str



class 							A_    (						__lowercase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			Any



class 							A_    (						__lowercase			):






 '''simple docstring'''




 @staticmethod
 def  snake_case__				(	_A) ->		List[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      Union[str, Any]			  =				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=8888							,  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							,  _A							,  _A							,  _A							,  _A) ->		List[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      List[Any]			  =				pipeline

     _UpperCAmelCase				:      List[Any]			  =				host
     _UpperCAmelCase				:      Tuple			  =				port
     _UpperCAmelCase				:      Union[str, 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}''')
         _UpperCAmelCase				:      str			  =				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  snake_case__				(	self) ->		Optional[Any]:

     """simple docstring"""



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


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

     """simple docstring"""



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


 def  snake_case__				(	self							,  _A = Body(_A							,  embed=_A)							,  _A = Body(_A							,  embed=_A)) ->		List[Any]:

     """simple docstring"""



     try:
         _UpperCAmelCase				:      int			  =				self._pipeline.tokenizer.tokenize(_A)

         if return_ids:
             _UpperCAmelCase				:      List[Any]			  =				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  snake_case__				(	self							,  _A = Body(_A							,  embed=_A)							,  _A = Body(_A							,  embed=_A)							,  _A = Body(_A							,  embed=_A)							,  ) ->		str:

     """simple docstring"""



     try:
         _UpperCAmelCase				:      Optional[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  snake_case__				(	self							,  _A=Body(_A							,  embed=_A)) ->		str:

     """simple docstring"""



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

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




