Machine Learning Tutorial Part 16: Model Deployment & Serving
๐ Why Deploy ML Models?
After training an ML model, you need to make it accessible to real users. Deployment allows users or systems to send input data and receive predictions via an API.
๐ Option 1: Flask API for Deployment
Flask is a lightweight Python web framework that can easily serve models as REST APIs.
from flask import Flask, request, jsonify
import pickle
model = pickle.load(open("model.pkl", "rb"))
app = Flask(__name__)
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
prediction = model.predict([data['features']])
return jsonify({'prediction': prediction.tolist()})
if __name__ == '__main__':
app.run(debug=True)
⚡ Option 2: FastAPI for Better Performance
FastAPI is a modern, high-performance framework that supports asynchronous code and automatic docs via Swagger UI.
from fastapi import FastAPI
from pydantic import BaseModel
import pickle
model = pickle.load(open("model.pkl", "rb"))
app = FastAPI()
class InputData(BaseModel):
features: list
@app.post("/predict")
def predict(data: InputData):
prediction = model.predict([data.features])
return {"prediction": prediction.tolist()}
๐งช Test Your API Locally
You can use tools like Postman or curl
to test your API:
curl -X POST http://localhost:8000/predict \
-H "Content-Type: application/json" \
-d '{"features": [5.1, 3.5, 1.4, 0.2]}'
๐ง๐ป Deployment Options
- Local Server – for development/testing
- Heroku – simple and free to start
- Render / Railway – easy modern alternatives to Heroku
- AWS/GCP/Azure – production-level hosting with scalability
๐ Quiz 1: Deployment Basics
Q: Which Python library is known for automatic API docs and speed?
๐ Quiz 2: Model Serving
Q: Which HTTP method should you use to send prediction data to your API?
๐ Summary
- Flask and FastAPI are great tools for serving ML models.
- Use
pickle
orjoblib
to save/load models. - Always test your API locally before deploying.
Machine Learning Tutorial Part 16: Model Deployment & Serving
๐ Why Deploy ML Models?
After training an ML model, you need to make it accessible to real users. Deployment allows users or systems to send input data and receive predictions via an API.
๐ Option 1: Flask API for Deployment
Flask is a lightweight Python web framework that can easily serve models as REST APIs.
from flask import Flask, request, jsonify
import pickle
model = pickle.load(open("model.pkl", "rb"))
app = Flask(__name__)
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
prediction = model.predict([data['features']])
return jsonify({'prediction': prediction.tolist()})
if __name__ == '__main__':
app.run(debug=True)
⚡ Option 2: FastAPI for Better Performance
FastAPI is a modern, high-performance framework that supports asynchronous code and automatic docs via Swagger UI.
from fastapi import FastAPI
from pydantic import BaseModel
import pickle
model = pickle.load(open("model.pkl", "rb"))
app = FastAPI()
class InputData(BaseModel):
features: list
@app.post("/predict")
def predict(data: InputData):
prediction = model.predict([data.features])
return {"prediction": prediction.tolist()}
๐งช Test Your API Locally
You can use tools like Postman or curl
to test your API:
curl -X POST http://localhost:8000/predict \
-H "Content-Type: application/json" \
-d '{"features": [5.1, 3.5, 1.4, 0.2]}'
๐ง๐ป Deployment Options
- Local Server – for development/testing
- Heroku – simple and free to start
- Render / Railway – easy modern alternatives to Heroku
- AWS/GCP/Azure – production-level hosting with scalability
๐ Quiz 1: Deployment Basics
Q: Which Python library is known for automatic API docs and speed?
๐ Quiz 2: Model Serving
Q: Which HTTP method should you use to send prediction data to your API?
๐ Summary
- Flask and FastAPI are great tools for serving ML models.
- Use
pickle
orjoblib
to save/load models. - Always test your API locally before deploying.
Comments
Post a Comment