2023-07-16 13:58:55 +00:00
|
|
|
from os.path import exists
|
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 typing import Optional, Union
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2023-07-15 11:17:18 +00:00
|
|
|
import numpy as np
|
2023-08-14 09:41:29 +00:00
|
|
|
from dynamicprompts.generators import CombinatorialPromptGenerator, RandomPromptGenerator
|
2023-08-14 03:23:09 +00:00
|
|
|
from pydantic import validator
|
|
|
|
|
2023-08-14 09:41:29 +00:00
|
|
|
from invokeai.app.invocations.primitives import StringCollectionOutput
|
2023-03-03 06:02:00 +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, InputField, InvocationContext, UIComponent, invocation
|
2023-06-13 10:50:55 +00:00
|
|
|
|
|
|
|
|
2023-09-04 08:11:56 +00:00
|
|
|
@invocation("dynamic_prompt", title="Dynamic Prompt", tags=["prompt", "collection"], category="prompt", version="1.0.0")
|
2023-06-13 10:50:55 +00:00
|
|
|
class DynamicPromptInvocation(BaseInvocation):
|
|
|
|
"""Parses a prompt using adieyal/dynamicprompts' random or combinatorial generator"""
|
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
prompt: str = InputField(description="The prompt to parse with dynamicprompts", ui_component=UIComponent.Textarea)
|
|
|
|
max_prompts: int = InputField(default=1, description="The number of prompts to generate")
|
|
|
|
combinatorial: bool = InputField(default=False, description="Whether to use the combinatorial generator")
|
2023-07-18 14:26:45 +00:00
|
|
|
|
2023-08-14 09:41:29 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> StringCollectionOutput:
|
2023-06-13 12:02:01 +00:00
|
|
|
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-08-14 09:41:29 +00:00
|
|
|
return StringCollectionOutput(collection=prompts)
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2023-07-15 11:17:18 +00:00
|
|
|
|
2023-09-04 08:11:56 +00:00
|
|
|
@invocation("prompt_from_file", title="Prompts from File", tags=["prompt", "file"], category="prompt", version="1.0.0")
|
2023-07-15 11:17:18 +00:00
|
|
|
class PromptsFromFileInvocation(BaseInvocation):
|
|
|
|
"""Loads prompts from a text file"""
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2023-08-24 11:42:32 +00:00
|
|
|
file_path: str = InputField(description="Path to prompt text file")
|
2023-08-14 03:23:09 +00:00
|
|
|
pre_prompt: Optional[str] = InputField(
|
2023-08-14 09:41:29 +00:00
|
|
|
default=None, description="String to prepend to each prompt", ui_component=UIComponent.Textarea
|
2023-08-14 03:23:09 +00:00
|
|
|
)
|
|
|
|
post_prompt: Optional[str] = InputField(
|
2023-08-14 09:41:29 +00:00
|
|
|
default=None, description="String to append to each prompt", ui_component=UIComponent.Textarea
|
2023-08-14 03:23:09 +00:00
|
|
|
)
|
|
|
|
start_line: int = InputField(default=1, ge=1, description="Line in the file to start start from")
|
|
|
|
max_prompts: int = InputField(default=1, ge=0, description="Max lines to read from file (0=all)")
|
2023-07-18 14:26:45 +00:00
|
|
|
|
2023-07-16 13:58:55 +00:00
|
|
|
@validator("file_path")
|
|
|
|
def file_path_exists(cls, v):
|
|
|
|
if not exists(v):
|
2023-07-16 15:27:01 +00:00
|
|
|
raise ValueError(FileNotFoundError)
|
2023-07-16 13:58:55 +00:00
|
|
|
return v
|
|
|
|
|
2023-08-14 09:41:29 +00:00
|
|
|
def promptsFromFile(
|
|
|
|
self,
|
|
|
|
file_path: str,
|
|
|
|
pre_prompt: Union[str, None],
|
|
|
|
post_prompt: Union[str, None],
|
|
|
|
start_line: int,
|
|
|
|
max_prompts: int,
|
|
|
|
):
|
2023-07-15 11:17:18 +00:00
|
|
|
prompts = []
|
|
|
|
start_line -= 1
|
|
|
|
end_line = start_line + max_prompts
|
|
|
|
if max_prompts <= 0:
|
|
|
|
end_line = np.iinfo(np.int32).max
|
2023-07-16 13:58:55 +00:00
|
|
|
with open(file_path) as f:
|
2023-07-15 11:17:18 +00:00
|
|
|
for i, line in enumerate(f):
|
|
|
|
if i >= start_line and i < end_line:
|
2023-07-16 13:58:55 +00:00
|
|
|
prompts.append((pre_prompt or "") + line.strip() + (post_prompt or ""))
|
2023-07-15 11:17:18 +00:00
|
|
|
if i >= end_line:
|
|
|
|
break
|
|
|
|
return prompts
|
|
|
|
|
2023-08-14 09:41:29 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> StringCollectionOutput:
|
2023-07-16 13:58:55 +00:00
|
|
|
prompts = self.promptsFromFile(
|
|
|
|
self.file_path, self.pre_prompt, self.post_prompt, self.start_line, self.max_prompts
|
|
|
|
)
|
2023-08-14 09:41:29 +00:00
|
|
|
return StringCollectionOutput(collection=prompts)
|