getPrediction

Category:Data

getPrediction(name, id, data, callback)

After importing an AI Model into App Lab, you can use getPrediction() to ask your model to make a decision based on the data you supply.

The name and id are generated when you import your AI Model into App Lab. The data is the information you've collected in your app that is used by the model to make a decision. Once the model makes it's decision, any code inside the callback function is executed.

Want to learn more about AI Lab? Read a short overview of what it is and how to use it here.

The data parameter must be a javascript object variable or a javascript object defined using curly brace and colon notation (see examples below). Once the model makes its decision, the result is stored in the variable value and can be used in the callback function. To view more information about the model that your app uses, use the Import AI Models screen to view the Model Card in your app.

Examples

Quiz - Where Is Your Ideal Vacation?

Uses a pre-trained Machine Learning model from AI Lab to predict your ideal vacation from 4 questions

                            var testValues = {};
onEvent("QuizWhereShouldYouTakeaVacation_predict", "click", function() {
    testValues.scent = getText("scent_dropdown");
		testValues.personality = getText("personality_dropdown");
		testValues.activity = getText("activity_dropdown");
		testValues.plant = getText("plant_dropdown");
    setText("QuizWhereShouldYouTakeaVacation_prediction", '');
    getPrediction("Quiz - Where Should You Take a Vacation?", "l1MvzjqO1K5L", testValues, function(value) {
      if (value == "Paris") {
        setScreen("parisScreen");
      }
      if (value == "New York") {
        setScreen("newYorkScreen");
      }
      if (value == "London") {
        setScreen("londonScreen");
      }
      if (value == "Bahamas") {
        setScreen("bahamasScreen");
      }
    });
  });
                        

Syntax

getPrediction(name, id, data, callback)

Parameters

Name Type Required? Description
name String The name of the model from when it was saved in AI Lab.
id String A unique identifier for your model that is generated by AI Lab. Changing this value can cause problems with your app.
data Object The data that is used by the model to make a prediction. Must be either a javascript object variable or a javascript object defined using curly brace and colon notation (see examples above).
callback Function The callback function that is run automatically after the model makes its decision. The result from the model is stored in the *value* variable and can be used within the callback function.

Returns

When getPrediction() is finished, the decision from the model is stored in the value variable and all code inside the callback function is automatically run.

Tips

Importing an AI Model will automatically generate the getPrediction() block for your model. You shouldn't need to change the name or id of the model, but you can make changes to the data parameter and the code inside the callback function.

Found a bug in the documentation? Let us know at documentation@code.org