1
1
# TODO
2
- # - [x] Support downloading files and conversion into Path when schema is URL
3
- # - [x] Support list outputs
4
- # - [x] Support iterator outputs
5
- # - [x] Support helpers for working with ContatenateIterator
6
- # - [ ] Support reusing output URL when passing to new method
7
- # - [ ] Support lazy downloading of files into Path
8
2
# - [ ] Support text streaming
9
3
# - [ ] Support file streaming
10
4
# - [ ] Support asyncio variant
28
22
from replicate .version import Version
29
23
30
24
25
+ __all__ = ["use" , "get_path_url" ]
26
+
27
+
31
28
def _in_module_scope () -> bool :
32
29
"""
33
30
Returns True when called from top level module scope.
@@ -41,9 +38,6 @@ def _in_module_scope() -> bool:
41
38
return False
42
39
43
40
44
- __all__ = ["use" ]
45
-
46
-
47
41
def _has_concatenate_iterator_output_type (openapi_schema : dict ) -> bool :
48
42
"""
49
43
Returns true if the model output type is ConcatenateIterator or
@@ -218,29 +212,41 @@ def ensure_path() -> Path:
218
212
path = _download_file (target )
219
213
return path
220
214
221
- object .__setattr__ (self , "__target__ " , target )
222
- object .__setattr__ (self , "__path__ " , ensure_path )
215
+ object .__setattr__ (self , "__replicate_target__ " , target )
216
+ object .__setattr__ (self , "__replicate_path__ " , ensure_path )
223
217
224
218
def __getattribute__ (self , name ) -> Any :
225
- if name in ("__path__ " , "__target__ " ):
219
+ if name in ("__replicate_path__ " , "__replicate_target__ " ):
226
220
return object .__getattribute__ (self , name )
227
221
228
222
# TODO: We should cover other common properties on Path...
229
223
if name == "__class__" :
230
224
return Path
231
225
232
- return getattr (object .__getattribute__ (self , "__path__ " )(), name )
226
+ return getattr (object .__getattribute__ (self , "__replicate_path__ " )(), name )
233
227
234
228
def __setattr__ (self , name , value ) -> None :
235
- if name in ("__path__ " , "__target__ " ):
229
+ if name in ("__replicate_path__ " , "__replicate_target__ " ):
236
230
raise ValueError ()
237
231
238
- object .__setattr__ (object .__getattribute__ (self , "__path__" )(), name , value )
232
+ object .__setattr__ (
233
+ object .__getattribute__ (self , "__replicate_path__" )(), name , value
234
+ )
239
235
240
236
def __delattr__ (self , name ) -> None :
241
- if name in ("__path__ " , "__target__ " ):
237
+ if name in ("__replicate_path__ " , "__replicate_target__ " ):
242
238
raise ValueError ()
243
- delattr (object .__getattribute__ (self , "__path__" )(), name )
239
+ delattr (object .__getattribute__ (self , "__replicate_path__" )(), name )
240
+
241
+
242
+ def get_path_url (path : Any ) -> str | None :
243
+ """
244
+ Return the remote URL (if any) for a Path output from a model.
245
+ """
246
+ try :
247
+ return object .__getattribute__ (path , "__replicate_target__" )
248
+ except AttributeError :
249
+ return None
244
250
245
251
246
252
@dataclass
@@ -252,7 +258,7 @@ class Run:
252
258
prediction : Prediction
253
259
schema : dict
254
260
255
- def wait (self ) -> Union [Any , Iterator [Any ]]:
261
+ def output (self ) -> Union [Any , Iterator [Any ]]:
256
262
"""
257
263
Wait for the prediction to complete and return its output.
258
264
"""
@@ -330,7 +336,7 @@ def _version(self) -> Version | None:
330
336
331
337
def __call__ (self , ** inputs : Dict [str , Any ]) -> Any :
332
338
run = self .create (** inputs )
333
- return run .wait ()
339
+ return run .output ()
334
340
335
341
def create (self , ** inputs : Dict [str , Any ]) -> Run :
336
342
"""
@@ -341,8 +347,8 @@ def create(self, **inputs: Dict[str, Any]) -> Run:
341
347
for key , value in inputs .items ():
342
348
if isinstance (value , OutputIterator ) and value .is_concatenate :
343
349
processed_inputs [key ] = str (value )
344
- elif isinstance (value , PathProxy ):
345
- processed_inputs [key ] = object . __getattribute__ ( value , "__target__" )
350
+ elif url := get_path_url (value ):
351
+ processed_inputs [key ] = url
346
352
else :
347
353
processed_inputs [key ] = value
348
354
0 commit comments