-
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
Introduce experimental FileOutput interface for models that output File and Path types #348
Conversation
Signed-off-by: Mattt Zmuda <mattt@replicate.com>
Signed-off-by: Mattt Zmuda <mattt@replicate.com>
Signed-off-by: Mattt Zmuda <mattt@replicate.com>
Signed-off-by: Mattt Zmuda <mattt@replicate.com>
Signed-off-by: Mattt Zmuda <mattt@replicate.com>
Signed-off-by: Mattt Zmuda <mattt@replicate.com>
SyncByteStream is an abstract class; ByteStream is a concrete class that inherits abstracts sync and async byte stream classes Signed-off-by: Mattt Zmuda <mattt@replicate.com>
Signed-off-by: Mattt Zmuda <mattt@replicate.com>
Signed-off-by: Mattt Zmuda <mattt@replicate.com>
Signed-off-by: Mattt Zmuda <mattt@replicate.com>
Signed-off-by: Mattt Zmuda <mattt@replicate.com>
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.
Great work, @aron! I made a couple changes and moved some things around to better match project conventions, but overall I think this is looking great.
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.
I love it. This is really excellent.
A thought about the iterator semantics... are there any existing patterns for iterators on file handles returning chunks of data? Usually file handles return lines or single bytes if you iterate over them, and HTTPX responses have the iter_*()
methods.
It feels like we should be consistent with one of those patterns rather than invent a new pattern. If it's the file handle pattern, which feels like the more universal thing to me, then we could also support the length argument to read()
to read chunks.
Not a big deal anyway and we can always iterate (zing!) before release.
@bfirsh this is great feedback thanks. I started off implementing If adding support for |
I've added support for data-uris. This puts it into parity with JS implementation. Also I did a quick skim of common web frameworks Django, Flask and FastAPI, all support streaming HTTP responses with iterators so I've updated the PR description with examples. But this aspect feels pretty good with the current implementation. |
OK brillant. If this is compatible with things that expect to get a binary file handle type thing, that's all I care about. @mattt Do you have any thoughts about file handle and iterator semantics? |
This PR is a proposal to add a new
FileOutput
type to the client SDK to abstract away file outputs from Replicate models.It can be enabled by passing the
use_file_output
flag to therun()
method (this will be moved to the constructor).When enabled any URLs (and soon data-uris) will be converted into a
FileOutput type
. This is essentially anIterable[bytes] | AsyncIterable[bytes]
that has two additional fields, the attributeurl
referencing underlying URL andread()
which will returnbytes
with the file data loaded into memory.The intention here is to make it easier to work with file outputs and allows us to optimize the delivery of file assets to the client in future iterations.
Usage is as follows:
For most basic cases you'll want to utilize either the
url
orread()
fields depending on whether you want to directly consume the file or pass it on.To access the file URL:
To consume the file directly:
Or for very large files they can be streamed:
Each of these methods has an equivalent
asyncio
API.For streaming responses from common frameworks, all support taking
Iterator
types:Django
FastAPI
Flask