Deploy the sentiment-reviews Model
This topic explains how to deploy the sentiment-reviews pre-trained sentiment prediction model. This model is trained on a variety of customer reviews and optimized for longer texts. It also supports attention weights output that can be used for highlighting the tokens that provide stronger sentiment; see Model output below for an example.
See also the sentiment-general model deployment.
1. Install the model in Fusion
-
Navigate to Collections > Jobs.
-
Select New > Create Seldon Core Model Deployment.
-
Configure the job as follows:
-
Job ID. The ID for this job, such as
deploy-sentiment-reviews
-
Model Name. The model name of the Seldon Core deployment that will be referenced in the Machine Learning pipeline stage configurations, such as
sentiment-reviews
. -
Docker Repository. The value is
lucidworks
. -
Image Name. The value is
sentiment-reviews:v1.0
. -
Kubernetes Secret Name for Model Repo. The value is left empty.
-
Output Column Names for Model. The value is
[label, score, tokens, attention_weights]
.
-
-
Click Save.
-
Click Run > Start.
2. Configure the Machine Learning pipeline stages
You can put your sentiment prediction model to work using the Machine Learning index stage or Machine Learning query stage. You will specify the same Model Name that you used when you installed the model above.
Generally, you only need to apply the model in the index pipeline, in order to perform sentiment prediction on your content. Optionally, you can configure the query pipeline in a similar way, to perform sentiment prediction on incoming queries and outgoing responses and apply special treatment depending on the prediction.
2.1. Configure the Machine Learning index stage
-
In your index pipeline, click Add a Stage > Machine Learning.
-
In the Model ID field, enter the model name you configured above, such as
sentiment-reviews
. -
In the Model input transformation script field, enter one of the following, depending on whether you want to output attention weights:
Without attention weights With attention weights var modelInput = new java.util.HashMap() modelInput.put("text", doc.getFirstFieldValue("text")) modelInput
var modelInput = new java.util.HashMap() modelInput.put("text", doc.getFirstFieldValue("text")) modelInput.put("attention_output", "true") modelInput
-
In the Model output transformation script field, enter the following:
Without attention weights With attention weights doc.addField("sentiment_label_s", modelOutput.get("label")[0]) doc.addField("sentiment_score_d", modelOutput.get("score")[0])
doc.addField("sentiment_label_s", modelOutput.get("label")[0]) doc.addField("sentiment_score_d", modelOutput.get("score")[0]) doc.addField("sentiment_attention_tokens_ss", modelOutput.get("tokens")) doc.addField("sentiment_attention_weights_ds", modelOutput.get("attention_weights"))
-
Save the pipeline.
2.2. Optional: Configure the Machine Learning query stage
-
In your query pipeline, click Add a Stage > Machine Learning.
-
In the Model ID field, enter the model name you configured above, such as sentiment-reviews.
-
In the Model input transformation script field, enter the following:
Without attention weights With attention weights var modelInput = new java.util.HashMap() modelInput.put("text", request.getFirstParam("q")) modelInput
var modelInput = new java.util.HashMap() modelInput.put("text", request.getFirstParam("q")) modelInput.put("attention_output", "true") modelInput
-
In the Model output transformation script field, enter the following, noting the sections that need to be uncommented if you are using attention weights:
// To put into request request.putSingleParam("sentiment_label", modelOutput.get("label")[0]) request.putSingleParam("sentiment_score", modelOutput.get("score")[0]) // With attention output also uncomment this // request.putSingleParam("sentiment_attention_tokens", modelOutput.get("tokens")) // request.putSingleParam("sentiment_attention_weights", modelOutput.get("attention_weights")) // To put into query context context.put("sentiment_label", modelOutput.get("label")[0]) context.put("sentiment_score", modelOutput.get("score")[0]) // With attention output also uncomment this // context.put("sentiment_attention_tokens", modelOutput.get("tokens")) // context.put("sentiment_attention_weights", modelOutput.get("attention_weights")) // To put into response documents (can be done only after Solr Query stage) var docs = response.get().getInnerResponse().getDocuments(); var ndocs = new java.util.ArrayList(); var attention_tokens = modelOutput.get("tokens") var attention_weights = modelOutput.get("attention_weights") var attention_tokens_arr = new java.util.ArrayList(attention_tokens.size()); var attention_weights_arr = new java.util.ArrayList(attention_weights.size()); for ( i = 0; i < attention_tokens.size(); i++) { attention_tokens_arr.add(attention_tokens[i]) attention_weights_arr.add(attention_weights[i]) } 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]) // With attention output also uncomment this // doc.putField("query_sentiment_attention_tokens", attention_tokens_arr) // doc.putField("query_sentiment_attention_weights", attention_weights_arr) ndocs.add(doc); } response.get().getInnerResponse().updateDocuments(ndocs);
-
Save the pipeline.
Model output
Both of the pre-trained models output the following:
-
a label,
negative
orpositive
-
a score from
-2
to2
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 is 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]