This problem is centred around developing a .NET Core Winforms application that will detect whether a message is spam or not, based on message content given by the user. To solve this problem, we will build an ML model that takes an input of one column Message, the 'Label' column which is what you want to predict, and in this case, is named 'Label', which will tell us the predicted result. To train our model, we will use the
SMS Spam Collection Dataset downloaded from
UCI ML Repository.
As we are classifying the text messages into one of two categories, we will use binary classification for this problem.
Solution
To solve this problem, first, we will build an ML model that we want to use. Then we will train this model on existing data, and lastly, we'll consume the model in our .NET Core Winforms Application to predict whether a few example messages are spam or not.
Prerequisites
Let's start.
Before going to build our Machine Learning model, we need to set up our .NET Core Winforms application.
First, we will create a Winforms application and set up the basic data structures that will be used later for Machine Learning model.
- Open Visual Studio and select Windows Forms App (.NET Core).
- Please enter your project name. I'm going to enter Spam Detection Winforms and click on the "Create" button.
- Now you can see our project in solution explorer, it has been created successfully.
- We have created our .NET Winforms desktop application successfully. Now, first of all, we will download the SMS Spam Collection Data Set from UCI ML Repository and transform the data structure according to our requirement.
- After downloading the data, extract the "zip" file.
- There are two columns inside the dataset file - Label and Message. In the original dataset, the Label column is having two values - spam and ham. We will replace spam and ham with True and False respectively. Our data will look like this (Top 5 records from dataset just for view).
- false Go until jurong point, crazy.. Available only in bugis n great world la e buffet... Cine there got amore wat...
- false Ok lar... Joking wif u oni...
- true Free entry in 2 a wkly comp to win FA Cup final tkts 21st May 2005. Text FA to 87121 to receive entry question(std txt rate)T&C's apply 08452810075over18's
- false U dun say so early hor... U c already then say...
- false Nah I don't think he goes to usf, he lives around here though
- Now, we will build and train our ML.NET Model using ML.NET Model Builder, a Visual Studio Extension. I've already installed ML.NET Model builder on my machine If you don't have you can download it from here.
- Now, right-click on your WinForms project and select add button and then click on the Machine Learning option
- ML.NET Model builder wizard has been opened. Now, please select the scenario for which you want to build a machine learning model. For instance, I want to build a spam detection machine learning model which isn't here, so I'll select the custom scenario.
- Now select the dataset file, select label column and input columns for our machine learning model. After selecting all options please click on the train button.
- Select the machine learning problem for which you want to build the model - like I want to detect spam detection whether it's spam or not so I'll select a binary classification problem. Set the training time for while you want to train the machine learning model.
- After clicking on the train button, you will notice that the training has started.
- Once the model training has been completed, you can evaluate the machine learning model performance.
- Our machine learning model has been built and trained successfully, now click on add projects button to add this machine learning model into our existing project so that we can consume this model into our .NET Core Winforms application.
- Now you can see that our new machine learning model projects have been added to our existing project solution. You can explore them using the Solution Explorer window.
- Now we will consume this machine learning model into our .NET Core Winforms application. So, first of all, we will create a new Form into our application.
- To create a new form, Right-click on WinForms project and then select Add>Form (Windows Form) option, a new window will be opened.
- Please enter your form name and click on Create button.
- After the form has been created, double click on the newly created form from solution explorer and your form will be opened in Winforms designer.
- Now open ToolBox and add a RichText to get a message from the user which we want to detect whether it's spam or not.
- Add a new Button from ToolBox and set the title to predict. You can see the designed form, as it's given below.
- Now double click on Predict button, and Visual Studio will automatically generate an event handler for this button.
- Inside your button event handler, we will write the code, we will get the message from the user and then will consume our machine learning model to make a prediction and after that, we will show the prediction/result into a MessageBox.
-
-
- var msg = txtMessage.Text;
-
- var prediction=ConsumeModel.Predict(new ModelInput
- {
- Message=msg,
- });
-
- MessageBox.Show($"Prediction: {(prediction.Prediction?"Spam":"Not Spam")} Score: {prediction.Score}");
- Here we can explore what's inside our consume class. Open Spam Detection WinformsML.Model project and click on ConsumeModel class.
- Inside Predict function, we are creating an instance of MLContext and after that, we are loading our machine learning model .zip file that we have build using ML.NET Model builder.
- After loading model, we are creating a ML.NET prediction engine to make our prediction.
-
- public class ConsumeModel
- {
-
-
- public static ModelOutput Predict(ModelInput input)
- {
-
-
- MLContext mlContext = new MLContext();
-
-
- string modelPath = @"C:\Users\mhabi\source\repos\Spam Detection Winforms\Spam Detection WinformsML.Model\MLModel.zip";
- ITransformer mlModel = mlContext.Model.Load(modelPath, out var modelInputSchema);
- var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
-
-
- ModelOutput result = predEngine.Predict(input);
- return result;
- }
- }
- This is a beginner article so that's why I'm not going into deep how the machine learning model is built and designed behind ML.NET Model Builder.
- Now click on debug button to run your .NET Core Winforms application.
Conclusion
So we have built the solution to our problem.
- We have created a .NET WinFormsApplication template.
- We have downloaded and SMS Spam Collection data set files for training our model.
- We have built and trained our machine learning model using ML.NET Model Builder.
- We have Consumed our ML.NET Model for SMS Spam Detection problem.
- At last, we built a user interface that would allow the user to enter their message to predict whether the message is spam or not.
Demo
Note
In this article, we learned how to develop a .NET Core WinForms Application for Spam Detection for Text Messages and how to build, train, and consume Spam Detection
ML.NET Machine Learning model in .NET Core WinForms.