DataRobot

DataRobot for Developers

Embed cutting edge AI into the applications and services you build.

Get Started    API Reference

Movie ratings - sentiment

In this tutorial, we walk you through building an AI-powered app that predicts sentiment (classification) from a user-provided review using DataRobot APIs.

Introduction

This guide will take you through the process adding sentiment prediction to a movie rating application from start to finish.

What you will learn

  • How to use DataRobot’s GUI and APIs to train and deploy a model
  • How to use DataRobot’s prediction APIs to integrate your deployed model in an application and make predictions with it.
  • How to use DataRobot’s APIs to train and deploy a model

What you won't learn

  • How to create the web application that renders the contents. You will be working with an existing application.
  • How to manually train the ML model - without the use of DataRobot

The steps you'll take

  • Review the app that you'll be working with app
  • Obtain and understand the dataset
  • Load the data into DataRobot and train your model
  • Deploy your model model and expose it as an API
  • Integrate the deployed model in your app

🚧

Before you start you'll need:

  • Access to DataRobot. You can request to join the trial using the form at this link.
  • Elementary knowledge of Node.JS, React, and Git.
  • In the example we use Vercel to deploy the app.

The movie rating app

This is the end product - a movie rating application that uses ML to determine the sentiment in ratings supplied by users.

The app is deployed at the link https://movie-rating-app.now.sh, and you can also interact with it below 👇

User flow

  1. The user sees a movie, and writes a review for it
  2. The app makes a request to DataRobot’s prediction API with the movie review, which returns a prediction of whether a review is positive or negative
  3. The app shows the sentiment to a user. You can then store that prediction to mark the review either positive or negative in your business logic.

Project setup

Get the source code

All source code is available in GitHub - https://github.com/datarobot-community/movie-rating-app
The skeleton project you will use to follow the tutorial is in the tutorial-start branch (which is the default, so you don't need to do anything).
The finished project is in the complete branch.

To get it:

git clone https://github.com/datarobot-community/movie-rating-app

cd movie-rating-app

Overview of the project

The app has 2 directories of interest:

  • movie-rating-classifier - the React app with one serverless function that makes predictions
  • resources - contains training dataset

Furthermore, in the movie-rating-classifier directory the files of interest are:

  • src/App.js - the main React app that shows the UI. All logic is in this file for simplicity.
  • api/prediction.js - the single serverless function exposed as the API which will trigger a call to DataRobot.

Install dependencies

  • Install the Vercel CLI which you'll use to deploy (if you don't have it yet).
npm install -g vercel

The app is located in movie-rating-classifier, so CD there and install the npm dependencies:

cd movie-rating-classifier

npm install

To run the app you must first set it up for deployment with Vercel. To do this, just run one command and follow the on-screen instructions:

vercel

Once you have done this, you can also run the project locally with vercel dev.

Creating the model - GUI

Importing data

Each dataset in DataRobot is associated with a Project.
When using the DataRobot GUI, you’re greeted with a page prompting you to upload a dataset.

Import data into DataRobot

Selecting Local File will let you search for the file in your computer. Take the ZIP file from the project's resources directory. After selecting it, DataRobot takes a few seconds to upload it (depending on your upload speeds), and starts analyzing the dataset.

Exploring the dataset

After it has analyzed the dataset (called Preliminary Exploratory Data Analysis) you are asked the question: What would you like to predict?

What would you like to predict?

Scrolling down on this page, you can also review how DataRobot understands your data.

Explore the dataset

In your case you have 2 features - review and sentiment, where review is of the type Text, and sentiment is of the type Categorical.

Training the models

Scroll back up to the top of the page and type sentiment in the Enter your target field.
It shows you the distribution of values in the sentiment. In this dataset, half are positive and half are negative. Underneath it, the big Start button is now available to press, which will kick off DataRobot’s AutoML modeling process - training our models.

Go ahead, press it... 👇

After specifying the target, the Start button becomes enabled

After selecting your modeling target and pressing Start you don’t have to do much other than wait for DataRobot to train a number of models and recommend to you the best model for deployment.

DataRobot leaderboard with various models

Deploying a model

Having several trained models to choose from and understanding which one is best suited for your data is great, but it’s not enough to integrate in your application.

For that you need to expose it as a prediction API endpoint, and that requires you to deploy a model.
Select the top one - the one that’s labeled as Recommended for Deployment, and select Predict, and Deploy tabs, respectively:

Model details - add new deployment

You can give your deployment a name and a description, and press the Deploy Model button.

Name deployment and add description

After a few seconds, you will be notified that your model has been successfully deployed, and prompted to click Open Deployment.

Deployment successful

You have successfully trained and deployed your ML model with DataRobot. Time to integrate it with your application.

