@@ -10,7 +10,7 @@ from Algorithmia import ADK
1010
1111def 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
7272def 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
102102def 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.
114115algorithm = 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.
117118algorithm.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
187188def 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)" )
0 commit comments