🚀
Model Deployment
01 Serving Model as API
++++
Data Science
May 2026×Notebook lesson

Notebook converted from Jupyter for blog publishing.

01-Serving-Model-as-API

Driptanil Datta
Driptanil DattaSoftware Developer

Serving a Model as an API

NOTE: While we show this inside a Jupyter Notebook, you would probably never deploy something as a notebook in a real-world setting. Everything here is in one cell to reflect that this should be a .py file. We also included a duplicate .py file in this folder.


NOTE: You will need to install Flask to serve the API: https://flask.palletsprojects.com/en/2.0.x/installation/ (opens in a new tab)

pip install flask

or

conda install flask


api.py (Run this as a script as shown in the video, NOT from within a Jupyter Cell)

############################
######## IMPORTS ##########
##########################
from flask import Flask, request, jsonify
import joblib
import pandas as pd
 
# Create Flask App
app = Flask(__name__)
 
 
# Create API routing call
@app.route('/predict', methods=['POST'])
def predict():
    
    # Get JSON Request
    feat_data = request.json
    # Convert JSON request to Pandas DataFrame
    df = pd.DataFrame(feat_data)
    # Match Column Na,es
    df = df.reindex(columns=col_names)
    # Get prediction
    prediction = list(model.predict(df))
    # Return JSON version of Prediction
    return jsonify({'prediction': str(prediction)})
 
        
 
if __name__ == '__main__':
 
    # LOADS MODEL AND FEATURE COLUMNS
    model = joblib.load("final_model.pkl") 
    col_names = joblib.load("column_names.pkl") 
 
    app.run(debug=True)

JSON Post Request

  1. POST to: http://127.0.0.1:5000/predict (opens in a new tab)
  2. Select Body
  3. Select Raw
  4. Select JSON(application/json)
  5. Supply JSON for Features: [{"TV":230.1,"radio":37.8,"newspaper":69.2}]
Drip

Driptanil Datta

Software Developer

Building full-stack systems, one commit at a time. This blog is a centralized learning archive for developers.

Legal Notes
Disclaimer

The content provided on this blog is for educational and informational purposes only. While I strive for accuracy, all information is provided "as is" without any warranties of completeness, reliability, or accuracy. Any action you take upon the information found on this website is strictly at your own risk.

Copyright & IP

Certain technical content, interview questions, and datasets are curated from external educational sources to provide a centralized learning resource. Respect for original authorship is maintained; no copyright infringement is intended. All trademarks, logos, and brand names are the property of their respective owners.

System Operational

© 2026 Driptanil Datta. All rights reserved.