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.
See also the sentiment-reviews 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-general
. -
Model Name. The model name of the Seldon Core deployment that will be referenced in the Machine Learning pipeline stage configurations, such as
sentiment-general
. -
Docker Repository. The value is
lucidworks
. -
Image Name. The value is
sentiment-general:v1.0
. -
Kubernetes Secret Name for Model Repo. This value is left empty.
-
Output Column Names for Model. The value is
[label, score]
.
-
-
Click Save.
-
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
-
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-general
. -
In the Model input transformation script field, enter the following:
var modelInput = new java.util.HashMap() modelInput.put("text", doc.getFirstFieldValue("text")) modelInput
-
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])
-
Save the pipeline.
2.2. 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-general.
-
In the Model input transformation script field, enter the following:
var modelInput = new java.util.HashMap() modelInput.put("text", request.getFirstParam("q")) modelInput
-
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);
-
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]