





import collections
import os
from typing import List, Optional, Tuple

from transformers.utils import is_jieba_available, requires_backends


if is_jieba_available():
     import jieba

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


_snake_case  :   Dict						   =  logging.get_logger(__name__)

_snake_case  :   Union[str, Any]						   =  {'vocab_file': 'vocab.txt'}

_snake_case  :   str						   =  {
    'vocab_file': {
        'openbmb/cpm-ant-10b': 'https://huggingface.co/openbmb/cpm-ant-10b/blob/main/vocab.txt',
    },
}

_snake_case  :   Dict						   =  {
    'openbmb/cpm-ant-10b': 1024,
}

def   a_   (			lowerCAmelCase_				:      int     ):
   __lowerCAmelCase							     =     collections.OrderedDict()
   with open(a_,     'r',     encoding='utf-8'     ) as reader:
      __lowerCAmelCase							     =     reader.readlines()
   for index, token in enumerate(a_     ):
      __lowerCAmelCase							     =     token.rstrip('\n'     )
      __lowerCAmelCase							     =     index
   return vocab



class     _UpperCAmelCase   (   a_   ):

  """simple docstring"""


  def __init__(    self			:		int ,  lowerCAmelCase_			:		Any ,  lowerCAmelCase_			:		int="<unk>" ,  lowerCAmelCase_			:		Dict=2_0_0					)							->						Union[str, Any]:
     __lowerCAmelCase							     =     vocab
     __lowerCAmelCase							     =     unk_token
     __lowerCAmelCase							     =     max_input_chars_per_word






  def     lowercase		(    self			:		Dict ,  lowerCAmelCase_			:		Any					)							->						Optional[Any]:
     __lowerCAmelCase							     =     list(lowercase_					)
     if len(lowercase_					) > self.max_input_chars_per_word:
        return [self.unk_token]

     __lowerCAmelCase							     =     0
     __lowerCAmelCase							     =     []
     while start < len(lowercase_					):
        __lowerCAmelCase							     =     len(lowercase_					)
        __lowerCAmelCase							     =     None
        while start < end:
           __lowerCAmelCase							     =     ''.join(chars[start:end]					)
           if substr in self.vocab:
              __lowerCAmelCase							     =     substr
              break
           end -= 1
        if cur_substr is None:
           sub_tokens.append(self.unk_token					)
           start += 1
        else:
           sub_tokens.append(lowercase_					)
           __lowerCAmelCase							     =     end

     return sub_tokens





