def route(options, body): #options: a list of strings that defines what functionality the user requested, includes user passed variables routes = { 'account': account, 'data': data, 'social': social, 'picture': picture, 'ml': ml } try: main_func = routes[ options[0]] #something in the routes dictionary above assert hasattr(main_func, options[1]) func_call = getattr(main_func, options[1]) #something like account.login #TODO assert that everything checks out #actually call the thing except: return constants.respond(err="Bad Path", statusCode="400") if (body is None): func_call(options) else: func_call(body)
def upload_annotations(options): print(options) #TODO add support for CSV #TODO assert all arguments are present #Take the annotations (a dictionary) bucket_name = "machine-learning2019" print("ANNOTA", options['annotations']) annotations = options['annotations'] s3 = boto3.resource("s3") bucket_source={"Bucket":bucket_name, "Key":"/"} for pic_url in annotations: print("PIC_URL", pic_url) bytesIO = BytesIO() print("type pic-rul annot", annotations[pic_url], type(annotations[pic_url])) bytesIO.write(json.dumps(annotations[pic_url]).encode()) bytesIO.seek(0) #TODO get model name and input that instead of default resnet s3.meta.client.upload_fileobj(bytesIO, bucket_name, 'resnet_keras.h5.dir/annotated_images/annotations/{}.json'.format(pic_url.split('.')[0])) src_unann = 'resnet_keras.h5.dir/unannotated_images/{}'.format(pic_url) dest_ann = 'resnet_keras.h5.dir/annotated_images/{}'.format(pic_url) bucket_source["Key"]=src_unann print("SRVC_UNANN", src_unann, bucket_source["Key"]) #s3.meta.client.download("resnet_keras.h5.dir/unannotated_images/1247343586733.jpeg", bucket_name, ".") # Copy object A as object B s3.Object(bucket_name, dest_ann).copy_from( CopySource=bucket_source) # Delete the former object A s3.Object(bucket_name, src_unann).delete() #s3.meta.client.copy(CopySource=bucket_source, Bucket=bucket_name, Key=dest_ann) return constants.respond(statusCode="200")
def register(options): #options: a list of variables given_email = options['email'] given_password = options['pass'] #HASH here given_password = auth.hash_password(given_password) #First connect to the table client = boto3.client('rds-data') #Check if the user already exist in the database and throw a 400 statuscode if they do existing_user = client.execute_statement( secretArn=constants.SECRET_ARN, database=constants.DB_NAME, resourceArn=constants.ARN, sql="SELECT email FROM UserData WHERE email = '%s'" % (given_email)) if (existing_user['records'] != []): print("user exists already") constants.ERR = "User already exists" constants.STATUS_CODE = 409 return existing_user = client.execute_statement( secretArn=constants.SECRET_ARN, database=constants.DB_NAME, resourceArn=constants.ARN, sql= "INSERT INTO UserData (email, pass, type, name) VALUES ('%s','%s','%s','%s')" % (given_email, given_password, 0, given_email)) #Return success return constants.respond(statusCode="200") #OK
def mobile_classify(options): print("MOBIL CLASS") pic_source = base64.b64decode(options['pic']) model=str(options["model"]) #create a client to make requests to ml api http = urllib3.PoolManager() bytesIO = BytesIO() bytesIO.write(pic_source) bytesIO.seek(0) s3 = boto3.resource("s3") bucket_name = "machine-learning2019" now = str(time.monotonic_ns()) pic_path = "{}.dir/unannotated_images/{}.jpeg".format(model, now) #upload the file that was passed to the S3 bucket s3 = boto3.resource("s3") bucket_name = "machine-learning2019" s3.meta.client.upload_fileobj(bytesIO, bucket_name, pic_path) #s3.meta.client.upload_file(pic_source, bucket_name, pic_path) print("MOBILE CLASS UPLAODED IMAGE TO S3") #TODO replace with actual request r = http.request('GET', 'ec2-3-18-109-238.us-east-2.compute.amazonaws.com:3000/predict?modelName={}&imageURL={}'.format(model, pic_path)) #resp stores the classifier prediction resp = json.loads(r.data.decode('utf-8')) return constants.respond(statusCode="200", res=resp)
def classify(options): #options: # picture:(str) a picture to classify. path to file # model: (str) the model to classify with #TODO assert these are proper types pic_source = str(options["pic"]) model=str(options["model"]) #create a client to make requests to ml api http = urllib3.PoolManager() bytesIO = BytesIO() bytesIO.write(http.request("GET", pic_source).data) bytesIO.seek(0) s3 = boto3.resource("s3") bucket_name = "machine-learning2019" now = str(time.monotonic_ns()) pic_path = "{}.dir/unannotated_images/{}.jpeg".format(model, now) #upload the file that was passed to the S3 bucket s3 = boto3.resource("s3") bucket_name = "machine-learning2019" s3.meta.client.upload_fileobj(bytesIO, bucket_name, pic_path) #s3.meta.client.upload_file(pic_source, bucket_name, pic_path) #TODO replace with actual request r = http.request('GET', 'ec2-3-18-109-238.us-east-2.compute.amazonaws.com:3000/predict?modelName={}&imageURL={}'.format(model, pic_path)) #resp stores the classifier prediction resp = json.loads(r.data.decode('utf-8')) return constants.respond(statusCode="200", res=resp)
def upload_model(options): model_url = options["modelURL"] model_name = options['modelName'] model_type = options['modelType'] #TODO make call to ML API http = urllib3.PoolManager() req_url = 'http://ec2-3-18-109-238.us-east-2.compute.amazonaws.com:3000/upload/new?modelFileURL={}&modelName={}&modelType={}'.format(model_url, model_name, model_type) r = http.request('POST', req_url) print(r.data.decode("utf-8")) return constants.respond(statusCode="200", res = json.loads(r.data.decode('utf-8')))
def main(event, context): constants.ERR = None constants.STATUS_CODE = "200" constants.RES = "Success" path = event['path'] httpType = event['httpMethod'] path = parse_path(path) # if('' in path): # print("lambda just tried to reinvoke itself. terminating") # return constants.respond(err="Parse error, lambda reinvoked", statusCode = "403") print(path) if (httpType == "GET"): print("Routing GET") route(path, None) else: print("Routing not GET") try: body = event['body'] except: return constants.respond(err="NO BODY", statusCode="404") try: body = json.loads(body) except ValueError as e: print("body error") return constants.respond(err="BODY ERROR, CHECK FORMAT", statusCode="400") route(path, body) return constants.respond(constants.ERR, constants.RES, constants.STATUS_CODE)
def list(options): http = urllib3.PoolManager() r = http.request('GET', 'http://ec2-3-18-109-238.us-east-2.compute.amazonaws.com:3000/models/list') print("YOOOOOO", r.data) resp = json.loads(r.data.decode('utf-8')) return constants.respond(statusCode="200", res=resp)
def dl_unannotated_imgs(options): return constants.respond(statusCode="200", res={'img1':"23879ryghfkvh2039toirghir9u3289tuoirshnksajshkvsjgh2938ut98urfhvjnkjhow8"})