|
| 1 | +--- |
| 2 | +title: Deploy and access XGBoost inference API |
| 3 | +weight: 8 |
| 4 | + |
| 5 | +### FIXED, DO NOT MODIFY |
| 6 | +layout: learningpathall |
| 7 | +--- |
| 8 | + |
| 9 | +## Deploy XGBoost inference API on SUSE Linux |
| 10 | + |
| 11 | +In this section, you'll deploy the trained XGBoost model as a Flask-based inference API on a GCP Axion Arm64 VM. You'll expose the API externally and access it from a browser using the VM public IP. |
| 12 | + |
| 13 | +You'll use: |
| 14 | + |
| 15 | +**Terminal A** → API server |
| 16 | + |
| 17 | +**Terminal B** → API testing |
| 18 | + |
| 19 | +## Connect to the VM |
| 20 | +Connect to the VM where the trained XGBoost model and Python environment were created. This VM will host the inference API service. |
| 21 | + |
| 22 | +```bash |
| 23 | +ssh <your-user>@<your-vm-ip> |
| 24 | +``` |
| 25 | + |
| 26 | +Navigate to the XGBoost project directory that contains the trained model files and scripts. |
| 27 | + |
| 28 | +```bash |
| 29 | +cd ~/xgboost-learning-path |
| 30 | +``` |
| 31 | + |
| 32 | +Activate the Python virtual environment to load all required Python packages and dependencies. |
| 33 | + |
| 34 | +```bash |
| 35 | +source xgb-env/bin/activate |
| 36 | +``` |
| 37 | + |
| 38 | +## Install Flask |
| 39 | +Flask is used to create the lightweight REST API that serves XGBoost predictions through HTTP requests. |
| 40 | + |
| 41 | +Create an updated requirements file containing all required Python dependencies. |
| 42 | + |
| 43 | +```bash |
| 44 | +cat > requirements.txt <<'EOF' |
| 45 | +xgboost |
| 46 | +numpy |
| 47 | +pandas |
| 48 | +scikit-learn |
| 49 | +matplotlib |
| 50 | +joblib |
| 51 | +flask |
| 52 | +EOF |
| 53 | +``` |
| 54 | + |
| 55 | +Install all dependencies including Flask inside the Python virtual environment. |
| 56 | + |
| 57 | +```bash |
| 58 | +pip install -r requirements.txt |
| 59 | +``` |
| 60 | + |
| 61 | +Verify that Flask is installed successfully. |
| 62 | + |
| 63 | +```bash |
| 64 | +pip list | grep Flask |
| 65 | +``` |
| 66 | + |
| 67 | +The output is similar to: |
| 68 | + |
| 69 | +```output |
| 70 | +Flask 3.1.3 |
| 71 | +``` |
| 72 | + |
| 73 | +## Create inference API |
| 74 | +In this step, you'll create a Flask-based API that loads the trained XGBoost model and exposes prediction endpoints over HTTP. |
| 75 | + |
| 76 | +The `/` endpoint is used for browser validation, while the `/predict` endpoint handles prediction requests using JSON input data. |
| 77 | + |
| 78 | +```bash |
| 79 | +cat > inference_api.py <<'EOF' |
| 80 | +from flask import Flask, request, jsonify |
| 81 | +import numpy as np |
| 82 | +import joblib |
| 83 | +
|
| 84 | +app = Flask(__name__) |
| 85 | +
|
| 86 | +model = joblib.load("xgboost-model.pkl") |
| 87 | +
|
| 88 | +@app.route("/", methods=["GET"]) |
| 89 | +def home(): |
| 90 | + return """ |
| 91 | + <h1>XGBoost API Running on GCP Axion Arm64</h1> |
| 92 | + <p>Inference API Status : Active</p> |
| 93 | + <p>Use POST /predict endpoint for predictions.</p> |
| 94 | + """ |
| 95 | +
|
| 96 | +@app.route("/predict", methods=["POST"]) |
| 97 | +def predict(): |
| 98 | +
|
| 99 | + try: |
| 100 | + data = request.json["features"] |
| 101 | +
|
| 102 | + prediction = model.predict(np.array([data])) |
| 103 | +
|
| 104 | + return jsonify({ |
| 105 | + "prediction": int(prediction[0]) |
| 106 | + }) |
| 107 | +
|
| 108 | + except Exception as e: |
| 109 | + return jsonify({ |
| 110 | + "error": str(e) |
| 111 | + }) |
| 112 | +
|
| 113 | +if __name__ == "__main__": |
| 114 | + app.run(host="0.0.0.0", port=8080) |
| 115 | +EOF |
| 116 | +``` |
| 117 | + |
| 118 | +## Start the inference API |
| 119 | +Start the Flask application so the API becomes accessible locally and externally through the VM public IP address. |
| 120 | + |
| 121 | +```bash |
| 122 | +python inference_api.py |
| 123 | +``` |
| 124 | + |
| 125 | +The output is similar to: |
| 126 | + |
| 127 | +```output |
| 128 | + * Running on all addresses (0.0.0.0) |
| 129 | + * Running on http://127.0.0.1:8080 |
| 130 | + * Running on http://10.128.15.209:8080 |
| 131 | +``` |
| 132 | + |
| 133 | +Leave this terminal running because the Flask server must remain active to handle incoming requests. |
| 134 | + |
| 135 | +## Access API from browser |
| 136 | +The Flask API can now be accessed externally from your browser using the VM public IP and port `8080`. |
| 137 | + |
| 138 | +Open: |
| 139 | + |
| 140 | +```text |
| 141 | +http://<VM-PUBLIC-IP>:8080 |
| 142 | +``` |
| 143 | + |
| 144 | +Example: |
| 145 | + |
| 146 | +```text |
| 147 | +http://35.xxx.xxx.xxx:8080 |
| 148 | +``` |
| 149 | + |
| 150 | +The output is similar to: |
| 151 | + |
| 152 | +```output |
| 153 | +XGBoost Inference API is Running |
| 154 | +API Status: Active |
| 155 | +
|
| 156 | +This API is running on Google Cloud Axion Arm64. |
| 157 | +
|
| 158 | +Use the POST /predict endpoint to send prediction requests. |
| 159 | +``` |
| 160 | + |
| 161 | +The following screenshot shows the XGBoost inference API successfully running and accessible from the browser. |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | +## Test inference locally |
| 166 | +Open terminal B and activate the same Python virtual environment used for the API server. |
| 167 | + |
| 168 | +```bash |
| 169 | +cd ~/xgboost-learning-path |
| 170 | +source xgb-env/bin/activate |
| 171 | +``` |
| 172 | + |
| 173 | +In this step, you'll send a prediction request to the XGBoost API using `curl`. The input data is passed as JSON to the `/predict` endpoint. |
| 174 | + |
| 175 | +```bash |
| 176 | +curl -X POST http://127.0.0.1:8080/predict \ |
| 177 | +-H "Content-Type: application/json" \ |
| 178 | +-d '{"features":[17.99,10.38,122.8,1001.0,0.1184,0.2776,0.3001,0.1471,0.2419,0.07871,1.095,0.9053,8.589,153.4,0.006399,0.04904,0.05373,0.01587,0.03003,0.006193,25.38,17.33,184.6,2019.0,0.1622,0.6656,0.7119,0.2654,0.4601,0.1189]}' |
| 179 | +``` |
| 180 | + |
| 181 | +The output is similar to: |
| 182 | + |
| 183 | +```output |
| 184 | +{"prediction":0} |
| 185 | +``` |
| 186 | +This confirms that the trained XGBoost model successfully received the input features and generated an inference response through the REST API. |
| 187 | + |
| 188 | +## What you've accomplished |
| 189 | + |
| 190 | +You've successfully: |
| 191 | + |
| 192 | +* Deployed XGBoost inference API on GCP Axion Arm64 |
| 193 | +* Exposed the API externally |
| 194 | +* Accessed the API using the VM public IP |
| 195 | +* Performed inference using REST API requests |
| 196 | +* Validated real-time predictions using Flask and XGBoost |
0 commit comments