|
3 | 3 | import yaml |
4 | 4 | import json as jsonlib |
5 | 5 |
|
| 6 | +# What a local action's run.py starts as: the image comes in, a result |
| 7 | +# goes out, and the model goes in the middle. |
| 8 | +MODEL_SCRIPT = """import datatorch |
| 9 | +
|
| 10 | +image_path = datatorch.get_input("imagePath") |
| 11 | +
|
| 12 | +# Load your model and run it on image_path here. Anything your model |
| 13 | +# needs must be installed in the Python this runner runs with. |
| 14 | +result = "ok" |
| 15 | +
|
| 16 | +datatorch.set_output("result", result) |
| 17 | +""" |
| 18 | + |
6 | 19 |
|
7 | 20 | @click.command( |
8 | 21 | help="Creates a basic hello world action in the current folder, or, with " |
@@ -62,22 +75,47 @@ def create(name, description, json, local_name, tag): |
62 | 75 | os.makedirs(target, exist_ok=True) |
63 | 76 | if name == "Hello World": |
64 | 77 | name = local_name |
65 | | - config = { |
66 | | - "$schema": "http://datatorch.io/schema/action.v1.json", |
67 | | - "name": name, |
68 | | - "description": description, |
69 | | - "inputs": { |
70 | | - "example": { |
71 | | - "type": "string", |
72 | | - "description": "Input string that will get printed to console.", |
73 | | - "default": "Default Example", |
74 | | - } |
75 | | - }, |
76 | | - "outputs": {}, |
77 | | - # Inputs reach shell commands as $INPUT_<NAME> environment variables; |
78 | | - # interpolating ${{ input.* }} into a command raises InputInjectionError. |
79 | | - "runs": {"using": "cmd", "command": 'echo "$INPUT_EXAMPLE"'}, |
80 | | - } |
| 78 | + if local_name is not None: |
| 79 | + # A local action is almost always someone's own model: start it |
| 80 | + # as one. The runner hands over the image's path; run.py returns |
| 81 | + # a result. Mirrors the in-browser editor's scaffold. |
| 82 | + config = { |
| 83 | + "$schema": "http://datatorch.io/schema/action.v1.json", |
| 84 | + "name": name, |
| 85 | + "description": ( |
| 86 | + description |
| 87 | + if description != "Action created using CI template." |
| 88 | + else "Runs my model on one image." |
| 89 | + ), |
| 90 | + "inputs": { |
| 91 | + "imagePath": { |
| 92 | + "type": "string", |
| 93 | + "required": True, |
| 94 | + "description": "Path of the image on disk", |
| 95 | + } |
| 96 | + }, |
| 97 | + "outputs": {"result": {"type": "string"}}, |
| 98 | + "runs": {"using": "python", "main": "run.py"}, |
| 99 | + } |
| 100 | + with open(os.path.join(target, "run.py"), "w") as script: |
| 101 | + script.write(MODEL_SCRIPT) |
| 102 | + else: |
| 103 | + config = { |
| 104 | + "$schema": "http://datatorch.io/schema/action.v1.json", |
| 105 | + "name": name, |
| 106 | + "description": description, |
| 107 | + "inputs": { |
| 108 | + "example": { |
| 109 | + "type": "string", |
| 110 | + "description": "Input string that will get printed to console.", |
| 111 | + "default": "Default Example", |
| 112 | + } |
| 113 | + }, |
| 114 | + "outputs": {}, |
| 115 | + # Inputs reach shell commands as $INPUT_<NAME> environment variables; |
| 116 | + # interpolating ${{ input.* }} into a command raises InputInjectionError. |
| 117 | + "runs": {"using": "cmd", "command": 'echo "$INPUT_EXAMPLE"'}, |
| 118 | + } |
81 | 119 | if json: |
82 | 120 | with open(os.path.join(target, "action-datatorch.json"), "w") as outfile: |
83 | 121 | jsonlib.dump(config, outfile, indent=2, sort_keys=True) |
|
0 commit comments