fix: align ML service response schemas
This commit is contained in:
@@ -3,7 +3,12 @@ use axum::{
|
|||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
Json,
|
Json,
|
||||||
};
|
};
|
||||||
use serde_json::json;
|
use serde::Serialize;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub struct ErrorResponse {
|
||||||
|
pub detail: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ServiceError {
|
pub enum ServiceError {
|
||||||
@@ -20,7 +25,7 @@ impl IntoResponse for ServiceError {
|
|||||||
ServiceError::PredictionFailed(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg),
|
ServiceError::PredictionFailed(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg),
|
||||||
};
|
};
|
||||||
|
|
||||||
let body = Json(json!({ "detail": detail }));
|
let body = Json(ErrorResponse { detail });
|
||||||
(status, body).into_response()
|
(status, body).into_response()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,17 +17,6 @@ pub struct MetadataResponse {
|
|||||||
pub labels: Vec<String>,
|
pub labels: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
|
||||||
pub struct PredictionResponse {
|
|
||||||
pub predictions: Vec<PredictionResult>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
|
||||||
pub struct PredictionResult {
|
|
||||||
pub label: String,
|
|
||||||
pub confidence: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn health_response(model_loaded: bool) -> HealthResponse {
|
pub fn health_response(model_loaded: bool) -> HealthResponse {
|
||||||
HealthResponse {
|
HealthResponse {
|
||||||
status: "ok".to_string(),
|
status: "ok".to_string(),
|
||||||
@@ -85,24 +74,4 @@ mod tests {
|
|||||||
vec!["Bercak Daun", "Daun Sehat", "Karat Daun", "Hawar Daun"]
|
vec!["Bercak Daun", "Daun Sehat", "Karat Daun", "Hawar Daun"]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn prediction_response_serializes() {
|
|
||||||
let response = PredictionResponse {
|
|
||||||
predictions: vec![
|
|
||||||
PredictionResult {
|
|
||||||
label: "Bercak Daun".to_string(),
|
|
||||||
confidence: 0.95,
|
|
||||||
},
|
|
||||||
PredictionResult {
|
|
||||||
label: "Daun Sehat".to_string(),
|
|
||||||
confidence: 0.05,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
let json = serde_json::to_string(&response).expect("failed to serialize");
|
|
||||||
assert!(json.contains("Bercak Daun"));
|
|
||||||
assert!(json.contains("0.95"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user