22
loading...
This website collects cookies to deliver better user experience
In the previous article, we learned about Machine Learning, the importance of Machine Learning Operations (MLOps) in the Machine Learning Lifecycle. We also learned how to frame a problem statement, gather required data, build a Machine Learning model, evaluate the model and save the model for future use.
Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries.
pip install flask
__name__
, __name__
will help python to locate where to load other dependencies into (in this case into our script). You can read this article from StackOverflow to understand the purpose of __name__
if __name__== '__main__'
# Importing the Flask module from flask library
from flask import Flask
# initializing our application with the name of our module
app = Flask(__name__)
# Variable to store my name( change to your name)
my_name = 'idowu'
# Initializing the homepage route
@app.route('/')
def greet(): # function that will be returned
return 'Hello ' + my_name
if __name__=='__main__':
app.run(debug=True)
python my_flask.py
@app.route('/')
decorator, this is because you are trying to access the homepage (which is /
). The @app.route decorator will automatically call the greet
function and the output of the function will be returned. You will notice that, we hardcoded the variable name into our script, which is not intuitive. What if we want to accept the name from users and return Hello + user_name
, /
) /greet/name
decorator. This decorator will allow users to insert their names ( this is similar to www.google.com/search/query). www.google.com is the home route url while /search
is for the search route.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def welcome():
return 'Hello welcome to my first flask website'
@app.route("/greet/<name>")
def greet_name(name):
return 'Hello ' + name
if __name__=='__main__':
app.run(debug=True)
my_flask.py
and click on the homepage url.python my_flask.py
/greet/their_name
after the homepage url i.e (http://127.0.0.1:5000/greet/their_username).Swagger allows you to describe the structure of your APIs so that machines can read them. The ability of APIs to describe their own structure is the root of all awesomeness in Swagger. Why is it so great? Well, by reading your API’s structure, we can automatically build beautiful and interactive API documentation.
Source
pip install flasgger
@app.route()
decorator for the greet url takes in a methods parameter with the value Get. There are two major methods that @app.route
receives
Inside the greet_name()
function, you will have to use docstring to define the Swagger interface.
N.B Make sure you indent the docstring with four spaces, also you will need to indent the details under the parameters and responses else swagger won't render the display.
To access the user_name under the parameters, you will need to use the request module to access the parameter
Lastly, you can return the greeting and the user_name.
from flask import Flask, request
from flasgger import Swagger
app = Flask(__name__)
Swagger(app)
@app.route("/")
def welcome():
return 'Hello, welcome to my first flask website'
@app.route('/greet', methods=["Get"])
def greet_name():
"""Greetings
This is using docstrings for specifications.
---
parameters:
- name: user_name
in: query
type: string
required: true
responses:
200:
description: "greetings with user's name"
"""
user_name = request.args.get("user_name")
print(user_name)
return "Hello " + user_name
if __name__=='__main__':
app.run(debug=True)
python my_flask.py
/apidocs
after the homepage url i.e http://127.0.0.1:5000/apidocs
Execute
button.from flask import Flask, request
from flasgger import Swagger
import joblib
lb_salecond = joblib.load('lb_sc')
lb_saletype = joblib.load('lb_st')
model = joblib.load('lr_model')
app = Flask(__name__)
Swagger(app)
@app.route("/")
def welcome():
return 'Hello welcome to my first flask website'
['Normal', 'Partial', 'Abnorml', 'Family', 'Alloca', 'AdjLand']
@app.route('/predict_price', methods=["Get"])
def predict_prices():
"""Welcome to Moowale House Prediction Website
This is using docstrings for specifications.
---
parameters:
- name: MSSubClass
in: query
type: integer
required: true
- name: LotFrontage
in: query
type: integer
required: true
- name: Year_sold
in: query
type: integer
required: true
- name: Sale_type
in: query
type: string
required: true
default: New
enum: ['WD', 'New', 'COD', 'ConLD', 'ConLI', 'ConLw', 'CWD', 'Oth', 'Con']
- name: Sale_condition
in: query
type: string
default: Normal
enum: ['Normal', 'Partial', 'Abnorml', 'Family', 'Alloca', 'AdjLand']
responses:
200:
description: House Price Prediction
"""
mssubclass = request.args.get("MSSubClass")
lotfrontage = request.args.get("LotFrontage")
year_sold = request.args.get("Year_sold")
saletype = request.args.get("Sale_type")
salecondition = request.args.get("Sale_condition")
label_encode_sc = lb_salecond.transform([salecondition])
label_encode_st = lb_saletype.transform([saletype])
columns_list = [mssubclass,lotfrontage,year_sold,label_encode_sc,label_encode_st]
price = round(model.predict([columns_list])[0])
return f'The predicted price is ${price:.0f}'
if __name__=='__main__':
app.run(debug=True)
/apidocs
after the homepage url. You will see the screen below,