Skip to content

Commit 0818abe

Browse files
committed
updated examples and readme files with new modelData system
1 parent c68b0fe commit 0818abe

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,21 @@ from Algorithmia import ADK
9393
# API calls will begin at the apply() method, with the request body passed as 'input'
9494
# For more details, see algorithmia.com/developers/algorithm-development/languages
9595

96-
def apply(input, globals):
96+
def apply(input, modelData):
9797
# If your apply function uses state that's loaded into memory via load, you can pass that loaded state to your apply
9898
# function by defining an additional "globals" parameter in your apply function.
99-
return "hello {} {}".format(str(input), str(globals['payload']))
99+
return "hello {} {}".format(str(input), str(modelData.user_data['payload']))
100100

101101

102-
def load():
102+
def load(modelData):
103103
# Here you can optionally define a function that will be called when the algorithm is loaded.
104104
# The return object from this function can be passed directly as input to your apply function.
105105
# A great example would be any model files that need to be available to this algorithm
106106
# during runtime.
107107

108108
# Any variables returned here, will be passed as the secondary argument to your 'algorithm' function
109-
globals = {}
110-
globals['payload'] = "Loading has been completed."
111-
return globals
109+
modelData.user_data['payload'] = "Loading has been completed."
110+
return modelData
112111

113112

114113
# This turns your library code into an algorithm that can run on the platform.
@@ -175,27 +174,26 @@ def infer_image(image_url, n, globals):
175174
return result
176175

177176

178-
def load(manifest):
177+
def load(modelData):
179178

180-
state = {}
181-
state["SMID_ALGO"] = "algo://util/SmartImageDownloader/0.2.x"
182-
state["model"] = load_model(manifest.get_model("squeezenet"))
183-
state["labels"] = load_labels(manifest.get_model("labels"))
184-
return state
179+
modelData.user_data["SMID_ALGO"] = "algo://util/SmartImageDownloader/0.2.x"
180+
modelData.user_data["model"] = load_model(modelData.get_model("squeezenet"))
181+
modelData.user_data["labels"] = load_labels(modelData.get_model("labels"))
182+
return modelData
185183

186184

187-
def apply(input, state):
185+
def apply(input, modelData):
188186
if isinstance(input, dict):
189187
if "n" in input:
190188
n = input["n"]
191189
else:
192190
n = 3
193191
if "data" in input:
194192
if isinstance(input["data"], str):
195-
output = infer_image(input["data"], n, state)
193+
output = infer_image(input["data"], n, modelData.user_data)
196194
elif isinstance(input["data"], list):
197195
for row in input["data"]:
198-
row["predictions"] = infer_image(row["image_url"], n, state)
196+
row["predictions"] = infer_image(row["image_url"], n, modelData.user_data)
199197
output = input["data"]
200198
else:
201199
raise Exception("\"data\" must be a image url or a list of image urls (with labels)")

examples/loaded_state_hello_world/src/Algorithm.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@
44
# API calls will begin at the apply() method, with the request body passed as 'input'
55
# For more details, see algorithmia.com/developers/algorithm-development/languages
66

7-
def apply(input, globals):
7+
def apply(input, modelData):
88
# If your apply function uses state that's loaded into memory via load, you can pass that loaded state to your apply
99
# function by defining an additional "globals" parameter in your apply function.
10-
return "hello {} {}".format(str(input), str(globals['payload']))
10+
return "hello {} {}".format(str(input), str(modelData.user_data['payload']))
1111

1212

13-
def load():
13+
def load(modelData):
1414
# Here you can optionally define a function that will be called when the algorithm is loaded.
1515
# The return object from this function can be passed directly as input to your apply function.
1616
# A great example would be any model files that need to be available to this algorithm
1717
# during runtime.
1818

1919
# Any variables returned here, will be passed as the secondary argument to your 'algorithm' function
20-
globals = {}
21-
globals['payload'] = "Loading has been completed."
22-
return globals
20+
modelData.user_data['payload'] = "Loading has been completed."
21+
return modelData
2322

2423

2524
# This turns your library code into an algorithm that can run on the platform.

examples/pytorch_image_classification/src/Algorithm.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,26 @@ def infer_image(image_url, n, globals):
5151
return result
5252

5353

54-
def load(manifest):
54+
def load(modelData):
5555

56-
state = {}
57-
state["SMID_ALGO"] = "algo://util/SmartImageDownloader/0.2.x"
58-
state["model"] = load_model(manifest.get_model("squeezenet"))
59-
state["labels"] = load_labels(manifest.get_model("labels"))
60-
return state
56+
modelData.user_data["SMID_ALGO"] = "algo://util/SmartImageDownloader/0.2.x"
57+
modelData.user_data["model"] = load_model(modelData.get_model("squeezenet"))
58+
modelData.user_data["labels"] = load_labels(modelData.get_model("labels"))
59+
return modelData
6160

6261

63-
def apply(input, state):
62+
def apply(input, modelData):
6463
if isinstance(input, dict):
6564
if "n" in input:
6665
n = input["n"]
6766
else:
6867
n = 3
6968
if "data" in input:
7069
if isinstance(input["data"], str):
71-
output = infer_image(input["data"], n, state)
70+
output = infer_image(input["data"], n, modelData.user_data)
7271
elif isinstance(input["data"], list):
7372
for row in input["data"]:
74-
row["predictions"] = infer_image(row["image_url"], n, state)
73+
row["predictions"] = infer_image(row["image_url"], n, modelData.user_data)
7574
output = input["data"]
7675
else:
7776
raise Exception("\"data\" must be a image url or a list of image urls (with labels)")

0 commit comments

Comments
 (0)