2023-04-06 04:06:05 +00:00
|
|
|
# Copyright (c) 2023 Kyle Schouviller (https://github.com/kyle0654)
|
|
|
|
|
2023-05-11 10:31:48 +00:00
|
|
|
import numpy as np
|
2023-09-06 01:27:08 +00:00
|
|
|
from typing import Literal
|
2023-05-11 10:31:48 +00:00
|
|
|
|
2023-09-06 01:27:08 +00:00
|
|
|
from invokeai.app.invocations.primitives import IntegerOutput, FloatOutput
|
2023-09-10 04:27:15 +00:00
|
|
|
from pydantic import validator
|
2023-04-06 04:06:05 +00:00
|
|
|
|
feat(nodes): move all invocation metadata (type, title, tags, category) to decorator
All invocation metadata (type, title, tags and category) are now defined in decorators.
The decorators add the `type: Literal["invocation_type"]: "invocation_type"` field to the invocation.
Category is a new invocation metadata, but it is not used by the frontend just yet.
- `@invocation()` decorator for invocations
```py
@invocation(
"sdxl_compel_prompt",
title="SDXL Prompt",
tags=["sdxl", "compel", "prompt"],
category="conditioning",
)
class SDXLCompelPromptInvocation(BaseInvocation, SDXLPromptInvocationBase):
...
```
- `@invocation_output()` decorator for invocation outputs
```py
@invocation_output("clip_skip_output")
class ClipSkipInvocationOutput(BaseInvocationOutput):
...
```
- update invocation docs
- add category to decorator
- regen frontend types
2023-08-30 08:35:12 +00:00
|
|
|
from .baseinvocation import BaseInvocation, FieldDescriptions, InputField, InvocationContext, invocation
|
2023-04-06 04:06:05 +00:00
|
|
|
|
|
|
|
|
2023-09-04 08:11:56 +00:00
|
|
|
@invocation("add", title="Add Integers", tags=["math", "add"], category="math", version="1.0.0")
|
2023-08-14 03:23:09 +00:00
|
|
|
class AddInvocation(BaseInvocation):
|
2023-09-10 04:52:53 +00:00
|
|
|
"""Adds two numbers"""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
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-09-04 08:11:56 +00:00
|
|
|
@invocation("sub", title="Subtract Integers", tags=["math", "subtract"], category="math", version="1.0.0")
|
2023-08-14 03:23:09 +00:00
|
|
|
class SubtractInvocation(BaseInvocation):
|
2023-04-06 04:06:05 +00:00
|
|
|
"""Subtracts two numbers"""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
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-09-04 08:11:56 +00:00
|
|
|
@invocation("mul", title="Multiply Integers", tags=["math", "multiply"], category="math", version="1.0.0")
|
2023-08-14 03:23:09 +00:00
|
|
|
class MultiplyInvocation(BaseInvocation):
|
2023-04-06 04:06:05 +00:00
|
|
|
"""Multiplies two numbers"""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
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-09-04 08:11:56 +00:00
|
|
|
@invocation("div", title="Divide Integers", tags=["math", "divide"], category="math", version="1.0.0")
|
2023-08-14 03:23:09 +00:00
|
|
|
class DivideInvocation(BaseInvocation):
|
2023-04-06 04:06:05 +00:00
|
|
|
"""Divides two numbers"""
|
2023-05-16 00:48:22 +00:00
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
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-09-04 08:11:56 +00:00
|
|
|
@invocation("rand_int", title="Random Integer", tags=["math", "random"], category="math", version="1.0.0")
|
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-08-14 03:23:09 +00:00
|
|
|
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))
|
2023-09-06 01:27:08 +00:00
|
|
|
|
|
|
|
|
2023-09-13 05:49:19 +00:00
|
|
|
@invocation(
|
|
|
|
"float_to_int",
|
|
|
|
title="Float To Integer",
|
|
|
|
tags=["math", "round", "integer", "float", "convert"],
|
|
|
|
category="math",
|
|
|
|
version="1.0.0",
|
|
|
|
)
|
2023-09-10 04:55:46 +00:00
|
|
|
class FloatToIntegerInvocation(BaseInvocation):
|
2023-09-10 04:27:15 +00:00
|
|
|
"""Rounds a float number to (a multiple of) an integer."""
|
2023-09-06 01:27:08 +00:00
|
|
|
|
2023-09-07 02:06:19 +00:00
|
|
|
value: float = InputField(default=0, description="The value to round")
|
2023-09-13 05:49:19 +00:00
|
|
|
multiple: int = InputField(default=1, ge=1, title="Multiple of", description="The multiple to round to")
|
|
|
|
method: Literal["Nearest", "Floor", "Ceiling", "Truncate"] = InputField(
|
|
|
|
default="Nearest", description="The method to use for rounding"
|
|
|
|
)
|
2023-09-06 01:27:08 +00:00
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> IntegerOutput:
|
2023-09-07 02:06:19 +00:00
|
|
|
if self.method == "Nearest":
|
|
|
|
return IntegerOutput(value=round(self.value / self.multiple) * self.multiple)
|
|
|
|
elif self.method == "Floor":
|
|
|
|
return IntegerOutput(value=np.floor(self.value / self.multiple) * self.multiple)
|
2023-09-10 04:27:15 +00:00
|
|
|
elif self.method == "Ceiling":
|
2023-09-07 02:06:19 +00:00
|
|
|
return IntegerOutput(value=np.ceil(self.value / self.multiple) * self.multiple)
|
2023-09-13 05:49:19 +00:00
|
|
|
else: # self.method == "Truncate"
|
2023-09-10 04:27:15 +00:00
|
|
|
return IntegerOutput(value=int(self.value / self.multiple) * self.multiple)
|
2023-09-06 01:27:08 +00:00
|
|
|
|
|
|
|
|
2023-09-07 02:06:19 +00:00
|
|
|
@invocation("round_float", title="Round Float", tags=["math", "round"], category="math", version="1.0.0")
|
2023-09-06 01:27:08 +00:00
|
|
|
class RoundInvocation(BaseInvocation):
|
|
|
|
"""Rounds a float to a specified number of decimal places."""
|
|
|
|
|
|
|
|
value: float = InputField(default=0, description="The float value")
|
|
|
|
decimals: int = InputField(default=0, description="The number of decimal places")
|
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> FloatOutput:
|
|
|
|
return FloatOutput(value=round(self.value, self.decimals))
|
|
|
|
|
|
|
|
|
2023-09-10 04:27:15 +00:00
|
|
|
INTEGER_OPERATIONS = Literal[
|
2023-09-13 06:24:46 +00:00
|
|
|
"ADD",
|
|
|
|
"SUB",
|
|
|
|
"MUL",
|
|
|
|
"DIV",
|
|
|
|
"EXP",
|
|
|
|
"MOD",
|
|
|
|
"ABS",
|
|
|
|
"MIN",
|
|
|
|
"MAX",
|
2023-09-10 04:27:15 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-09-13 06:24:46 +00:00
|
|
|
INTEGER_OPERATIONS_LABELS = dict(
|
|
|
|
ADD="Add A+B",
|
|
|
|
SUB="Subtract A-B",
|
|
|
|
MUL="Multiply A*B",
|
|
|
|
DIV="Divide A/B",
|
|
|
|
EXP="Exponentiate A^B",
|
|
|
|
MOD="Modulus A%B",
|
|
|
|
ABS="Absolute Value of A",
|
|
|
|
MIN="Minimum(A,B)",
|
|
|
|
MAX="Maximum(A,B)",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-10 04:27:15 +00:00
|
|
|
@invocation(
|
|
|
|
"integer_math",
|
|
|
|
title="Integer Math",
|
|
|
|
tags=[
|
|
|
|
"math",
|
|
|
|
"integer",
|
|
|
|
"add",
|
|
|
|
"subtract",
|
|
|
|
"multiply",
|
|
|
|
"divide",
|
|
|
|
"modulus",
|
|
|
|
"power",
|
|
|
|
"absolute value",
|
|
|
|
"min",
|
2023-09-13 05:49:19 +00:00
|
|
|
"max",
|
|
|
|
],
|
|
|
|
category="math",
|
|
|
|
version="1.0.0",
|
2023-09-10 04:27:15 +00:00
|
|
|
)
|
|
|
|
class IntegerMathInvocation(BaseInvocation):
|
|
|
|
"""Performs integer math."""
|
|
|
|
|
2023-09-13 06:24:46 +00:00
|
|
|
operation: INTEGER_OPERATIONS = InputField(
|
|
|
|
default="ADD", description="The operation to perform", ui_choice_labels=INTEGER_OPERATIONS_LABELS
|
|
|
|
)
|
2023-09-06 01:27:08 +00:00
|
|
|
a: int = InputField(default=0, description=FieldDescriptions.num_1)
|
|
|
|
b: int = InputField(default=0, description=FieldDescriptions.num_2)
|
|
|
|
|
2023-09-10 06:17:53 +00:00
|
|
|
@validator("b")
|
|
|
|
def no_unrepresentable_results(cls, v, values):
|
2023-09-13 06:24:46 +00:00
|
|
|
if values["operation"] == "DIV" and v == 0:
|
2023-09-10 04:27:15 +00:00
|
|
|
raise ValueError("Cannot divide by zero")
|
2023-09-13 06:24:46 +00:00
|
|
|
elif values["operation"] == "MOD" and v == 0:
|
2023-09-10 04:27:15 +00:00
|
|
|
raise ValueError("Cannot divide by zero")
|
2023-09-13 06:24:46 +00:00
|
|
|
elif values["operation"] == "EXP" and v < 0:
|
2023-09-10 04:27:15 +00:00
|
|
|
raise ValueError("Result of exponentiation is not an integer")
|
|
|
|
return v
|
2023-09-06 13:39:47 +00:00
|
|
|
|
2023-09-10 04:27:15 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> IntegerOutput:
|
2023-09-13 05:49:19 +00:00
|
|
|
# Python doesn't support switch statements until 3.10, but InvokeAI supports back to 3.9
|
2023-09-13 06:24:46 +00:00
|
|
|
if self.operation == "ADD":
|
2023-09-10 04:27:15 +00:00
|
|
|
return IntegerOutput(value=self.a + self.b)
|
2023-09-13 06:24:46 +00:00
|
|
|
elif self.operation == "SUB":
|
2023-09-10 04:27:15 +00:00
|
|
|
return IntegerOutput(value=self.a - self.b)
|
2023-09-13 06:24:46 +00:00
|
|
|
elif self.operation == "MUL":
|
2023-09-10 04:27:15 +00:00
|
|
|
return IntegerOutput(value=self.a * self.b)
|
2023-09-13 06:24:46 +00:00
|
|
|
elif self.operation == "DIV":
|
2023-09-10 04:27:15 +00:00
|
|
|
return IntegerOutput(value=int(self.a / self.b))
|
2023-09-13 06:24:46 +00:00
|
|
|
elif self.operation == "EXP":
|
2023-09-13 05:49:19 +00:00
|
|
|
return IntegerOutput(value=self.a**self.b)
|
2023-09-13 06:24:46 +00:00
|
|
|
elif self.operation == "MOD":
|
2023-09-10 04:27:15 +00:00
|
|
|
return IntegerOutput(value=self.a % self.b)
|
2023-09-13 06:24:46 +00:00
|
|
|
elif self.operation == "ABS":
|
2023-09-10 04:27:15 +00:00
|
|
|
return IntegerOutput(value=abs(self.a))
|
2023-09-13 06:24:46 +00:00
|
|
|
elif self.operation == "MIN":
|
2023-09-10 04:27:15 +00:00
|
|
|
return IntegerOutput(value=min(self.a, self.b))
|
2023-09-13 06:24:46 +00:00
|
|
|
else: # self.operation == "MAX":
|
2023-09-10 04:27:15 +00:00
|
|
|
return IntegerOutput(value=max(self.a, self.b))
|
|
|
|
|
|
|
|
|
|
|
|
FLOAT_OPERATIONS = Literal[
|
2023-09-13 06:24:46 +00:00
|
|
|
"ADD",
|
|
|
|
"SUB",
|
|
|
|
"MUL",
|
|
|
|
"DIV",
|
|
|
|
"EXP",
|
|
|
|
"ABS",
|
|
|
|
"SQRT",
|
|
|
|
"MIN",
|
|
|
|
"MAX",
|
2023-09-10 04:27:15 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-09-13 06:24:46 +00:00
|
|
|
FLOAT_OPERATIONS_LABELS = dict(
|
|
|
|
ADD="Add A+B",
|
|
|
|
SUB="Subtract A-B",
|
|
|
|
MUL="Multiply A*B",
|
|
|
|
DIV="Divide A/B",
|
|
|
|
EXP="Exponentiate A^B",
|
|
|
|
ABS="Absolute Value of A",
|
|
|
|
SQRT="Square Root of A",
|
|
|
|
MIN="Minimum(A,B)",
|
|
|
|
MAX="Maximum(A,B)",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-10 04:27:15 +00:00
|
|
|
@invocation(
|
|
|
|
"float_math",
|
|
|
|
title="Float Math",
|
2023-09-13 05:49:19 +00:00
|
|
|
tags=["math", "float", "add", "subtract", "multiply", "divide", "power", "root", "absolute value", "min", "max"],
|
2023-09-10 04:27:15 +00:00
|
|
|
category="math",
|
2023-09-13 05:49:19 +00:00
|
|
|
version="1.0.0",
|
2023-09-10 04:27:15 +00:00
|
|
|
)
|
|
|
|
class FloatMathInvocation(BaseInvocation):
|
|
|
|
"""Performs floating point math."""
|
|
|
|
|
2023-09-13 06:24:46 +00:00
|
|
|
operation: FLOAT_OPERATIONS = InputField(
|
|
|
|
default="ADD", description="The operation to perform", ui_choice_labels=FLOAT_OPERATIONS_LABELS
|
|
|
|
)
|
2023-09-10 04:27:15 +00:00
|
|
|
a: float = InputField(default=0, description=FieldDescriptions.num_1)
|
|
|
|
b: float = InputField(default=0, description=FieldDescriptions.num_2)
|
2023-09-06 13:39:47 +00:00
|
|
|
|
2023-09-10 06:17:53 +00:00
|
|
|
@validator("b")
|
|
|
|
def no_unrepresentable_results(cls, v, values):
|
2023-09-13 06:24:46 +00:00
|
|
|
if values["operation"] == "DIV" and v == 0:
|
2023-09-10 04:27:15 +00:00
|
|
|
raise ValueError("Cannot divide by zero")
|
2023-09-13 06:24:46 +00:00
|
|
|
elif values["operation"] == "EXP" and values["a"] == 0 and v < 0:
|
2023-09-10 04:27:15 +00:00
|
|
|
raise ValueError("Cannot raise zero to a negative power")
|
2023-09-13 06:24:46 +00:00
|
|
|
elif values["operation"] == "EXP" and type(values["a"] ** v) == complex:
|
2023-09-10 04:27:15 +00:00
|
|
|
raise ValueError("Root operation resulted in a complex number")
|
|
|
|
return v
|
2023-09-06 13:39:47 +00:00
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> FloatOutput:
|
2023-09-13 05:49:19 +00:00
|
|
|
# Python doesn't support switch statements until 3.10, but InvokeAI supports back to 3.9
|
2023-09-13 06:24:46 +00:00
|
|
|
if self.operation == "ADD":
|
2023-09-10 04:27:15 +00:00
|
|
|
return FloatOutput(value=self.a + self.b)
|
2023-09-13 06:24:46 +00:00
|
|
|
elif self.operation == "SUB":
|
2023-09-10 04:27:15 +00:00
|
|
|
return FloatOutput(value=self.a - self.b)
|
2023-09-13 06:24:46 +00:00
|
|
|
elif self.operation == "MUL":
|
2023-09-10 04:27:15 +00:00
|
|
|
return FloatOutput(value=self.a * self.b)
|
2023-09-13 06:24:46 +00:00
|
|
|
elif self.operation == "DIV":
|
2023-09-10 04:27:15 +00:00
|
|
|
return FloatOutput(value=self.a / self.b)
|
2023-09-13 06:24:46 +00:00
|
|
|
elif self.operation == "EXP":
|
2023-09-13 05:49:19 +00:00
|
|
|
return FloatOutput(value=self.a**self.b)
|
2023-09-13 06:24:46 +00:00
|
|
|
elif self.operation == "SQRT":
|
2023-09-10 04:27:15 +00:00
|
|
|
return FloatOutput(value=np.sqrt(self.a))
|
2023-09-13 06:24:46 +00:00
|
|
|
elif self.operation == "ABS":
|
2023-09-10 04:27:15 +00:00
|
|
|
return FloatOutput(value=abs(self.a))
|
2023-09-13 06:24:46 +00:00
|
|
|
elif self.operation == "MIN":
|
2023-09-10 04:27:15 +00:00
|
|
|
return FloatOutput(value=min(self.a, self.b))
|
2023-09-13 06:24:46 +00:00
|
|
|
else: # self.operation == "MAX":
|
2023-09-13 05:49:19 +00:00
|
|
|
return FloatOutput(value=max(self.a, self.b))
|