Home Artificial Intelligence Unlocking ML Potential: Deploying Flask Rest API for Machine Learning Models on Microsoft Azure Create a machine Learning Model Create a Flask Rest API Service Deploying Machine Learning Model using Flask API to Microsoft Azure

Unlocking ML Potential: Deploying Flask Rest API for Machine Learning Models on Microsoft Azure Create a machine Learning Model Create a Flask Rest API Service Deploying Machine Learning Model using Flask API to Microsoft Azure

4
Unlocking ML Potential: Deploying Flask Rest API for Machine Learning Models on Microsoft Azure
Create a machine Learning Model
Create a Flask Rest API Service
Deploying Machine Learning Model using Flask API to Microsoft Azure

When you are a knowledge scientist or machine learning engineer, chances are high that you’ve got built plenty of machine learning models in your profession. These models can provide beneficial insights and predictions, but what good are they if they continue to be confined to your local environment?

Deploying your machine learning model to an internet service can allow you to to share your work with the world, and make it accessible to a wider audience. One popular option to do that is by making a Flask REST API service, which may be used to reveal your machine learning model to the general public.

Flask is a well-liked Python web framework that lets you easily create web applications and APIs.

Deploying your machine learning model to an internet service using Flask generally is a powerful option to share your work with the world. So once you’ve got created a flask application, what next?

You’ll want to deploy your flask app to some cloud service and the most effective place could be Microsoft Azure.

Microsoft Azure provides a variety of services that may allow you to to deploy, manage and construct scalable web apps.

Read on to find out about deploying your machine learning model using Flask to Microsoft Azure.

Train your machine learning model of your selection and save the weights of your model in .h5 format. You should use the jupyter notebook in my git repository in your reference (Link here)

Save Your ML Model

Let’s begin through the use of Flask to create a Rest API service, which can function our place to begin.

By leveraging Flask’s simplicity and adaptability, we will rapidly develop Rest APIs that cater to varied development needs

Getting began with flask app
Below is a basic snippet the way to start with a flask app

From flask importFlask
app=Flask(__name__)
@app.route("/")
def index():
return"

Hello Azure!

"

You’ll be able to clone the repository (discuss with to github repo) to access a sample template for a Flask app that features a machine learning model prediction script. The implementation mentioned here relies on that repository.
You’ll be able to customize the script in keeping with your specific requirements and incorporate your individual machine learning model implementation.

After completing the creation of a basic Flask app, it’s needed to define a route that serves as an API endpoint to invoke our service. Below is a snippet of the API endpoint which can serve our purpose (this snippet is same from the git repository).

@app.route('/predict', methods=['GET', 'POST'])
@cross_origin(allow_headers=['Content-Type'])
def upload():
if request.method == 'POST':

"""testing purpose"""
filestr = request.files['file'].read()
file_bytes = np.frombuffer(filestr, np.uint8)
img = cv2.imdecode(file_bytes, cv2.IMREAD_UNCHANGED)
img = cv2.resize(img,(224,224))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
x = img_to_array(img)
x = preprocess_input(x)

prediction = model.predict(x.reshape(1,224,224,3))
output = np.argmax(prediction)
predicted_class = CLASS_NAMES[output]
max_prob = np.max(prediction)
if max_prob >0.75:
return {
'class' : predicted_class,
'confidence' : float(max_prob)
}
else:
return NONE
return None

After loading our saved model, we process incoming user requests to generate predictions, that are then sent back as responses. This permits us to offer real-time predictions to users based on their input data.

Let’s Run the Flask App and check it out by making a POST call using an image through postman.

Flask App Run

Our Flask App is up and running.
Lets test our API endpoint in Postman.

Input Image

Send a post request and see whether it’s returning the json response or not

API Response

The Flask app is functioning easily and is providing a satisfactory response.

That is where the fun begins 🙂

We will probably be using Microsoft Azure’s web app service to deploy our Flask REST API, benefiting from its capabilities for seamless hosting and scalability.

Let me walk you thru the steps

1. Log in to the https://portal.azure.com/

2. Create a resource

3. Create a recent web app

4. Fill in the next details as per your requirement Create a recent resource group if you happen to don’t have a Resource Group. (You’ll be able to look more into what’s a Resource Group. Link)

5. Click on Review + Create

6. Wait while your deployment gets ready

7. Navigate to ‘Go to Resource’

8. Within the left pane of your Resource, you’d see a piece for Deployment. Click on Deployment Center

9. You’ll be able to now link your deployment to your github account or another cloud based service shown within the drop down

10. Replenish your account details and hit save

11. Within the overview you’ll have the ability to see your default Domain

12. You may as well see your workflow process within the actions of your Github repository which you linked to your deployment

13. Remember what route we set for our prediction. It was /predict. So go over to your postman and use this default domain to check out our Rest API endpoint.

Voila. Our API endpoint is up and running.

14. Utilize this API endpoint in your web app or another suitable context to showcase its capabilities to the world and make the predictions accessible to a broader audience.

Prediction response in webapp

Thanks for exploring my article documenting the deployment of machine learning models using Flask API on Microsoft Azure. I hope it equips you to showcase your models to the world and leverage the ability of Azure’s infrastructure. Joyful Learning!!!

4 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here