seed default presets and handle them in UI

This commit is contained in:
Mary Hipp 2024-08-08 15:02:41 -04:00
parent 587f59b25b
commit 3f9a674d4b
4 changed files with 70 additions and 25 deletions

View File

@ -0,0 +1,16 @@
[
{
"name": "Concept Art (Painterly)",
"preset_data": {
"positive_prompt": "{prompt} (digital painting)++, (textured 2d media)+++, depth and (painterly artwork)+++, intricate linework and brushwork, brush collection, highlights and shading, detail",
"negative_prompt": "photo++++ negative, faded, distorted, sketch, , flat+. anime, smooth+++"
}
},
{
"name": "Photography (Black and White)",
"preset_data": {
"positive_prompt": "(minimalist black and white photo)+++, (strong contrast)++, (clean textures)++, natural light, sharp focus, high detail, stark, dramatic, serene, simple composition, atmospheric, moody, refined, shadow play, 50mm lens, f/4",
"negative_prompt": "negative, blurry, faded, distorted, sketch, flat+. anime"
}
}
]

View File

@ -23,11 +23,11 @@ class StylePresetChanges(BaseModel, extra="forbid"):
class StylePresetWithoutId(BaseModel): class StylePresetWithoutId(BaseModel):
name: str = Field(description="The name of the style preset.") name: str = Field(description="The name of the style preset.")
preset_data: PresetData = Field(description="The preset data") preset_data: PresetData = Field(description="The preset data")
is_default: Optional[bool] = Field(description="Whether or not the style preset is default", default=False)
class StylePresetRecordDTO(StylePresetWithoutId): class StylePresetRecordDTO(StylePresetWithoutId):
id: str = Field(description="The style preset ID.") id: str = Field(description="The style preset ID.")
is_default: bool = Field(description="Whether or not the style preset is default")
@classmethod @classmethod
def from_dict(cls, data: dict[str, Any]) -> "StylePresetRecordDTO": def from_dict(cls, data: dict[str, Any]) -> "StylePresetRecordDTO":

View File

@ -1,3 +1,5 @@
import json
from pathlib import Path
from invokeai.app.services.invoker import Invoker from invokeai.app.services.invoker import Invoker
from invokeai.app.services.shared.sqlite.sqlite_database import SqliteDatabase from invokeai.app.services.shared.sqlite.sqlite_database import SqliteDatabase
from invokeai.app.services.style_preset_records.style_preset_records_base import StylePresetRecordsStorageBase from invokeai.app.services.style_preset_records.style_preset_records_base import StylePresetRecordsStorageBase
@ -19,6 +21,7 @@ class SqliteStylePresetRecordsStorage(StylePresetRecordsStorageBase):
def start(self, invoker: Invoker) -> None: def start(self, invoker: Invoker) -> None:
self._invoker = invoker self._invoker = invoker
self._sync_default_style_presets()
def get(self, id: str) -> StylePresetRecordDTO: def get(self, id: str) -> StylePresetRecordDTO:
"""Gets a style preset by ID.""" """Gets a style preset by ID."""
@ -51,15 +54,12 @@ class SqliteStylePresetRecordsStorage(StylePresetRecordsStorageBase):
INSERT OR IGNORE INTO style_presets ( INSERT OR IGNORE INTO style_presets (
id, id,
name, name,
preset_data preset_data,
is_default
) )
VALUES (?, ?, ?); VALUES (?, ?, ?, ?);
""", """,
( (id, style_preset.name, style_preset.preset_data.model_dump_json(), style_preset.is_default),
id,
style_preset.name,
style_preset.preset_data.model_dump_json(),
),
) )
self._conn.commit() self._conn.commit()
except Exception: except Exception:
@ -142,3 +142,30 @@ class SqliteStylePresetRecordsStorage(StylePresetRecordsStorageBase):
raise raise
finally: finally:
self._lock.release() self._lock.release()
def _sync_default_style_presets(self) -> None:
"""Syncs default style presets to the database. Internal use only."""
try:
self._lock.acquire()
self._cursor.execute(
"""--sql
DELETE FROM style_presets
WHERE is_default = True;
"""
)
try:
with open(Path(__file__).parent / Path("default_style_presets.json"), "r") as file:
presets: list[StylePresetWithoutId] = json.load(file)
for preset in presets:
style_preset = StylePresetWithoutId(is_default=True, **preset)
self.create(style_preset)
except Exception as e:
raise Exception()
self._conn.commit()
except Exception:
self._conn.rollback()
raise
finally:
self._lock.release()

View File

@ -110,6 +110,7 @@ export const StylePresetListItem = ({ preset }: { preset: StylePresetRecordWithI
)} )}
</Flex> </Flex>
{!preset.is_default && (
<Flex alignItems="center" gap="1"> <Flex alignItems="center" gap="1">
<IconButton <IconButton
size="sm" size="sm"
@ -127,6 +128,7 @@ export const StylePresetListItem = ({ preset }: { preset: StylePresetRecordWithI
icon={<PiTrashBold />} icon={<PiTrashBold />}
/> />
</Flex> </Flex>
)}
</Flex> </Flex>
<Flex flexDir="column" gap="1"> <Flex flexDir="column" gap="1">