Skip to content

Commit

Permalink
Copy prompt parameters from custom workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
ronan36880 committed Nov 22, 2024
1 parent 04c7d08 commit 2dff431
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
18 changes: 18 additions & 0 deletions ai_diffusion/custom_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,10 @@ def live_result(self):
assert self._last_result and self._last_job, "No live result available"
return self._last_result, self._last_job

def try_set_params(self, params: dict):
self.params = _coerce_with_fallback(params, self.params, self.metadata)
self.graph_changed.emit()


def _coerce(params: dict[str, Any], types: list[CustomParam]):
def use(value, default):
Expand All @@ -575,3 +579,17 @@ def use(value, default):
return value

return {t.name: use(params.get(t.name), t.default) for t in types}


def _coerce_with_fallback(
params: dict[str, Any], fallback: dict[str, Any], types: list[CustomParam]
):
def use(value, fallback, default):
value_or_fallback = value if value is not None else fallback
if default is None:
return value_or_fallback
if value_or_fallback is None or not base_type_match(value, default):
return default
return value_or_fallback

return {t.name: use(params.get(t.name), fallback.get(t.name), t.default) for t in types}
5 changes: 4 additions & 1 deletion ai_diffusion/ui/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..properties import Binding, Bind, bind, bind_combo, bind_toggle
from ..image import Bounds, Extent, Image
from ..jobs import Job, JobQueue, JobState, JobKind, JobParams
from ..model import Model, InpaintContext, RootRegion, ProgressKind
from ..model import Model, InpaintContext, RootRegion, ProgressKind, Workspace
from ..style import Styles
from ..root import root
from ..workflow import InpaintMode, FillMode
Expand Down Expand Up @@ -374,6 +374,9 @@ def _copy_prompt(self):
if isinstance(active, RootRegion):
active.negative = job.params.metadata.get("negative_prompt", "")

if self._model.workspace is Workspace.custom and self._model.document.is_active:
self._model.custom.try_set_params(job.params.metadata)

def _copy_strength(self):
if job := self.selected_job:
self._model.strength = job.params.strength
Expand Down

0 comments on commit 2dff431

Please sign in to comment.