-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for list in inputs (#1561)
- added support for list[File] and list[Path]; - only in cog, still need to add list support in web UI; - updated docs/python.md;
- Loading branch information
Showing
21 changed files
with
174 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -269,3 +269,29 @@ class Predictor(BasePredictor): | |
upscaled_image.save(output_path) | ||
return Path(output_path) | ||
``` | ||
|
||
## `List` | ||
|
||
The List type is also supported in inputs. It can hold any supported type. | ||
|
||
Example for **List[Path]**: | ||
```py | ||
class Predictor(BasePredictor): | ||
def predict(self, paths: list[Path]) -> str: | ||
output_parts = [] # Use a list to collect file contents | ||
for path in paths: | ||
with open(path) as f: | ||
output_parts.append(f.read()) | ||
return "".join(output_parts) | ||
``` | ||
The corresponding cog command: | ||
```bash | ||
$ echo test1 > 1.txt | ||
$ echo test2 > 2.txt | ||
$ cog predict -i [email protected] -i [email protected] | ||
Running prediction... | ||
test1 | ||
|
||
test2 | ||
``` | ||
- Note the repeated inputs with the same name "paths" which constitute the list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 7 additions & 4 deletions
11
test-integration/test_integration/fixtures/file-input-project/predict.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
from cog import BasePredictor, Path | ||
from cog import BasePredictor, File | ||
|
||
|
||
class Predictor(BasePredictor): | ||
def predict(self, path: Path) -> str: | ||
with open(path) as f: | ||
return f.read() | ||
def predict(self, file: File) -> str: | ||
content = file.read() | ||
if isinstance(content, bytes): | ||
# Decode bytes to str assuming UTF-8 encoding; adjust if needed | ||
content = content.decode('utf-8') | ||
return content |
1 change: 1 addition & 0 deletions
1
test-integration/test_integration/fixtures/file-input-project/test.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
text |
3 changes: 3 additions & 0 deletions
3
test-integration/test_integration/fixtures/file-list-input-project/cog.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
build: | ||
python_version: "3.11" | ||
predict: "predict.py:Predictor" |
14 changes: 14 additions & 0 deletions
14
test-integration/test_integration/fixtures/file-list-input-project/predict.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from cog import BasePredictor, File | ||
|
||
|
||
class Predictor(BasePredictor): | ||
def predict(self, files: list[File]) -> str: | ||
output_parts = [] # Use a list to collect file contents | ||
for f in files: | ||
# Assuming file content is in bytes, decode to str before appending | ||
content = f.read() | ||
if isinstance(content, bytes): | ||
# Decode bytes to str assuming UTF-8 encoding; adjust if needed | ||
content = content.decode('utf-8') | ||
output_parts.append(content) | ||
return "\n\n".join(output_parts) |
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
test-integration/test_integration/fixtures/path-input-project/predict.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from cog import BasePredictor, Path | ||
|
||
|
||
class Predictor(BasePredictor): | ||
def predict(self, path: Path) -> str: | ||
with open(path) as f: | ||
return f.read() |
3 changes: 3 additions & 0 deletions
3
test-integration/test_integration/fixtures/path-list-input-project/cog.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
build: | ||
python_version: "3.11" | ||
predict: "predict.py:Predictor" |
9 changes: 9 additions & 0 deletions
9
test-integration/test_integration/fixtures/path-list-input-project/predict.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from cog import BasePredictor, Path | ||
|
||
class Predictor(BasePredictor): | ||
def predict(self, paths: list[Path]) -> str: | ||
output_parts = [] # Use a list to collect file contents | ||
for path in paths: | ||
with open(path) as f: | ||
output_parts.append(f.read()) | ||
return "".join(output_parts) |
3 changes: 3 additions & 0 deletions
3
test-integration/test_integration/fixtures/path-list-output-project/cog.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
build: | ||
python_version: "3.8" | ||
predict: "predict.py:Predictor" |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ def test_predict_takes_int_inputs_and_returns_ints_to_stdout(): | |
|
||
|
||
def test_predict_takes_file_inputs(tmpdir_factory): | ||
project_dir = Path(__file__).parent / "fixtures/file-input-project" | ||
project_dir = Path(__file__).parent / "fixtures/path-input-project" | ||
out_dir = pathlib.Path(tmpdir_factory.mktemp("project")) | ||
shutil.copytree(project_dir, out_dir, dirs_exist_ok=True) | ||
with open(out_dir / "input.txt", "w") as fh: | ||
|
@@ -46,7 +46,7 @@ def test_predict_takes_file_inputs(tmpdir_factory): | |
|
||
|
||
def test_predict_writes_files_to_files(tmpdir_factory): | ||
project_dir = Path(__file__).parent / "fixtures/file-output-project" | ||
project_dir = Path(__file__).parent / "fixtures/path-output-project" | ||
out_dir = pathlib.Path(tmpdir_factory.mktemp("project")) | ||
shutil.copytree(project_dir, out_dir, dirs_exist_ok=True) | ||
result = subprocess.run( | ||
|
@@ -61,7 +61,7 @@ def test_predict_writes_files_to_files(tmpdir_factory): | |
|
||
|
||
def test_predict_writes_files_to_files_with_custom_name(tmpdir_factory): | ||
project_dir = Path(__file__).parent / "fixtures/file-output-project" | ||
project_dir = Path(__file__).parent / "fixtures/path-output-project" | ||
out_dir = pathlib.Path(tmpdir_factory.mktemp("project")) | ||
shutil.copytree(project_dir, out_dir, dirs_exist_ok=True) | ||
result = subprocess.run( | ||
|
@@ -76,7 +76,7 @@ def test_predict_writes_files_to_files_with_custom_name(tmpdir_factory): | |
|
||
|
||
def test_predict_writes_multiple_files_to_files(tmpdir_factory): | ||
project_dir = Path(__file__).parent / "fixtures/file-list-output-project" | ||
project_dir = Path(__file__).parent / "fixtures/path-list-output-project" | ||
out_dir = pathlib.Path(tmpdir_factory.mktemp("project")) | ||
shutil.copytree(project_dir, out_dir, dirs_exist_ok=True) | ||
result = subprocess.run( | ||
|
@@ -229,3 +229,24 @@ def test_predict_many_inputs_with_existing_image(docker_image, tmpdir_factory): | |
capture_output=True, | ||
) | ||
assert result.stdout.decode() == "hello default 20 world jpg foo 6\n" | ||
|
||
|
||
def test_predict_path_list_input(tmpdir_factory): | ||
project_dir = Path(__file__).parent / "fixtures/path-list-input-project" | ||
out_dir = pathlib.Path(tmpdir_factory.mktemp("project")) | ||
shutil.copytree(project_dir, out_dir, dirs_exist_ok=True) | ||
with open(out_dir / "1.txt", "w") as fh: | ||
fh.write("test1") | ||
with open(out_dir / "2.txt", "w") as fh: | ||
fh.write("test2") | ||
cmd = ["cog", "predict", "-i", "[email protected]", "-i", "[email protected]"] | ||
|
||
result = subprocess.run( | ||
cmd, | ||
cwd=out_dir, | ||
check=True, | ||
capture_output=True, | ||
) | ||
stdout = result.stdout.decode() | ||
assert "test1" in stdout | ||
assert "test2" in stdout |