Files
zeavis-edu/apps/ml-service/README.md
T

229 lines
4.5 KiB
Markdown
Raw Normal View History

# ML Inference Service — ZeaVis Edu
2026-05-23 10:51:59 +00:00
> Layanan inferensi machine learning berbasis Rust/Axum + ONNX Runtime untuk klasifikasi penyakit daun jagung.
2026-05-23 10:51:59 +00:00
← [Kembali ke README utama](../../README.md)
---
## Daftar Isi
1. [Fitur](#1-fitur)
2. [Prasyarat & Instalasi](#2-prasyarat--instalasi)
3. [Menjalankan Service](#3-menjalankan-service)
4. [Environment Variables](#4-environment-variables)
5. [Endpoint API](#5-endpoint-api)
6. [Verifikasi & Testing](#6-verifikasi--testing)
7. [Docker Deployment](#7-docker-deployment)
8. [Troubleshooting](#8-troubleshooting)
---
## 1. Fitur
2026-05-23 10:51:59 +00:00
- **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
2026-05-23 10:51:59 +00:00
- Rust 1.70+ dan Cargo
- Model ONNX di `../../Machine_Learning/model/model.onnx` (atau path custom via `MODEL_PATH`)
Dependensi Rust sudah terdaftar di `Cargo.toml`. Cargo akan mengunduh dan mengkompilasi otomatis saat pertama kali build.
```bash
cargo build
```
Output build lokal berada di `target/` dan direktori tersebut diabaikan oleh Git.
---
## 3. Menjalankan Service
2026-05-23 10:51:59 +00:00
2026-05-23 11:05:49 +00:00
Semua perintah di bawah dijalankan dari direktori `apps/ml-service`.
### Opsi 1: Default (Port 4012)
2026-05-23 10:51:59 +00:00
```bash
cargo run
```
Service akan mencari model di path default:
2026-05-23 10:51:59 +00:00
```
../../Machine_Learning/model/model.onnx
```
2026-08-02 16:49:11 +07:00
### Opsi 2: Local Development dengan .env.example (Port 4012)
2026-05-23 11:01:41 +00:00
```bash
source .env.example
cargo run
```
### Opsi 3: Custom Model Path & Port
2026-05-23 10:51:59 +00:00
```bash
2026-05-23 11:01:41 +00:00
ML_SERVICE_PORT=9000 MODEL_PATH=/path/to/model.onnx cargo run
```
2026-05-23 10:51:59 +00:00
---
## 4. Environment Variables
2026-05-23 10:51:59 +00:00
| Variable | Default | Keterangan |
|---|---|---|
2026-05-23 11:01:41 +00:00
| `ML_SERVICE_HOST` | `0.0.0.0` | Bind address |
2026-08-02 16:14:27 +07:00
| `ML_SERVICE_PORT` | `4012` | Bind port |
2026-05-23 10:51:59 +00:00
| `MODEL_PATH` | `../../Machine_Learning/model/model.onnx` | Path ke file model ONNX |
| `MODEL_INPUT_SIZE` | `224` | Ukuran input gambar (224×224 untuk EfficientNetV2B0) |
2026-05-23 10:51:59 +00:00
| `RUST_LOG` | `info` | Level logging (debug, info, warn, error) |
---
2026-05-23 10:51:59 +00:00
## 5. Endpoint API
### Health Check
2026-05-23 10:51:59 +00:00
```bash
curl http://localhost:4012/health
2026-05-23 10:51:59 +00:00
```
```json
{
"status": "ok",
2026-05-23 11:09:16 +00:00
"model_loaded": true
2026-05-23 10:51:59 +00:00
}
```
### Metadata
2026-05-23 10:51:59 +00:00
```bash
curl http://localhost:4012/metadata
2026-05-23 10:51:59 +00:00
```
```json
{
2026-05-23 11:09:16 +00:00
"service_name": "zeavis-ml-service",
"service_version": "0.1.0",
2026-05-23 10:51:59 +00:00
"model_path": "../../Machine_Learning/model/model.onnx",
2026-05-23 11:09:16 +00:00
"model_loaded": true,
2026-05-23 10:51:59 +00:00
"input_size": 224,
"labels": ["Bercak Daun", "Daun Sehat", "Karat Daun", "Hawar Daun"]
2026-05-23 10:51:59 +00:00
}
```
### Prediksi
2026-05-23 10:51:59 +00:00
Upload gambar daun jagung untuk klasifikasi:
```bash
curl -X POST http://localhost:4012/predict \
2026-05-23 10:51:59 +00:00
-F "file=@/path/to/corn-leaf.jpg"
```
```json
{
2026-05-23 11:09:16 +00:00
"label": "Daun Sehat",
"confidence": 0.95,
"probabilities": {
"Bercak Daun": 0.02,
"Daun Sehat": 0.95,
"Karat Daun": 0.01,
"Hawar Daun": 0.02
2026-05-23 10:51:59 +00:00
}
}
```
---
## 6. Verifikasi & Testing
2026-05-23 10:51:59 +00:00
### Build Produksi
```bash
cargo build --release
# Binary di target/release/zeavis-ml-service
2026-05-23 10:51:59 +00:00
```
### Menjalankan Tests
```bash
cargo test
```
2026-08-02 16:14:27 +07:00
### Verifikasi Manual (default port 4012)
2026-05-23 11:01:41 +00:00
```bash
# 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"
2026-05-23 11:01:41 +00:00
```
---
2026-05-23 11:01:41 +00:00
## 7. Docker Deployment
2026-05-23 10:51:59 +00:00
Service dapat di-deploy via Docker. Build dari root repository karena Dockerfile menyalin source service dan artifact ONNX dari beberapa direktori repo.
2026-05-23 10:51:59 +00:00
```bash
docker build -f apps/ml-service/Dockerfile -t zeavis-ml-service .
2026-08-02 16:14:27 +07:00
docker run -p 4012:4012 zeavis-ml-service
2026-05-23 10:51:59 +00:00
```
Pastikan `Machine_Learning/model/model.onnx` sudah dibuat sebelum build image.
---
2026-05-23 10:51:59 +00:00
## 8. Troubleshooting
2026-05-23 10:51:59 +00:00
### Model tidak ditemukan
2026-05-23 10:51:59 +00:00
**Error:** `Failed to load model: No such file or directory`
**Solusi:**
```bash
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:**
```bash
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:
```bash
cargo clean
cargo build
```
---
← [Kembali ke README utama](../../README.md) • [Pipeline ML →](../../Machine_Learning/README.md) • [Infra →](../../infra/README.md)