@@ -5,121 +5,125 @@ const modzy = require('modzy-sdk');
5
5
logger . level = "info" ;
6
6
7
7
// The system admin can provide the right base API URL, the API key can be downloaded from your profile page on Modzy.
8
- // You can config those params as is described in the readme file (as environment variables, or by using the .env file), or you
9
- // or you can just update the BASE_URL and API_KEY vars and use this sample code (not recommended for production environments).
8
+ // You can configure those params as described in the README file (as environment variables, or by using the .env file),
9
+ // or you can just update the BASE_URL and API_KEY variables and use this sample code (not recommended for production environments).
10
10
// The MODZY_BASE_URL should point to the API services route which may be different from the Modzy page URL.
11
11
// (ie: https://modzy.example.com/api).
12
12
const BASE_URL = process . env . MODZY_BASE_URL ;
13
- // The MODZY_API_KEY is your own personal API key. It is composed by a public part, a dot character and a private part
13
+ // The MODZY_API_KEY is your own personal API key. It is composed by a public part, a dot character, and a private part
14
14
// (ie: AzQBJ3h4B1z60xNmhAJF.uQyQh8putLIRDi1nOldh).
15
- const API_KEY = process . env . MODZY_API_KEY ;
15
+ const API_KEY = process . env . MODZY_API_KEY ;
16
16
17
- // Client initialization
17
+ // Client initialization:
18
18
// Initialize the ApiClient instance with the BASE_URL and the API_KEY to store those arguments
19
19
// for the following API calls.
20
20
const modzyClient = new modzy . ModzyClient ( BASE_URL , API_KEY ) ;
21
21
22
22
// Create a Job with an AWS input, wait, and retrieve results:
23
23
24
- async function createJobWithAWSInput ( ) {
25
- try {
24
+ async function createJobWithAWSInput ( ) {
25
+ try {
26
26
// Get the model object:
27
- // If you already know the model identifier (i.e.: you got from the URL of the model details page or the input sample),
28
- // you can skip this step. If you don't you can find the model identifier by using its name as follows:
27
+ // If you already know the model identifier (i.e.: you got it from the URL of the model details page or the input sample),
28
+ // you can skip this step. If you don't, you can find the model identifier by using its name as follows:
29
29
let model = await modzyClient . getModelByName ( "Facial Embedding" ) ;
30
30
// Or if you already know the model id and want to know more about the model, you can use this instead:
31
- //let model = await modzyClient.getModel("f7e252e26a");
32
-
33
- // The model identifier is under the modelId key. You can take a look at the other keys by uncommenting the following line
31
+ //let model = await modzyClient.getModel("f7e252e26a");
32
+ //You can find more information about how to query the models on the model_samples.js file.
33
+
34
+ // The model identifier is under the modelId key. You can take a look at the other keys by uncommenting the following line
34
35
logger . info ( Object . keys ( model ) . toString ( ) . replace ( '\n' , ' ' ) ) ;
35
36
// Or just log the model identifier and the latest version
36
37
logger . info ( `The model identifier is ${ model . modelId } and the latest version is ${ model . latestVersion } ` ) ;
38
+
37
39
// Get the model version object:
38
40
// If you already know the model version and the input key(s) of the model version you can skip this step. Also, you can
39
- // use the following code block to know about the inputs keys and skip the call on future job submissions.
41
+ // use the following code block to know about the input keys and skip the call on future job submissions.
40
42
let modelVersion = await modzyClient . getModelVersion ( model . modelId , model . latestVersion ) ;
41
- // The info stored in modelVersion provides insights about the amount of time that the model can spend processing, the inputs , and
43
+ // The info stored in modelVersion provides insights about the amount of time that the model can spend processing, the input , and
42
44
// output keys of the model.
43
- logger . info ( `Ths model version is ${ modelVersion . version } ` ) ;
45
+ logger . info ( `This model version is ${ modelVersion . version } ` ) ;
44
46
logger . info ( ` timeouts: status ${ modelVersion . timeout . status } ms, run ${ modelVersion . timeout . run } ms ` ) ;
45
47
logger . info ( " inputs: " ) ;
46
- for ( key in modelVersion . inputs ) {
48
+ for ( key in modelVersion . inputs ) {
47
49
let input = modelVersion . inputs [ key ] ;
48
50
logger . info ( ` key ${ input . name } , type ${ input . acceptedMediaTypes } , description: ${ input . description } ` ) ;
49
51
}
50
52
logger . info ( " outputs: " )
51
- for ( key in modelVersion . outputs ) {
53
+ for ( key in modelVersion . outputs ) {
52
54
let output = modelVersion . outputs [ key ] ;
53
55
logger . info ( ` key ${ output . name } , type ${ output . mediaType } , description: ${ output . description } ` ) ;
54
56
}
57
+
55
58
// Send the job:
56
59
// Amazon Simple Storage Service (AWS S3) is an object storage service (for more info visit: https://aws.amazon.com/s3/?nc1=h_ls).
57
- // It allows to store images, videos, or other content as files. In order to use as input type, provide the following properties:
60
+ // It allows to store images, videos, or other content as files. In order to use it as an input type, provide the following properties:
58
61
// AWS Access Key: replace <<AccessKey>>
59
- const ACCESS_KEY = "<<AccessKey>>" ;
62
+ const ACCESS_KEY = "<<AccessKey>>" ;
60
63
// AWS Secret Access Key: replace <<SecretAccessKey>>
61
- const SECRET_ACCESS_KEY = "<<SecretAccessKey>>" ;
64
+ const SECRET_ACCESS_KEY = "<<SecretAccessKey>>" ;
62
65
// AWS Default Region : replace <<AWSRegion>>
63
- const REGION = "<<AWSRegion>>" ;
66
+ const REGION = "<<AWSRegion>>" ;
64
67
// The Bucket Name: replace <<BucketName>>
65
- const BUCKET_NAME = "<<BucketName>>" ;
68
+ const BUCKET_NAME = "<<BucketName>>" ;
66
69
// The File Key: replace <<FileId>> (remember, this model needs an image as input)
67
- const FILE_KEY = "<<FileId>>" ;
68
- // With the info about the model (identifier), the model version (version string, input/output keys), you are ready to
69
- // submit the job. Just prepare the source object:
70
- let sources = { "source-key" : { "image" : { 'bucket' : BUCKET_NAME , 'key' : FILE_KEY } } } ;
71
- // An inference job groups input data that you send to a model. You can send any amount of inputs to
72
- // process and you can identify and refer to a specific input by the key that you assign, for example we can add:
73
- sources [ "second-key" ] = { "image" : { 'bucket' : BUCKET_NAME , 'key' : FILE_KEY } } ;
74
- sources [ "another-key" ] = { "image" : { 'bucket' : BUCKET_NAME , 'key' : FILE_KEY } } ;
70
+ const FILE_KEY = "<<FileId>>" ;
71
+ // With the info about the model (identifier) and the model version (version string, input/output keys), you are ready to
72
+ // submit the job. Just prepare the source object:
73
+ let sources = { "source-key" : { "image" : { 'bucket' : BUCKET_NAME , 'key' : FILE_KEY } } } ;
74
+ // An inference job groups input data sent to a model. You can send any amount of inputs to
75
+ // process and you can identify and refer to a specific input by the key assigned. For example we can add:
76
+ sources [ "second-key" ] = { "image" : { 'bucket' : BUCKET_NAME , 'key' : FILE_KEY } } ;
77
+ sources [ "another-key" ] = { "image" : { 'bucket' : BUCKET_NAME , 'key' : FILE_KEY } } ;
75
78
// If you send a wrong input key, the model fails to process the input.
76
- sources [ "wrong-key" ] = { "a.wrong.key" : { 'bucket' : BUCKET_NAME , 'key' : FILE_KEY } } ;
79
+ sources [ "wrong-key" ] = { "a.wrong.key" : { 'bucket' : BUCKET_NAME , 'key' : FILE_KEY } } ;
77
80
// If you send a correct input key, but a wrong AWS S3 value key, the model fails to process the input.
78
- sources [ "wrong-value" ] = { "image" : { 'bucket' : BUCKET_NAME , 'key' : "wrong-aws-file-key.png" } } ;
79
- // When you have all your inputs ready, you can use our helper method to submit the job as follows:
81
+ sources [ "wrong-value" ] = { "image" : { 'bucket' : BUCKET_NAME , 'key' : "wrong-aws-file-key.png" } } ;
82
+ // When you have all your inputs ready, you can use our helper method to submit the job as follows:
80
83
let job = await modzyClient . submitJobAWSS3 (
81
84
model . modelId ,
82
85
modelVersion . version ,
83
- ACCESS_KEY ,
84
- SECRET_ACCESS_KEY ,
86
+ ACCESS_KEY ,
87
+ SECRET_ACCESS_KEY ,
85
88
REGION ,
86
- sources
89
+ sources
87
90
) ;
88
91
// Modzy creates the job and queue for processing. The job object contains all the info that you need to keep track
89
92
// of the process, the most important being the job identifier and the job status.
90
- logger . info ( "job: " + job . jobIdentifier + " " + job . status ) ;
93
+ logger . info ( "job: " + job . jobIdentifier + " " + job . status ) ;
91
94
// The job moves to SUBMITTED, meaning that Modzy acknowledged the job and sent it to the queue to be processed.
92
- // We provide a helper method to listen until the job finishes processing. it will listen until the job finishes
95
+ // We provide a helper method to listen until the job finishes processing. It listens until the job finishes
93
96
// and moves to COMPLETED, CANCELED, or TIMEOUT.
94
97
job = await modzyClient . blockUntilComplete ( job ) ;
95
98
// Get the results:
96
99
// Check the status of the job. Jobs may be canceled or may reach a timeout.
97
- if ( job . status === "COMPLETED" ) {
100
+ if ( job . status === "COMPLETED" ) {
98
101
// A completed job means that all the inputs were processed by the model. Check the results for each
99
- // input keys provided in the source object to see the model output.
102
+ // input key provided in the source object to see the model output.
100
103
let result = await modzyClient . getResult ( job . jobIdentifier ) ;
101
- // The result object has some useful info:
104
+ // The results object has some useful info:
102
105
logger . info ( `Result: finished: ${ result . finished } , total: ${ result . total } , completed: ${ result . completed } , failed: ${ result . failed } ` ) ;
103
- // Notice that we are iterating through the same input sources keys
104
- for ( key in sources ) {
105
- // The result object has the individual results of each job input. In this case the output key is called
106
+ // Notice that we are iterating through the same input source keys
107
+ for ( key in sources ) {
108
+ // The results object has the individual results of each job input. In this case the output key is called
106
109
// results.json, so we can get the results as follows:
107
- if ( result . results [ key ] ) {
110
+ if ( result . results [ key ] ) {
108
111
let model_res = result . results [ key ] [ "results.json" ] ;
109
112
// The output for this model comes in a JSON format, so we can directly log the model results:
110
113
logger . info ( ` ${ key } : ${ JSON . stringify ( model_res ) } ` ) ;
111
114
}
112
- else {
115
+ else {
116
+ // If the model raises an error, we can get the specific error message:
113
117
logger . warn ( ` ${ key } : failure ${ result . failures [ key ] [ 'error' ] } ` ) ;
114
118
}
115
119
}
116
120
}
117
- else {
121
+ else {
118
122
log . warn ( `The job ends with status ${ job . status } ` ) ;
119
- }
123
+ }
120
124
}
121
- catch ( error ) {
122
- logger . warn ( "Unexpected error " + error ) ;
125
+ catch ( error ) {
126
+ logger . warn ( "Unexpected error " + error ) ;
123
127
}
124
128
}
125
129
0 commit comments