Skip to content

This repository provides a complete, practical example demonstrating how to build, train, and deploy a Sentiment Analysis machine learning model using ML.NET. The solution is divided into two parts: Console Application: Trains a multi-class sentiment classification model on sample text data (e.g., product reviews, tweets), evaluates its accuracy,

Notifications You must be signed in to change notification settings

sandeeppaldotnet/Sentiment-Analysis-with-Machine-Learning-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Sentiment-Analysis-with-Machine-Learning-AI

Sentiment Analysis with ML.NET - Console Trainer + ASP.NET Core Web API

This repository demonstrates how to:

  • Train a Sentiment Analysis model using ML.NET in a Console Application
  • Save the trained model as a .zip file
  • Use the saved model to predict sentiment in an ASP.NET Core Web API

Table of Contents


Prerequisites

  • .NET 6 SDK or later
  • Visual Studio 2022 or VS Code (with C# extension)
  • Basic knowledge of C# and ASP.NET Core

Step 1: Train the Model (Console App)

  1. Open the SentimentModelTrainer console app project.

  2. Add a CSV file sentiment-data.csv in the root folder with the following format:

    Text,Label
    "I love this product",Positive
    "This is terrible",Negative
    "It was okay",Neutral
    
  3. Review Program.cs which:

    • Loads the data from CSV
    • Builds and trains a multi-class classification pipeline
    • Evaluates the model accuracy
    • Saves the trained model as SentimentModel.zip
  4. Run the Console App:

    dotnet run
  5. After running, the SentimentModel.zip file will be created automatically in the output folder, depending on your build configuration:

    • For Debug builds:
      bin/Debug/net6.0/SentimentModel.zip
    • For Release builds:
      bin/Release/net6.0/SentimentModel.zip

Step 2: Use the Model in Web API

  1. Open the SentimentWebAPI project.

  2. Copy the SentimentModel.zip file from the console app output folder to the Web API project folder (e.g., root or /MLModels).

  3. In the Web API, the model is loaded once and used to predict sentiment for incoming requests.

  4. The API controller expects a JSON POST body like:

    {
      "Text": "This product is amazing!",
      "Label": ""  // Label must be present but can be empty to satisfy ML.NET schema
    }
  5. The controller responds with the predicted sentiment label.


Step 3: Test the API

  • Run the Web API project.

  • Use Postman, curl, or any HTTP client to POST to:

    POST https://localhost:<port>/api/sentiment
    Content-Type: application/json
    
  • Example JSON body:

    {
      "Text": "I really enjoyed this movie!",
      "Label": ""
    }
  • Example response:

    {
      "text": "I really enjoyed this movie!",
      "sentiment": "Positive"
    }

Where is the Model Zip File?

  • The trained model file SentimentModel.zip is generated by the console app after training.
  • Its location depends on the build configuration:
    • Debug: bin/Debug/net6.0/SentimentModel.zip
    • Release: bin/Release/net6.0/SentimentModel.zip
  • Make sure to copy this file into your Web API project folder to load and use the model.

📸 Screenshots

🧠 Model Training in Console App

Training in Console App

🌐 Prediction via Web API

Prediction using Web API

Notes

  • The Label property is required in the input class and must be present during prediction, even if its value is empty. This is because ML.NET expects the input schema to match exactly what was used during training.
  • You can customize the classes and API further to separate training input and prediction input if needed.
  • For more advanced scenarios, consider using ML.NET's Model Builder tool or training on larger datasets.

Feel free to open issues or pull requests for suggestions or improvements!


Happy coding! 🚀

About

This repository provides a complete, practical example demonstrating how to build, train, and deploy a Sentiment Analysis machine learning model using ML.NET. The solution is divided into two parts: Console Application: Trains a multi-class sentiment classification model on sample text data (e.g., product reviews, tweets), evaluates its accuracy,

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages