Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions replicate/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def encode_json(
return base64_encode_file(obj)
return client.files.create(obj).urls["get"]
if HAS_NUMPY:
if isinstance(obj, np.bool_): # type: ignore
return bool(obj)
if isinstance(obj, np.integer): # type: ignore
return int(obj)
if isinstance(obj, np.floating): # type: ignore
Expand Down Expand Up @@ -83,6 +85,8 @@ async def async_encode_json(
return base64_encode_file(obj)
return (await client.files.async_create(obj)).urls["get"]
if HAS_NUMPY:
if isinstance(obj, np.bool_): # type: ignore
return bool(obj)
if isinstance(obj, np.integer): # type: ignore
return int(obj)
if isinstance(obj, np.floating): # type: ignore
Expand Down
18 changes: 17 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import io
import json

import pytest

from replicate.helpers import base64_encode_file
from replicate.client import Client
from replicate.helpers import async_encode_json, base64_encode_file, encode_json


@pytest.mark.parametrize(
Expand Down Expand Up @@ -33,3 +35,17 @@ def test_base64_encode_file(content, filename, expected):
# Call the function and check the result
result = base64_encode_file(file)
assert result == expected


@pytest.mark.parametrize("value", [True, False])
async def test_numpy_boolean_prediction_inputs(value):
np = pytest.importorskip("numpy")
inputs = {"enabled": np.bool_(value), "flags": [np.bool_(value)]}
client = Client()
for payload in (
encode_json(inputs, client),
await async_encode_json(inputs, client),
):
decoded = json.loads(json.dumps(payload))
assert decoded["enabled"] is value
assert decoded["flags"][0] is value