



'''simple docstring'''





import re
import time
from typing import Optional

import IPython.display as disp

from ..trainer_callback import TrainerCallback
from ..trainer_utils import IntervalStrategy, has_length

def 			a_	(				_lowerCAmelCase			) -> Tuple:
  __lowerCamelCase			:			Dict       			=					int(_lowerCAmelCase			)
  __lowerCamelCase			,__lowerCamelCase			,__lowerCamelCase			:			Any       			=					t // 3600, (t // 60) % 60, t % 60
  return F'{h}:{m:02d}:{s:02d}' if h != 0 else F'{m:02d}:{s:02d}'

def 			a_	(				_lowerCAmelCase			,_lowerCAmelCase			,_lowerCAmelCase			,_lowerCAmelCase			,_lowerCAmelCase=300			) -> int:
  # docstyle-ignore
  return F'\n    <div>\n      {prefix}\n      <progress value=\'{value}\' max=\'{total}\' style=\'width:{width}px; height:20px; vertical-align: middle;\'></progress>\n      {label}\n    </div>\n    '

def 			a_	(				_lowerCAmelCase			) -> Dict:
  __lowerCamelCase			:			Optional[int]       			=					'<table border="1" class="dataframe">\n'
  html_code += """  <thead>\n <tr style="text-align: left;">\n"""
  for i in items[0]:
    html_code += F'      <th>{i}</th>\n'
  html_code += "    </tr>\n  </thead>\n  <tbody>\n"
  for line in items[1:]:
    html_code += "    <tr>\n"
    for elt in line:
      __lowerCamelCase			:			str       			=					F'{elt:.6f}' if isinstance(_lowerCAmelCase			,_lowerCAmelCase			) else str(_lowerCAmelCase			)
      html_code += F'      <td>{elt}</td>\n'
    html_code += "    </tr>\n"
  html_code += "  </tbody>\n</table><p>"
  return html_code






class    lowerCamelCase_       :
  """simple docstring"""




  a_				   =5
  a_				   =0.2




  def __init__(					self :      Union[str, Any]     ,      _a :      int     ,      _a :      Optional[str] = None     ,      _a :      bool = True     ,      _a :      Optional["NotebookTrainingTracker"] = None     ,      _a :      int = 300     ,      )   ->   Tuple:
    __lowerCamelCase			:			Tuple       			=					total
    __lowerCamelCase			:			Optional[int]       			=					'' if prefix is None else prefix
    __lowerCamelCase			:			List[Any]       			=					leave
    __lowerCamelCase			:			Optional[int]       			=					parent
    __lowerCamelCase			:			Optional[Any]       			=					width
    __lowerCamelCase			:			Tuple       			=					None
    __lowerCamelCase			:			Optional[int]       			=					None
    __lowerCamelCase			:			Dict       			=					None




  def 		_lowercase	(					self :      int     ,      _a :      int     ,      _a :      bool = False     ,      _a :      str = None  )   ->   Optional[int]:
    __lowerCamelCase			:			List[Any]       			=					value
    if comment is not None:
      __lowerCamelCase			:			List[str]       			=					comment
    if self.last_value is None:
      __lowerCamelCase			:			Union[str, Any]       			=					time.time()
      __lowerCamelCase			:			Optional[Any]       			=					value
      __lowerCamelCase			:			List[str]       			=					None
      __lowerCamelCase			:			str       			=					self.warmup
      __lowerCamelCase			:			Optional[int]       			=					1
      self.update_bar(_a  )
    elif value <= self.last_value and not force_update:
      return
    elif force_update or self.first_calls > 0 or value >= min(self.last_value + self.wait_for     ,      self.total  ):
      if self.first_calls > 0:
        self.first_calls -= 1
      __lowerCamelCase			:			Union[str, Any]       			=					time.time()
      __lowerCamelCase			:			str       			=					current_time - self.start_time
      # We could have value = self.start_value if the update is called twixe with the same start value.
      if value > self.start_value:
        __lowerCamelCase			:			Optional[int]       			=					self.elapsed_time / (value - self.start_value)
      else:
        __lowerCamelCase			:			List[Any]       			=					None
      if value >= self.total:
        __lowerCamelCase			:			Tuple       			=					self.total
        __lowerCamelCase			:			Any       			=					None
        if not self.leave:
          self.close()
      elif self.average_time_per_item is not None:
        __lowerCamelCase			:			Optional[Any]       			=					self.average_time_per_item * (self.total - value)
      self.update_bar(_a  )
      __lowerCamelCase			:			Tuple       			=					value
      __lowerCamelCase			:			Dict       			=					current_time
      if self.average_time_per_item is None:
        __lowerCamelCase			:			Dict       			=					1
      else:
        __lowerCamelCase			:			Tuple       			=					max(int(self.update_every / self.average_time_per_item  )     ,      1  )




  def 		_lowercase	(					self :      Optional[Any]     ,      _a :      List[str]     ,      _a :      Tuple=None  )   ->   Optional[int]:
    __lowerCamelCase			:			List[Any]       			=					' ' * (len(str(self.total  )  ) - len(str(_a  )  )) + str(_a  )
    if self.elapsed_time is None:
      __lowerCamelCase			:			Tuple       			=					f'[{spaced_value}/{self.total} : < :'
    elif self.predicted_remaining is None:
      __lowerCamelCase			:			int       			=					f'[{spaced_value}/{self.total} {format_time(self.elapsed_time  )}'
    else:
      __lowerCamelCase			:			Dict       			=					(
          f'[{spaced_value}/{self.total} {format_time(self.elapsed_time  )} <'
          f' {format_time(self.predicted_remaining  )}'
      )
      self.label += f', {1/self.average_time_per_item:.2f} it/s'
    self.label += "]" if self.comment is None or len(self.comment  ) == 0 else f', {self.comment}]'
    self.display()




  def 		_lowercase	(					self :      List[str]  )   ->   Tuple:
    __lowerCamelCase			:			Tuple       			=					html_progress_bar(self.value     ,      self.total     ,      self.prefix     ,      self.label     ,      self.width  )
    if self.parent is not None:
      # If this is a child bar, the parent will take care of the display.
      self.parent.display()
      return
    if self.output is None:
      __lowerCamelCase			:			Optional[int]       			=					disp.display(disp.HTML(self.html_code  )     ,      display_id=_a  )
    else:
      self.output.update(disp.HTML(self.html_code  )  )




  def 		_lowercase	(					self :      str  )   ->   List[Any]:
    if self.parent is None and self.output is not None:
      self.output.update(disp.HTML(''  )  )






