Deploy The sentiment-general Model

This topic explains how to deploy the sentiment-general pre-trained sentiment prediction model. This is a general-purpose sentiment prediction model, trained on short sentences. It is suitable for short texts and for intent prediction.

1. Install the model in Fusion

  1. Navigate to Collections > Jobs.

  2. Select New > Create Seldon Core Model Deployment.

  3. Configure the job as follows:

    • Job ID - the ID for this job, such as deploy-sentiment-general.

    • Model Name - model name of the Seldon Core deployment that will be referenced in the Machine Learning pipeline stage configurations, such as sentiment-general.

    • Docker Repository - lucidworks

    • Image Name - sentiment-general:v1.0

    • Kubernetes Secret Name for Model Repo - (empty)

    • Output Column Names for Model - [label, score]

  4. Click Save.

  5. Click Run > Start.

2. Configure the Machine Learning pipeline stages

In your index or query pipelines add Machine Learning stage and specify sentiment-general in the Model ID field (or a custom model name that was used during deployment).

2.1. Configure the Machine Learning index stage

  1. In your index pipeline, click Add a Stage > Machine Learning.

  2. In the Model ID field, enter the model name you configured above, such as sentiment-general.

  3. In the Model input transformation script field, enter the following:

    var modelInput = new java.util.HashMap()
    modelInput.put("text", doc.getFirstFieldValue("text"))
    modelInput
  4. In the Model output transformation script field, enter the following:

    doc.addField("sentiment_label_s", modelOutput.get("label")[0])
    doc.addField("sentiment_score_d", modelOutput.get("score")[0])
  5. Save the pipeline.

2.2. Configure the Machine Learning query stage

  1. In your query pipeline, click Add a Stage > Machine Learning.

  2. In the Model ID field, enter the model name you configured above, such as sentiment-general.

  3. In the Model input transformation script field, enter the following:

    var modelInput = new java.util.HashMap()
    modelInput.put("text", request.getFirstParam("q"))
    modelInput
  4. In the Model output transformation script field, enter the following:

    // To put into request
    request.putSingleParam("sentiment_label", modelOutput.get("label")[0])
    request.putSingleParam("sentiment_score", modelOutput.get("score")[0])
    
    // To put into query context
    context.put("sentiment_label", modelOutput.get("label")[0])
    context.put("sentiment_score", modelOutput.get("score")[0])
    
    // To put into response documents. NOTE: This can be done only after Solr Query stage
    var docs = response.get().getInnerResponse().getDocuments();
    var ndocs = new java.util.ArrayList();
    
    for (var i=0; i<docs.length;i++){
      var doc = docs[i];
      doc.putField("query_sentiment_label", modelOutput.get("label")[0])
      doc.putField("query_sentiment_score", modelOutput.get("score")[0])
      ndocs.add(doc);
    }
    
    response.get().getInnerResponse().updateDocuments(ndocs);
  5. Save the pipeline.

Model output

Both of the pre-trained models output the following:

  • a label, negative or positive

  • a score from -2 to 2

The sentiment-reviews:v1.0 model also optionally outputs tokens and their corresponding attention weights, that is, the weight that each token carries in the sentiment prediction. The total of all attention weights is always 1, that is, each value represents a percentage of the total weight. In the example below, "awesome" has the highest weight because it expresses the strongest sentiment compared to other tokens in the string:

// Input
text = "That's awesome!"

// Output
sentiment_label = ‘positive’
sentiment_score = 1.998
sentiment_attention_tokens = ['That', "'", 's', 'awesome', '!']
sentiment_attention_weights = [0.154, 0.078, 0.069, 0.444, 0.255]