forked from aws-samples/machine-learning-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.m
92 lines (73 loc) · 2.83 KB
/
ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// ViewController.m
// ObjectiveCSample
//
// Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Amazon Software License (the "License").
// You may not use this file except in compliance with the License.
// A copy of the License is located at
//
// http://aws.amazon.com/asl/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express
// or implied. See the License for the specific language governing permissions
// and limitations under the License.
//
#import "ViewController.h"
#import "AWSMachineLearning.h"
@interface ViewController ()
@end
// Cache real-time endpoint
NSString *endpoint;
// Amazon Machine Learning Client
AWSMachineLearning *machineLearning;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
machineLearning = [AWSMachineLearning defaultMachineLearning];
// Specify your ML Model
NSString *MLModelId = @"YOUR-ML-MODEL-ID";
AWSMachineLearningGetMLModelInput *getMLModelInput = [AWSMachineLearningGetMLModelInput new];
getMLModelInput.MLModelId = MLModelId;
// Call Get ML Model
[[[machineLearning getMLModel:getMLModelInput] continueWithSuccessBlock:^id(AWSTask *task) {
AWSMachineLearningGetMLModelOutput *getMLModelOutput = task.result;
if (getMLModelOutput.status != AWSMachineLearningEntityStatusCompleted) {
NSLog(@"ML Model is not completed");
return nil;
}
if (getMLModelOutput.endpointInfo.endpointStatus != AWSMachineLearningRealtimeEndpointStatusReady) {
NSLog(@"Real-time endpoint is not ready");
return nil;
}
endpoint = getMLModelOutput.endpointInfo.endpointUrl;
// Since model is complete and real-time endpoint is ready, call predict
return [self predict:MLModelId withRecord: @{}];
}] continueWithBlock:^id(AWSTask *task) {
if (task.error) {
NSLog(@"Error %@", task.error);
}
if (task.exception) {
NSLog(@"Exception %@", task.exception);
}
if (task.result) {
AWSMachineLearningPredictOutput *predictOutput = task.result;
NSLog(@"Prediction: %@", predictOutput.prediction);
}
return nil;
}];
}
- (AWSTask *) predict:(NSString *)mlModelId withRecord:(NSDictionary *)record {
AWSMachineLearningPredictInput *predictInput = [AWSMachineLearningPredictInput new];
predictInput.predictEndpoint = endpoint;
predictInput.MLModelId = mlModelId;
predictInput.record = record;
return [machineLearning predict:predictInput];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end