|
1 | 1 | import base64 |
2 | 2 | import io |
3 | 3 | import mimetypes |
| 4 | +from collections.abc import Mapping, Sequence |
4 | 5 | from pathlib import Path |
5 | 6 | from types import GeneratorType |
6 | | -from typing import TYPE_CHECKING, Any, Optional |
| 7 | +from typing import TYPE_CHECKING, Any, AsyncIterator, Iterator, Optional |
| 8 | + |
| 9 | +import httpx |
7 | 10 |
|
8 | 11 | if TYPE_CHECKING: |
9 | 12 | from replicate.client import Client |
@@ -108,3 +111,80 @@ def base64_encode_file(file: io.IOBase) -> str: |
108 | 111 | mimetypes.guess_type(getattr(file, "name", ""))[0] or "application/octet-stream" |
109 | 112 | ) |
110 | 113 | return f"data:{mime_type};base64,{encoded_body}" |
| 114 | + |
| 115 | + |
| 116 | +class FileOutput(httpx.SyncByteStream, httpx.AsyncByteStream): |
| 117 | + """ |
| 118 | + An object that can be used to read the contents of an output file |
| 119 | + created by running a Replicate model. |
| 120 | + """ |
| 121 | + |
| 122 | + url: str |
| 123 | + """ |
| 124 | + The file URL. |
| 125 | + """ |
| 126 | + |
| 127 | + _client: "Client" |
| 128 | + |
| 129 | + def __init__(self, url: str, client: "Client") -> None: |
| 130 | + self.url = url |
| 131 | + self._client = client |
| 132 | + |
| 133 | + def read(self) -> bytes: |
| 134 | + if self.url.startswith("data:"): |
| 135 | + _, encoded = self.url.split(",", 1) |
| 136 | + return base64.b64decode(encoded) |
| 137 | + |
| 138 | + with self._client._client.stream("GET", self.url) as response: |
| 139 | + response.raise_for_status() |
| 140 | + return response.read() |
| 141 | + |
| 142 | + def __iter__(self) -> Iterator[bytes]: |
| 143 | + if self.url.startswith("data:"): |
| 144 | + yield self.read() |
| 145 | + return |
| 146 | + |
| 147 | + with self._client._client.stream("GET", self.url) as response: |
| 148 | + response.raise_for_status() |
| 149 | + yield from response.iter_bytes() |
| 150 | + |
| 151 | + async def aread(self) -> bytes: |
| 152 | + if self.url.startswith("data:"): |
| 153 | + _, encoded = self.url.split(",", 1) |
| 154 | + return base64.b64decode(encoded) |
| 155 | + |
| 156 | + async with self._client._async_client.stream("GET", self.url) as response: |
| 157 | + response.raise_for_status() |
| 158 | + return await response.aread() |
| 159 | + |
| 160 | + async def __aiter__(self) -> AsyncIterator[bytes]: |
| 161 | + if self.url.startswith("data:"): |
| 162 | + yield await self.aread() |
| 163 | + return |
| 164 | + |
| 165 | + async with self._client._async_client.stream("GET", self.url) as response: |
| 166 | + response.raise_for_status() |
| 167 | + async for chunk in response.aiter_bytes(): |
| 168 | + yield chunk |
| 169 | + |
| 170 | + def __str__(self) -> str: |
| 171 | + return self.url |
| 172 | + |
| 173 | + |
| 174 | +def transform_output(value: Any, client: "Client") -> Any: |
| 175 | + """ |
| 176 | + Transform the output of a prediction to a `FileOutput` object if it's a URL. |
| 177 | + """ |
| 178 | + |
| 179 | + def transform(obj: Any) -> Any: |
| 180 | + if isinstance(obj, Mapping): |
| 181 | + return {k: transform(v) for k, v in obj.items()} |
| 182 | + elif isinstance(obj, Sequence) and not isinstance(obj, str): |
| 183 | + return [transform(item) for item in obj] |
| 184 | + elif isinstance(obj, str) and ( |
| 185 | + obj.startswith("https:") or obj.startswith("data:") |
| 186 | + ): |
| 187 | + return FileOutput(obj, client) |
| 188 | + return obj |
| 189 | + |
| 190 | + return transform(value) |
0 commit comments