-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix a couple of bugs in the base64 file_encoding_strategy #398
Conversation
d405512
to
0fbc64a
Compare
This commit adds tests for the `file_encoding_strategy` argument for `replicate.run()` and fixes two bugs that surfaced: 1. `replicate.run()` would convert the file provided into base64 encoded data but not a valid data URL. We now use the `base64_encode_file` function used for outputs. 2. `replicate.async_run()` accepted but did not use the `file_encoding_strategy` flag at all. This is fixed, though it is worth noting that `base64_encode_file` is not optimized for async workflows and will block. This migth be okay as the file sizes expected for data URL paylaods should be very small.
0fbc64a
to
c724064
Compare
@@ -43,7 +43,7 @@ def encode_json( | |||
return encode_json(file, client, file_encoding_strategy) | |||
if isinstance(obj, io.IOBase): | |||
if file_encoding_strategy == "base64": | |||
return base64.b64encode(obj.read()).decode("utf-8") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lolwut. how did this ever work? did it just... not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It did not.
if isinstance(obj, io.IOBase): | ||
return (await client.files.async_create(obj)).urls["get"] | ||
if file_encoding_strategy == "base64": | ||
# TODO: This should ideally use an async based file reader path. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably isn't too hard if we're prepared to take a dep on aiofile
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM if I've understood correctly. Did the base64 support previously just not work?
This commit adds tests for the
file_encoding_strategy
argument forreplicate.run()
and fixes two bugs that surfaced:replicate.run()
would convert the file provided into base64 encoded data but not a valid data URL. We now use thebase64_encode_file
function used for outputs.replicate.async_run()
accepted but did not use thefile_encoding_strategy
flag at all. This is fixed, though it is worth noting thatbase64_encode_file
is not optimized for async workflows and will block. This might be okay as the file sizes expected for data URL payloads should be very small.