USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
Br
Ethiopian Birr
¥
China Yuan Renminbi
Pakistan Rupee
£
Egypt Pound
د.إ
United Arab Emirates dirham
R
South Africa Rand
ر.س
Saudi Arabia Riyal

Deploying Machine Learning Models with Flask

Created by Vishal Verma in Articles 19 Feb 2024
Share

Today, we're going to talk about making a super cool web app. Imagine a web app that can guess things using machine learning – how awesome is that? Don't worry if it sounds a bit tricky; we'll take it step by step, and you'll have a fantastic web app by the end.


What You Need


To get started, make sure you have two things:

  1. 1. Your computer with a special program called Python (version 3.6 or newer).
  2. 2. A little bit of knowledge about machine learning (the computer guessing things) and a tiny bit about Flask (a tool for making web apps).

Step 1: Install Some Stuff


Open a special window on your computer – we call it terminal. Then, type in some magic words:


python -m venv venv

Activate this magic space:



  • If you're using Windows, type venv\Scripts\activate.

  • If you're using MacOS/Linux, type source venv/bin/activate.


Now, let's add more magic:


pip install Flask scikit-learn

Step 2: Make Your Web Home


Imagine your web app having a home. Inside this home, there's a special file called app.py. This file is like the heart of your web app:


# app.py
from flask import Flask, render_template

app = Flask(__name__)

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

if __name__ == '__main__':
app.run(debug=True)

Step 3: Organize Your Stuff


Think of your web app like a tidy room. Make a folder called templates inside your web app's home. In this folder, make an HTML drawing called index.html. This drawing will be how your web app looks:


<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Machine Learning Model Deploymenttitle>
<body>
<h1>Welcome to our Machine Learning Deployment Apph1>

Step 4: Show Your Magic Model

Make another space in your web app's home called model. In this space, put a magic drawing (let's call it model.pkl). Now, tell your web app's heart about this magic:


# app.py
from flask import Flask, render_template
import joblib

app = Flask(__name__)

# Load the magic drawing
model = joblib.load('model/model.pkl')

@app.route('/')
def home():
# Add a bit of magic to make guesses using the drawing
# For example: prediction = model.predict([[input_data]])
return render_template('index.html')

Step 5: See the Future (Prediction Place)


Tell your web app's heart to have a special place for guesses:


# app.py
from flask import Flask, render_template, request, jsonify
import joblib

app = Flask(__name__)

# Load the magic drawing
model = joblib.load('model/model.pkl')

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

@app.route('/predict', methods=['POST'])
def predict():
# Get special info from the request
data = request.get_json(force=True)

# Do magic guesses using the drawing
# For example: prediction = model.predict([[input_data]])

# Show the guess in a special way
return jsonify({'prediction': 'your_prediction'})

if __name__ == '__main__':
app.run(debug=True)

Flask Documentation

To learn more about Flask, you can explore the official documentation here.


Step 6: Try Your Web App


Start your web app:


python app.py

Go to http://127.0.0.1:5000/ on your computer to see your web app's first page.

In the next article, we will create a real-application that will predict the output based on user input. We will discuss the following in our next post:

  1. 1. Train a Machine Learning Model.
  2. 2. Create a Prediction Function.
  3. 3. Build an HTML-based form to collect user input.
  4. 4. Create functions to handle the user input, generate response, and display it in browser.

A glimpse of the application that we will be covering in our next article:

Application:

Result:



Conclusion


Congratulations! You've successfully created a simple Flask web application that can serve machine learning predictions. This tutorial provides a basic structure, and you can expand upon it by incorporating more complex machine learning models, enhancing the user interface, and deploying it to production servers. Flask's flexibility and simplicity make it an excellent choice for deploying machine learning models and building interactive applications.

Comments (0)

Share

Share this post with others

GDPR

When you visit any of our websites, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and manage your preferences. Please note, that blocking some types of cookies may impact your experience of the site and the services we are able to offer.