Skip to content

Commit b8ea618

Browse files
committed
comments
1 parent 1c151c4 commit b8ea618

File tree

1 file changed

+54
-50
lines changed

1 file changed

+54
-50
lines changed

samples/job_with_aws_input_sample.js

+54-50
Original file line numberDiff line numberDiff line change
@@ -5,121 +5,125 @@ const modzy = require('modzy-sdk');
55
logger.level = "info";
66

77
// 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).
1010
// The MODZY_BASE_URL should point to the API services route which may be different from the Modzy page URL.
1111
// (ie: https://modzy.example.com/api).
1212
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
1414
// (ie: AzQBJ3h4B1z60xNmhAJF.uQyQh8putLIRDi1nOldh).
15-
const API_KEY = process.env.MODZY_API_KEY;
15+
const API_KEY = process.env.MODZY_API_KEY;
1616

17-
// Client initialization
17+
// Client initialization:
1818
// Initialize the ApiClient instance with the BASE_URL and the API_KEY to store those arguments
1919
// for the following API calls.
2020
const modzyClient = new modzy.ModzyClient(BASE_URL, API_KEY);
2121

2222
// Create a Job with an AWS input, wait, and retrieve results:
2323

24-
async function createJobWithAWSInput(){
25-
try{
24+
async function createJobWithAWSInput() {
25+
try {
2626
// 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:
2929
let model = await modzyClient.getModelByName("Facial Embedding");
3030
// 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
3435
logger.info(Object.keys(model).toString().replace('\n', ' '));
3536
// Or just log the model identifier and the latest version
3637
logger.info(`The model identifier is ${model.modelId} and the latest version is ${model.latestVersion}`);
38+
3739
// Get the model version object:
3840
// 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.
4042
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
4244
// output keys of the model.
43-
logger.info(`Ths model version is ${modelVersion.version}`);
45+
logger.info(`This model version is ${modelVersion.version}`);
4446
logger.info(` timeouts: status ${modelVersion.timeout.status}ms, run ${modelVersion.timeout.run}ms `);
4547
logger.info(" inputs: ");
46-
for(key in modelVersion.inputs){
48+
for (key in modelVersion.inputs) {
4749
let input = modelVersion.inputs[key];
4850
logger.info(` key ${input.name}, type ${input.acceptedMediaTypes}, description: ${input.description}`);
4951
}
5052
logger.info(" outputs: ")
51-
for(key in modelVersion.outputs){
53+
for (key in modelVersion.outputs) {
5254
let output = modelVersion.outputs[key];
5355
logger.info(` key ${output.name}, type ${output.mediaType}, description: ${output.description}`);
5456
}
57+
5558
// Send the job:
5659
// 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:
5861
// AWS Access Key: replace <<AccessKey>>
59-
const ACCESS_KEY="<<AccessKey>>";
62+
const ACCESS_KEY = "<<AccessKey>>";
6063
// AWS Secret Access Key: replace <<SecretAccessKey>>
61-
const SECRET_ACCESS_KEY="<<SecretAccessKey>>";
64+
const SECRET_ACCESS_KEY = "<<SecretAccessKey>>";
6265
// AWS Default Region : replace <<AWSRegion>>
63-
const REGION="<<AWSRegion>>";
66+
const REGION = "<<AWSRegion>>";
6467
// The Bucket Name: replace <<BucketName>>
65-
const BUCKET_NAME="<<BucketName>>";
68+
const BUCKET_NAME = "<<BucketName>>";
6669
// 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 } };
7578
// 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 } };
7780
// 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:
8083
let job = await modzyClient.submitJobAWSS3(
8184
model.modelId,
8285
modelVersion.version,
83-
ACCESS_KEY,
84-
SECRET_ACCESS_KEY,
86+
ACCESS_KEY,
87+
SECRET_ACCESS_KEY,
8588
REGION,
86-
sources
89+
sources
8790
);
8891
// Modzy creates the job and queue for processing. The job object contains all the info that you need to keep track
8992
// 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);
9194
// 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
9396
// and moves to COMPLETED, CANCELED, or TIMEOUT.
9497
job = await modzyClient.blockUntilComplete(job);
9598
// Get the results:
9699
// 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") {
98101
// 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.
100103
let result = await modzyClient.getResult(job.jobIdentifier);
101-
// The result object has some useful info:
104+
// The results object has some useful info:
102105
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
106109
// results.json, so we can get the results as follows:
107-
if( result.results[key] ){
110+
if (result.results[key]) {
108111
let model_res = result.results[key]["results.json"];
109112
// The output for this model comes in a JSON format, so we can directly log the model results:
110113
logger.info(` ${key}: ${JSON.stringify(model_res)}`);
111114
}
112-
else{
115+
else {
116+
// If the model raises an error, we can get the specific error message:
113117
logger.warn(` ${key}: failure ${result.failures[key]['error']}`);
114118
}
115119
}
116120
}
117-
else{
121+
else {
118122
log.warn(`The job ends with status ${job.status}`);
119-
}
123+
}
120124
}
121-
catch(error){
122-
logger.warn("Unexpected error "+error);
125+
catch (error) {
126+
logger.warn("Unexpected error " + error);
123127
}
124128
}
125129

0 commit comments

Comments
 (0)