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






	def 							UpperCAmelCase_		(							*__SCREAMING_SNAKE_CASE ,							**__SCREAMING_SNAKE_CASE							):
		pass





	UpperCAmelCase	      = False


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






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE							):
	lowercase       =    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        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     dict







class        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     List[str]
	_UpperCamelCase     :     Optional[List[int]]







class        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     str







class        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     Any







class        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


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





	def __init__(       self		,    snake_case		,    snake_case		,    snake_case		,    snake_case       ):
		lowercase       =    pipeline

		lowercase       =    host
		lowercase       =    port
		lowercase       =    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}'''       )
			lowercase       =    FastAPI(
			    routes=[
			        APIRoute(
			            '/'		,    self.model_info		,    response_model=snake_case		,    response_class=snake_case		,    methods=['GET']		,    ),
			        APIRoute(
			            '/tokenize'		,    self.tokenize		,    response_model=snake_case		,    response_class=snake_case		,    methods=['POST']		,    ),
			        APIRoute(
			            '/detokenize'		,    self.detokenize		,    response_model=snake_case		,    response_class=snake_case		,    methods=['POST']		,    ),
			        APIRoute(
			            '/forward'		,    self.forward		,    response_model=snake_case		,    response_class=snake_case		,    methods=['POST']		,    ),
			    ]		,    timeout=600		,    )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		run(self._app		,    host=self.host		,    port=self.port		,    workers=self.workers       )





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





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case = Body(snake_case		,    embed=snake_case       )		,    snake_case = Body(snake_case		,    embed=snake_case       )       ):
		try:
			lowercase       =    self._pipeline.tokenizer.tokenize(snake_case       )

			if return_ids:
				lowercase       =    self._pipeline.tokenizer.convert_tokens_to_ids(snake_case       )
				return ServeTokenizeResult(tokens=snake_case		,    tokens_ids=snake_case       )
			else:
				return ServeTokenizeResult(tokens=snake_case       )

		except Exception as e:
			raise HTTPException(status_code=500		,    detail={'model': '', 'error': str(snake_case       )}       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case = Body(snake_case		,    embed=snake_case       )		,    snake_case = Body(snake_case		,    embed=snake_case       )		,    snake_case = Body(snake_case		,    embed=snake_case       )		,    ):
		try:
			lowercase       =    self._pipeline.tokenizer.decode(snake_case		,    snake_case		,    snake_case       )
			return ServeDeTokenizeResult(model=''		,    text=snake_case       )
		except Exception as e:
			raise HTTPException(status_code=500		,    detail={'model': '', 'error': str(snake_case       )}       )





	async def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case=Body(snake_case		,    embed=snake_case       )       ):

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

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



import re

import jax.numpy as jnp
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.random import PRNGKey

from ..utils import logging


UpperCAmelCase	      = logging.get_logger(__name__)






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE							):
	lowercase       =    r'\w+[.]\d+'
	lowercase       =    re.findall(__SCREAMING_SNAKE_CASE ,							__SCREAMING_SNAKE_CASE							)
	for pat in pats:
		lowercase       =    key.replace(__SCREAMING_SNAKE_CASE ,							'_'.join(pat.split('.'							)							)							)
	return key






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE ,							__SCREAMING_SNAKE_CASE ,							__SCREAMING_SNAKE_CASE							):
	lowercase       =    pt_tuple_key[:-1] + ('scale',)
	if (
	    any('norm' in str_ for str_ in pt_tuple_key							)
	    and (pt_tuple_key[-1] == "bias")
	    and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict)
	    and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict)
	):
		lowercase       =    pt_tuple_key[:-1] + ('scale',)
		return renamed_pt_tuple_key, pt_tensor
	elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict:
		lowercase       =    pt_tuple_key[:-1] + ('scale',)
		return renamed_pt_tuple_key, pt_tensor

	# embedding
	if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict:
		lowercase       =    pt_tuple_key[:-1] + ('embedding',)
		return renamed_pt_tuple_key, pt_tensor

	# conv layer
	lowercase       =    pt_tuple_key[:-1] + ('kernel',)
	if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4:
		lowercase       =    pt_tensor.transpose(2 ,							3 ,							1 ,							0							)
		return renamed_pt_tuple_key, pt_tensor

	# linear layer
	lowercase       =    pt_tuple_key[:-1] + ('kernel',)
	if pt_tuple_key[-1] == "weight":
		lowercase       =    pt_tensor.T
		return renamed_pt_tuple_key, pt_tensor

	# old PyTorch layer norm weight
	lowercase       =    pt_tuple_key[:-1] + ('weight',)
	if pt_tuple_key[-1] == "gamma":
		return renamed_pt_tuple_key, pt_tensor

	# old PyTorch layer norm bias
	lowercase       =    pt_tuple_key[:-1] + ('bias',)
	if pt_tuple_key[-1] == "beta":
		return renamed_pt_tuple_key, pt_tensor

	return pt_tuple_key, pt_tensor






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE ,							__SCREAMING_SNAKE_CASE ,							__SCREAMING_SNAKE_CASE=42							):
	# Step 1: Convert pytorch tensor to numpy
	lowercase       =    {k: v.numpy() for k, v in pt_state_dict.items()}

	# Step 2: Since the model is stateless, get random Flax params
	lowercase       =    flax_model.init_weights(PRNGKey(__SCREAMING_SNAKE_CASE							)							)

	lowercase       =    flatten_dict(__SCREAMING_SNAKE_CASE							)
	lowercase       =    {}

	# Need to change some parameters name to match Flax names
	for pt_key, pt_tensor in pt_state_dict.items():
		lowercase       =    rename_key(__SCREAMING_SNAKE_CASE							)
		lowercase       =    tuple(renamed_pt_key.split('.'							)							)

		# Correctly rename weight parameters
		lowercase  ,					lowercase       =    rename_key_and_reshape_tensor(__SCREAMING_SNAKE_CASE ,							__SCREAMING_SNAKE_CASE ,							__SCREAMING_SNAKE_CASE							)

		if flax_key in random_flax_state_dict:
			if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
				raise ValueError(
				    F'''PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape '''
				    F'''{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.'''							)

        # also add unexpected weight so that warning is thrown
		lowercase       =    jnp.asarray(__SCREAMING_SNAKE_CASE							)

	return unflatten_dict(__SCREAMING_SNAKE_CASE							)

