2023-04-06 04:06:05 +00:00
|
|
|
# Copyright (c) 2023 Kyle Schouviller (https://github.com/kyle0654)
|
|
|
|
|
2023-04-10 09:07:48 +00:00
|
|
|
from typing import Literal
|
2023-04-06 04:06:05 +00:00
|
|
|
|
|
|
|
from pydantic import BaseModel, Field
|
2023-05-11 10:31:48 +00:00
|
|
|
import numpy as np
|
|
|
|
|
2023-05-16 00:48:22 +00:00
|
|
|
from .baseinvocation import (
|
|
|
|
BaseInvocation,
|
|
|
|
BaseInvocationOutput,
|
|
|
|
InvocationContext,
|
|
|
|
InvocationConfig,
|
|
|
|
)
|
2023-04-06 04:06:05 +00:00
|
|
|
|
|
|
|
|
2023-04-10 09:07:48 +00:00
|
|
|
class MathInvocationConfig(BaseModel):
|
|
|
|
"""Helper class to provide all math invocations with additional config"""
|
|
|
|
|
|
|
|
# Schema customisation
|
|
|
|
class Config(InvocationConfig):
|
|
|
|
schema_extra = {
|
|
|
|
"ui": {
|
|
|
|
"tags": ["math"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-06 04:06:05 +00:00
|
|
|
class IntOutput(BaseInvocationOutput):
|
|
|
|
"""An integer output"""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
|
|
|
# fmt: off
|
2023-04-06 04:06:05 +00:00
|
|
|
type: Literal["int_output"] = "int_output"
|
|
|
|
a: int = Field(default=None, description="The output integer")
|
2023-05-16 00:48:22 +00:00
|
|
|
# fmt: on
|
2023-04-06 04:06:05 +00:00
|
|
|
|
|
|
|
|
2023-04-10 09:07:48 +00:00
|
|
|
class AddInvocation(BaseInvocation, MathInvocationConfig):
|
2023-04-06 04:06:05 +00:00
|
|
|
"""Adds two numbers"""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
|
|
|
# fmt: off
|
2023-04-06 04:06:05 +00:00
|
|
|
type: Literal["add"] = "add"
|
|
|
|
a: int = Field(default=0, description="The first number")
|
|
|
|
b: int = Field(default=0, description="The second number")
|
2023-05-16 00:48:22 +00:00
|
|
|
# fmt: on
|
2023-04-06 04:06:05 +00:00
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> IntOutput:
|
|
|
|
return IntOutput(a=self.a + self.b)
|
|
|
|
|
|
|
|
|
2023-04-10 09:07:48 +00:00
|
|
|
class SubtractInvocation(BaseInvocation, MathInvocationConfig):
|
2023-04-06 04:06:05 +00:00
|
|
|
"""Subtracts two numbers"""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
|
|
|
# fmt: off
|
2023-04-06 04:06:05 +00:00
|
|
|
type: Literal["sub"] = "sub"
|
|
|
|
a: int = Field(default=0, description="The first number")
|
|
|
|
b: int = Field(default=0, description="The second number")
|
2023-05-16 00:48:22 +00:00
|
|
|
# fmt: on
|
2023-04-06 04:06:05 +00:00
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> IntOutput:
|
|
|
|
return IntOutput(a=self.a - self.b)
|
|
|
|
|
|
|
|
|
2023-04-10 09:07:48 +00:00
|
|
|
class MultiplyInvocation(BaseInvocation, MathInvocationConfig):
|
2023-04-06 04:06:05 +00:00
|
|
|
"""Multiplies two numbers"""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
|
|
|
# fmt: off
|
2023-04-06 04:06:05 +00:00
|
|
|
type: Literal["mul"] = "mul"
|
|
|
|
a: int = Field(default=0, description="The first number")
|
|
|
|
b: int = Field(default=0, description="The second number")
|
2023-05-16 00:48:22 +00:00
|
|
|
# fmt: on
|
2023-04-06 04:06:05 +00:00
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> IntOutput:
|
|
|
|
return IntOutput(a=self.a * self.b)
|
|
|
|
|
|
|
|
|
2023-04-10 09:07:48 +00:00
|
|
|
class DivideInvocation(BaseInvocation, MathInvocationConfig):
|
2023-04-06 04:06:05 +00:00
|
|
|
"""Divides two numbers"""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
|
|
|
# fmt: off
|
2023-04-06 04:06:05 +00:00
|
|
|
type: Literal["div"] = "div"
|
|
|
|
a: int = Field(default=0, description="The first number")
|
|
|
|
b: int = Field(default=0, description="The second number")
|
2023-05-16 00:48:22 +00:00
|
|
|
# fmt: on
|
2023-04-06 04:06:05 +00:00
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> IntOutput:
|
|
|
|
return IntOutput(a=int(self.a / self.b))
|
2023-05-11 10:31:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RandomIntInvocation(BaseInvocation):
|
|
|
|
"""Outputs a single random integer."""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
|
|
|
# fmt: off
|
2023-05-11 10:31:48 +00:00
|
|
|
type: Literal["rand_int"] = "rand_int"
|
2023-05-16 00:48:22 +00:00
|
|
|
low: int = Field(default=0, description="The inclusive low value")
|
|
|
|
high: int = Field(
|
|
|
|
default=np.iinfo(np.int32).max, description="The exclusive high value"
|
|
|
|
)
|
|
|
|
# fmt: on
|
2023-05-11 10:31:48 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> IntOutput:
|
2023-05-16 00:48:22 +00:00
|
|
|
return IntOutput(a=np.random.randint(self.low, self.high))
|