1
0
Fork 0
onnx-web/api/onnx_web/worker/command.py

118 lines
2.6 KiB
Python
Raw Normal View History

from enum import Enum
2023-04-24 22:39:06 +00:00
from typing import Any, Callable, Dict
class JobStatus(str, Enum):
PENDING = "pending"
RUNNING = "running"
SUCCESS = "success"
FAILED = "failed"
CANCELLED = "cancelled"
UNKNOWN = "unknown"
class JobType(str, Enum):
TXT2TXT = "txt2txt"
TXT2IMG = "txt2img"
IMG2IMG = "img2img"
INPAINT = "inpaint"
UPSCALE = "upscale"
BLEND = "blend"
CHAIN = "chain"
2024-01-05 01:09:52 +00:00
class Progress:
2024-01-06 02:13:57 +00:00
"""
Generic counter with current and expected/final/total value. Can be used to count up or down.
Counter is considered "complete" when the current value is greater than or equal to the total value, and "empty"
when the current value is zero.
"""
2024-01-05 01:09:52 +00:00
current: int
total: int
def __init__(self, current: int, total: int) -> None:
self.current = current
self.total = total
def __str__(self) -> str:
return "%s/%s" % (self.current, self.total)
2024-01-07 14:16:13 +00:00
def __eq__(self, other: Any) -> bool:
if isinstance(other, Progress):
return self.current == other.current and self.total == other.total
return False
2024-01-05 01:09:52 +00:00
def tojson(self):
return {
"current": self.current,
"total": self.total,
}
2024-01-06 02:12:41 +00:00
def complete(self) -> bool:
return self.current >= self.total
def empty(self) -> bool:
# TODO: what if total is also 0?
return self.current == 0
2024-01-05 01:09:52 +00:00
class ProgressCommand:
device: str
job: str
job_type: str
status: JobStatus
2024-01-05 01:09:52 +00:00
result: Any # really StageResult but that would be a very circular import
steps: Progress
stages: Progress
tiles: Progress
def __init__(
self,
job: str,
job_type: str,
device: str,
status: JobStatus,
2024-01-05 01:09:52 +00:00
steps: Progress,
stages: Progress,
tiles: Progress,
2024-01-04 05:17:35 +00:00
result: Any = None,
):
self.job = job
self.job_type = job_type
self.device = device
self.status = status
2024-01-05 01:09:52 +00:00
# progress info
self.steps = steps
self.stages = stages
self.tiles = tiles
2024-01-05 01:09:52 +00:00
self.result = result
class JobCommand:
device: str
name: str
job_type: str
fn: Callable[..., None]
args: Any
2023-04-24 22:39:06 +00:00
kwargs: Dict[str, Any]
def __init__(
self,
name: str,
device: str,
job_type: str,
fn: Callable[..., None],
args: Any,
2023-04-24 22:39:06 +00:00
kwargs: Dict[str, Any],
):
2023-03-26 16:49:58 +00:00
self.device = device
self.name = name
self.job_type = job_type
self.fn = fn
self.args = args
self.kwargs = kwargs