Salary Prediction ML Model Web Using Flask on Docker Container

Govind Bhardwaj
5 min readMay 26, 2021

--

Task Description -

  • Pull “Centos” Docker Image from Docker HUB and launch container with “Centos” image .
  • Install python interpreter over Docker Container .
  • Create Machine Learning model for salary prediction over Docker Container .
  • Create Website to predict Salary and use Flask

We will get also some introduction about “Docker” , “Python” , “Machine Learning” whenever respective step will come up .

  • My Docker Host IP → 192.168.13.53 , This is a RedHat 8 system and Docker is already installed and service is already in running state and enabled .
# ifconfig enp0s3
  • We are stop firewalld service in this case for simplicity
# systemctl stop firewalld

Step -1

Pull “Centos” Docker Image from Docker HUB and launch container with “Centos” image .

  • Docker is a tool which is based on containerisation technology .
  • Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.
  • Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.
  • Docker Hub is the world’s easiest way to create, manage, and deliver your teams’ container applications.
  • To pull “centos” image with “latest” tagged -
# docker pull centos:latest
  • To launch container with “centos” image ( container name is “ml1” ) -
# docker run -it -p 80:5000 --name ml1 centos:latest

Here I publish container port 5000 on Docker host 80 so that if outside Docker Host want to connect with container then they connect through Docker Host port 80 with Docker Host IP ( “192.168.13.53” )

Step -2

Install “python” interpreter over Docker Container

  • Python is an interpreted high-level general-purpose programming language.
  • Python’s design philosophy emphasizes code readability with its notable use of significant indentation.
  • To install “python” interpreter over Docker Container -
# yum install python3 -y
  • To go out from container terminal press bellow keys -
Ctrl + p + q

Step -3

Create “Machine Learning” model over Docker Container

  • Machine learning is the study of computer algorithms that improve automatically through experience and by the use of data.
  • It is seen as a part of artificial intelligence.
  • We have a dataset “SalaryData.csv” on Docker Host ( base os where docker is running )
  • Copy this dataset to Docker Container “ml1”
# docker cp SalaryData.csv ml1:/
  • To get again container “ml1” terminal ( check “SalaryData.csv” is also there “/” )
# docker exec -it ml1 bash
  • Install “vim” command for file editing in container “ml1”
# yum install vim -y
  • Install “scikit-learn” , “numpy” and “pandas” library over Container “ml1”
# pip3 install scitkit-learn  numpy  pandas
  • Python Code For Salary Prediction in “Salary_Prediction_Code.py” file
# Import "pandes" library
import pandas
# Store "SalaryData.csv" data into 'dataset' variable
dataset = pandas.read_csv("SalaryData.csv")
# "YearsExperience" is feature for this dataset
X = dataset["YearsExperience"].values.reshape(-1,1)
# "Salary" is target
Y = dataset["Salary"]
from sklearn.linear_model import LinearRegression# Create Model
model = LinearRegression()
# Train the Model
model.fit(X,Y)
# "dump" method is used to save model into file so that we can use this model further in future
from joblib import dump
# Save model into 'Salary_Predictor' file
dump(model,"Salary_Predictor.pk1")
  • Run “Salary_Prediction_Code.py” file ( Model Creation)
# python3 Salary_Prediction_Code.py

Step -4

Create Website to predict Salary and use Flask

  • Flask is a micro web framework written in Python.
  • It is classified as a microframework because it does not require particular tools or libraries.
  • It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.
  • Install python flask library on Conatiner “ml1”
# pip3 install flask
  • Flask “app.py” code ( inside same direcoty “/” where “Salary_Predictory.pk1” is ) on Container “ml1”
from flask import Flask , render_template , request# Create App
app = Flask("Salary")
# Route creation for take "Years of Experience"
@app.route("/exp")
def YearsExperience():
# "exp.html" file has html code to take "Years of Experience" data
return render_template("exp.html")
# Route for provide Salary Information
@app.route("/salary",methods=["GET"])
def Salary():
from joblib import load
# Load 'Salary_Predictory.pk1' model file
model = load("Salary_Predictor.pk1")
# 'exp' variable have data give by user
exp = request.args.get("exp")
return render_template("salary.html", exp=exp, salary=model.predict([[float(exp)]])[0])
  • Html Code “exp.html” ( inside “/templates” directory) on Container “ml1”
# ls templates 
# cat templates/exp.html
  • Html Code “salary.html” ( Inside /templates directory ) on Contaiern “ml1”
# cat templates/salary.html
  • To get container “ml1” IP run below command on Docker Host
# docker container inspect ml1 | grep IPAddress
  • Run flask on Container “ml1” with its IP ( “172.17.0.2”) and Port 5000
# flask run -h 172.17.0.2 -p 5000
  • Search On Browser with Docker Host IP on port 80
URL -    http://192.168.13.53:80/exp

Our Task is successfully done

--

--

No responses yet