Skip to content

Commit

Permalink
fix: Block CN from Inpainting (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
db0 authored Feb 18, 2024
1 parent 7444ae3 commit ecb4df5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

# 4.31.2

* Specialized some generic rcs
* Blocked ControlNet + Inpainting

# 4.31.1

* Documents rc return to swagger
Expand Down
17 changes: 16 additions & 1 deletion README_return_codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,19 @@ The errors returned by the AI horde are always in this json format
| BadRequest | Generic HTTP 400 code. You should typically never see this |
| Forbidden | Generic HTTP 401 code. You should typically never see this |
| Locked | Generic HTTP code. You should typically never see this |

| ControlNetInpaintingMismatch | ControlNet cannot be used with inpainting at this time |
| ControlNetInpaintingMismatch | ControlNet does not work with SDXL currently |
| HiResFixMismatch | hires fix does not work with SDXL currently |
| TooManyLoras | You cannot request more than 5 loras per generation |
| BadLoraVersion | explicit LoRa version requests have to be a version ID (i.e integer). |
| TooManyTIs | You cannot request more than 20 Textual Inversions per generation. |
| BetaAnonForbidden | Anonymous users cannot use the SDXL_beta. |
| BetaComparisonFault | You need to request at least 2 images for SDXL to allow for comparison |
| BadCFGDecimals | cfg_scale must be rounded to 2 decimal places |
| BadCFGNumber | cfg_scale must be a valid number |
| BannedClientAgent | Banned client Agent |
| SpecialMissingPayload | Special models have to include a special payload |
| SpecialForbidden | This model can only be requested by a specific username |
| SpecialMissingUsername | Special models must always include the username, in the form of 'horde_special::user#id' |
| SpecialModelNeedsSpecialUser | Only special users can request a special model.", "SpecialModelNeedsSpecialUser|
| SpecialFieldNeedsSpecialUser | Only special users can send a special field |
32 changes: 18 additions & 14 deletions horde/apis/v2/stable.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,22 @@ def validate(self):
if not self.safe_ip:
raise e.NotTrusted(rc="UntrustedUnsafeIP")
if not self.user.special and self.params.get("special"):
raise e.BadRequest("Only special users can send a special field.")
raise e.BadRequest("Only special users can send a special field.", "SpecialFieldNeedsSpecialUser")
for model in self.args.models:
if "horde_special" in model:
if not self.user.special:
raise e.BadRequest("Only special users can request a special model.")
raise e.Forbidden("Only special users can request a special model.", "SpecialModelNeedsSpecialUser")
usermodel = model.split("::")
if len(usermodel) == 1:
raise e.BadRequest(
"Special models must always include the username, in the form of 'horde_special::user#id'",
rc="SpecialMissingUsername"
)
user_alias = usermodel[1]
if self.user.get_unique_alias() != user_alias:
raise e.BadRequest(f"This model can only be requested by {user_alias}")
raise e.Forbidden(f"This model can only be requested by {user_alias}", "SpecialForbidden")
if not self.params.get("special"):
raise e.BadRequest("Special models have to include a special payload")
raise e.BadRequest("Special models have to include a special payload", rc="SpecialMissingPayload")
if not self.args.source_image and self.args.source_mask:
raise e.SourceMaskUnnecessary
if self.params.get("control_type") in ["normal", "mlsd", "hough"] and any(
Expand All @@ -135,19 +136,21 @@ def validate(self):
# raise e.UnsupportedModel("This feature is disabled for the moment.")
if "control_type" in self.params and not self.args.source_image:
raise e.BadRequest("Controlnet Requires a source image.", rc="ControlNetSourceMissing")
if "control_type" in self.params and self.args.source_processing == "inpainting":
raise e.BadRequest("ControlNet cannot be used with inpainting at this time", rc="ControlNetInpaintingMismatch")
if any(model_reference.get_model_baseline(model_name).startswith("stable_diffusion_xl") for model_name in self.args.models):
if self.params.get("hires_fix", False) is True:
raise e.BadRequest("hires fix does not work with SDXL currently.")
raise e.BadRequest("hires fix does not work with SDXL currently.", rc="HiResFixMismatch")
if "control_type" in self.params:
raise e.BadRequest("ControlNet does not work with SDXL currently.")
raise e.BadRequest("ControlNet does not work with SDXL currently.", rc="ControlNetSDXLMismatch")
if "loras" in self.params:
if len(self.params["loras"]) > 5:
raise e.BadRequest("You cannot request more than 5 loras per generation.")
raise e.BadRequest("You cannot request more than 5 loras per generation.", rc="TooManyLoras")
for lora in self.params["loras"]:
if lora.get("is_version") and not lora["name"].isdigit():
raise e.BadRequest("explicit LoRa version requests have to be a version ID (i.e integer).")
raise e.BadRequest("explicit LoRa version requests have to be a version ID (i.e integer).", rc="BadLoraVersion")
if "tis" in self.params and len(self.params["tis"]) > 20:
raise e.BadRequest("You cannot request more than 20 Textual Inversions per generation.")
raise e.BadRequest("You cannot request more than 20 Textual Inversions per generation.", rc="TooManyTIs")
if self.params.get("init_as_image") and self.params.get("return_control_map"):
raise e.UnsupportedModel(
"Invalid ControlNet parameters - cannot send inital map and return the same map",
Expand All @@ -160,10 +163,10 @@ def validate(self):
# If the beta has been requested, it takes over the model list
if "SDXL_beta::stability.ai#6901" in self.models:
if self.user.is_anon():
raise e.Forbidden("Anonymous users cannot use the SDXL_beta.")
raise e.Forbidden("Anonymous users cannot use the SDXL_beta.", rc="BetaAnonForbidden")
self.models = ["SDXL_beta::stability.ai#6901"]
if self.params["n"] == 1:
raise e.BadRequest("You need to request at least 2 images for SDXL to allow for comparison")
raise e.BadRequest("You need to request at least 2 images for SDXL to allow for comparison", rc="BetaComparisonFault")
# SDXL_Beta always generates 2 images
self.params["n"] = 2
# if any(model_name.startswith("stable_diffusion_2") for model_name in self.args.models):
Expand All @@ -182,18 +185,19 @@ def validate(self):
try:
rounded_cfg_scale = round(cfg_scale, 2)
if rounded_cfg_scale != cfg_scale:
raise e.BadRequest("cfg_scale must be rounded to 2 decimal places")
raise e.BadRequest("cfg_scale must be rounded to 2 decimal places", rc="BadCFGDecimals")
except (TypeError, ValueError):
logger.warning(
f"Invalid cfg_scale: {cfg_scale} for user {self.username} when it should be already validated.",
)
raise e.BadRequest("cfg_scale must be a valid number")
raise e.BadRequest("cfg_scale must be a valid number", rc="BadCFGNumber")

if self.args["Client-Agent"] in ["My-Project:v0.0.1:My-Contact"]:
raise e.BadRequest(
raise e.Forbidden(
"This Client-Agent appears badly designed and is causing too many warnings. "
"First ensure it provides a proper name and contact details. "
"Then contact us on Discord to discuss the issue it's creating.",
rc="BannedClientAgent"
)

# We split this into its own function, so that it may be overriden
Expand Down
2 changes: 1 addition & 1 deletion horde/consts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
HORDE_VERSION = "4.31.1"
HORDE_VERSION = "4.31.2"

WHITELISTED_SERVICE_IPS = {
"212.227.227.178", # Turing Bot
Expand Down
15 changes: 15 additions & 0 deletions horde/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@
"BadRequest",
"Forbidden",
"Locked",
"ControlNetMismatch",
"HiResFixMismatch",
"TooManyLoras",
"BadLoraVersion",
"TooManyTIs",
"BetaAnonForbidden",
"BetaComparisonFault",
"BadCFGDecimals",
"BadCFGNumber",
"BadClientAgent",
"SpecialMissingPayload",
"SpecialForbidden",
"SpecialMissingUsername",
"SpecialModelNeedsSpecialUser",
"SpecialFieldNeedsSpecialUser",
]


Expand Down

0 comments on commit ecb4df5

Please sign in to comment.