class    lowerCamelCase_       (							SCREAMING_SNAKE_CASE_	):
  """simple docstring"""




  def __init__(					self :      Optional[int]     ,      _a :      Optional[Any]     ,      _a :      str=None  )   ->   str:
    super().__init__(_a  )
    __lowerCamelCase			:			str       			=					None if column_names is None else [column_names]
    __lowerCamelCase			:			str       			=					None




  def 		_lowercase	(					self :      List[str]  )   ->   int:
    __lowerCamelCase			:			str       			=					html_progress_bar(self.value     ,      self.total     ,      self.prefix     ,      self.label     ,      self.width  )
    if self.inner_table is not None:
      self.html_code += text_to_html_table(self.inner_table  )
    if self.child_bar is not None:
      self.html_code += self.child_bar.html_code
    if self.output is None:
      __lowerCamelCase			:			Dict       			=					disp.display(disp.HTML(self.html_code  )     ,      display_id=_a  )
    else:
      self.output.update(disp.HTML(self.html_code  )  )




  def 		_lowercase	(					self :      Tuple     ,      _a :      Union[str, Any]  )   ->   str:
    if self.inner_table is None:
      __lowerCamelCase			:			List[Any]       			=					[list(values.keys()  ), list(values.values()  )]
    else:
      __lowerCamelCase			:			str       			=					self.inner_table[0]
      if len(self.inner_table  ) == 1:
        # We give a chance to update the column names at the first iteration
        for key in values.keys():
          if key not in columns:
            columns.append(_a  )
        __lowerCamelCase			:			Dict       			=					columns
      self.inner_table.append([values[c] for c in columns]  )




  def 		_lowercase	(					self :      int     ,      _a :      Union[str, Any]     ,      _a :      Union[str, Any]=None     ,      _a :      Union[str, Any]=300  )   ->   List[Any]:
    __lowerCamelCase			:			int       			=					NotebookProgressBar(_a     ,      prefix=_a     ,      parent=self     ,      width=_a  )
    return self.child_bar




  def 		_lowercase	(					self :      List[Any]  )   ->   Union[str, Any]:
    __lowerCamelCase			:			Union[str, Any]       			=					None
    self.display()






