code: def progressapi(req: ProgressRequest):
      active = req.id_task == current_task
      queued = req.id_task in pending_tasks
      completed = req.id_task in finished_tasks
  
      if not active:
          textinfo = "Waiting..."
          if queued:
              sorted_queued = sorted(
                  pending_tasks.keys(), key=lambda x: pending_tasks[x]
              )
              queue_index = sorted_queued.index(req.id_task)
              textinfo = "In queue: {}/{}".format(
                  queue_index + 1, len(sorted_queued)
              )
          return ProgressResponse(
              active=active,
              queued=queued,
              completed=completed,
              id_live_preview=-1,
              textinfo=textinfo,
          )
  
      progress = 0
  
      job_count, job_no = shared.state.job_count, shared.state.job_no
      sampling_steps, sampling_step = (
          shared.state.sampling_steps,
          shared.state.sampling_step,
      )
  
      if job_count > 0:
          progress += job_no / job_count
      if sampling_steps > 0 and job_count > 0:
          progress += 1 / job_count * sampling_step / sampling_steps
  
      progress = min(progress, 1)
  
      elapsed_since_start = time.time() - shared.state.time_start
      predicted_duration = (
          elapsed_since_start / progress if progress > 0 else None
      )
      eta = (
          predicted_duration - elapsed_since_start
          if predicted_duration is not None
          else None
      )
  
      live_preview = None
      id_live_preview = req.id_live_preview
  
      if opts.live_previews_enable and req.live_preview:
          shared.state.set_current_image()
          if shared.state.id_live_preview != req.id_live_preview:
              image = shared.state.current_image
              if image is not None:
                  buffered = io.BytesIO()
  
                  if opts.live_previews_image_format == "png":
                      # using optimize for large images takes an enormous amount of time
                      if max(*image.size) <= 256:
                          save_kwargs = {"optimize": True}
                      else:
                          save_kwargs = {
                              "optimize": False,
                              "compress_level": 1,
                          }
  
                  else:
                      save_kwargs = {}
  
                  image.save(
                      buffered,
                      format=opts.live_previews_image_format,
                      **save_kwargs,
                  )
                  base64_image = base64.b64encode(
                      buffered.getvalue()
                  ).decode("ascii")
                  live_preview = f"data:image/{opts.live_previews_image_format};base64,{base64_image}"
                  id_live_preview = shared.state.id_live_preview
  
      return ProgressResponse(
          active=active,
          queued=queued,
          completed=completed,
          progress=progress,
          eta=eta,
          live_preview=live_preview,
          id_live_preview=id_live_preview,
          textinfo=shared.state.textinfo,
      )
  
file_path: stable-diffusion-webui/modules/progress.py
imports: import base64
  import io
  import time
  import gradio
  from pydantic import BaseModel, Field
  from modules.shared import opts
  import modules.shared
label_desc: Use image node_id 6 for calling a global function with example usage: progressapi(req) and returns: ProgressResponse, ProgressResponse