


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    :   int	    =					True
except (ImportError, AttributeError):
     snake_case    :   List[Any]	    =					object
     def     __lowerCamelCase						(			*UpperCAmelCase_						:		Optional[Any]			,  **UpperCAmelCase_						:		str						):




           """simple docstring"""





           pass






     snake_case    :   Any	    =					False


snake_case    :   int	    =					logging.get_logger('''transformers-cli/serving''')
def     __lowerCamelCase						(			UpperCAmelCase_						:		Namespace						):




      """simple docstring"""





      a						:Dict										=	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 		_snake_case  (				_snake_case							):
 SCREAMING_SNAKE_CASE__				=	42







class 		_snake_case  (				_snake_case							):
 SCREAMING_SNAKE_CASE__				=	42
 SCREAMING_SNAKE_CASE__				=	42







class 		_snake_case  (				_snake_case							):
 SCREAMING_SNAKE_CASE__				=	42







class 		_snake_case  (				_snake_case							):
 SCREAMING_SNAKE_CASE__				=	42







class 		_snake_case  (				_snake_case							):




 @staticmethod
 def 	SCREAMING_SNAKE_CASE__    (		_lowerCamelCase   ):
       a						:List[str]										=	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=8888 ,  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 ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase ,  _lowerCamelCase   ):
       a						:Optional[int]										=	pipeline

       a						:Optional[Any]										=	host
       a						:Optional[Any]										=	port
       a						: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}'''   )
             a						:Any										=	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=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 ,  _lowerCamelCase = Body(_lowerCamelCase ,  embed=_lowerCamelCase   ) ,  _lowerCamelCase = Body(_lowerCamelCase ,  embed=_lowerCamelCase   )   ):
       try:
             a						:Any										=	self._pipeline.tokenizer.tokenize(_lowerCamelCase   )

             if return_ids:
                   a						:List[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=500 ,  detail={'''model''': '''''', '''error''': str(_lowerCamelCase   )}   )




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




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

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

       try:
             # Forward through the model
             a						:Optional[int]										=	self._pipeline(_lowerCamelCase   )
             return ServeForwardResult(output=_lowerCamelCase   )
       except Exception as e:
             raise HTTPException(500 ,  {'''error''': str(_lowerCamelCase   )}   )









import os

import pytest

from datasets import (
    get_dataset_config_info,
    get_dataset_config_names,
    get_dataset_infos,
    get_dataset_split_names,
    inspect_dataset,
    inspect_metric,
)


snake_case    :   Any	    =					pytest.mark.integration
@pytest.mark.parametrize('''path'''			,  ['''paws''', '''csv''']						)
def     __lowerCamelCase						(			UpperCAmelCase_						:		Optional[int]			,  UpperCAmelCase_						:		Optional[Any]						):




      """simple docstring"""





      inspect_dataset(UpperCAmelCase_			,  UpperCAmelCase_						)
      a						:List[Any]										=	path + '''.py'''
      assert script_name in os.listdir(UpperCAmelCase_						)
      assert "__pycache__" not in os.listdir(UpperCAmelCase_						)
@pytest.mark.filterwarnings('''ignore:inspect_metric is deprecated:FutureWarning'''						)
@pytest.mark.filterwarnings('''ignore:metric_module_factory is deprecated:FutureWarning'''						)
@pytest.mark.parametrize('''path'''			,  ['''accuracy''']						)
def     __lowerCamelCase						(			UpperCAmelCase_						:		List[Any]			,  UpperCAmelCase_						:		Any						):




      """simple docstring"""





      inspect_metric(UpperCAmelCase_			,  UpperCAmelCase_						)
      a						:Dict										=	path + '''.py'''
      assert script_name in os.listdir(UpperCAmelCase_						)
      assert "__pycache__" not in os.listdir(UpperCAmelCase_						)
@pytest.mark.parametrize(
    '''path, config_name, expected_splits'''			,  [
        ('''squad''', '''plain_text''', ['''train''', '''validation''']),
        ('''dalle-mini/wit''', '''dalle-mini--wit''', ['''train''']),
        ('''paws''', '''labeled_final''', ['''train''', '''test''', '''validation''']),
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		Optional[int]			,  UpperCAmelCase_						:		List[Any]			,  UpperCAmelCase_						:		str						):




      """simple docstring"""





      a						:List[str]										=	get_dataset_config_info(UpperCAmelCase_			,  config_name=UpperCAmelCase_						)
      assert info.config_name == config_name
      assert list(info.splits.keys()						) == expected_splits
@pytest.mark.parametrize(
    '''path, config_name, expected_exception'''			,  [
        ('''paws''', None, ValueError),
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		List[Any]			,  UpperCAmelCase_						:		str			,  UpperCAmelCase_						:		Tuple						):




      """simple docstring"""





      with pytest.raises(UpperCAmelCase_						):
            get_dataset_config_info(UpperCAmelCase_			,  config_name=UpperCAmelCase_						)
@pytest.mark.parametrize(
    '''path, expected'''			,  [
        ('''squad''', '''plain_text'''),
        ('''acronym_identification''', '''default'''),
        ('''lhoestq/squad''', '''plain_text'''),
        ('''lhoestq/test''', '''default'''),
        ('''lhoestq/demo1''', '''lhoestq--demo1'''),
        ('''dalle-mini/wit''', '''dalle-mini--wit'''),
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		Tuple			,  UpperCAmelCase_						:		int						):




      """simple docstring"""





      a						:List[str]										=	get_dataset_config_names(UpperCAmelCase_						)
      assert expected in config_names
@pytest.mark.parametrize(
    '''path, expected_configs, expected_splits_in_first_config'''			,  [
        ('''squad''', ['''plain_text'''], ['''train''', '''validation''']),
        ('''dalle-mini/wit''', ['''dalle-mini--wit'''], ['''train''']),
        ('''paws''', ['''labeled_final''', '''labeled_swap''', '''unlabeled_final'''], ['''train''', '''test''', '''validation''']),
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		Tuple			,  UpperCAmelCase_						:		Tuple			,  UpperCAmelCase_						:		Optional[int]						):




      """simple docstring"""





      a						:Optional[int]										=	get_dataset_infos(UpperCAmelCase_						)
      assert list(infos.keys()						) == expected_configs
      a						:Union[str, Any]										=	expected_configs[0]
      assert expected_config in infos
      a						:List[Any]										=	infos[expected_config]
      assert info.config_name == expected_config
      assert list(info.splits.keys()						) == expected_splits_in_first_config
@pytest.mark.parametrize(
    '''path, expected_config, expected_splits'''			,  [
        ('''squad''', '''plain_text''', ['''train''', '''validation''']),
        ('''dalle-mini/wit''', '''dalle-mini--wit''', ['''train''']),
        ('''paws''', '''labeled_final''', ['''train''', '''test''', '''validation''']),
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		Optional[int]			,  UpperCAmelCase_						:		Any			,  UpperCAmelCase_						:		Optional[int]						):




      """simple docstring"""





      a						:Union[str, Any]										=	get_dataset_infos(UpperCAmelCase_						)
      assert expected_config in infos
      a						:int										=	infos[expected_config]
      assert info.config_name == expected_config
      assert list(info.splits.keys()						) == expected_splits






@pytest.mark.parametrize(
    '''path, config_name, expected_exception'''			,  [
        ('''paws''', None, ValueError),
    ]			,  )
def     __lowerCamelCase						(			UpperCAmelCase_						:		Union[str, Any]			,  UpperCAmelCase_						:		Optional[int]			,  UpperCAmelCase_						:		str						):




      """simple docstring"""





      with pytest.raises(UpperCAmelCase_						):
            get_dataset_split_names(UpperCAmelCase_			,  config_name=UpperCAmelCase_						)





