Welcome to Bonus Day 31 of the 30 Days of Data Science series! 🎉 Today, we’ll explore how to deploy your machine learning models or applications to Cloud Platforms using Flask/FastAPI. By the end, you'll understand the steps to deploy to AWS, Azure, and GCP.
Deployment is a crucial step in bringing your data science project to life. It allows others to interact with your model or application in real-time. We'll focus on deploying applications using Flask or FastAPI to popular cloud platforms like:
- AWS (Amazon Web Services)
- Azure
- GCP (Google Cloud Platform)
Ensure your project folder is structured properly:
project/
|-- app.py # Main application script
|-- model.pkl # Serialized ML model (if applicable)
|-- templates/
| |-- index.html # Frontend files (if needed)
|-- requirements.txt # Python dependencies
|-- Dockerfile # (Optional) Docker configuration
List all dependencies in a requirements.txt
file:
Flask==2.1.2
pandas==1.5.3
numpy==1.23.5
scikit-learn==1.1.3
Generate automatically:
pip freeze > requirements.txt
Here’s a minimal Flask application:
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route("/", methods=["GET"])
def home():
return "Welcome to the Flask Deployment!"
@app.route("/predict", methods=["POST"])
def predict():
data = request.json
prediction = sum(data["values"]) # Example prediction logic
return jsonify({"prediction": prediction})
if __name__ == "__main__":
app.run(debug=True)
Run locally:
python app.py
Here’s a minimal FastAPI application:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class InputData(BaseModel):
values: list[int]
@app.get("/")
def home():
return {"message": "Welcome to FastAPI Deployment!"}
@app.post("/predict")
def predict(data: InputData):
prediction = sum(data.values) # Example prediction logic
return {"prediction": prediction}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
Run locally:
uvicorn app:app --reload
-
Set Up an EC2 Instance:
- Go to the AWS Management Console.
- Launch an EC2 instance with an Ubuntu AMI.
-
Install Dependencies on EC2: SSH into the instance and set up the environment:
sudo apt update && sudo apt upgrade sudo apt install python3-pip pip3 install -r requirements.txt
-
Run the Application:
python3 app.py
-
Expose the Application:
- Open port 5000 (or your application port) in the AWS security group.
- Access the app using the public IP of the EC2 instance.
-
Set Up an App Service:
- Go to the Azure Portal.
- Create an App Service and select a Python runtime.
-
Deploy Code:
- Zip your project folder and upload it through the Azure portal.
-
Configure Startup Command: Add the following startup command in the portal:
gunicorn -w 4 -k uvicorn.workers.UvicornWorker app:app
-
Access the Application:
- Use the App Service URL provided by Azure.
-
Set Up a Google Cloud Project:
- Go to the GCP Console.
- Enable the App Engine API.
-
Create an
app.yaml
File:runtime: python39 entrypoint: gunicorn -w 4 -k uvicorn.workers.UvicornWorker app:app
-
Deploy the Application:
gcloud app deploy
-
Access the Application:
- Use the provided GCP URL.
-
Create a Flask/FastAPI application that:
- Accepts an input text.
- Returns the sentiment (positive/negative) using a pre-trained model.
-
Deploy this application to any cloud platform of your choice.
In this bonus session, we learned how to:
- Prepare a Flask/FastAPI application for deployment.
- Deploy to AWS EC2, Azure App Service, and Google Cloud Platform.
- Configure cloud platforms and expose services to the internet.
🎉 Congratulations on completing the 30 Days of Data Science series with this bonus day! You’re now equipped to deploy your projects to the world. 🌎