File tree Expand file tree Collapse file tree 3 files changed +20
-18
lines changed
Expand file tree Collapse file tree 3 files changed +20
-18
lines changed Original file line number Diff line number Diff line change 11import base64
22import io
33import mimetypes
4+ from collections .abc import Mapping , Sequence
45from pathlib import Path
56from types import GeneratorType
67from typing import TYPE_CHECKING , Any , Optional
@@ -108,3 +109,20 @@ def base64_encode_file(file: io.IOBase) -> str:
108109 mimetypes .guess_type (getattr (file , "name" , "" ))[0 ] or "application/octet-stream"
109110 )
110111 return f"data:{ mime_type } ;base64,{ encoded_body } "
112+
113+
114+ def transform_output (value : Any , client : "Client" ) -> Any :
115+ from replicate .stream import FileOutput # pylint: disable=import-outside-toplevel
116+
117+ def transform (obj : Any ) -> Any :
118+ if isinstance (obj , Mapping ):
119+ return {k : transform (v ) for k , v in obj .items ()}
120+ elif isinstance (obj , Sequence ) and not isinstance (obj , str ):
121+ return [transform (item ) for item in obj ]
122+ elif isinstance (obj , str ) and (
123+ obj .startswith ("https:" ) or obj .startswith ("data:" )
124+ ):
125+ return FileOutput (obj , client )
126+ return obj
127+
128+ return transform (value )
Original file line number Diff line number Diff line change 1- from collections .abc import Mapping , Sequence
21from typing import (
32 TYPE_CHECKING ,
43 Any ,
1413
1514from replicate import identifier
1615from replicate .exceptions import ModelError
16+ from replicate .helpers import transform_output
1717from replicate .model import Model
1818from replicate .prediction import Prediction
1919from replicate .schema import make_schema_backwards_compatible
20- from replicate .stream import FileOutput
2120from replicate .version import Version , Versions
2221
2322if TYPE_CHECKING :
@@ -140,19 +139,4 @@ def _make_async_output_iterator(
140139 return None
141140
142141
143- def transform_output (value : Any , client : "Client" ) -> Any :
144- def transform (obj : Any ) -> Any :
145- if isinstance (obj , Mapping ):
146- return {k : transform (v ) for k , v in obj .items ()}
147- elif isinstance (obj , Sequence ) and not isinstance (obj , str ):
148- return [transform (item ) for item in obj ]
149- elif isinstance (obj , str ) and (
150- obj .startswith ("https:" ) or obj .startswith ("data:" )
151- ):
152- return FileOutput (obj , client )
153- return obj
154-
155- return transform (value )
156-
157-
158142__all__ : List = []
Original file line number Diff line number Diff line change 11import asyncio
22import sys
3+ from typing import cast
34
45import httpx
56import pytest
67import respx
78
8- from typing import cast
99import replicate
1010from replicate .client import Client
1111from replicate .exceptions import ModelError , ReplicateError
You can’t perform that action at this time.
0 commit comments