Skip to content

Commit

Permalink
add a "quiet" flag to the HTTP API
Browse files Browse the repository at this point in the history
When "quiet" is enabled, prediction responses do not include the original
input. This change also keeps the typehints consistent by making `input`
optional in `PredictionResponse` (but still required in `PredictionRequest`).

Signed-off-by: Daniel Keyes <daniel.keyes@sony.com>
  • Loading branch information
danielkeyes-sony committed Oct 10, 2024
1 parent a6219fa commit c002cb3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions python/cog/command/ast_openapi_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
"default": ["start", "output", "logs", "completed"],
"items": { "$ref": "#/components/schemas/WebhookEvent" },
"type": "array"
},
"quiet": {
"title": "Quiet",
"type": "boolean",
"default": false
}
},
"title": "PredictionRequest",
Expand Down
8 changes: 6 additions & 2 deletions python/cog/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ def default_events(cls) -> List["WebhookEvent"]:


class PredictionBaseModel(pydantic.BaseModel):
input: Dict[str, Any]

if PYDANTIC_V2:
model_config = pydantic.ConfigDict(use_enum_values=True) # type: ignore
else:
Expand All @@ -66,6 +64,8 @@ class Config:


class PredictionRequest(PredictionBaseModel):
input: Dict[str, Any]

id: Optional[str] = None
created_at: Optional[datetime] = None

Expand All @@ -77,6 +77,8 @@ class PredictionRequest(PredictionBaseModel):
default=WebhookEvent.default_events(),
)

quiet: bool = False

@classmethod
def with_types(cls, input_type: Type[Any]) -> Any:
# [compat] Input is implicitly optional -- previous versions of the
Expand All @@ -88,6 +90,8 @@ def with_types(cls, input_type: Type[Any]) -> Any:


class PredictionResponse(PredictionBaseModel):
input: Optional[Dict[str, Any]] = None

output: Any = None

id: Optional[str] = None
Expand Down
3 changes: 3 additions & 0 deletions python/cog/server/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ def __init__(
else:
request_dict = prediction_request.dict()

if prediction_request.quiet:
request_dict.pop("input", None)

self._p = schema.PredictionResponse(**request_dict)
self._p.status = schema.Status.PROCESSING
self._output_type_multi = None
Expand Down
40 changes: 40 additions & 0 deletions python/tests/server/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,43 @@ def test_predict_task_file_uploads_multi():
"http://example.com/hello.jpg",
"http://example.com/world.jpg",
]


def test_predict_quiet():
p = PredictionRequest(
input={"hello": "there"},
id=None,
created_at=None,
output_file_prefix=None,
webhook=None,
quiet=False,
)
t = PredictTask(p)

assert t.result.status == Status.PROCESSING
assert t.result.output is None
assert t.result.logs == ""
assert isinstance(t.result.started_at, datetime)
t.set_output_type(multi=False)
t.append_output("giraffes")
assert t.result.output == "giraffes"
assert t.result.input == {"hello": "there"}

p = PredictionRequest(
input={"hello": "there"},
id=None,
created_at=None,
output_file_prefix=None,
webhook=None,
quiet=True,
)
t = PredictTask(p)

assert t.result.status == Status.PROCESSING
assert t.result.output is None
assert t.result.logs == ""
assert isinstance(t.result.started_at, datetime)
t.set_output_type(multi=False)
t.append_output("giraffes")
assert t.result.output == "giraffes"
assert t.result.input == None

0 comments on commit c002cb3

Please sign in to comment.