InvokeAI/invokeai/app/invocations/prompt.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

58 lines
2.0 KiB
Python
Raw Normal View History

2023-06-13 12:02:01 +00:00
from typing import Literal
2023-03-03 06:02:00 +00:00
from pydantic.fields import Field
2023-03-03 06:02:00 +00:00
2023-06-13 10:50:55 +00:00
from .baseinvocation import BaseInvocation, BaseInvocationOutput, InvocationContext
2023-06-13 12:02:01 +00:00
from dynamicprompts.generators import RandomPromptGenerator, CombinatorialPromptGenerator
2023-03-03 06:02:00 +00:00
class PromptOutput(BaseInvocationOutput):
"""Base class for invocations that output a prompt"""
#fmt: off
2023-03-03 06:02:00 +00:00
type: Literal["prompt"] = "prompt"
prompt: str = Field(default=None, description="The output prompt")
#fmt: on
class Config:
schema_extra = {
'required': [
'type',
'prompt',
]
}
2023-06-13 10:50:55 +00:00
2023-06-13 12:02:01 +00:00
class PromptCollectionOutput(BaseInvocationOutput):
"""Base class for invocations that output a collection of prompts"""
2023-06-13 10:50:55 +00:00
# fmt: off
2023-06-13 12:02:01 +00:00
type: Literal["prompt_collection_output"] = "prompt_collection_output"
2023-06-13 10:50:55 +00:00
2023-06-13 12:02:01 +00:00
prompt_collection: list[str] = Field(description="The output prompt collection")
count: int = Field(description="The size of the prompt collection")
2023-06-13 10:50:55 +00:00
# fmt: on
class Config:
2023-06-13 12:02:01 +00:00
schema_extra = {"required": ["type", "prompt_collection", "count"]}
2023-06-13 10:50:55 +00:00
class DynamicPromptInvocation(BaseInvocation):
"""Parses a prompt using adieyal/dynamicprompts' random or combinatorial generator"""
type: Literal["dynamic_prompt"] = "dynamic_prompt"
2023-06-13 12:02:01 +00:00
prompt: str = Field(description="The prompt to parse with dynamicprompts")
2023-06-13 10:50:55 +00:00
max_prompts: int = Field(default=1, description="The number of prompts to generate")
combinatorial: bool = Field(
default=False, description="Whether to use the combinatorial generator"
)
2023-06-13 12:02:01 +00:00
def invoke(self, context: InvocationContext) -> PromptCollectionOutput:
if self.combinatorial:
generator = CombinatorialPromptGenerator()
prompts = generator.generate(self.prompt, max_prompts=self.max_prompts)
else:
generator = RandomPromptGenerator()
prompts = generator.generate(self.prompt, num_images=self.max_prompts)
2023-06-13 10:50:55 +00:00
2023-06-13 12:02:01 +00:00
return PromptCollectionOutput(prompt_collection=prompts, count=len(prompts))