class     _UpperCAmelCase   (   a_   ):

  """simple docstring"""


  a_      =			VOCAB_FILES_NAMES
  a_      =			PRETRAINED_VOCAB_FILES_MAP
  a_      =			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  a_      =			['''input_ids''', '''attention_mask''']
  a_      =			False






  def __init__(    self			:		Optional[Any] ,  lowerCAmelCase_			:		Any ,  lowerCAmelCase_			:		Any="<d>" ,  lowerCAmelCase_			:		Dict="</d>" ,  lowerCAmelCase_			:		Dict="<s>" ,  lowerCAmelCase_			:		List[Any]="</s>" ,  lowerCAmelCase_			:		str="<pad>" ,  lowerCAmelCase_			:		Tuple="<unk>" ,  lowerCAmelCase_			:		Optional[int]="</n>" ,  lowerCAmelCase_			:		List[str]="</_>" ,  lowerCAmelCase_			:		Tuple="left" ,  **lowerCAmelCase_			:		Optional[int] ,  )							->						str:
     requires_backends(self ,  ['jieba']					)
     super().__init__(
         bod_token=lowercase_ ,  eod_token=lowercase_ ,  bos_token=lowercase_ ,  eos_token=lowercase_ ,  pad_token=lowercase_ ,  unk_token=lowercase_ ,  line_token=lowercase_ ,  space_token=lowercase_ ,  padding_side=lowercase_ ,  **lowercase_ ,  )
     __lowerCAmelCase							     =     bod_token
     __lowerCAmelCase							     =     eod_token
     __lowerCAmelCase							     =     load_vocab(lowercase_					)
     __lowerCAmelCase							     =     self.encoder[space_token]
     __lowerCAmelCase							     =     self.encoder[line_token]

     del self.encoder[space_token]
     del self.encoder[line_token]

     __lowerCAmelCase							     =     collections.OrderedDict(sorted(self.encoder.items() ,  key=lambda lowerCAmelCase_					: x[1]					)					)
     __lowerCAmelCase							     =     {v: k for k, v in self.encoder.items()}

     __lowerCAmelCase							     =     WordpieceTokenizer(vocab=self.encoder ,  unk_token=self.unk_token					)






  @property
  def     lowercase		(    self			:		int					)							->						List[str]:
     return self.encoder[self.bod_token]






  @property
  def     lowercase		(    self			:		List[str]					)							->						List[str]:
     return self.encoder[self.eod_token]






  @property
  def     lowercase		(    self			:		List[str]					)							->						List[Any]:
     return self.encoder["\n"]






  @property
  def     lowercase		(    self			:		str					)							->						int:
     return len(self.encoder					)






  def     lowercase		(    self			:		Any					)							->						int:
     return dict(self.encoder ,  **self.added_tokens_encoder					)






  def     lowercase		(    self			:		int ,  lowerCAmelCase_			:		Tuple					)							->						Tuple:
     __lowerCAmelCase							     =     []
     for x in jieba.cut(lowercase_ ,  cut_all=lowercase_					):
        output_tokens.extend(self.wordpiece_tokenizer.tokenize(lowercase_					)					)
     return output_tokens






  def     lowercase		(    self			:		Any ,  lowerCAmelCase_			:		Dict ,  **lowerCAmelCase_			:		List[Any]					)							->						Dict:
     __lowerCAmelCase							     =     [i for i in token_ids if i >= 0]
     __lowerCAmelCase							     =     [
         x for x in token_ids if x != self.pad_token_id and x != self.eos_token_id and x != self.bos_token_id
     ]
     return super()._decode(lowercase_ ,  **lowercase_					)






  def     lowercase		(    self			:		Any ,  lowerCAmelCase_			:		Tuple					)							->						List[str]:
     return token in self.encoder






  def     lowercase		(    self			:		Any ,  lowerCAmelCase_			:		int					)							->						str:
     return "".join(lowercase_					)






  def     lowercase		(    self			:		Any ,  lowerCAmelCase_			:		Union[str, Any]					)							->						Optional[Any]:
     return self.encoder.get(lowercase_ ,  self.encoder.get(self.unk_token					)					)






  def     lowercase		(    self			:		Dict ,  lowerCAmelCase_			:		str					)							->						int:
     return self.decoder.get(lowercase_ ,  self.unk_token					)






  def     lowercase		(    self			:		str ,  lowerCAmelCase_			:		Any ,  lowerCAmelCase_			:		Optional[int] = None					)							->						Tuple[str]:

     if os.path.isdir(lowercase_					):
        __lowerCAmelCase							     =     os.path.join(
            lowercase_ ,  (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']					)
     else:
        __lowerCAmelCase							     =     (filename_prefix + '-' if filename_prefix else '') + save_directory
     __lowerCAmelCase							     =     0
     if " " in self.encoder:
        __lowerCAmelCase							     =     self.encoder[' ']
        del self.encoder[" "]
     if "\n" in self.encoder:
        __lowerCAmelCase							     =     self.encoder['\n']
        del self.encoder["\n"]
     __lowerCAmelCase							     =     collections.OrderedDict(sorted(self.encoder.items() ,  key=lambda lowerCAmelCase_					: x[1]					)					)
     with open(lowercase_ ,  'w' ,  encoding='utf-8'					) as writer:
        for token, token_index in self.encoder.items():
           if index != token_index:
              logger.warning(
                  f"""Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive."""
                  ' Please check that the vocabulary is not corrupted!'					)
              __lowerCAmelCase							     =     token_index
           writer.write(token + '\n'					)
           index += 1
     return (vocab_file,)






  def     lowercase		(    self			:		Tuple ,  lowerCAmelCase_			:		int ,  lowerCAmelCase_			:		Union[str, Any] = None					)							->						List[int]:

     if token_ids_a is None:
        return [self.bos_token_id] + token_ids_a
     return [self.bos_token_id] + token_ids_a + [self.bos_token_id] + token_ids_a






  def     lowercase		(    self			:		Optional[Any] ,  lowerCAmelCase_			:		int ,  lowerCAmelCase_			:		Tuple = None ,  lowerCAmelCase_			:		Any = False					)							->						List[int]:

     if already_has_special_tokens:
        return super().get_special_tokens_mask(
            token_ids_a=lowercase_ ,  token_ids_a=lowercase_ ,  already_has_special_tokens=lowercase_					)

     if token_ids_a is not None:
        return [1] + ([0] * len(lowercase_					)) + [1] + ([0] * len(lowercase_					))
     return [1] + ([0] * len(lowercase_					))






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  :   int						   =  object

     def   a_   (			*lowerCAmelCase_				:      List[str],     **lowerCAmelCase_				:      Optional[Any]     ):
        pass




     _snake_case  :   Union[str, Any]						   =  False


_snake_case  :   int						   =  logging.get_logger('transformers-cli/serving')

def   a_   (			lowerCAmelCase_				:      Namespace     ):
   __lowerCAmelCase							     =     pipeline(
       task=args.task,     model=args.model if args.model else None,     config=args.config,     tokenizer=args.tokenizer,     device=args.device,     )
   return ServeCommand(lowerCAmelCase_,     args.host,     args.port,     args.workers     )



class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			42





class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			42
  a_      =			42





class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			42





class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			42





class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  @staticmethod
  def     lowercase		(    lowerCAmelCase_			:		ArgumentParser					)							->						Union[str, Any]:
     __lowerCAmelCase							     =     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=8_8_8_8 ,  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			:		List[str] ,  lowerCAmelCase_			:		Pipeline ,  lowerCAmelCase_			:		str ,  lowerCAmelCase_			:		int ,  lowerCAmelCase_			:		int					)							->						str:
     __lowerCAmelCase							     =     pipeline

     __lowerCAmelCase							     =     host
     __lowerCAmelCase							     =     port
     __lowerCAmelCase							     =     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}"""					)
        __lowerCAmelCase							     =     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=6_0_0 ,  )






  def     lowercase		(    self			:		Tuple					)							->						str:
     run(self._app ,  host=self.host ,  port=self.port ,  workers=self.workers					)






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






  def     lowercase		(    self			:		int ,  lowerCAmelCase_			:		str = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					) ,  lowerCAmelCase_			:		bool = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					)					)							->						Dict:
     try:
        __lowerCAmelCase							     =     self._pipeline.tokenizer.tokenize(lowerCAmelCase_					)

        if return_ids:
           __lowerCAmelCase							     =     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=5_0_0 ,  detail={'model': '', 'error': str(lowerCAmelCase_					)}					)






  def     lowercase		(    self			:		int ,  lowerCAmelCase_			:		List[int] = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					) ,  lowerCAmelCase_			:		bool = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					) ,  lowerCAmelCase_			:		bool = Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					) ,  )							->						Union[str, Any]:
     try:
        __lowerCAmelCase							     =     self._pipeline.tokenizer.decode(lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_					)
        return ServeDeTokenizeResult(model='' ,  text=lowerCAmelCase_					)
     except Exception as e:
        raise HTTPException(status_code=5_0_0 ,  detail={'model': '', 'error': str(lowerCAmelCase_					)}					)






  async def     lowercase		(    self			:		List[Any] ,  lowerCAmelCase_			:		Union[str, Any]=Body(lowerCAmelCase_ ,  embed=lowerCAmelCase_					)					)							->						int:

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

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

