Home → Uncategorized → Diamond Price Predictor

Uncategorized

Building a Machine Learning-Based Diamond Price Predictor: How I Created a Flask Web App Hosted on Azure

July 19, 2025 · Rajat

PS – These projects are from year 2022 – year 2023. Restored these post from old hosting service.

Introduction

Diamonds are one of the most coveted and valuable gemstones in the world, with their worth determined by various factors such as carat, diamond size, color, and clarity. Determining the value of a diamond can be a complex process. In recent years, machine learning has become increasingly popular for predicting diamond prices.

In this blog post, I will share my experience of building a diamond price prediction model using machine learning: the data cleaning process, training the model on inputs such as carat, size, colour and clarity, creating a Flask web app, and hosting it on Azure App Service — plus a webpage for interacting with the API using HTML, Bootstrap 5, CSS, and JavaScript.

Steps to achieve the creation of this project

  1. Collect Data: The data used for this project was taken from Kaggle: https://www.kaggle.com/datasets/shivam2503/diamonds
  2. Clean the Data: Remove irrelevant or missing data, normalize, and convert categorical variables into numerical values.
  3. Train a Machine Learning Model: A Jupyter notebook covers data cleaning and training. (PS – the notebook was created ages ago by me.)
  4. Build a Flask Web App: A Flask app serves as an API for the trained model. The code below was tested locally before deployment on Azure — install all dependencies before running it.
from flask import Flask,url_for, jsonify, request
from flask.wrappers import Response
from joblib import dump, load
from flask_cors import CORS, cross_origin

app = Flask(__name__)
app.ml_model = load('diamond_predictor.joblib')

@app.route('/diamonds_api/', methods=['GET', 'POST'])
@cross_origin()
def hello():
    query_params = request.args
    color = int(query_params.get('color'))
    clarity = int(query_params.get('clarity'))
    cut = int(query_params.get('cut'))
    carat = float(query_params.get('carat'))
    x = float(query_params.get('x'))
    y = float(query_params.get('y'))
    z = float(query_params.get('z'))
    table = float(query_params.get('table'))
    depth_percent = 2 * z/(x+y)
    input_params = [[carat,cut,color, clarity, depth_percent, table, x, y, z]]
    output = app.ml_model.predict(input_params)
    return jsonify({'price':str(output[0]),
                    **query_params.to_dict()})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=105)

5. Host the Flask App on Azure App Service: Azure App Service is a cloud-based platform for deploying and managing web apps. The Azure tutorial to deploy a Flask app was really helpful.

6. Build a Webpage: A webpage lets users interact with the API using HTML, Bootstrap 5, CSS, and JavaScript.

Diamond price predictor web app

Summary

After completing all the steps, I successfully created a diamond price prediction web app: a machine learning model trained on a clean dataset of diamond prices and characteristics, hosted on a Flask web app deployed on Azure App Service, with a webpage for interaction. The project demonstrates the power of machine learning for price prediction and the benefits of cloud platforms like Azure — with valuable experience in data cleaning, machine learning, web development, and cloud computing.

← Previous post Next post →