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
|
|
|
|
2023-05-11 10:31:48 +00:00
|
|
|
import numpy as np
|
|
|
|
|
2023-08-14 09:41:29 +00:00
|
|
|
from invokeai.app.invocations.primitives import IntegerOutput
|
2023-04-06 04:06:05 +00:00
|
|
|
|
2023-08-14 09:41:29 +00:00
|
|
|
from .baseinvocation import BaseInvocation, FieldDescriptions, InputField, InvocationContext, tags, title
|
2023-04-06 04:06:05 +00:00
|
|
|
|
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
@title("Add Integers")
|
|
|
|
@tags("math")
|
|
|
|
class AddInvocation(BaseInvocation):
|
2023-04-06 04:06:05 +00:00
|
|
|
"""Adds two numbers"""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
2023-04-06 04:06:05 +00:00
|
|
|
type: Literal["add"] = "add"
|
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
# Inputs
|
|
|
|
a: int = InputField(default=0, description=FieldDescriptions.num_1)
|
|
|
|
b: int = InputField(default=0, description=FieldDescriptions.num_2)
|
2023-07-18 14:26:45 +00:00
|
|
|
|
2023-08-14 09:41:29 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> IntegerOutput:
|
2023-08-18 10:17:21 +00:00
|
|
|
return IntegerOutput(value=self.a + self.b)
|
2023-04-06 04:06:05 +00:00
|
|
|
|
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
@title("Subtract Integers")
|
|
|
|
@tags("math")
|
|
|
|
class SubtractInvocation(BaseInvocation):
|
2023-04-06 04:06:05 +00:00
|
|
|
"""Subtracts two numbers"""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
2023-04-06 04:06:05 +00:00
|
|
|
type: Literal["sub"] = "sub"
|
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
# Inputs
|
|
|
|
a: int = InputField(default=0, description=FieldDescriptions.num_1)
|
|
|
|
b: int = InputField(default=0, description=FieldDescriptions.num_2)
|
2023-07-18 14:26:45 +00:00
|
|
|
|
2023-08-14 09:41:29 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> IntegerOutput:
|
2023-08-18 10:17:21 +00:00
|
|
|
return IntegerOutput(value=self.a - self.b)
|
2023-04-06 04:06:05 +00:00
|
|
|
|
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
@title("Multiply Integers")
|
|
|
|
@tags("math")
|
|
|
|
class MultiplyInvocation(BaseInvocation):
|
2023-04-06 04:06:05 +00:00
|
|
|
"""Multiplies two numbers"""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
2023-04-06 04:06:05 +00:00
|
|
|
type: Literal["mul"] = "mul"
|
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
# Inputs
|
|
|
|
a: int = InputField(default=0, description=FieldDescriptions.num_1)
|
|
|
|
b: int = InputField(default=0, description=FieldDescriptions.num_2)
|
2023-07-18 14:26:45 +00:00
|
|
|
|
2023-08-14 09:41:29 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> IntegerOutput:
|
2023-08-18 10:17:21 +00:00
|
|
|
return IntegerOutput(value=self.a * self.b)
|
2023-04-06 04:06:05 +00:00
|
|
|
|
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
@title("Divide Integers")
|
|
|
|
@tags("math")
|
|
|
|
class DivideInvocation(BaseInvocation):
|
2023-04-06 04:06:05 +00:00
|
|
|
"""Divides two numbers"""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
2023-04-06 04:06:05 +00:00
|
|
|
type: Literal["div"] = "div"
|
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
# Inputs
|
|
|
|
a: int = InputField(default=0, description=FieldDescriptions.num_1)
|
|
|
|
b: int = InputField(default=0, description=FieldDescriptions.num_2)
|
2023-07-18 14:26:45 +00:00
|
|
|
|
2023-08-14 09:41:29 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> IntegerOutput:
|
2023-08-18 10:17:21 +00:00
|
|
|
return IntegerOutput(value=int(self.a / self.b))
|
2023-05-11 10:31:48 +00:00
|
|
|
|
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
@title("Random Integer")
|
|
|
|
@tags("math")
|
2023-05-11 10:31:48 +00:00
|
|
|
class RandomIntInvocation(BaseInvocation):
|
|
|
|
"""Outputs a single random integer."""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
2023-05-11 10:31:48 +00:00
|
|
|
type: Literal["rand_int"] = "rand_int"
|
2023-08-14 03:23:09 +00:00
|
|
|
|
|
|
|
# Inputs
|
|
|
|
low: int = InputField(default=0, description="The inclusive low value")
|
|
|
|
high: int = InputField(default=np.iinfo(np.int32).max, description="The exclusive high value")
|
2023-07-18 14:26:45 +00:00
|
|
|
|
2023-08-14 09:41:29 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> IntegerOutput:
|
2023-08-18 10:17:21 +00:00
|
|
|
return IntegerOutput(value=np.random.randint(self.low, self.high))
|