ML Inference Service — ZeaVis Edu
Layanan inferensi machine learning berbasis Rust/Axum + ONNX Runtime untuk klasifikasi penyakit daun jagung.
Daftar Isi
- Fitur
- Prasyarat & Instalasi
- Menjalankan Service
- Environment Variables
- Endpoint API
- Verifikasi & Testing
- Docker Deployment
- Troubleshooting
1. Fitur
- Framework: Axum (async Rust web framework)
- Runtime Inferensi: ONNX Runtime untuk kompatibilitas lintas platform
- Model: EfficientNetV2B0 dalam format ONNX
- Endpoint: Health check, metadata, dan prediksi gambar
- Multipart Upload: Dukungan upload gambar langsung via HTTP POST
2. Prasyarat & Instalasi
- Rust 1.70+ dan Cargo
- Model ONNX di
../../Machine_Learning/model/model.onnx(atau path custom viaMODEL_PATH)
Dependensi Rust sudah terdaftar di Cargo.toml. Cargo akan mengunduh dan mengkompilasi otomatis saat pertama kali build.
cargo build
Output build lokal berada di target/ dan direktori tersebut diabaikan oleh Git.
3. Menjalankan Service
Semua perintah di bawah dijalankan dari direktori apps/ml-service.
Opsi 1: Default (Port 4012)
cargo run
Service akan mencari model di path default:
../../Machine_Learning/model/model.onnx
Opsi 2: Local Development dengan .env.example (Port 4012)
source .env.example
cargo run
Opsi 3: Custom Model Path & Port
ML_SERVICE_PORT=9000 MODEL_PATH=/path/to/model.onnx cargo run
4. Environment Variables
| Variable | Default | Keterangan |
|---|---|---|
ML_SERVICE_HOST |
0.0.0.0 |
Bind address |
ML_SERVICE_PORT |
4012 |
Bind port |
MODEL_PATH |
../../Machine_Learning/model/model.onnx |
Path ke file model ONNX |
MODEL_INPUT_SIZE |
224 |
Ukuran input gambar (224×224 untuk EfficientNetV2B0) |
RUST_LOG |
info |
Level logging (debug, info, warn, error) |
5. Endpoint API
Health Check
curl http://localhost:4012/health
{
"status": "ok",
"model_loaded": true
}
Metadata
curl http://localhost:4012/metadata
{
"service_name": "zeavis-ml-service",
"service_version": "0.1.0",
"model_path": "../../Machine_Learning/model/model.onnx",
"model_loaded": true,
"input_size": 224,
"labels": ["Bercak Daun", "Daun Sehat", "Karat Daun", "Hawar Daun"]
}
Prediksi
Upload gambar daun jagung untuk klasifikasi:
curl -X POST http://localhost:4012/predict \
-F "file=@/path/to/corn-leaf.jpg"
{
"label": "Daun Sehat",
"confidence": 0.95,
"probabilities": {
"Bercak Daun": 0.02,
"Daun Sehat": 0.95,
"Karat Daun": 0.01,
"Hawar Daun": 0.02
}
}
6. Verifikasi & Testing
Build Produksi
cargo build --release
# Binary di target/release/zeavis-ml-service
Menjalankan Tests
cargo test
Verifikasi Manual (default port 4012)
# 1. Start service
cargo run
# 2. Health check
curl http://localhost:4012/health
# 3. Metadata
curl http://localhost:4012/metadata
# 4. Prediksi
curl -X POST http://localhost:4012/predict \
-F "file=@../../Machine_Learning/dataset/Daun\ Sehat/sample.jpg"
7. Docker Deployment
Service dapat di-deploy via Docker. Build dari root repository karena Dockerfile menyalin source service dan artifact ONNX dari beberapa direktori repo.
docker build -f apps/ml-service/Dockerfile -t zeavis-ml-service .
docker run -p 4012:4012 zeavis-ml-service
Pastikan Machine_Learning/model/model.onnx sudah dibuat sebelum build image.
8. Troubleshooting
Model tidak ditemukan
Error: Failed to load model: No such file or directory
Solusi:
ls -la ../../Machine_Learning/model/model.onnx
# Atau set path custom:
MODEL_PATH=/absolute/path/to/model.onnx cargo run
Port sudah digunakan
Error: Address already in use
Solusi:
ML_SERVICE_PORT=9000 cargo run
# Cek port yang digunakan:
lsof -i :4012
ONNX Runtime tidak kompatibel
Error: ONNX Runtime initialization failed
Solusi: Pastikan binary ONNX Runtime kompatibel dengan sistem operasi. Jika masalah persisten:
cargo clean
cargo build