Fix a couple of bugs in the base64 file_encoding_strategy#398
Merged
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
| 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") |
Contributor
There was a problem hiding this comment.
lolwut. how did this ever work? did it just... 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. |
Contributor
There was a problem hiding this comment.
This probably isn't too hard if we're prepared to take a dep on aiofile.
nickstenning
approved these changes
Nov 15, 2024
Contributor
nickstenning
left a comment
There was a problem hiding this comment.
LGTM if I've understood correctly. Did the base64 support previously just not work?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit adds tests for the
file_encoding_strategyargument 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_filefunction used for outputs.replicate.async_run()accepted but did not use thefile_encoding_strategyflag at all. This is fixed, though it is worth noting thatbase64_encode_fileis 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.