class    lowerCamelCase_       (							SCREAMING_SNAKE_CASE_	):
  """simple docstring"""




  def __init__(					self :      Any  )   ->   int:
    __lowerCamelCase			:			Union[str, Any]       			=					None
    __lowerCamelCase			:			List[str]       			=					None
    __lowerCamelCase			:			Dict       			=					False




  def 		_lowercase	(					self :      Tuple     ,      _a :      int     ,      _a :      Union[str, Any]     ,      _a :      Union[str, Any]     ,      **_a :      Optional[int]  )   ->   Optional[Any]:
    __lowerCamelCase			:			List[Any]       			=					'Epoch' if args.evaluation_strategy == IntervalStrategy.EPOCH else 'Step'
    __lowerCamelCase			:			Union[str, Any]       			=					0
    __lowerCamelCase			:			Optional[int]       			=					0
    __lowerCamelCase			:			Optional[Any]       			=					[self.first_column] + ['Training Loss']
    if args.evaluation_strategy != IntervalStrategy.NO:
      column_names.append('Validation Loss'  )
    __lowerCamelCase			:			List[str]       			=					NotebookTrainingTracker(state.max_steps     ,      _a  )




  def 		_lowercase	(					self :      Optional[int]     ,      _a :      List[str]     ,      _a :      Dict     ,      _a :      Any     ,      **_a :      Optional[int]  )   ->   Optional[int]:
    __lowerCamelCase			:			Union[str, Any]       			=					int(state.epoch  ) if int(state.epoch  ) == state.epoch else f'{state.epoch:.2f}'
    self.training_tracker.update(
        state.global_step + 1     ,      comment=f'Epoch {epoch}/{state.num_train_epochs}'     ,      force_update=self._force_next_update     ,      )
    __lowerCamelCase			:			Optional[int]       			=					False




  def 		_lowercase	(					self :      Optional[int]     ,      _a :      Any     ,      _a :      Optional[int]     ,      _a :      List[Any]     ,      _a :      List[Any]=None     ,      **_a :      Any  )   ->   List[str]:
    if not has_length(_a  ):
      return
    if self.prediction_bar is None:
      if self.training_tracker is not None:
        __lowerCamelCase			:			List[Any]       			=					self.training_tracker.add_child(len(_a  )  )
      else:
        __lowerCamelCase			:			Union[str, Any]       			=					NotebookProgressBar(len(_a  )  )
      self.prediction_bar.update(1  )
    else:
      self.prediction_bar.update(self.prediction_bar.value + 1  )




  def 		_lowercase	(					self :      Optional[Any]     ,      _a :      str     ,      _a :      Dict     ,      _a :      Dict     ,      **_a :      List[str]  )   ->   List[Any]:
    if self.prediction_bar is not None:
      self.prediction_bar.close()
    __lowerCamelCase			:			Any       			=					None




  def 		_lowercase	(					self :      Dict     ,      _a :      str     ,      _a :      List[str]     ,      _a :      Optional[Any]     ,      _a :      str=None     ,      **_a :      str  )   ->   Optional[int]:
    # Only for when there is no evaluation
    if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs:
      __lowerCamelCase			:			Union[str, Any]       			=					{'Training Loss': logs['loss']}
      # First column is necessarily Step sine we're not in epoch eval strategy
      __lowerCamelCase			:			Union[str, Any]       			=					state.global_step
      self.training_tracker.write_line(_a  )




  def 		_lowercase	(					self :      int     ,      _a :      int     ,      _a :      List[Any]     ,      _a :      List[str]     ,      _a :      Dict=None     ,      **_a :      List[Any]  )   ->   Any:
    if self.training_tracker is not None:
      __lowerCamelCase			:			Optional[Any]       			=					{'Training Loss': 'No log', 'Validation Loss': 'No log'}
      for log in reversed(state.log_history  ):
        if "loss" in log:
          __lowerCamelCase			:			Optional[int]       			=					log['loss']
          break

      if self.first_column == "Epoch":
        __lowerCamelCase			:			Tuple       			=					int(state.epoch  )
      else:
        __lowerCamelCase			:			List[str]       			=					state.global_step
      __lowerCamelCase			:			Optional[int]       			=					'eval'
      for k in metrics:
        if k.endswith('_loss'  ):
          __lowerCamelCase			:			Dict       			=					re.sub(R'\_loss$'     ,      ''     ,      _a  )
      __lowerCamelCase			:			Tuple       			=					metrics.pop('total_flos'     ,      _a  )
      __lowerCamelCase			:			Optional[Any]       			=					metrics.pop('epoch'     ,      _a  )
      __lowerCamelCase			:			Tuple       			=					metrics.pop(f'{metric_key_prefix}_runtime'     ,      _a  )
      __lowerCamelCase			:			Any       			=					metrics.pop(f'{metric_key_prefix}_samples_per_second'     ,      _a  )
      __lowerCamelCase			:			List[Any]       			=					metrics.pop(f'{metric_key_prefix}_steps_per_second'     ,      _a  )
      __lowerCamelCase			:			List[Any]       			=					metrics.pop(f'{metric_key_prefix}_jit_compilation_time'     ,      _a  )
      for k, v in metrics.items():
        if k == f'{metric_key_prefix}_loss':
          __lowerCamelCase			:			str       			=					v
        else:
          __lowerCamelCase			:			List[Any]       			=					k.split('_'  )
          __lowerCamelCase			:			Any       			=					' '.join([part.capitalize() for part in splits[1:]]  )
          __lowerCamelCase			:			Tuple       			=					v
      self.training_tracker.write_line(_a  )
      self.training_tracker.remove_child()
      __lowerCamelCase			:			Dict       			=					None
      # Evaluation takes a long time so we should force the next update.
      __lowerCamelCase			:			Tuple       			=					True




  def 		_lowercase	(					self :      int     ,      _a :      Tuple     ,      _a :      Union[str, Any]     ,      _a :      str     ,      **_a :      Tuple  )   ->   Tuple:
    self.training_tracker.update(
        state.global_step     ,      comment=f'Epoch {int(state.epoch  )}/{state.num_train_epochs}'     ,      force_update=_a  )
    __lowerCamelCase			:			Tuple       			=					None











