use std::fs;
use bentoml::prelude::*;
use serde::Deserialize;
#[derive(Deserialize, Debug)]
struct Label {
name: String,
score: f64,
}
#[derive(Deserialize, Debug)]
struct Classification {
labels: Vec<Label>,
}
#[tokio::main]
async fn main() -> Result<()> {
let client = Client::builder()
.with_base_url("http://localhost:3000")
.build()?;
if !client.is_ready().await? {
eprintln!("service is not ready; is it running on :3000?");
return Ok(());
}
let image = fs::read("image.jpg").unwrap_or_else(|_| {
eprintln!("image.jpg not found; sending placeholder bytes");
b"\xff\xd8\xff".to_vec()
});
let body = Multipart::new().field("top_k", &3).part(
"image",
Part::new(image).file_name("image.jpg").mime("image/jpeg"),
);
let response = client.endpoint("classify").call_multipart(body).await?;
let result: Classification = response.json().await?;
for label in &result.labels {
println!("{:20} {:.3}", label.name, label.score);
}
Ok(())
}