0
0
Ml-pythonHow-ToBeginner ยท 4 min read

How to Deploy ML Model as API: Simple Steps and Example

To deploy an ML model as an API, you can use a web framework like Flask to create endpoints that accept input data and return predictions. First, load your trained model, then define API routes to handle requests and respond with model outputs in JSON format.
๐Ÿ“

Syntax

Here is the basic syntax to deploy an ML model as an API using Flask:

  • import necessary libraries
  • Load your trained ML model
  • Create a Flask app instance
  • Define API routes with @app.route decorators
  • Parse input data from requests
  • Use the model to predict based on input
  • Return predictions as JSON responses
  • Run the Flask app
python
from flask import Flask, request, jsonify
import pickle

# Load trained model
model = pickle.load(open('model.pkl', 'rb'))

app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json(force=True)
    features = data['features']
    prediction = model.predict([features])
    return jsonify({'prediction': int(prediction[0])})

if __name__ == '__main__':
    app.run(debug=True)
๐Ÿ’ป

Example

This example shows how to deploy a simple scikit-learn model as an API using Flask. It loads a trained model, accepts JSON input with features, and returns the prediction.

python
from flask import Flask, request, jsonify
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import pickle

# Train and save a simple model (run once)
iris = load_iris()
X, y = iris.data, iris.target
model = RandomForestClassifier()
model.fit(X, y)
with open('model.pkl', 'wb') as f:
    pickle.dump(model, f)

# Load the model
model = pickle.load(open('model.pkl', 'rb'))

app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json(force=True)
    features = data['features']  # expects list of 4 floats
    prediction = model.predict([features])
    return jsonify({'prediction': int(prediction[0])})

if __name__ == '__main__':
    app.run(debug=True)
Output
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
โš ๏ธ

Common Pitfalls

  • Not handling input format: The API must expect and parse input data correctly, usually JSON with the right keys.
  • Model file missing or incompatible: Ensure the model file is saved and loaded properly with matching library versions.
  • Not running Flask app properly: Use if __name__ == '__main__' guard to run the app.
  • Not returning JSON response: Always return predictions wrapped in jsonify for proper API response.
python
## Wrong way: returning raw prediction without JSON
@app.route('/predict', methods=['POST'])
def predict_wrong():
    data = request.get_json(force=True)
    features = data['features']
    prediction = model.predict([features])
    return str(prediction[0])  # returns plain string, not JSON

## Right way:
@app.route('/predict', methods=['POST'])
def predict_right():
    data = request.get_json(force=True)
    features = data['features']
    prediction = model.predict([features])
    return jsonify({'prediction': int(prediction[0])})
๐Ÿ“Š

Quick Reference

Steps to deploy ML model as API:

  • Train and save your ML model (e.g., with pickle)
  • Create a Flask app and load the model
  • Define API endpoint(s) to accept input and return predictions
  • Parse input JSON and preprocess if needed
  • Use model to predict and return JSON response
  • Run the Flask app to serve the API
โœ…

Key Takeaways

Use a web framework like Flask to create API endpoints for your ML model.
Always load your trained model before handling prediction requests.
Parse input data carefully and return predictions as JSON responses.
Test your API locally before deploying to a cloud or server.
Handle errors and input validation to make your API robust.