




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						:							Optional[Any]							=  True
except (ImportError, AttributeError):
		__A						:							Union[str, Any]							=  object
		def 					lowerCamelCase_    (    *SCREAMING_SNAKE_CASE					,	**SCREAMING_SNAKE_CASE   ):
					'''simple docstring'''
					pass




		__A						:							List[str]							=  False


__A						:							Optional[int]							=  logging.get_logger("""transformers-cli/serving""")
def 					lowerCamelCase_    (    SCREAMING_SNAKE_CASE   ):
			'''simple docstring'''
			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(SCREAMING_SNAKE_CASE					,	args.host					,	args.port					,	args.workers   )




class 			UpperCAmelCase_      (   A       ):




							'''simple docstring'''



							a__						  =     42





class 			UpperCAmelCase_      (   A       ):




							'''simple docstring'''



							a__						  =     42
							a__						  =     42





class 			UpperCAmelCase_      (   A       ):




							'''simple docstring'''



							a__						  =     42





class 			UpperCAmelCase_      (   A       ):




							'''simple docstring'''



							a__						  =     42





class 			UpperCAmelCase_      (   A       ):




							'''simple docstring'''



							@staticmethod
							def    _UpperCAmelCase     (					a       :	ArgumentParser			)			->					Tuple:
										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=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       :	Any       ,     a       :	Pipeline       ,     a       :	str       ,     a       :	int       ,     a       :	int			)			->					Any:
										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=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    _UpperCAmelCase     (					self       :	Tuple			)			->					List[Any]:
										run(self._app       ,     host=self.host       ,     port=self.port       ,     workers=self.workers			)





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





							def    _UpperCAmelCase     (					self       :	List[str]       ,     a       :	str = Body(a       ,     embed=a			)       ,     a       :	bool = Body(a       ,     embed=a			)			)			->					Optional[int]:
										try:
													SCREAMING_SNAKE_CASE           =					self._pipeline.tokenizer.tokenize(a			)

													if return_ids:
																SCREAMING_SNAKE_CASE           =					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    _UpperCAmelCase     (					self       :	str       ,     a       :	List[int] = Body(a       ,     embed=a			)       ,     a       :	bool = Body(a       ,     embed=a			)       ,     a       :	bool = Body(a       ,     embed=a			)       ,     )			->					Dict:
										try:
													SCREAMING_SNAKE_CASE           =					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    _UpperCAmelCase     (					self       :	str       ,     a       :	int=Body(a       ,     embed=a			)			)			->					Tuple:

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

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












import os
import sys
import unittest


__A						:							Any							=  os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, """utils"""))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
__A						:							List[Any]							=  os.path.join(git_repo_path, """src""", """diffusers""")




class 			UpperCAmelCase_      (   unittest.TestCase       ):




							'''simple docstring'''



							def    _UpperCAmelCase     (					self       :	Any			)			->					Optional[Any]:
										SCREAMING_SNAKE_CASE           =					find_backend("""    if not is_torch_available():"""			)
										self.assertEqual(a       ,     """torch"""			)

										# backend_with_underscore = find_backend("    if not is_tensorflow_text_available():")
										# self.assertEqual(backend_with_underscore, "tensorflow_text")

										SCREAMING_SNAKE_CASE           =					find_backend("""    if not (is_torch_available() and is_transformers_available()):"""			)
										self.assertEqual(a       ,     """torch_and_transformers"""			)

										# double_backend_with_underscore = find_backend(
										#    "    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"
										# )
										# self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text")

										SCREAMING_SNAKE_CASE           =					find_backend(
										    """    if not (is_torch_available() and is_transformers_available() and is_onnx_available()):"""			)
										self.assertEqual(a       ,     """torch_and_transformers_and_onnx"""			)





							def    _UpperCAmelCase     (					self       :	List[str]			)			->					int:
										SCREAMING_SNAKE_CASE           =					read_init()
										# We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
										self.assertIn("""torch"""       ,     a			)
										self.assertIn("""torch_and_transformers"""       ,     a			)
										self.assertIn("""flax_and_transformers"""       ,     a			)
										self.assertIn("""torch_and_transformers_and_onnx"""       ,     a			)

										# Likewise, we can't assert on the exact content of a key
										self.assertIn("""UNet2DModel"""       ,     objects["""torch"""]			)
										self.assertIn("""FlaxUNet2DConditionModel"""       ,     objects["""flax"""]			)
										self.assertIn("""StableDiffusionPipeline"""       ,     objects["""torch_and_transformers"""]			)
										self.assertIn("""FlaxStableDiffusionPipeline"""       ,     objects["""flax_and_transformers"""]			)
										self.assertIn("""LMSDiscreteScheduler"""       ,     objects["""torch_and_scipy"""]			)
										self.assertIn("""OnnxStableDiffusionPipeline"""       ,     objects["""torch_and_transformers_and_onnx"""]			)





							def    _UpperCAmelCase     (					self       :	Any			)			->					int:
										SCREAMING_SNAKE_CASE           =					create_dummy_object("""CONSTANT"""       ,     """'torch'"""			)
										self.assertEqual(a       ,     """\nCONSTANT = None\n"""			)

										SCREAMING_SNAKE_CASE           =					create_dummy_object("""function"""       ,     """'torch'"""			)
										self.assertEqual(
										    a       ,     """\ndef function(*args, **kwargs):\n    requires_backends(function, 'torch')\n"""			)

										SCREAMING_SNAKE_CASE           =					"""
class FakeClass(metaclass=DummyObject):
    _backends = 'torch'

    def __init__(self, *args, **kwargs):
        requires_backends(self, 'torch')

    @classmethod
    def from_config(cls, *args, **kwargs):
        requires_backends(cls, 'torch')

    @classmethod
    def from_pretrained(cls, *args, **kwargs):
        requires_backends(cls, 'torch')
"""
										SCREAMING_SNAKE_CASE           =					create_dummy_object("""FakeClass"""       ,     """'torch'"""			)
										self.assertEqual(a       ,     a			)







							def    _UpperCAmelCase     (					self       :	Union[str, Any]			)			->					Union[str, Any]:
										SCREAMING_SNAKE_CASE           =					"""# This file is autogenerated by the command `make fix-copies`, do not edit.
from ..utils import DummyObject, requires_backends


CONSTANT = None


def function(*args, **kwargs):
    requires_backends(function, [\"torch\"])


class FakeClass(metaclass=DummyObject):
    _backends = [\"torch\"]

    def __init__(self, *args, **kwargs):
        requires_backends(self, [\"torch\"])

    @classmethod
    def from_config(cls, *args, **kwargs):
        requires_backends(cls, [\"torch\"])

    @classmethod
    def from_pretrained(cls, *args, **kwargs):
        requires_backends(cls, [\"torch\"])
"""
										SCREAMING_SNAKE_CASE           =					create_dummy_files({"""torch""": ["""CONSTANT""", """function""", """FakeClass"""]}			)
										self.assertEqual(dummy_files["""torch"""]       ,     a			)






