Skip to content

Commit 972a797

Browse files
committed
renamed state to globals and added an optional print variable to init for test purposes
1 parent a096eae commit 972a797

File tree

5 files changed

+35
-33
lines changed

5 files changed

+35
-33
lines changed

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ from Algorithmia import ADK
1010

1111
def apply(input):
1212
# If your apply function uses state that's loaded into memory via load, you can pass that loaded state to your apply
13-
# function by defining an additional "state" parameter in your apply function; but it's optional!
13+
# function by defining an additional "globals" parameter in your apply function; but it's optional!
1414
return "hello {}".format(str(input))
1515

1616

@@ -71,7 +71,7 @@ from Algorithmia import ADK
7171

7272
def apply(input):
7373
# If your apply function uses state that's loaded into memory via load, you can pass that loaded state to your apply
74-
# function by defining an additional "state" parameter in your apply function; but it's optional!
74+
# function by defining an additional "globals" parameter in your apply function; but it's optional!
7575
return "hello {}".format(str(input))
7676

7777

@@ -93,10 +93,10 @@ 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, state):
96+
def apply(input, globals):
9797
# If your apply function uses state that's loaded into memory via load, you can pass that loaded state to your apply
98-
# function by defining an additional "state" parameter in your apply function.
99-
return "hello {} {}".format(str(input), str(state))
98+
# function by defining an additional "globals" parameter in your apply function.
99+
return "hello {} {}".format(str(input), str(globals['payload']))
100100

101101

102102
def load():
@@ -105,14 +105,15 @@ def load():
105105
# A great example would be any model files that need to be available to this algorithm
106106
# during runtime.
107107
# Any variables returned here, will be passed as the secondary argument to your 'algorithm' function
108-
109-
return "Loading has been completed."
108+
globals = {}
109+
globals['payload'] = "Loading has been completed."
110+
return globals
110111

111112

112113
# This turns your library code into an algorithm that can run on the platform.
113114
# If you intend to use loading operations, remember to pass a `load` function as a second variable.
114115
algorithm = ADK(apply, load)
115-
# The 'serve()' function actually starts the algorithm, you can follow along in the source code
116+
# The 'init()' function actually starts the algorithm, you can follow along in the source code
116117
# to see how everything works.
117118
algorithm.init("Algorithmia")
118119

@@ -163,9 +164,9 @@ def get_image(image_url):
163164
return img_data
164165

165166

166-
def infer_image(image_url, n, state):
167-
model = state['model']
168-
labels = state['labels']
167+
def infer_image(image_url, n, globals):
168+
model = globals['model']
169+
labels = globals['labels']
169170
image_data = get_image(image_url)
170171
transformed = transforms.Compose([
171172
transforms.ToTensor(),
@@ -185,22 +186,22 @@ def infer_image(image_url, n, state):
185186

186187

187188
def load():
188-
output = {'model': load_model("squeezenet"), 'labels': load_labels()}
189-
return output
189+
globals = {'model': load_model("squeezenet"), 'labels': load_labels()}
190+
return globals
190191

191192

192-
def apply(input, state):
193+
def apply(input, globals):
193194
if isinstance(input, dict):
194195
if "n" in input:
195196
n = input['n']
196197
else:
197198
n = 3
198199
if "data" in input:
199200
if isinstance(input['data'], str):
200-
output = infer_image(input['data'], n, state)
201+
output = infer_image(input['data'], n, globals)
201202
elif isinstance(input['data'], list):
202203
for row in input['data']:
203-
row['predictions'] = infer_image(row['image_url'], n, state)
204+
row['predictions'] = infer_image(row['image_url'], n, globals)
204205
output = input['data']
205206
else:
206207
raise Exception("'data' must be a image url or a list of image urls (with labels)")

adk/ADK.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def process_loop(self):
125125
finally:
126126
self.write_to_pipe(response_obj)
127127

128-
def init(self, local_payload=None):
128+
def init(self, local_payload=None, pprint=print):
129129
try:
130130
if self.load_func:
131131
self.load()
@@ -137,7 +137,7 @@ def init(self, local_payload=None):
137137
apply_result = self.apply_func(local_payload, self.load_result)
138138
else:
139139
apply_result = self.apply_func(local_payload)
140-
print(self.format_response(apply_result))
140+
pprint(self.format_response(apply_result))
141141

142142
else:
143143
self.process_loop()

examples/hello_world/src/Algorithm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def apply(input):
88
# If your apply function uses state that's loaded into memory via load, you can pass that loaded state to your apply
9-
# function by defining an additional "state" parameter in your apply function; but it's optional!
9+
# function by defining an additional "globals" parameter in your apply function; but it's optional!
1010
return "hello {}".format(str(input))
1111

1212

examples/loaded_state_hello_world/src/Algorithm.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
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, state):
7+
def apply(input, globals):
88
# If your apply function uses state that's loaded into memory via load, you can pass that loaded state to your apply
9-
# function by defining an additional "state" parameter in your apply function.
10-
return "hello {} {}".format(str(input), str(state))
9+
# function by defining an additional "globals" parameter in your apply function.
10+
return "hello {} {}".format(str(input), str(globals['payload']))
1111

1212

1313
def load():
@@ -16,13 +16,14 @@ def load():
1616
# A great example would be any model files that need to be available to this algorithm
1717
# during runtime.
1818
# Any variables returned here, will be passed as the secondary argument to your 'algorithm' function
19-
20-
return "Loading has been completed."
19+
globals = {}
20+
globals['payload'] = "Loading has been completed."
21+
return globals
2122

2223

2324
# This turns your library code into an algorithm that can run on the platform.
2425
# If you intend to use loading operations, remember to pass a `load` function as a second variable.
2526
algorithm = ADK(apply, load)
26-
# The 'serve()' function actually starts the algorithm, you can follow along in the source code
27+
# The 'init()' function actually starts the algorithm, you can follow along in the source code
2728
# to see how everything works.
2829
algorithm.init("Algorithmia")

examples/pytorch_image_classification/src/Algorithm.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ def get_image(image_url):
4141
return img_data
4242

4343

44-
def infer_image(image_url, n, state):
45-
model = state['model']
46-
labels = state['labels']
44+
def infer_image(image_url, n, globals):
45+
model = globals['model']
46+
labels = globals['labels']
4747
image_data = get_image(image_url)
4848
transformed = transforms.Compose([
4949
transforms.ToTensor(),
@@ -63,22 +63,22 @@ def infer_image(image_url, n, state):
6363

6464

6565
def load():
66-
output = {'model': load_model("squeezenet"), 'labels': load_labels()}
67-
return output
66+
globals = {'model': load_model("squeezenet"), 'labels': load_labels()}
67+
return globals
6868

6969

70-
def apply(input, state):
70+
def apply(input, globals):
7171
if isinstance(input, dict):
7272
if "n" in input:
7373
n = input['n']
7474
else:
7575
n = 3
7676
if "data" in input:
7777
if isinstance(input['data'], str):
78-
output = infer_image(input['data'], n, state)
78+
output = infer_image(input['data'], n, globals)
7979
elif isinstance(input['data'], list):
8080
for row in input['data']:
81-
row['predictions'] = infer_image(row['image_url'], n, state)
81+
row['predictions'] = infer_image(row['image_url'], n, globals)
8282
output = input['data']
8383
else:
8484
raise Exception("'data' must be a image url or a list of image urls (with labels)")

0 commit comments

Comments
 (0)