class_docstr: The ``AutoScaler`` can be used to automatically change the number of replicas of the given server in response to
  changes in the number of incoming requests. Incoming requests will be batched and balanced across the replicas.
  
  Args:
      min_replicas: The number of works to start when app initializes.
      max_replicas: The max number of works to spawn to handle the incoming requests.
      scale_out_interval: The number of seconds to wait before checking whether to increase the number of servers.
      scale_in_interval: The number of seconds to wait before checking whether to decrease the number of servers.
      endpoint: Provide the REST API path.
      max_batch_size: (auto-batching) The number of requests to process at once.
      timeout_batching: (auto-batching) The number of seconds to wait before sending the requests to process.
      input_type: Input type.
      output_type: Output type.
      cold_start_proxy: If provided, the proxy will be used while the worker machines are warming up.
  
  .. testcode::
  
      from lightning.app import LightningApp
      from lightning.app.components import AutoScaler
  
      # Example 1: Auto-scaling serve component out-of-the-box
      app = LightningApp(
          app.components.AutoScaler(
              MyPythonServer,
              min_replicas=1,
              max_replicas=8,
              scale_out_interval=10,
              scale_in_interval=10,
          )
      )
  
  
      # Example 2: Customizing the scaling logic
      class MyAutoScaler(AutoScaler):
          def scale(self, replicas: int, metrics: dict) -> int:
              pending_requests_per_running_or_pending_work = metrics["pending_requests"] / (
                  replicas + metrics["pending_works"]
              )
  
              # upscale
              max_requests_per_work = self.max_batch_size
              if pending_requests_per_running_or_pending_work >= max_requests_per_work:
                  return replicas + 1
  
              # downscale
              min_requests_per_work = max_requests_per_work * 0.25
              if pending_requests_per_running_or_pending_work < min_requests_per_work:
                  return replicas - 1
  
              return replicas
  
  
      app = LightningApp(
          MyAutoScaler(
              MyPythonServer,
              min_replicas=1,
              max_replicas=8,
              scale_out_interval=10,
              scale_in_interval=10,
              max_batch_size=8,  # for auto batching
              timeout_batching=1,  # for auto batching
          )
      )
class_docstr_tok: ["The","``","AutoScaler","``","can","be","used","to","automatically","change","the","number","of","replicas","of","the","given","server","in","response","to","changes","in","the","number","of","incoming","requests",".","Incoming","requests","will","be","batched","and","balanced","across","the","replicas",".","Args",":","min_replicas",":","The","number","of","works","to","start","when","app","initializes",".","max_replicas",":","The","max","number","of","works","to","spawn","to","handle","the","incoming","requests",".","scale_out_interval",":","The","number","of","seconds","to","wait","before","checking","whether","to","increase","the","number","of","servers",".","scale_in_interval",":","The","number","of","seconds","to","wait","before","checking","whether","to","decrease","the","number","of","servers",".","endpoint",":","Provide","the","REST","API","path",".","max_batch_size",":","(","auto-batching",")","The","number","of","requests","to","process","at","once",".","timeout_batching",":","(","auto-batching",")","The","number","of","seconds","to","wait","before","sending","the","requests","to","process",".","input_type",":","Input","type",".","output_type",":","Output","type",".","cold_start_proxy",":","If","provided",",","the","proxy","will","be","used","while","the","worker","machines","are","warming","up",".","..","testcode",":",":","from","lightning.app","import","LightningApp","from","lightning.app.components","import","AutoScaler","#","Example","1",":","Auto-scaling","serve","component","out-of-the-box","app","=","LightningApp","(","app.components.AutoScaler","(","MyPythonServer",",","min_replicas=1",",","max_replicas=8",",","scale_out_interval=10",",","scale_in_interval=10",",",")",")","#","Example","2",":","Customizing","the","scaling","logic","class","MyAutoScaler","(","AutoScaler",")",":","def","scale","(","self",",","replicas",":","int",",","metrics",":","dict",")","-",">","int",":","pending_requests_per_running_or_pending_work","=","metrics","[","``","pending_requests","''","]","\/","(","replicas","+","metrics","[","``","pending_works","''","]",")","#","upscale","max_requests_per_work","=","self.max_batch_size","if","pending_requests_per_running_or_pending_work",">","=","max_requests_per_work",":","return","replicas","+","1","#","downscale","min_requests_per_work","=","max_requests_per_work","*","0.25","if","pending_requests_per_running_or_pending_work","<","min_requests_per_work",":","return","replicas","-","1","return","replicas","app","=","LightningApp","(","MyAutoScaler","(","MyPythonServer",",","min_replicas=1",",","max_replicas=8",",","scale_out_interval=10",",","scale_in_interval=10",",","max_batch_size=8",",","#","for","auto","batching","timeout_batching=1",",","#","for","auto","batching",")",")"]
code: def __init__(
      self,
      work_cls: Type[LightningWork],
      min_replicas: int = 1,
      max_replicas: int = 4,
      scale_out_interval: Numeric = 10,
      scale_in_interval: Numeric = 10,
      max_batch_size: int = 8,
      timeout_batching: float = 1,
      endpoint: str = "api/predict",
      input_type: Type[BaseModel] = Dict,
      output_type: Type[BaseModel] = Dict,
      cold_start_proxy: Union[ColdStartProxy, str, None] = None,
      *work_args: Any,
      **work_kwargs: Any,
  ) -> None:
      super().__init__()
      self.num_replicas = 0
      self._work_registry = {}
  
      self._work_cls = work_cls
      self._work_args = work_args
      self._work_kwargs = work_kwargs
  
      self._input_type = input_type
      self._output_type = output_type
      self.scale_out_interval = scale_out_interval
      self.scale_in_interval = scale_in_interval
      self.max_batch_size = max_batch_size
  
      if max_replicas < min_replicas:
          raise ValueError(
              f"`max_replicas={max_replicas}` must be less than or equal to `min_replicas={min_replicas}`."
          )
      self.max_replicas = max_replicas
      self.min_replicas = min_replicas
      self._last_autoscale = time.time()
      self.fake_trigger = 0
  
      self.load_balancer = _LoadBalancer(
          input_type=self._input_type,
          output_type=self._output_type,
          endpoint=endpoint,
          max_batch_size=max_batch_size,
          timeout_batching=timeout_batching,
          cache_calls=True,
          parallel=True,
          api_name=self._work_cls.__name__,
          cold_start_proxy=cold_start_proxy,
      )
  
imports: import asyncio
  import logging
  import time
  import uuid
  from itertools import cycle
  from typing import Any, Dict, List, Optional, Tuple, Type, Union
  from typing import SupportsFloat
  import requests
  import uvicorn
  from fastapi import FastAPI, HTTPException, Request
  from fastapi.middleware.cors import CORSMiddleware
  from fastapi.responses import RedirectResponse
  from pydantic import BaseModel
  from starlette.staticfiles import StaticFiles
  from lightning.app.components.serve.cold_start_proxy import ColdStartProxy
  from lightning.app.core.flow import LightningFlow
  from lightning.app.core.work import LightningWork
  from lightning.app.utilities.app_helpers import Logger
  from lightning.app.utilities.cloud import is_running_in_cloud
  from lightning.app.utilities.imports import _is_aiohttp_available, requires
  from lightning.app.utilities.packaging.cloud_compute import CloudCompute
label_desc: Use image node_id 1 to create a new AutoScaler object from inherited base classes: LightningFlow with example: obj = AutoScaler(work_cls, min_replicas, max_replicas, scale_out_interval, scale_in_interval, max_batch_size, timeout_batching, endpoint, input_type, output_type, cold_start_proxy)