

"""simple docstring"""






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

	snake_case_ : Dict   =							True
except (ImportError, AttributeError):
	snake_case_ : Any   =							object



	def 	lowercase_						(     *_lowercase							:		Optional[Any]				,   **_lowercase							:		Any						):




						'''simple docstring'''






						pass




	snake_case_ : Tuple   =							False


snake_case_ : int   =							logging.get_logger("""transformers-cli/serving""")



def 	lowercase_						(     _lowercase							:		Namespace						):




					'''simple docstring'''






					UpperCAmelCase      : Optional[int]   =					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    snake_case__	(			__a  ):
			SCREAMING_SNAKE_CASE__  					=    42







class    snake_case__	(			__a  ):
			SCREAMING_SNAKE_CASE__  					=    42
			SCREAMING_SNAKE_CASE__  					=    42







class    snake_case__	(			__a  ):
			SCREAMING_SNAKE_CASE__  					=    42







class    snake_case__	(			__a  ):
			SCREAMING_SNAKE_CASE__  					=    42







class    snake_case__	(			__a  ):







			@staticmethod
			def       __lowerCAmelCase					(      lowercase      :			ArgumentParser	):





								'''simple docstring'''



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







			def __init__(      self      :			Tuple      ,	lowercase      :			Pipeline      ,	lowercase      :			str      ,	lowercase      :			int      ,	lowercase      :			int	):





								'''simple docstring'''



								UpperCAmelCase      : Optional[int]   =					pipeline

								UpperCAmelCase      : str   =					host
								UpperCAmelCase      : List[str]   =					port
								UpperCAmelCase      : Tuple   =					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      : List[str]   =					FastAPI(
													    routes=[
													        APIRoute(
													            "/"      ,	self.model_info      ,	response_model=_lowercase      ,	response_class=_lowercase      ,	methods=["GET"]      ,	),
													        APIRoute(
													            "/tokenize"      ,	self.tokenize      ,	response_model=_lowercase      ,	response_class=_lowercase      ,	methods=["POST"]      ,	),
													        APIRoute(
													            "/detokenize"      ,	self.detokenize      ,	response_model=_lowercase      ,	response_class=_lowercase      ,	methods=["POST"]      ,	),
													        APIRoute(
													            "/forward"      ,	self.forward      ,	response_model=_lowercase      ,	response_class=_lowercase      ,	methods=["POST"]      ,	),
													    ]      ,	timeout=6_00      ,	)







			def       __lowerCAmelCase					(      self      :			List[str]	):





								'''simple docstring'''



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







			def       __lowerCAmelCase					(      self      :			int	):





								'''simple docstring'''



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







			def       __lowerCAmelCase					(      self      :			Dict      ,	lowercase      :			str = Body(_lowercase      ,	embed=_lowercase	)      ,	lowercase      :			bool = Body(_lowercase      ,	embed=_lowercase	)	):





								'''simple docstring'''



								try:
													UpperCAmelCase      : List[str]   =					self._pipeline.tokenizer.tokenize(_lowercase	)

													if return_ids:
																		UpperCAmelCase      : Any   =					self._pipeline.tokenizer.convert_tokens_to_ids(_lowercase	)
																		return ServeTokenizeResult(tokens=_lowercase      ,	tokens_ids=_lowercase	)
													else:
																		return ServeTokenizeResult(tokens=_lowercase	)

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







			def       __lowerCAmelCase					(      self      :			List[Any]      ,	lowercase      :			List[int] = Body(_lowercase      ,	embed=_lowercase	)      ,	lowercase      :			bool = Body(_lowercase      ,	embed=_lowercase	)      ,	lowercase      :			bool = Body(_lowercase      ,	embed=_lowercase	)      ,	):





								'''simple docstring'''



								try:
													UpperCAmelCase      : List[str]   =					self._pipeline.tokenizer.decode(_lowercase      ,	_lowercase      ,	_lowercase	)
													return ServeDeTokenizeResult(model=""      ,	text=_lowercase	)
								except Exception as e:
													raise HTTPException(status_code=5_00      ,	detail={"model": "", "error": str(_lowercase	)}	)







			async def       __lowerCAmelCase					(      self      :			str      ,	lowercase      :			int=Body(_lowercase      ,	embed=_lowercase	)	):





								'''simple docstring'''



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

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



# coding=utf-8
# Copyright 2023 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# this script dumps information about the environment

import os
import platform
import sys


UpperCAmelCase_           =				'''3'''

print('''Python version:''', sys.version)

print('''OS platform:''', platform.platform())
print('''OS architecture:''', platform.machine())

try:
  import torch

  print('''Torch version:''', torch.__version__)
  print('''Cuda available:''', torch.cuda.is_available())
  print('''Cuda version:''', torch.version.cuda)
  print('''CuDNN version:''', torch.backends.cudnn.version())
  print('''Number of GPUs available:''', torch.cuda.device_count())
except ImportError:
  print('''Torch version:''', None)

try:
  import transformers

  print('''transformers version:''', transformers.__version__)
except ImportError:
  print('''transformers version:''', None)