Skip to content

Commit 408e1c4

Browse files
andreasjanssonbfirsh
authored andcommitted
Document returning Path and input constraints in Python
Signed-off-by: andreasjansson <andreas@replicate.ai>
1 parent e236101 commit 408e1c4

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

docs/python.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,21 @@ The `run()` function takes an arbitrary list of named arguments, where each argu
2828

2929
`run()` can output strings, numbers, `pathlib.Path` objects, or lists or dicts of those types. We are working on support for other types of output, but for now we recommend using base-64 encoded strings or `pathlib.Path`s for more complex outputs.
3030

31+
### Returning `pathlib.Path` objects
32+
3133
If the output is a `pathlib.Path` object, that will be returned by the built-in HTTP server as a file download.
3234

33-
## `@cog.input(name, type, default, help)`
35+
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:
36+
37+
```python
38+
def predict(self, input):
39+
output = do_some_processing(input)
40+
out_path = Path(tempfile.mkdtemp()) / "my-file.txt"
41+
out_path.write_text(output)
42+
return out_path
43+
```
44+
45+
## `@cog.input(name, type, default, help, min=None, max=None, options=None)`
3446

3547
The `@cog.input()` annotation describes a single input to the `run()` function. The `name` must correspond to an argument name in `run()`.
3648

@@ -49,3 +61,7 @@ The `pathlib.Path` input type is used for file inputs.
4961
`default` can be any value, or `None` if the input is optional. If no `default` is set, the input is required.
5062

5163
You can document the input argument using `help`. This documentation is surfaced in `cog show <model-id>`.
64+
65+
Type-specific arguments:
66+
* `min` and `max` can be used with `int` and `float` inputs to constrain their ranges.
67+
* `options` can be used with `str`, `int`, and `float` inputs to limit the set of acceptable values.

0 commit comments

Comments
 (0)