Skip to content

Commit 7b7f365

Browse files
committed
text
1 parent f4751e8 commit 7b7f365

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

Diff for: samples/job_with_text_input_sample.js

+38-34
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ 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 config 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

1717
// Client initialization
1818
// Initialize the ApiClient instance with the BASE_URL and the API_KEY to store those arguments
@@ -21,85 +21,89 @@ const modzyClient = new modzy.ModzyClient(BASE_URL, API_KEY);
2121

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

24-
async function createJobWithTextInput(){
25-
try{
24+
async function createJobWithTextInput() {
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),
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),
2828
// 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("Sentiment Analysis");
3030
// Or if you already know the model id and want to know more about the model, you can use this instead:
3131
//let model = await modzyClient.getModel("ed542963de");
32-
32+
//You can find more information about how to query the models on the model_samples.js file.
33+
3334
// 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:
38-
// If you already know the model version and the input key(s) of the model version you can skip this step. Also, you can
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
3941
// use the following code block to know about the inputs 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
}
5557

5658
// Send the job:
57-
// With the info about the model (identifier), the model version (version string, input/output keys), you are ready to
59+
// With the info about the model (identifier) and the model version (version string, input/output keys), you are ready to
5860
// submit the job. Just prepare the source object:
59-
let sources = {"source-key": {"input.txt": "Modzy is great!"}};
61+
let sources = { "source-key": { "input.txt": "Modzy is great!" } };
6062
// An inference job groups input data that you send to a model. You can send any amount of inputs to
61-
// process and you can identify and refer to a specific input by the key that you assign, for example we can add:
62-
sources["second-key"] = {"input.txt": "Sometimes I really hate ribs"}
63-
sources["another-key"] = {"input.txt": "Born and raised in Pennsylvania, Swift moved to Nashville, Tennessee, at the age of 14 to pursue a career in country music"}
63+
// process and you can identify and refer to a specific input by the key assigned. For example we can add:
64+
sources["second-key"] = { "input.txt": "Sometimes I really hate ribs" }
65+
sources["another-key"] = { "input.txt": "Born and raised in Pennsylvania, Swift moved to Nashville, Tennessee, at the age of 14 to pursue a career in country music" }
6466
// If you send a wrong input key, the model fails to process the input.
65-
sources["wrong-key"] = {"a.wrong.key": "This input is wrong!"}
67+
sources["wrong-key"] = { "a.wrong.key": "This input is wrong!" }
6668
// When you have all your inputs ready, you can use our helper method to submit the job as follows:
67-
let job = await modzyClient.submitJobText(model.modelId,modelVersion.version, sources);
69+
let job = await modzyClient.submitJobText(model.modelId, modelVersion.version, sources);
6870
// Modzy creates the job and queue for processing. The job object contains all the info that you need to keep track
6971
// of the process, the most important being the job identifier and the job status.
70-
logger.info("job: "+job.jobIdentifier+" "+job.status);
72+
logger.info("job: " + job.jobIdentifier + " " + job.status);
7173
// The job moves to SUBMITTED, meaning that Modzy acknowledged the job and sent it to the queue to be processed.
72-
// We provide a helper method to listen until the job finishes processing. it will listen until the job finishes
74+
// We provide a helper method to listen until the job finishes processing. It listens until the job finishes
7375
// and moves to COMPLETED, CANCELED, or TIMEOUT.
7476
job = await modzyClient.blockUntilComplete(job);
77+
7578
// Get the results:
7679
// Check the status of the job. Jobs may be canceled or may reach a timeout.
77-
if( job.status === "COMPLETED" ){
80+
if (job.status === "COMPLETED") {
7881
// A completed job means that all the inputs were processed by the model. Check the results for each
79-
// input keys provided in the source object to see the model output.
82+
// input key provided in the source object to see the model output.
8083
let result = await modzyClient.getResult(job.jobIdentifier);
81-
// The result object has some useful info:
84+
// The results object has some useful info:
8285
logger.info(`Result: finished: ${result.finished}, total: ${result.total}, completed: ${result.completed}, failed: ${result.failed}`);
83-
// Notice that we are iterating through the same input sources keys
84-
for( key in sources ){
85-
// The result object has the individual results of each job input. In this case the output key is called
86+
// Notice that we are iterating through the same input source keys
87+
for (key in sources) {
88+
// The results object has the individual results of each job input. In this case the output key is called
8689
// results.json, so we can get the results as follows:
87-
if( result.results[key] ){
90+
if (result.results[key]) {
8891
let model_res = result.results[key]["results.json"];
8992
// The output for this model comes in a JSON format, so we can directly log the model results:
9093
logger.info(` ${key}: ${JSON.stringify(model_res)}`);
9194
}
92-
else{
95+
else {
96+
// If the model raises an error, we can get the specific error message:
9397
logger.warn(` ${key}: failure ${result.failures[key]['error']}`);
9498
}
9599
}
96100
}
97-
else{
101+
else {
98102
log.warn(`The job ends with status ${job.status}`);
99-
}
103+
}
100104
}
101-
catch(error){
102-
logger.warn("Unexpected error "+error);
105+
catch (error) {
106+
logger.warn("Unexpected error " + error);
103107
}
104108
}
105109

0 commit comments

Comments
 (0)