End to End Machine Learning Project for Spam Detection Use Case

Simran AnandSimran Anand
8 min read

Introduction

In the digital age, spam messages have become a persistent annoyance, infiltrating our mobile phones with unwanted and often fraudulent content. Machine learning (ML) offers a powerful solution to this problem, enabling us to automatically detect and filter out spam messages. In one of my YouTube videos, a comprehensive tutorial was presented, demonstrating how to build an SMS spam classification web application using the naive Bayes ML algorithm.

For video explanation of the topic in depth with demonstrations, check out my End To End Data Science playlist on my YouTube channel.

The tutorial covers the entire development process, starting with the ML model training using naive Bayes to classify spam and non-spam messages. The video then transitions to frontend development, where Flask, a lightweight web application framework, is used to create the user interface. Additionally, the tutorial demonstrates how to Dockerize the Flask application, ensuring easy deployment and scalability. Finally, the video concludes with deploying the web app on Microsoft Azure, making it accessible to users worldwide.

This blog article aims to summarize the key steps and concepts covered in the YouTube video, providing readers with a high-level overview of how to build and deploy an SMS spam classification web app using ML and web development technologies. Whether you're a seasoned developer or new to ML and web development, this tutorial offers valuable insights into the process of creating a real-world ML application.

Naive Bayes for Classification

Naive Bayes is a popular algorithm in machine learning, particularly for classification tasks like spam detection. It is based on Bayes' theorem, which describes the probability of an event, based on prior knowledge of conditions that might be related to the event.

In the context of SMS spam classification, Naive Bayes works by calculating the probability that a message is spam or not spam (ham), given the words in the message. It assumes that the presence of each word in the message is independent of the presence of other words, which is why it's called "naive." Despite this simplifying assumption, Naive Bayes often performs well in practice, especially for text classification tasks.

To classify a new message, the algorithm calculates the probability that the message is spam and the probability that it is ham, based on the words in the message. It then compares these probabilities and classifies the message as spam if the probability of spam is higher, or as ham otherwise. Naive Bayes is efficient, easy to implement, and works well with large datasets, making it a popular choice for text classification tasks like spam detection.

Flask for Developing Front End

Flask, a lightweight web application framework for Python, is commonly used to create endpoints and frontends for machine learning (ML) projects, including classification tasks. Here's how Flask can be used for this purpose:

1. Creating Endpoints: Flask allows you to create endpoints that can receive incoming data, process it, and return the results. For an ML classification project, you can create an endpoint that accepts text input (e.g., an SMS message) and returns the classification result (e.g., spam or ham). You would typically use Flask's `route` decorator to define the endpoint and its associated function.

2. Frontend Development: Flask can also be used to serve HTML templates and create the frontend of your application. You can use HTML, CSS, and JavaScript to create a user interface where users can input text and see the classification result. Flask's 'render_template' function allows you to render HTML templates and pass data to them for dynamic content generation.

Example code for a simple Flask UI for an ML classification project:

from flask import Flask, render_template, request import your_ml_model # Import your trained ML model for classification

app = Flask(name)

@app.route('/') def home(): return render_template('index.html')

@app.route('/classify', methods=['POST']) def classify(): text = request.form['text'] result = your_ml_model.predict([text])[0] # Assuming your model has a predict method return render_template('result.html', result=result)

if name == 'main': app.run(debug=True)

In this example, `index.html` would be a simple HTML form where users can input text, and 'result.html' would display the classification result. When the form is submitted, the 'classify' endpoint is called, which uses your trained ML model to classify the input text and returns the result to the user.

Docker for Containerization of your Flask Web App

Docker is a platform that allows you to package, distribute, and run applications in containers. Containers are lightweight, standalone, and executable packages that contain everything needed to run a piece of software, including the code, runtime, libraries, and dependencies. Docker provides a way to build, ship, and run applications reliably across different environments.

To dockerize a Flask app, you would typically create a Dockerfile in your project directory. The Dockerfile contains instructions for building a Docker image, which is a lightweight, standalone, executable package that contains your application and its dependencies. Here's an example Dockerfile for a Flask app:

# Use an official Python runtime as a base image

FROM python:3.9-slim

# Set the working directory in the container

WORKDIR /app

# Copy the Flask app code into the container

COPY . /app

# Install any dependencies

RUN pip install --no-cache-dir -r requirements.txt

# Expose the port the app runs on

EXPOSE 5000

# Define the command to run the Flask app

CMD ["python", "app.py"]

In this Dockerfile:

- `FROM python:3.9-slim`: Uses the official Python Docker image as the base image.

- `WORKDIR /app`: Sets the working directory inside the container to `/app`.

- `COPY . /app`: Copies the current directory (which contains your Flask app code) into the container.

- `RUN pip install --no-cache-dir -r requirements.txt`: Installs any dependencies required by your Flask app.

- `EXPOSE 5000`: Exposes port 5000, which is the default port for Flask apps.

