|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import io |
| 4 | +import base64 |
| 5 | +import mimetypes |
| 6 | +from typing import Any, Iterator, AsyncIterator |
| 7 | +from typing_extensions import override |
| 8 | + |
| 9 | +import httpx |
| 10 | + |
| 11 | +from .._utils import is_mapping, is_sequence |
| 12 | +from .._client import ReplicateClient, AsyncReplicateClient |
| 13 | + |
| 14 | + |
| 15 | +def base64_encode_file(file: io.IOBase) -> str: |
| 16 | + """ |
| 17 | + Base64 encode a file. |
| 18 | +
|
| 19 | + Args: |
| 20 | + file: A file handle to upload. |
| 21 | + Returns: |
| 22 | + str: A base64-encoded data URI. |
| 23 | + """ |
| 24 | + |
| 25 | + file.seek(0) |
| 26 | + body = file.read() |
| 27 | + |
| 28 | + # Ensure the file handle is in bytes |
| 29 | + body = body.encode("utf-8") if isinstance(body, str) else body |
| 30 | + encoded_body = base64.b64encode(body).decode("utf-8") |
| 31 | + |
| 32 | + mime_type = mimetypes.guess_type(getattr(file, "name", ""))[0] or "application/octet-stream" |
| 33 | + return f"data:{mime_type};base64,{encoded_body}" |
| 34 | + |
| 35 | + |
| 36 | +class FileOutput(httpx.SyncByteStream): |
| 37 | + """ |
| 38 | + An object that can be used to read the contents of an output file |
| 39 | + created by running a Replicate model. |
| 40 | + """ |
| 41 | + |
| 42 | + url: str |
| 43 | + """ |
| 44 | + The file URL. |
| 45 | + """ |
| 46 | + |
| 47 | + _client: ReplicateClient |
| 48 | + |
| 49 | + def __init__(self, url: str, client: ReplicateClient) -> None: |
| 50 | + self.url = url |
| 51 | + self._client = client |
| 52 | + |
| 53 | + def read(self) -> bytes: |
| 54 | + if self.url.startswith("data:"): |
| 55 | + _, encoded = self.url.split(",", 1) |
| 56 | + return base64.b64decode(encoded) |
| 57 | + |
| 58 | + with self._client._client.stream("GET", self.url) as response: |
| 59 | + response.raise_for_status() |
| 60 | + return response.read() |
| 61 | + |
| 62 | + @override |
| 63 | + def __iter__(self) -> Iterator[bytes]: |
| 64 | + if self.url.startswith("data:"): |
| 65 | + yield self.read() |
| 66 | + return |
| 67 | + |
| 68 | + with self._client._client.stream("GET", self.url) as response: |
| 69 | + response.raise_for_status() |
| 70 | + yield from response.iter_bytes() |
| 71 | + |
| 72 | + @override |
| 73 | + def __str__(self) -> str: |
| 74 | + return self.url |
| 75 | + |
| 76 | + @override |
| 77 | + def __repr__(self) -> str: |
| 78 | + return f'{self.__class__.__name__}("{self.url}")' |
| 79 | + |
| 80 | + |
| 81 | +class AsyncFileOutput(httpx.AsyncByteStream): |
| 82 | + """ |
| 83 | + An object that can be used to read the contents of an output file |
| 84 | + created by running a Replicate model. |
| 85 | + """ |
| 86 | + |
| 87 | + url: str |
| 88 | + """ |
| 89 | + The file URL. |
| 90 | + """ |
| 91 | + |
| 92 | + _client: AsyncReplicateClient |
| 93 | + |
| 94 | + def __init__(self, url: str, client: AsyncReplicateClient) -> None: |
| 95 | + self.url = url |
| 96 | + self._client = client |
| 97 | + |
| 98 | + async def read(self) -> bytes: |
| 99 | + if self.url.startswith("data:"): |
| 100 | + _, encoded = self.url.split(",", 1) |
| 101 | + return base64.b64decode(encoded) |
| 102 | + |
| 103 | + async with self._client._client.stream("GET", self.url) as response: |
| 104 | + response.raise_for_status() |
| 105 | + return await response.aread() |
| 106 | + |
| 107 | + @override |
| 108 | + async def __aiter__(self) -> AsyncIterator[bytes]: |
| 109 | + if self.url.startswith("data:"): |
| 110 | + yield await self.read() |
| 111 | + return |
| 112 | + |
| 113 | + async with self._client._client.stream("GET", self.url) as response: |
| 114 | + response.raise_for_status() |
| 115 | + async for chunk in response.aiter_bytes(): |
| 116 | + yield chunk |
| 117 | + |
| 118 | + @override |
| 119 | + def __str__(self) -> str: |
| 120 | + return self.url |
| 121 | + |
| 122 | + @override |
| 123 | + def __repr__(self) -> str: |
| 124 | + return f'{self.__class__.__name__}("{self.url}")' |
| 125 | + |
| 126 | + |
| 127 | +def transform_output(value: Any, client: ReplicateClient | AsyncReplicateClient) -> Any: |
| 128 | + """ |
| 129 | + Transform the output of a prediction to a `FileOutput` object if it's a URL. |
| 130 | + """ |
| 131 | + |
| 132 | + def transform(obj: Any) -> Any: |
| 133 | + if is_mapping(obj): |
| 134 | + return {k: transform(v) for k, v in obj.items()} |
| 135 | + elif is_sequence(obj) and not isinstance(obj, str): |
| 136 | + return [transform(item) for item in obj] |
| 137 | + elif isinstance(obj, str) and (obj.startswith("https:") or obj.startswith("data:")): |
| 138 | + if isinstance(client, AsyncReplicateClient): |
| 139 | + return AsyncFileOutput(obj, client) |
| 140 | + return FileOutput(obj, client) |
| 141 | + return obj |
| 142 | + |
| 143 | + return transform(value) |
0 commit comments