Skip to content

Commit

Permalink
fix: prevent crash when style includes comfyui randomized prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
db0 committed Feb 2, 2025
1 parent e734892 commit ffdf8e0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ SPDX-License-Identifier: AGPL-3.0-or-later

# Changelog

# 4.46.2

* Fixes crash when styles included comfyUI-style {randomized|strings}
* Allows styles to avoid falling back to default params such as width/height. When a style doesn't have a width/height specified, and the user provided values for them, they will be used for the style instead of the horde defaults.


# 4.46.1

Fixes crash when trying to set messages to foreign workers
Expand Down
4 changes: 2 additions & 2 deletions horde/apis/models/stable_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ def __init__(self, api):
"ModelStyleInputParamsStable",
self.root_model_generation_payload_style_stable_nodefaults,
{
"steps": fields.Integer(default=30, required=False, min=1, max=500),
"steps": fields.Integer(required=False, min=1, max=500),
},
)
self.input_model_style = api.model(
Expand Down Expand Up @@ -1178,6 +1178,6 @@ def __init__(self, api):
"use_count": fields.Integer(description="The amount of times this style has been used in generations."),
"creator": fields.String(description="The alias of the user to whom this style belongs to.", example="db0#1"),
"examples": fields.List(fields.Nested(self.response_model_style_example, skip_none=True)),
"shared_key": fields.Nested(self.response_model_sharedkey_details),
"shared_key": fields.Nested(self.response_model_sharedkey_details, skip_none=True),
},
)
19 changes: 14 additions & 5 deletions horde/apis/v2/stable.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import requests
from flask import request
from flask_restx import Resource, reqparse
from loguru import logger

import horde.apis.limiter_api as lim
import horde.classes.base.stats as stats
from horde import exceptions as e
from horde.apis.models.stable_v2 import ImageModels, ImageParsers
from horde.apis.models.v2 import get_default_keys_of_model
from horde.apis.v2.base import (
GenerateTemplate,
JobPopTemplate,
Expand All @@ -38,7 +38,6 @@
from horde.flask import HORDE, cache, db
from horde.image import calculate_image_tiles, ensure_source_image_uploaded
from horde.limiter import limiter
from horde.logger import logger
from horde.model_reference import model_reference
from horde.patreon import patrons
from horde.utils import does_extra_text_reference_exist, hash_dictionary
Expand Down Expand Up @@ -382,13 +381,23 @@ def apply_style(self):
self.negprompt = "###" + self.negprompt
# We need to use defaultdict to avoid getting keyerrors in case the style author added
# Erroneous keys in the string
self.prompt = self.existing_style.prompt.format_map(defaultdict(str, p=self.prompt, np=self.negprompt))
# We have to do this sort of replacement to prevent randomized comfyUI strings from getting confused
# With the {p}/{np} prompt replacement areas
self.prompt = (
self.existing_style.prompt.replace("{", "{{")
.replace("}", "}}")
.replace("{{p}}", "{p}")
.replace("{{np}}", "{np}")
.format_map(defaultdict(str, p=self.prompt, np=self.negprompt))
)
# self.prompt = self.existing_style.prompt.format_map(defaultdict(str, p=self.prompt, np=self.negprompt))
requested_n = self.params.get("n", 1)
user_params = self.params
self.params = self.existing_style.params
self.params["n"] = requested_n
# This is to prevent mandatory params from going missing after applying a style which doesn't provide them.
for default_param in get_default_keys_of_model(models.input_model_generation_payload):
# This allows a style without specified width/height to receive these variables from the user.
default_params = {"width", "height"}
for default_param in default_params:
if default_param not in self.params and default_param in user_params:
self.params[default_param] = user_params[default_param]
self.nsfw = self.existing_style.nsfw
Expand Down
2 changes: 1 addition & 1 deletion horde/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: AGPL-3.0-or-later

HORDE_VERSION = "4.46.0"
HORDE_VERSION = "4.46.2"
HORDE_API_VERSION = "2.5"

WHITELISTED_SERVICE_IPS = {
Expand Down

0 comments on commit ffdf8e0

Please sign in to comment.