user3798780
user3798780

Reputation: 101

iOS Amazon Machine Learning Swift

Amazon has documentation on how to use their Machine Learning platform on iOS but don't have a Swift implementation and I am having trouble translating it to Swift. Here is the Objective-C code:

    // Use a created model that has a created real-time endpoint
NSString *MLModelId = @"example-model-id";

// Call GetMLModel to get the realtime endpoint URL
AWSMachineLearningGetMLModelInput *getMLModelInput =          [AWSMachineLearningGetMLModelInput new];
getMLModelInput.MLModelId = MLModelId;

[[[MachineLearning getMLModel:getMLModelInput]    continueWithSuccessBlock:^id(AWSTask *task) {
   AWSMachineLearningGetMLModelOutput *getMLModelOutput = task.result;

   // Validate that the ML model is completed
   if (getMLModelOutput.status != AWSMachineLearningEntityStatusCompleted) {
       NSLog(@"ML Model is not completed");
       return nil;
    }

    // Validate that the realtime endpoint is ready
    if (getMLModelOutput.endpointInfo.endpointStatus !=     AWSMachineLearningRealtimeEndpointStatusReady) {
       NSLog(@"Realtime endpoint is not ready");
       return nil;
    }
}
AWSMachineLearningPredictInput *predictInput = [AWSMachineLearningPredictInput new];
predictInput.predictEndpoint = getMLModelOutput.endpointInfo.endpointUrl;
predictInput.MLModelId = MLModelId;
predictInput.record = @{};

// Call and return prediction
return [MachineLearning predict:predictInput];

Here is my attempted swift code

var  getMLModelInput  = AWSMachineLearningGetMLModelInput()
// Use a created model that has a created real-time endpoint
let MLModelId = "example-model-id"

// Call GetMLModel to get the realtime endpoint URL
getMLModelInput.MLModelId = MLModelId;
let task = AWSMachineLearning.getMLModel(getMLModelInput) // This line won't work because the method .getMLModel expects and instance of AWSMachineLearning

I was trying to model my Swift code after code used for uploads to s3 like this:

    let transferManager = AWSS3TransferManager.defaultS3TransferManager()

    let testFileURL1 = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("tmp")

    let uploadRequest1 : AWSS3TransferManagerUploadRequest = AWSS3TransferManagerUploadRequest()

    let data = userCSV.dataUsingEncoding(NSUTF8StringEncoding)
    data!.writeToURL(testFileURL1, atomically: true)

    uploadRequest1.bucket = "users/1"
    uploadRequest1.key =  "tmpkey.csv"
    uploadRequest1.body = testFileURL1

    let task = transferManager.upload(uploadRequest1)
    task.continueWithBlock { (task) -> AnyObject! in
        if task.error != nil {
            print("Error: \(task.error)")
        } else {
            print("Upload successful")

        }
        return nil
    }

but I can't figure out how to build the task object for the Machine Learning models. Any help would be much appreciated!

Upvotes: 0

Views: 345

Answers (1)

Yosuke
Yosuke

Reputation: 3759

The code snippet on the AWS website is missing one line in the beginning:

AWSMachineLearning *MachineLearning = [AWSMachineLearning defaultMachineLearning];

You can translate this to Swift like this

let MachineLearning = AWSMachineLearning.defaultMachineLearning()

Then you can call something like this:

let MachineLearning = AWSMachineLearning.defaultMachineLearning()

let getMLModelInput  = AWSMachineLearningGetMLModelInput()
// Use a created model that has a created real-time endpoint
getMLModelInput.MLModelId = "example-model-id"

MachineLearning.getMLModel(getMLModelInput).continueWithBlock { (task) -> AnyObject? in
    //
}

You should take a look at this integration test case for more details.

Upvotes: 2

Related Questions