aimodelshare 0.3.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. aimodelshare/README.md +26 -0
  2. aimodelshare/__init__.py +100 -0
  3. aimodelshare/aimsonnx.py +2381 -0
  4. aimodelshare/api.py +836 -0
  5. aimodelshare/auth.py +163 -0
  6. aimodelshare/aws.py +511 -0
  7. aimodelshare/aws_client.py +173 -0
  8. aimodelshare/base_image.py +154 -0
  9. aimodelshare/bucketpolicy.py +106 -0
  10. aimodelshare/color_mappings/color_mapping_keras.csv +121 -0
  11. aimodelshare/color_mappings/color_mapping_pytorch.csv +117 -0
  12. aimodelshare/containerisation.py +244 -0
  13. aimodelshare/containerization.py +712 -0
  14. aimodelshare/containerization_templates/Dockerfile.txt +8 -0
  15. aimodelshare/containerization_templates/Dockerfile_PySpark.txt +23 -0
  16. aimodelshare/containerization_templates/buildspec.txt +14 -0
  17. aimodelshare/containerization_templates/lambda_function.txt +40 -0
  18. aimodelshare/custom_approach/__init__.py +1 -0
  19. aimodelshare/custom_approach/lambda_function.py +17 -0
  20. aimodelshare/custom_eval_metrics.py +103 -0
  21. aimodelshare/data_sharing/__init__.py +0 -0
  22. aimodelshare/data_sharing/data_sharing_templates/Dockerfile.txt +3 -0
  23. aimodelshare/data_sharing/data_sharing_templates/__init__.py +1 -0
  24. aimodelshare/data_sharing/data_sharing_templates/buildspec.txt +15 -0
  25. aimodelshare/data_sharing/data_sharing_templates/codebuild_policies.txt +129 -0
  26. aimodelshare/data_sharing/data_sharing_templates/codebuild_trust_relationship.txt +12 -0
  27. aimodelshare/data_sharing/download_data.py +620 -0
  28. aimodelshare/data_sharing/share_data.py +373 -0
  29. aimodelshare/data_sharing/utils.py +8 -0
  30. aimodelshare/deploy_custom_lambda.py +246 -0
  31. aimodelshare/documentation/Makefile +20 -0
  32. aimodelshare/documentation/karma_sphinx_theme/__init__.py +28 -0
  33. aimodelshare/documentation/karma_sphinx_theme/_version.py +2 -0
  34. aimodelshare/documentation/karma_sphinx_theme/breadcrumbs.html +70 -0
  35. aimodelshare/documentation/karma_sphinx_theme/layout.html +172 -0
  36. aimodelshare/documentation/karma_sphinx_theme/search.html +50 -0
  37. aimodelshare/documentation/karma_sphinx_theme/searchbox.html +14 -0
  38. aimodelshare/documentation/karma_sphinx_theme/static/css/custom.css +2 -0
  39. aimodelshare/documentation/karma_sphinx_theme/static/css/custom.css.map +1 -0
  40. aimodelshare/documentation/karma_sphinx_theme/static/css/theme.css +2751 -0
  41. aimodelshare/documentation/karma_sphinx_theme/static/css/theme.css.map +1 -0
  42. aimodelshare/documentation/karma_sphinx_theme/static/css/theme.min.css +2 -0
  43. aimodelshare/documentation/karma_sphinx_theme/static/css/theme.min.css.map +1 -0
  44. aimodelshare/documentation/karma_sphinx_theme/static/font/fontello.eot +0 -0
  45. aimodelshare/documentation/karma_sphinx_theme/static/font/fontello.svg +32 -0
  46. aimodelshare/documentation/karma_sphinx_theme/static/font/fontello.ttf +0 -0
  47. aimodelshare/documentation/karma_sphinx_theme/static/font/fontello.woff +0 -0
  48. aimodelshare/documentation/karma_sphinx_theme/static/font/fontello.woff2 +0 -0
  49. aimodelshare/documentation/karma_sphinx_theme/static/js/theme.js +68 -0
  50. aimodelshare/documentation/karma_sphinx_theme/theme.conf +9 -0
  51. aimodelshare/documentation/make.bat +35 -0
  52. aimodelshare/documentation/requirements.txt +2 -0
  53. aimodelshare/documentation/source/about.rst +18 -0
  54. aimodelshare/documentation/source/advanced_features.rst +137 -0
  55. aimodelshare/documentation/source/competition.rst +218 -0
  56. aimodelshare/documentation/source/conf.py +58 -0
  57. aimodelshare/documentation/source/create_credentials.rst +86 -0
  58. aimodelshare/documentation/source/example_notebooks.rst +132 -0
  59. aimodelshare/documentation/source/functions.rst +151 -0
  60. aimodelshare/documentation/source/gettingstarted.rst +390 -0
  61. aimodelshare/documentation/source/images/creds1.png +0 -0
  62. aimodelshare/documentation/source/images/creds2.png +0 -0
  63. aimodelshare/documentation/source/images/creds3.png +0 -0
  64. aimodelshare/documentation/source/images/creds4.png +0 -0
  65. aimodelshare/documentation/source/images/creds5.png +0 -0
  66. aimodelshare/documentation/source/images/creds_file_example.png +0 -0
  67. aimodelshare/documentation/source/images/predict_tab.png +0 -0
  68. aimodelshare/documentation/source/index.rst +110 -0
  69. aimodelshare/documentation/source/modelplayground.rst +132 -0
  70. aimodelshare/exceptions.py +11 -0
  71. aimodelshare/generatemodelapi.py +1270 -0
  72. aimodelshare/iam/codebuild_policy.txt +129 -0
  73. aimodelshare/iam/codebuild_trust_relationship.txt +12 -0
  74. aimodelshare/iam/lambda_policy.txt +15 -0
  75. aimodelshare/iam/lambda_trust_relationship.txt +12 -0
  76. aimodelshare/json_templates/__init__.py +1 -0
  77. aimodelshare/json_templates/api_json.txt +155 -0
  78. aimodelshare/json_templates/auth/policy.txt +1 -0
  79. aimodelshare/json_templates/auth/role.txt +1 -0
  80. aimodelshare/json_templates/eval/policy.txt +1 -0
  81. aimodelshare/json_templates/eval/role.txt +1 -0
  82. aimodelshare/json_templates/function/policy.txt +1 -0
  83. aimodelshare/json_templates/function/role.txt +1 -0
  84. aimodelshare/json_templates/integration_response.txt +5 -0
  85. aimodelshare/json_templates/lambda_policy_1.txt +15 -0
  86. aimodelshare/json_templates/lambda_policy_2.txt +8 -0
  87. aimodelshare/json_templates/lambda_role_1.txt +12 -0
  88. aimodelshare/json_templates/lambda_role_2.txt +16 -0
  89. aimodelshare/leaderboard.py +174 -0
  90. aimodelshare/main/1.txt +132 -0
  91. aimodelshare/main/1B.txt +112 -0
  92. aimodelshare/main/2.txt +153 -0
  93. aimodelshare/main/3.txt +134 -0
  94. aimodelshare/main/4.txt +128 -0
  95. aimodelshare/main/5.txt +109 -0
  96. aimodelshare/main/6.txt +105 -0
  97. aimodelshare/main/7.txt +144 -0
  98. aimodelshare/main/8.txt +142 -0
  99. aimodelshare/main/__init__.py +1 -0
  100. aimodelshare/main/authorization.txt +275 -0
  101. aimodelshare/main/eval_classification.txt +79 -0
  102. aimodelshare/main/eval_lambda.txt +1709 -0
  103. aimodelshare/main/eval_regression.txt +80 -0
  104. aimodelshare/main/lambda_function.txt +8 -0
  105. aimodelshare/main/nst.txt +149 -0
  106. aimodelshare/model.py +1543 -0
  107. aimodelshare/modeluser.py +215 -0
  108. aimodelshare/moral_compass/README.md +408 -0
  109. aimodelshare/moral_compass/__init__.py +65 -0
  110. aimodelshare/moral_compass/_version.py +3 -0
  111. aimodelshare/moral_compass/api_client.py +601 -0
  112. aimodelshare/moral_compass/apps/__init__.py +69 -0
  113. aimodelshare/moral_compass/apps/ai_consequences.py +540 -0
  114. aimodelshare/moral_compass/apps/bias_detective.py +714 -0
  115. aimodelshare/moral_compass/apps/ethical_revelation.py +898 -0
  116. aimodelshare/moral_compass/apps/fairness_fixer.py +889 -0
  117. aimodelshare/moral_compass/apps/judge.py +888 -0
  118. aimodelshare/moral_compass/apps/justice_equity_upgrade.py +853 -0
  119. aimodelshare/moral_compass/apps/mc_integration_helpers.py +820 -0
  120. aimodelshare/moral_compass/apps/model_building_game.py +1104 -0
  121. aimodelshare/moral_compass/apps/model_building_game_beginner.py +687 -0
  122. aimodelshare/moral_compass/apps/moral_compass_challenge.py +858 -0
  123. aimodelshare/moral_compass/apps/session_auth.py +254 -0
  124. aimodelshare/moral_compass/apps/shared_activity_styles.css +349 -0
  125. aimodelshare/moral_compass/apps/tutorial.py +481 -0
  126. aimodelshare/moral_compass/apps/what_is_ai.py +853 -0
  127. aimodelshare/moral_compass/challenge.py +365 -0
  128. aimodelshare/moral_compass/config.py +187 -0
  129. aimodelshare/placeholders/model.onnx +0 -0
  130. aimodelshare/placeholders/preprocessor.zip +0 -0
  131. aimodelshare/playground.py +1968 -0
  132. aimodelshare/postprocessormodules.py +157 -0
  133. aimodelshare/preprocessormodules.py +373 -0
  134. aimodelshare/pyspark/1.txt +195 -0
  135. aimodelshare/pyspark/1B.txt +181 -0
  136. aimodelshare/pyspark/2.txt +220 -0
  137. aimodelshare/pyspark/3.txt +204 -0
  138. aimodelshare/pyspark/4.txt +187 -0
  139. aimodelshare/pyspark/5.txt +178 -0
  140. aimodelshare/pyspark/6.txt +174 -0
  141. aimodelshare/pyspark/7.txt +211 -0
  142. aimodelshare/pyspark/8.txt +206 -0
  143. aimodelshare/pyspark/__init__.py +1 -0
  144. aimodelshare/pyspark/authorization.txt +258 -0
  145. aimodelshare/pyspark/eval_classification.txt +79 -0
  146. aimodelshare/pyspark/eval_lambda.txt +1441 -0
  147. aimodelshare/pyspark/eval_regression.txt +80 -0
  148. aimodelshare/pyspark/lambda_function.txt +8 -0
  149. aimodelshare/pyspark/nst.txt +213 -0
  150. aimodelshare/python/my_preprocessor.py +58 -0
  151. aimodelshare/readme.md +26 -0
  152. aimodelshare/reproducibility.py +181 -0
  153. aimodelshare/sam/Dockerfile.txt +8 -0
  154. aimodelshare/sam/Dockerfile_PySpark.txt +24 -0
  155. aimodelshare/sam/__init__.py +1 -0
  156. aimodelshare/sam/buildspec.txt +11 -0
  157. aimodelshare/sam/codebuild_policies.txt +129 -0
  158. aimodelshare/sam/codebuild_trust_relationship.txt +12 -0
  159. aimodelshare/sam/codepipeline_policies.txt +173 -0
  160. aimodelshare/sam/codepipeline_trust_relationship.txt +12 -0
  161. aimodelshare/sam/spark-class.txt +2 -0
  162. aimodelshare/sam/template.txt +54 -0
  163. aimodelshare/tools.py +103 -0
  164. aimodelshare/utils/__init__.py +78 -0
  165. aimodelshare/utils/optional_deps.py +38 -0
  166. aimodelshare/utils.py +57 -0
  167. aimodelshare-0.3.7.dist-info/METADATA +298 -0
  168. aimodelshare-0.3.7.dist-info/RECORD +171 -0
  169. aimodelshare-0.3.7.dist-info/WHEEL +5 -0
  170. aimodelshare-0.3.7.dist-info/licenses/LICENSE +5 -0
  171. aimodelshare-0.3.7.dist-info/top_level.txt +1 -0
@@ -0,0 +1,174 @@
1
+ # Tabular time series regression
2
+
3
+ import boto3
4
+ import pandas as pd
5
+ import os
6
+ import numpy as np
7
+ import onnxruntime as rt
8
+ import json
9
+
10
+ def get_model_onnx(runtimemodel_s3_filename="runtime_model.onnx"):
11
+ s3 = boto3.resource('s3')
12
+ obj = s3.Object("$bucket_name", "$unique_model_id" +
13
+ "/runtime_model.onnx")
14
+ model = rt.InferenceSession(obj.get()['Body'].read())
15
+ return model
16
+
17
+ def _get_pyspark_modules():
18
+ import pyspark
19
+ import pyspark.ml
20
+ import re
21
+
22
+ pyspark_modules = ['ml', 'ml.feature', 'ml.classification', 'ml.clustering', 'ml.regression']
23
+
24
+ models_modules_dict = {}
25
+
26
+ for i in pyspark_modules:
27
+ models_list = [j for j in dir(eval('pyspark.'+i, {'pyspark': pyspark})) if callable(getattr(eval('pyspark.'+i, {'pyspark': pyspark}), j))]
28
+ models_list = [j for j in models_list if re.match('^[A-Z]', j)]
29
+
30
+ for k in models_list:
31
+ models_modules_dict[k] = 'pyspark.'+i
32
+
33
+ return models_modules_dict
34
+
35
+ def pyspark_model_from_string(model_type):
36
+ import importlib
37
+
38
+ models_modules_dict = _get_pyspark_modules()
39
+ module = models_modules_dict[model_type]
40
+ model_class = getattr(importlib.import_module(module), model_type)
41
+ return model_class
42
+
43
+ def get_preprocessor(preprocessor_s3_filename="runtime_preprocessor.zip"):
44
+ import os
45
+ import pickle
46
+ import tempfile
47
+ from io import BytesIO
48
+ from pathlib import Path
49
+ from zipfile import ZipFile
50
+
51
+ #create temporary folder
52
+ temp_dir = tempfile.gettempdir()
53
+
54
+ # there are some other zip files on temp_dir that might affect import process
55
+ temp_dir = os.path.join(temp_dir, "$unique_model_id")
56
+ os.makedirs(temp_dir, exist_ok=True)
57
+
58
+ s3 = boto3.resource("s3")
59
+ bucket = s3.Bucket("$bucket_name")
60
+
61
+ zip_obj = s3.Object(bucket_name="$bucket_name",
62
+ key="$unique_model_id/runtime_preprocessor.zip")
63
+ buffer = BytesIO(zip_obj.get()["Body"].read())
64
+ z = ZipFile(buffer)
65
+ # Extract all the contents of zip file in temp directory
66
+ z.extractall(temp_dir)
67
+
68
+ # Then import all pkl files you want from bucket (need to generate this list from
69
+ # function globals
70
+ pickle_file_list = []
71
+ zip_file_list = []
72
+ for file in os.listdir(temp_dir):
73
+ if file.endswith(".pkl"):
74
+ pickle_file_list.append(os.path.join(temp_dir, file))
75
+ if file.endswith(".zip"):
76
+ zip_file_list.append(os.path.join(temp_dir, file))
77
+
78
+ for i in pickle_file_list:
79
+ objectname = str(os.path.basename(i)).replace(".pkl", "")
80
+ objects = { objectname: "" }
81
+ globals()[objectname] = pickle.load(open(str(i), "rb"))
82
+
83
+ # Need spark session and context to instantiate model object
84
+ # zip_file_list is only used by pyspark
85
+ if len(zip_file_list):
86
+ from pyspark.sql import SparkSession
87
+
88
+ spark = SparkSession \
89
+ .builder \
90
+ .appName('Pyspark Model') \
91
+ .getOrCreate()
92
+
93
+ for i in zip_file_list:
94
+ objectnames = str(os.path.basename(i)).replace(".zip", "").split("__")
95
+ dir_path = i.replace(".zip", "")
96
+ Path(dir_path).mkdir(parents=True, exist_ok=True)
97
+
98
+ # Create a ZipFile Object and load module.zip in it
99
+ with ZipFile(i, 'r') as zipObj:
100
+ # Extract all the contents of zip file in current directory
101
+ zipObj.extractall(dir_path)
102
+
103
+ preprocessor_type = objectnames[0].split("_")[0]
104
+ objectname = objectnames[1]
105
+ preprocessor_class = pyspark_model_from_string(preprocessor_type)
106
+ if preprocessor_type == "PipelineModel":
107
+ print(preprocessor_class)
108
+ preprocessor_model = preprocessor_class(stages=None)
109
+ else:
110
+ preprocessor_model = preprocessor_class()
111
+
112
+ preprocessor_model = preprocessor_model.load(dir_path)
113
+ globals()[objectname] = preprocessor_model
114
+
115
+ # First import preprocessor function to session from preprocessor.py
116
+ exec(open(os.path.join(temp_dir, 'preprocessor.py')).read(),globals())
117
+ return preprocessor
118
+
119
+ def get_runtimedata(runtimedata_s3_filename="runtime_data.json"):
120
+ s3 = boto3.resource('s3')
121
+ obj = s3.Object("$bucket_name", "$unique_model_id"+"/"+runtimedata_s3_filename)
122
+ runtime_data = json.load(obj.get()['Body'])
123
+
124
+ return runtime_data
125
+
126
+
127
+ runtime_data = get_runtimedata(runtimedata_s3_filename="runtime_data.json")
128
+
129
+ preprocessor_type = runtime_data["runtime_preprocessor"]
130
+
131
+ runtime_model = runtime_data["runtime_model"]["name"]
132
+
133
+ # Load model
134
+ model = get_model_onnx(runtimemodel_s3_filename='runtime_model.onnx')
135
+
136
+ # Load preprocessor
137
+ preprocessor = get_preprocessor(preprocessor_s3_filename="runtime_preprocessor.zip")
138
+
139
+ def predict(event, model, preprocessor):
140
+ body = event["body"]
141
+ import six
142
+ if isinstance(event["body"], six.string_types):
143
+ body = json.loads(event["body"])
144
+ print(body["data"])
145
+ bodydata = pd.DataFrame.from_dict(body["data"])
146
+ else:
147
+ print(body["data"])
148
+ bodydata = pd.DataFrame.from_dict(body["data"])
149
+ print(bodydata)
150
+
151
+ try:
152
+ input_data = preprocessor(bodydata)
153
+ except:
154
+ input_data = preprocessor(bodydata).astype(np.float32).toarray()
155
+
156
+ # generate prediction using preprocessed input data
157
+ input_name = model.get_inputs()[0].name
158
+
159
+ res = model.run(None, {input_name: input_data})
160
+
161
+ return res[0][0].tolist()
162
+
163
+
164
+ def handler(event, context):
165
+ result = predict(event, model, preprocessor)
166
+ return {"statusCode": 200,
167
+ "headers": {
168
+ "Access-Control-Allow-Origin": "*",
169
+ "Access-Control-Allow-Credentials": True,
170
+ "Allow": "GET, OPTIONS, POST",
171
+ "Access-Control-Allow-Methods": "GET, OPTIONS, POST",
172
+ "Access-Control-Allow-Headers": "*"
173
+ },
174
+ "body": json.dumps(result)}
@@ -0,0 +1,211 @@
1
+ # audio time series classification
2
+
3
+ import boto3
4
+ import os
5
+ import numpy as np
6
+ import json
7
+ import onnxruntime as rt
8
+ import base64
9
+ import six
10
+ from functools import partial
11
+ import os.path
12
+ from os import path
13
+ import pandas as pd
14
+
15
+ def get_model_onnx(runtimemodel_s3_filename="runtime_model.onnx"):
16
+ s3 = boto3.resource('s3')
17
+ obj = s3.Object("$bucket_name", "$unique_model_id" +
18
+ "/runtime_model.onnx")
19
+ model = rt.InferenceSession(obj.get()['Body'].read())
20
+ return model
21
+
22
+ def _get_pyspark_modules():
23
+ import pyspark
24
+ import pyspark.ml
25
+ import re
26
+
27
+ pyspark_modules = ['ml', 'ml.feature', 'ml.classification', 'ml.clustering', 'ml.regression']
28
+
29
+ models_modules_dict = {}
30
+
31
+ for i in pyspark_modules:
32
+ models_list = [j for j in dir(eval('pyspark.'+i, {'pyspark': pyspark})) if callable(getattr(eval('pyspark.'+i, {'pyspark': pyspark}), j))]
33
+ models_list = [j for j in models_list if re.match('^[A-Z]', j)]
34
+
35
+ for k in models_list:
36
+ models_modules_dict[k] = 'pyspark.'+i
37
+
38
+ return models_modules_dict
39
+
40
+ def pyspark_model_from_string(model_type):
41
+ import importlib
42
+
43
+ models_modules_dict = _get_pyspark_modules()
44
+ module = models_modules_dict[model_type]
45
+ model_class = getattr(importlib.import_module(module), model_type)
46
+ return model_class
47
+
48
+ def get_preprocessor(preprocessor_s3_filename="runtime_preprocessor.zip"):
49
+ import os
50
+ import pickle
51
+ import tempfile
52
+ from io import BytesIO
53
+ from pathlib import Path
54
+ from zipfile import ZipFile
55
+
56
+
57
+ #create temporary folder
58
+ temp_dir = tempfile.gettempdir()
59
+
60
+ # there are some other zip files on temp_dir that might affect import process
61
+ temp_dir = os.path.join(temp_dir, "$unique_model_id")
62
+ os.makedirs(temp_dir, exist_ok=True)
63
+
64
+ s3 = boto3.resource("s3")
65
+ bucket = s3.Bucket("$bucket_name")
66
+
67
+ zip_obj = s3.Object(bucket_name="$bucket_name",
68
+ key="$unique_model_id/runtime_preprocessor.zip")
69
+ buffer = BytesIO(zip_obj.get()["Body"].read())
70
+ z = ZipFile(buffer)
71
+ # Extract all the contents of zip file in temp directory
72
+ z.extractall(temp_dir)
73
+
74
+ # Then import all pkl files you want from bucket (need to generate this list from
75
+ # function globals
76
+ pickle_file_list = []
77
+ zip_file_list = []
78
+ for file in os.listdir(temp_dir):
79
+ if file.endswith(".pkl"):
80
+ pickle_file_list.append(os.path.join(temp_dir, file))
81
+ if file.endswith(".zip"):
82
+ zip_file_list.append(os.path.join(temp_dir, file))
83
+
84
+ for i in pickle_file_list:
85
+ objectname = str(os.path.basename(i)).replace(".pkl", "")
86
+ objects = { objectname: "" }
87
+ globals()[objectname] = pickle.load(open(str(i), "rb"))
88
+
89
+ # Need spark session and context to instantiate model object
90
+ # zip_file_list is only used by pyspark
91
+ if len(zip_file_list):
92
+ from pyspark.sql import SparkSession
93
+
94
+ spark = SparkSession \
95
+ .builder \
96
+ .appName('Pyspark Model') \
97
+ .getOrCreate()
98
+
99
+ for i in zip_file_list:
100
+ objectnames = str(os.path.basename(i)).replace(".zip", "").split("__")
101
+ dir_path = i.replace(".zip", "")
102
+ Path(dir_path).mkdir(parents=True, exist_ok=True)
103
+
104
+ # Create a ZipFile Object and load module.zip in it
105
+ with ZipFile(i, 'r') as zipObj:
106
+ # Extract all the contents of zip file in current directory
107
+ zipObj.extractall(dir_path)
108
+
109
+ preprocessor_type = objectnames[0].split("_")[0]
110
+ objectname = objectnames[1]
111
+ preprocessor_class = pyspark_model_from_string(preprocessor_type)
112
+ if preprocessor_type == "PipelineModel":
113
+ print(preprocessor_class)
114
+ preprocessor_model = preprocessor_class(stages=None)
115
+ else:
116
+ preprocessor_model = preprocessor_class()
117
+
118
+ preprocessor_model = preprocessor_model.load(dir_path)
119
+ globals()[objectname] = preprocessor_model
120
+
121
+ # First import preprocessor function to session from preprocessor.py
122
+ exec(open(os.path.join(temp_dir, 'preprocessor.py')).read(),globals())
123
+ return preprocessor
124
+
125
+
126
+ def get_runtimedata(runtimedata_s3_filename="runtime_data.json"):
127
+
128
+ s3 = boto3.resource('s3')
129
+ obj = s3.Object("$bucket_name", "$unique_model_id"+"/"+runtimedata_s3_filename)
130
+ runtime_data = json.load(obj.get()['Body'])
131
+
132
+ return runtime_data
133
+
134
+
135
+ runtime_data=get_runtimedata(runtimedata_s3_filename="runtime_data.json")
136
+
137
+ # preprocessor_type=runtime_data["runtime_preprocessor"]
138
+
139
+ runtime_model=runtime_data["runtime_model"]["name"]
140
+
141
+ model=get_model_onnx(runtimemodel_s3_filename='runtime_model.onnx')
142
+
143
+ # Load preprocessor
144
+
145
+ preprocessor=get_preprocessor(preprocessor_s3_filename="runtime_preprocessor.zip")
146
+
147
+ def predict(event,model,preprocessor):
148
+
149
+ # Load base64 encoded audio stored within "data" key of event dictionary
150
+ print(event["body"])
151
+ body = event["body"]
152
+ if isinstance(event["body"], six.string_types):
153
+ body = json.loads(event["body"])
154
+ # only supporting wav extension as of now
155
+ extension = body['extension']
156
+ bodydata=body["data"]
157
+
158
+
159
+ sample = base64.decodebytes(bytearray(bodydata, "utf-8"))
160
+
161
+ # Save audio to local file, read into session, and preprocess image with preprocessor function
162
+ with open("/tmp/audiotopredict."+extension, "wb") as fh:
163
+ fh.write(base64.b64decode(bodydata))
164
+
165
+
166
+ input_data = preprocessor(f"/tmp/audiotopredict.{extension}")
167
+
168
+ # Generate prediction using preprocessed input data
169
+ print("The model expects input shape:", model.get_inputs()[0].shape)
170
+
171
+ input_name = model.get_inputs()[0].name
172
+
173
+ input_data = np.float32(input_data)
174
+
175
+ res = model.run(None, {input_name: input_data})
176
+
177
+ #extract predicted probability for all classes, extract predicted label
178
+
179
+ prob = res[0]
180
+
181
+ def predict_classes(x):
182
+ proba=x
183
+ if proba.shape[-1] > 1:
184
+ return proba.argmax(axis=-1)
185
+ else:
186
+ return (proba > 0.5).astype("int32")
187
+
188
+ prediction_index=predict_classes(prob)
189
+
190
+ labels=$labels
191
+ result=list(map(lambda x: labels[x], prediction_index))
192
+
193
+ os.remove("/tmp/audiotopredict."+extension)
194
+
195
+ # pyspark returns np.longlong which is not json serializable.
196
+ if len(result) and type(result[0]) == np.longlong:
197
+ result = [int(x) for x in result]
198
+
199
+ return result
200
+
201
+ def handler(event, context):
202
+ result = predict(event,model,preprocessor)
203
+ return {"statusCode": 200,
204
+ "headers": {
205
+ "Access-Control-Allow-Origin" : "*",
206
+ "Access-Control-Allow-Credentials": True,
207
+ "Allow" : "GET, OPTIONS, POST",
208
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
209
+ "Access-Control-Allow-Headers" : "*"
210
+ },
211
+ "body" : json.dumps(result)}
@@ -0,0 +1,206 @@
1
+ # video classification
2
+ import boto3
3
+ import os
4
+ import numpy as np
5
+ import json
6
+ import onnxruntime as rt
7
+ import base64
8
+ import six
9
+ from os import path
10
+
11
+ import json
12
+ import warnings
13
+ import pandas as pd
14
+
15
+ def get_model_onnx(runtimemodel_s3_filename="runtime_model.onnx"):
16
+ s3 = boto3.resource('s3')
17
+ obj = s3.Object("$bucket_name", "$unique_model_id" +
18
+ "/runtime_model.onnx")
19
+ model = rt.InferenceSession(obj.get()['Body'].read())
20
+ return model
21
+
22
+ def get_runtimedata(runtimedata_s3_filename="runtime_data.json"):
23
+ s3 = boto3.resource('s3')
24
+ obj = s3.Object("$bucket_name", "$unique_model_id" +
25
+ "/"+runtimedata_s3_filename)
26
+ runtime_data = json.load(obj.get()['Body'])
27
+
28
+ return runtime_data
29
+
30
+ def _get_pyspark_modules():
31
+ import pyspark
32
+ import pyspark.ml
33
+ import re
34
+
35
+ pyspark_modules = ['ml', 'ml.feature', 'ml.classification', 'ml.clustering', 'ml.regression']
36
+
37
+ models_modules_dict = {}
38
+
39
+ for i in pyspark_modules:
40
+ models_list = [j for j in dir(eval('pyspark.'+i, {'pyspark': pyspark})) if callable(getattr(eval('pyspark.'+i, {'pyspark': pyspark}), j))]
41
+ models_list = [j for j in models_list if re.match('^[A-Z]', j)]
42
+
43
+ for k in models_list:
44
+ models_modules_dict[k] = 'pyspark.'+i
45
+
46
+ return models_modules_dict
47
+
48
+ def pyspark_model_from_string(model_type):
49
+ import importlib
50
+
51
+ models_modules_dict = _get_pyspark_modules()
52
+ module = models_modules_dict[model_type]
53
+ model_class = getattr(importlib.import_module(module), model_type)
54
+ return model_class
55
+
56
+ def get_preprocessor(preprocessor_s3_filename="runtime_preprocessor.zip"):
57
+ import os
58
+ import pickle
59
+ import tempfile
60
+ from io import BytesIO
61
+ from pathlib import Path
62
+ from zipfile import ZipFile
63
+
64
+ #create temporary folder
65
+ temp_dir = tempfile.gettempdir()
66
+
67
+ # there are some other zip files on temp_dir that might affect import process
68
+ temp_dir = os.path.join(temp_dir, "$unique_model_id")
69
+ os.makedirs(temp_dir, exist_ok=True)
70
+
71
+ s3 = boto3.resource("s3")
72
+ bucket = s3.Bucket("$bucket_name")
73
+
74
+ zip_obj = s3.Object(bucket_name="$bucket_name",
75
+ key="$unique_model_id/runtime_preprocessor.zip")
76
+ buffer = BytesIO(zip_obj.get()["Body"].read())
77
+ z = ZipFile(buffer)
78
+ # Extract all the contents of zip file in temp directory
79
+ z.extractall(temp_dir)
80
+
81
+ # Then import all pkl files you want from bucket (need to generate this list from
82
+ # function globals
83
+ pickle_file_list = []
84
+ zip_file_list = []
85
+ for file in os.listdir(temp_dir):
86
+ if file.endswith(".pkl"):
87
+ pickle_file_list.append(os.path.join(temp_dir, file))
88
+ if file.endswith(".zip"):
89
+ zip_file_list.append(os.path.join(temp_dir, file))
90
+
91
+ for i in pickle_file_list:
92
+ objectname = str(os.path.basename(i)).replace(".pkl", "")
93
+ objects = { objectname: "" }
94
+ globals()[objectname] = pickle.load(open(str(i), "rb"))
95
+
96
+ # Need spark session and context to instantiate model object
97
+ # zip_file_list is only used by pyspark
98
+ if len(zip_file_list):
99
+ from pyspark.sql import SparkSession
100
+
101
+ spark = SparkSession \
102
+ .builder \
103
+ .appName('Pyspark Model') \
104
+ .getOrCreate()
105
+
106
+ for i in zip_file_list:
107
+ objectnames = str(os.path.basename(i)).replace(".zip", "").split("__")
108
+ dir_path = i.replace(".zip", "")
109
+ Path(dir_path).mkdir(parents=True, exist_ok=True)
110
+
111
+ # Create a ZipFile Object and load module.zip in it
112
+ with ZipFile(i, 'r') as zipObj:
113
+ # Extract all the contents of zip file in current directory
114
+ zipObj.extractall(dir_path)
115
+
116
+ preprocessor_type = objectnames[0].split("_")[0]
117
+ objectname = objectnames[1]
118
+ preprocessor_class = pyspark_model_from_string(preprocessor_type)
119
+ if preprocessor_type == "PipelineModel":
120
+ print(preprocessor_class)
121
+ preprocessor_model = preprocessor_class(stages=None)
122
+ else:
123
+ preprocessor_model = preprocessor_class()
124
+
125
+ preprocessor_model = preprocessor_model.load(dir_path)
126
+ globals()[objectname] = preprocessor_model
127
+
128
+ # First import preprocessor function to session from preprocessor.py
129
+ exec(open(os.path.join(temp_dir, 'preprocessor.py')).read(),globals())
130
+ return preprocessor
131
+
132
+
133
+ def predict(event, model, preprocessor):
134
+
135
+ # Load base64 encoded /. stored within "data" key of event dictionary
136
+ # print(event["body"])
137
+ body = event["body"]
138
+ if isinstance(event["body"], six.string_types):
139
+ body = json.loads(event["body"])
140
+ # only supporting wav extension as of now
141
+ extension = body['extension']
142
+ bodydata = body["data"]
143
+
144
+ sample = base64.decodebytes(bytearray(bodydata, "utf-8"))
145
+
146
+ # Save video to local file, read into session, and preprocess image with preprocessor function
147
+ with open("/tmp/videotopredict."+extension, "wb") as fh:
148
+ fh.write(base64.b64decode(bodydata))
149
+
150
+ input_data = preprocessor(f"/tmp/videotopredict.{extension}")
151
+
152
+ # Generate prediction using preprocessed input data
153
+ print("The model expects input shape:", model.get_inputs()[0].shape)
154
+
155
+ input_name = model.get_inputs()[0].name
156
+ input_data = np.float32(input_data)
157
+
158
+ res = model.run(None, {input_name: input_data})
159
+
160
+ # extract predicted probability for all classes, extract predicted label
161
+
162
+ prob = res[0]
163
+
164
+ def predict_classes(x):
165
+ proba = x
166
+ if proba.shape[-1] > 1:
167
+ return proba.argmax(axis=-1)
168
+ else:
169
+ return (proba > 0.5).astype("int32")
170
+
171
+ prediction_index = predict_classes(prob)
172
+
173
+ labels = $labels
174
+ result = list(map(lambda x: labels[x], prediction_index))
175
+
176
+ os.remove("/tmp/videotopredict."+extension)
177
+
178
+ # pyspark returns np.longlong which is not json serializable.
179
+ if len(result) and type(result[0]) == np.longlong:
180
+ result = [int(x) for x in result]
181
+
182
+ return result
183
+
184
+
185
+ runtime_data = get_runtimedata(runtimedata_s3_filename="runtime_data.json")
186
+
187
+ runtime_model = runtime_data["runtime_model"]["name"]
188
+
189
+ model = get_model_onnx(runtimemodel_s3_filename='runtime_model.onnx')
190
+
191
+ # Load preprocessor
192
+ preprocessor = get_preprocessor(
193
+ preprocessor_s3_filename="runtime_preprocessor.zip")
194
+
195
+
196
+ def handler(event, context):
197
+ result = predict(event, model, preprocessor)
198
+ return {"statusCode": 200,
199
+ "headers": {
200
+ "Access-Control-Allow-Origin": "*",
201
+ "Access-Control-Allow-Credentials": True,
202
+ "Allow": "GET, OPTIONS, POST",
203
+ "Access-Control-Allow-Methods": "GET, OPTIONS, POST",
204
+ "Access-Control-Allow-Headers": "*"
205
+ },
206
+ "body": json.dumps(result)}
@@ -0,0 +1 @@
1
+