- `CMD ["python", "app.py"]`: Specifies the command to run your Flask app when the container starts.

To build the Docker image, you would run the following command in your project directory (assuming your Dockerfile is named `Dockerfile`):

docker build -t my-flask-app .

This command builds a Docker image named `my-flask-app` from the current directory (`.`), which contains your Flask app code and the Dockerfile. Once the image is built, you can run it as a container using the following command:

docker run -p 5000:5000 my-flask-app

This command runs the `my-flask-app` container and forwards port 5000 from the container to port 5000 on your host machine, allowing you to access your Flask app from a web browser.

For more details on how Docker helps Data Scientists, check out this video.

Deployment of Dockerized Container on Microsoft Azure's App Engine

To deploy a Dockerized web app on Microsoft Azure's App Service, you can follow these steps:

1. Prepare Your Dockerized App: Ensure that your web app is Dockerized and runs locally. You should have a Dockerfile in the root directory of your project.

2. Create an Azure Container Registry (ACR): ACR is used to store your Docker images. You can create an ACR in the Azure portal.

3. Push Docker Image to ACR: Use the Docker CLI to push your Docker image to the ACR you created. Replace `myacr.azurecr.io/myapp:v1` with your ACR address and image name.

docker tag myapp:v1 myacr.azurecr.io/myapp:v1

docker push myacr.azurecr.io/myapp:v1

4. Create an Azure Web App: In the Azure portal, create a new Web App. Choose Docker Container as the publish method and select the image from your ACR.

5. Configure App Settings: In the Azure portal, go to your Web App's settings and configure any environment variables or settings needed for your app.

6. Deploy the App: Once the Web App is created and configured, you can deploy your Dockerized app by clicking the "Deploy" button in the Azure portal or using the Azure CLI.

7. Monitor and Manage: Azure provides monitoring and management tools for your Web App, including logs, metrics, and scaling options.

8. Access Your Web App: Once deployed, your Dockerized web app will be accessible via the URL of your Azure Web App.

By following these steps, you can deploy and host your Dockerized web app on Microsoft Azure's App Service, allowing you to take advantage of Azure's scalability, reliability, and management features.

You can deploy a Dockerized web app on Azure without using the Azure Container Registry (ACR) CLI. Here's how you can do it using the Azure portal GUI:

1. Prepare Your Dockerized App: Ensure your web app is Dockerized and runs locally. You should have a Dockerfile in the root directory of your project.

2. Push Docker Image to Docker Hub: If you don't want to use ACR, you can push your Docker image to Docker Hub or any other Docker image registry that Azure supports.

3. Create an Azure Web App:

- Go to the Azure portal (https://portal.azure.com).

- Click on "Create a resource" and search for "Web App".

- Click on "Create" and fill in the required details like subscription, resource group, app name, etc.

- Under "Configure container", select "Docker Container" as the Publish option.

- Choose "Docker Hub" as the registry and enter the image name and tag (e.g., `yourusername/yourapp:latest`).

4. Configure App Settings: In the Azure portal, go to your Web App's settings and configure any environment variables or settings needed for your app.

5. Deploy the App:

- Once the Web App is created and configured, click on "Review + create".

- Review your settings and click on "Create" to deploy your Dockerized web app.

6. Monitor and Manage: Azure provides monitoring and management tools for your Web App, including logs, metrics, and scaling options.

7. Access Your Web App: Once deployed, your Dockerized web app will be accessible via the URL of your Azure Web App.

This approach allows you to deploy your Dockerized web app to Azure using Docker Hub or any other Docker image registry, without the need for the ACR CLI.

Conclusion

In conclusion, we have successfully demonstrated how to build a SMS spam classification web app using the Naive Bayes ML algorithm, Flask for frontend development, Docker for containerization, and Microsoft Azure for deployment. This project showcases the seamless integration of machine learning, web development, and cloud computing technologies to create a practical and scalable solution. By following this example, developers can gain valuable insights into the end-to-end process of developing and deploying machine learning-based web applications, empowering them to create innovative solutions in their own projects.

Subscribe to CSE Insights by Simran Anand on YouTube for detailed and interesting technical topics. Follow Simran Anand on LinkedIn for tech content.

To avail personalized 1:1 mentorship and career guidance, book a call with me on Topmate.

For long term mentorship sessions on Data Science, Software Development, Artificial Intelligence, Machine Learning and Data Analytics, book a session with me here.

Hope you liked the article! Please do like, and share with your friends to spread the knowledge ๐Ÿ’ซโœจ๏ธ

Thank you ๐Ÿ€

24
Subscribe to my newsletter

Read articles from Simran Anand directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Simran Anand
Simran Anand

I am a Software Engineer, Educator and a Content Creator graduated in Computer Science Engineering from Vellore Institute of Technology. I am currently working as a Senior Software Engineer in Machine Learning at Bosch Global Software Technologies. My specializations include Data Science, Artificial Intelligence and Data Analytics. I have a flair for educating people, writing articles, working on projects and delivering Public Speeches.