'''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

   _UpperCamelCase											=		True
except (ImportError, AttributeError):
   _UpperCamelCase											=		object

   def 			a_	(				*_lowerCAmelCase			,**_lowerCAmelCase			) -> Union[str, Any]:
     pass


   _UpperCamelCase											=		False


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

def 			a_	(				_lowerCAmelCase			) -> Optional[int]:
  __lowerCamelCase			:			List[Any]       			=					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    lowerCamelCase_       (							SCREAMING_SNAKE_CASE_	):
  """simple docstring"""




  a_				   =42






class    lowerCamelCase_       (							SCREAMING_SNAKE_CASE_	):
  """simple docstring"""




  a_				   =42
  a_				   =42






class    lowerCamelCase_       (							SCREAMING_SNAKE_CASE_	):
  """simple docstring"""




  a_				   =42






class    lowerCamelCase_       (							SCREAMING_SNAKE_CASE_	):
  """simple docstring"""




  a_				   =42






class    lowerCamelCase_       (							SCREAMING_SNAKE_CASE_	):
  """simple docstring"""




  @staticmethod
  def 		_lowercase	(					_a :      ArgumentParser  )   ->   Optional[Any]:
    __lowerCamelCase			:			Union[str, Any]       			=					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=8888     ,      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 :      int     ,      _a :      Pipeline     ,      _a :      str     ,      _a :      int     ,      _a :      int  )   ->   Optional[int]:
    __lowerCamelCase			:			int       			=					pipeline

    __lowerCamelCase			:			List[Any]       			=					host
    __lowerCamelCase			:			int       			=					port
    __lowerCamelCase			:			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}'  )
      __lowerCamelCase			:			Optional[Any]       			=					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 		_lowercase	(					self :      Optional[Any]  )   ->   List[str]:
    run(self._app     ,      host=self.host     ,      port=self.port     ,      workers=self.workers  )




  def 		_lowercase	(					self :      Tuple  )   ->   int:
    return ServeModelInfoResult(infos=vars(self._pipeline.model.config  )  )




  def 		_lowercase	(					self :      str     ,      _a :      str = Body(_a     ,      embed=_a  )     ,      _a :      bool = Body(_a     ,      embed=_a  )  )   ->   Optional[int]:
    try:
      __lowerCamelCase			:			Dict       			=					self._pipeline.tokenizer.tokenize(_a  )

      if return_ids:
        __lowerCamelCase			:			int       			=					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 		_lowercase	(					self :      Optional[Any]     ,      _a :      List[int] = Body(_a     ,      embed=_a  )     ,      _a :      bool = Body(_a     ,      embed=_a  )     ,      _a :      bool = Body(_a     ,      embed=_a  )     ,      )   ->   List[Any]:
    try:
      __lowerCamelCase			:			Dict       			=					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 		_lowercase	(					self :      Dict     ,      _a :      Any=Body(_a     ,      embed=_a  )  )   ->   Optional[int]:

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

    try:
      # Forward through the model
      __lowerCamelCase			:			Union[str, Any]       			=					self._pipeline(_a  )
      return ServeForwardResult(output=_a  )
    except Exception as e:
      raise HTTPException(500     ,      {'error': str(_a  )}  )






