Skip to content

Commit 703db05

Browse files
committed
Document cog.File() and cog.Path() (#452)
* document cog.Path() Signed-off-by: Zeke Sikelianos <[email protected]> * document cog.File() Signed-off-by: Zeke Sikelianos <[email protected]> * fix import Signed-off-by: Zeke Sikelianos <[email protected]> * whoops don't forget Input Signed-off-by: Zeke Sikelianos <[email protected]> * update docs for cog.File and cog.Path based on feedback from Benzo Signed-off-by: Zeke Sikelianos <[email protected]>
1 parent 4aed616 commit 703db05

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

docs/python.md

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Tip: Run [`cog init`](getting-started-own-model#initialization) to generate an a
1111
- [`Predictor.predict(**kwargs)`](#predictorpredictkwargs)
1212
- [`Input(**kwargs)`](#inputkwargs)
1313
- [`Output(BaseModel)`](#outputbasemodel)
14+
- [`File()`](#file)
15+
- [`Path()`](#path)
1416

1517
## `BasePredictor`
1618

@@ -54,21 +56,7 @@ This _required_ method is where you call the model that was loaded during `setup
5456

5557
The `predict()` method takes an arbitrary list of named arguments, where each argument name must correspond to an [`Input()`](#inputkwargs) annotation.
5658

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-
def predict(self, image: Path = Input(description="Image to enlarge")) -> Path:
67-
output = do_some_processing(image)
68-
out_path = Path(tempfile.mkdtemp()) / "my-file.txt"
69-
out_path.write_text(output)
70-
return out_path
71-
```
59+
`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.
7260

7361
## `Input(**kwargs)`
7462

@@ -119,3 +107,45 @@ class Predictor(BasePredictor):
119107
def predict(self) -> Output:
120108
return Output(text="hello", file=io.StringIO("hello"))
121109
```
110+
111+
## `File()`
112+
113+
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+
from PIL import Image
120+
121+
class Predictor(BasePredictor):
122+
def predict(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+
class Predictor(BasePredictor):
143+
def predict(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.
148+
output_path = Path(tempfile.mkdtemp()) / "upscaled.png"
149+
upscaled_image.save(output)
150+
return Path(output_path)
151+
```

0 commit comments

Comments
 (0)