Integrate the model

The final step of ML development is to integrate your deployed model in your application.

Prediction API

Prediction API calls have the following format:

curl POST 'https://PREDICTION_SERVER_ENDPOINT/predApi/v1.0/deployments/DEPLOYMENT_ID/predictions' 
--header 'Authorization: Bearer API_KEY' \
--header 'datarobot-key: DATAROBOT_KEY' \
--header 'Content-Type: application/json' \
--data-raw ' [
   {
        "review": "review copy..."
   }. ...
  ]'

To make API calls to the deployed models, you’ll need a few values that you will find on the Deployments section: prediction server endpoint, To make API calls to the deployed models, you’ll need a few values that you will find on the Deployments section: prediction server endpoint, deployment ID, DataRobot Key, and your API key.

Endpoints and API keys

You can find that in the Summary section of the Overview tab:

Prediction server endpoint

Deployment ID
You can either find this in the browser’s address bar when looking at the deployment page:

deployment ID in the browser address bar

You can also find it in the code snippet of the Integrations tab:

Deployment ID in the integrations tab code snippet

DataRobot Key
This is also located in the code snippet of the Integrations tab:

DataRobot key in the integrations tab code snippet

API Key
While you can also find your API key in the code snippet, you can also find and create it in the Developer Tools section of DataRobot. It is located in the top right corner icon of the page (from your profile).

Developer Tools section

Having noted all these values, you are now ready to move on to the application.

Implementing the app

The skeleton project is mostly created for you. All you need to do is add a single API method that will call the DataRobot prediction API.

Add your DataRobot API variables and API credentials

Open the file movie-rating-classified/api/_env.js, and populate it with your values that you got from DataRobot in the previous steps:

module.exports = {
    PREDICTION_SERVER: 'YOUR_PREDICTION_SERVER',
    DEPLOYMENT_ID: 'YOUR_DEPLOYMENT_ID',
    API_KEY: 'YOUR_API_KEY',
    DATAROBOT_KEY: 'YOUR_DATAROBOT_KEY'
}

Your api/prediction.js lambda function imports these as env and is able to access them like env.API_KEY.

Make a prediction request from the backend

Now open api/prediction.js, and change the stub function to the following:

module.exports = async (req, res) => {

    let review = req.body.review
    console.log("Predicting review: ")
    console.log(review)

    let response = await unirest.post(`${process.env.DATAROBOT_PREDICTION_SERVER}/predApi/v1.0/deployments/${process.env.DATAROBOT_DEPLOYMENT_ID}/predictions`)
    .headers({
        "Authorization": `Bearer ${process.env.DATAROBOT_API_KEY}`,
        "datarobot-key": process.env.DATAROBOT_KEY
    })
    .type('json')
    .send(
        [{
        "review": review
    }]
    )

    res.send({prediction: response.body.data[0].prediction})
}

This is a single lambda function, that when deployed to Vercel will expose this function as your own serverless API, called from your React application.

It makes the request to the DataRobot's prediction API with your payload, review.
The prediction server endpoint, deployment ID, API key, and DataRobot key are all taken from the environment variables that you saved in .env in the previous step.

📘

Prediction request and response are arrays

Request body is an array of objects
You needed to wrap the review in a JSON object in an array.
DataRobot's prediction API always expects an Array of objects to predict.
In this example you're only sending one item to predict, but you could be sending multiple predictions in batches.

Response body is an array of predictions
Similarly, the response body will always come back as an array of predictions.
Make sure to handle it accordingly.

Show the prediction response

In src/App.js, find the function submitReview and change it to include the following.

const submitReview =  async () => {
    console.log("Form submitted!")

    console.log(reviewInput.current.value)
    let myReview = reviewInput.current.value
    console.log(myReview)

    setReview(myReview)
    console.log(review)

    let predictionResponse = await predictMovieScore(myReview)

    let prediction = predictionResponse.prediction === 'positive' ? '👍' : '👎'

    setPredictionScore(prediction)
  }

You only added 2 lines:

let prediction = predictionResponse.prediction === 'positive' ? '👍' : '👎'
setPredictionScore(prediction)

This takes the response from your api/prediction.js function, either positive or negative, and displays it as the 👍 or 👎 emoji in the predictionScore React state variable, which will trigger the App's render() function and show a modal popup with the score.

That's it! You can verify it's working correctly by writing a review for a film, and pressing review. Positive reviews and negative reviews will be classified correctly.

You have successfully trained, deployed, and integrated your ML model into an application. It really takes just a single API call to DataRobot's prediction server to get it working.

Updated 4 months ago


Movie ratings - sentiment


In this tutorial, we walk you through building an AI-powered app that predicts sentiment (classification) from a user-provided review using DataRobot APIs.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.