In this tutorial article, we will learn step by step to generate a custom Machine Learning model from ML.NET for sentiment analysis in text, and then learn to consume that model from a web application on ASP.NET with the MVVM pattern (Model, View, ViewModel).
What is ML.NET and how does it work?
ML.NET is an open source Machine Learning platform that allows us to transform input data into an ML model, and then be able to integrate it into any type of application on the .NET ecosystem, whether web, mobile, desktop, or any other type of application.
These are the types of models that we can generate in ML.NET:
In our case we will focus on the classification/categorization of text comments.
Text sentiment analysis
For sentiment analysis, the intention is to be able to classify text entries into two groups: positive comments (1) and negative comments (0).
To achieve this goal, we will perform three activities in this article:
Meet the necessary requirements and prepare our solution in Visual Studio.
Create our Machine Learning model with ML.NET.
Consume the model generated from ASP.NET.
With this mentioned, let’s get started!
Part 1: Prerequisites and Solution Preparation in Visual Studio
In order to generate our Machine Learning model, we will use the Model Builder interpreter of ML.NET. For this, we need to have the following workload in Visual Studio (.NET Cross-Platform Development):
With this workload, we can now create our solution. In this case, the intention is to consume the ML model that we are going to generate from a web application, for this purpose we will use the DotVVM framework that allows us to work with HTML pages and C# classes on ASP.NET with the MVVM pattern (Model, View, ViewModel).
Part 2: Building the Machine Learning Model
It’s time to see ML.NET in action! As in any other process in the world of Machine Learning to generate a model, the steps to build the sentiment analysis model are as follows:
Set the ML scenario.
Prepare the environment for data training.
Load the dataset.
Train the model.
Evaluate the model.
Consume the model.
To begin with, in the main project of our solution we will add a component of type Machine Learning:
With this option, the ML.NET Model Builder interpreter will be initialized and we can start customizing the model.
Scenario: The first step is to select the scenario with which we want to work, in this case it will be Text classification:
Environment: Now, we must select the computing resources for the training of our model. For text classification in particular we can only use the CPU of our computer.
Data source: This is one of the most important parts, since with the data we can customize our model. To do this we can select two types of sources, either by making a connection and at the same time a query to a SQL Server database, or by means of a CSV file.
In this case we will use a CSV file with two columns, the first with the text in question, and the second column with the type of comment (0 negative — 1 positive).
Here’s an example of a dataset: wikipedia-detox-250-line-data.tsv. By loading it we can have a preview of the data, and select the column that we want to predict later.
Training: With everything we have already established so far, we can already perform the training process, and finally generate the desired model. In this section the only thing we can customize is the maximum time in which ML.NET can search for the ideal model for text classification.
At the end of the training process we can visualize how many models ML.NET evaluated, and the accuracy of the best model found.
Evaluation: Up to this point we already have the model established, anyway, from the Model Builder of ML.NET we can evaluate the model with a particular example.
Consume: Finally, the interpreter will give us options to consume the created model, either by creating other projects (by console or with a web api), or indicating the calls we must make from our own application.
In this case, we can already consume the model from our web application with DotVVM on ASP.NET with the new generated package: MLModel.mbconfig.
Part 3: Consume the generated model from ASP.NET
To consume the model we must refer to the MLModel class, which was generated by ML.NET in the group MLModel.mbconfig:
In this sense, it is necessary to establish an instance of ModelInput with the comment we want to predict, and then call the Predict method, and obtain an object of type ModelOutput with the corresponding prediction (1 positive, 0 negative), and its probability.
Viewmodel: For this purpose, and to be able to establish a web page with DotVVM, in a ViewModel, in this case the DefaultViewModel.cs, we will define three attributes or global variables:
public string InputText { get; set; }
public string Result { get; set; } = null;
public decimal? Score { get; set; } = null;
These variables will allow us to store the text entered by the user in InputText, and represent the results of the prediction in the attributes Result and Score.
Then we can set a method in this class, in such a way that it represents the action of a button to make the prediction:
public void Predict()
{
var sampleData = new MLModel.ModelInput()
{
Col0 = InputText,
};
//Load model and predict output
var output = MLModel.Predict(sampleData);
if ((int)output.Prediction == 0)
{
Result = "Negative.";
}
else
{
Result = "Positive.";
}
Score = decimal.Round((decimal)(output.Score[(int)output.Prediction] * 100), 2);
}
In this code we have three important parts:
Initialize an MLModel.ModelInput with the comment entered by the user.
Call the MLModel.Predict with the ModelInput as a parameter, thus obtaining the ModelOutput with the results.
Prepare the Result to visualize the result later (positive or negative), and its accuracy in the prediction.
View: Very well, we can now construct the view by referencing the attributes and method created in the ViewModel. This view will be encoded in the file Default.dothtml as follows:
<table style="border: hidden">
<tbody style="border: hidden">
<tr style="border: hidden">
<td style="border: hidden" align="center">
<dot:TextBox Text="{value: InputText}" placeholder="Write your text..." />
</td>
<td style="border: hidden" align="center">
<dot:Button Click="{command: Predict()}" Text="Predict" />
</td>
<td style="border: hidden" align="center">
<b>{{value: Result}}</b> {{value: Score}}
</td>
</tr>
</tbody>
</table>
Here the most important thing is the TextBox control for the user to enter their comment and store it in the InputText attribute, the call to the Predict() method with a Button, and the display of the results by displaying the values of the variables Result, and Score.
Examples running: With this View, and its ViewModel, our website is ready. Here we can see two examples at runtime:
The source code for this example is available in this GitHub repository: DotvvmValidationSample.
Source: Medium - Daniel Gomez Jaramillo
The Tech Platform
Comments