You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -54,21 +56,7 @@ This _required_ method is where you call the model that was loaded during `setup
54
56
55
57
The `predict()` method takes an arbitrary list of named arguments, where each argument name must correspond to an [`Input()`](#inputkwargs) annotation.
56
58
57
-
`predict()` can return strings, numbers, `pathlib.Path` objects, or lists or dicts of those types. You can also define a custom [`Output()`](#outputbasemodel) for more complex return types.
58
-
59
-
#### Returning `pathlib.Path` objects
60
-
61
-
If the output is a `pathlib.Path` object, that will be returned by the built-in HTTP server as a file download.
62
-
63
-
To output `pathlib.Path` objects the file needs to exist, which means that you probably need to create a temporary file first. This file will automatically be deleted by Cog after it has been returned. For example:
64
-
65
-
```python
66
-
defpredict(self, image: Path = Input(description="Image to enlarge")) -> Path:
`predict()` can return strings, numbers, [`cog.Path`](#path) objects representing files on disk, or lists or dicts of those types. You can also define a custom [`Output()`](#outputbasemodel) for more complex return types.
72
60
73
61
## `Input(**kwargs)`
74
62
@@ -119,3 +107,45 @@ class Predictor(BasePredictor):
The `cog.File` object is used to get files in and out of models. It represents a _file handle_.
114
+
115
+
For models that return a `cog.File` object, the prediction output returned by Cog's built-in HTTP server will be a URL.
116
+
117
+
```python
118
+
from cog import BasePredictor, File, Input, Path
119
+
fromPILimport Image
120
+
121
+
classPredictor(BasePredictor):
122
+
defpredict(self, source_image: File = Input(description="Image to enlarge")) -> File:
123
+
pillow_img = Image.open(source_image)
124
+
upscaled_image = do_some_processing(pillow_img)
125
+
return File(upscaled_image)
126
+
```
127
+
128
+
## `Path()`
129
+
130
+
The `cog.Path` object is used to get files in and out of models. It represents a _path to a file on disk_.
131
+
132
+
`cog.Path` is a subclass of Python's [`pathlib.Path`](https://docs.python.org/3/library/pathlib.html#basic-use) and can be used as a drop-in replacement.
133
+
134
+
For models that return a `cog.Path` object, the prediction output returned by Cog's built-in HTTP server will be a URL.
135
+
136
+
This example takes an input file, resizes it, and returns the resized image:
137
+
138
+
```python
139
+
import tempfile
140
+
from cog import BasePredictor, Input, Path
141
+
142
+
classPredictor(BasePredictor):
143
+
defpredict(self, image: Path = Input(description="Image to enlarge")) -> Path:
144
+
upscaled_image = do_some_processing(image)
145
+
146
+
# To output `cog.Path` objects the file needs to exist, so create a temporary file first.
147
+
# This file will automatically be deleted by Cog after it has been returned.
0 commit comments