Skip to content

Commit

Permalink
fix: account for empty skipped in ImageGenerateJobPopResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
tazlin committed Feb 1, 2024
1 parent 07e0e4e commit f1866b5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions horde_sdk/ai_horde_api/apimodels/generate/_pop.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class ImageGenerateJobPopSkippedStatus(HordeAPIDataObject):
controlnet: int = Field(default=0, ge=0)
"""How many waiting requests were skipped because they requested a controlnet."""

def is_empty(self) -> bool:
"""Whether or not this object has any non-zero values."""
return len(self.model_fields_set) == 0


class ImageGenerateJobPopPayload(ImageGenerateParamMixin):
prompt: str | None = None
Expand Down Expand Up @@ -128,6 +132,9 @@ def validate_id(cls, v: str | JobID) -> JobID | str:
@model_validator(mode="after")
def ids_present(self) -> ImageGenerateJobPopResponse:
"""Ensure that either id_ or ids is present."""
if self.skipped.is_empty() and self.model is None:
return self

if self.id_ is None and len(self.ids) == 0:
raise ValueError("Neither id_ nor ids were present in the response.")

Expand Down
39 changes: 39 additions & 0 deletions tests/ai_horde_api/test_ai_horde_api_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Unit tests for AI-Horde API models."""
import json
from uuid import UUID

import pytest
Expand Down Expand Up @@ -604,3 +605,41 @@ def test_AlchemyPopResponse() -> None:
container_multiple_forms = {test_alchemy_pop_response_multiple_forms}
assert test_alchemy_pop_response_multiple_forms in container_multiple_forms
assert test_alchemy_pop_response_multiple_forms_copy in container_multiple_forms


def test_ImageGenerateJobPopSkippedStatus() -> None:
testing_skipped_status = ImageGenerateJobPopSkippedStatus()

assert testing_skipped_status.is_empty()


def test_problem_payload() -> None:
json_from_api = """
{
"payload": {
"ddim_steps": 30,
"n_iter": 1,
"sampler_name": "k_euler_a",
"cfg_scale": 7.5,
"height": 512,
"width": 512,
"karras": false,
"tiling": false,
"hires_fix": false,
"image_is_control": false,
"return_control_map": false
},
"id": null,
"ids": [],
"skipped": {},
"model": null,
"source_image": null,
"source_processing": "img2img",
"source_mask": null,
"r2_upload": null,
"r2_uploads": null
}"""

problem_payload = json.loads(json_from_api)

ImageGenerateJobPopResponse.model_validate(problem_payload)

0 comments on commit f1866b5

Please sign in to comment.