Skip to content

Commit

Permalink
Update server-tflite/Cargo.toml
Browse files Browse the repository at this point in the history
Signed-off-by: csh <458761603@qq.com>
  • Loading branch information
L-jasmine committed May 8, 2024
1 parent 5774f05 commit 5ff5900
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
3 changes: 3 additions & 0 deletions server-tflite/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build]
target = "wasm32-wasi"
rustflags = ["--cfg", "wasmedge", "--cfg", "tokio_unstable"]
9 changes: 7 additions & 2 deletions server-tflite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ name = "wasmedge_hyper_server_tflite"
version = "0.1.0"
edition = "2021"

[patch.crates-io]
tokio = { git = "https://github.com/second-state/wasi_tokio.git", branch = "v1.36.x" }
socket2 = { git = "https://github.com/second-state/socket2.git", branch = "v0.5.x" }
hyper = { git = "https://github.com/second-state/wasi_hyper.git", branch = "v0.14.x" }

[dependencies]
hyper_wasi = { version = "0.15", features = ["full"]}
tokio_wasi = { version = "1", features = ["rt", "macros", "net", "time", "io-util"]}
hyper = { version = "0.14", features = ["full"]}
tokio = { version = "1", features = ["rt", "macros", "net", "time", "io-util"]}
image = { version = "0.23.14", default-features = false, features = ["gif", "jpeg", "ico", "png", "tiff", "webp", "bmp"] }
wasi-nn = "0.4.0"
anyhow = "1.0"
33 changes: 18 additions & 15 deletions server-tflite/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Method, Request, Response, StatusCode, Server};
use hyper::{Body, Method, Request, Response, Server, StatusCode};
use image::io::Reader;
use image::DynamicImage;
use std::convert::Infallible;
use std::io::Cursor;
use std::net::SocketAddr;
use std::result::Result;
use std::io::Cursor;
use image::io::Reader;
use image::DynamicImage;
use wasi_nn::{GraphBuilder, GraphEncoding, ExecutionTarget, TensorType};
use tokio::net::TcpListener;
use wasi_nn::{ExecutionTarget, GraphBuilder, GraphEncoding, TensorType};

/// This is our service handler. It receives a Request, routes on its
/// path, and returns a Future of a Response.
async fn classify(req: Request<Body>) -> Result<Response<Body>, anyhow::Error> {
let model_data: &[u8] = include_bytes!("models/mobilenet_v1_1.0_224/mobilenet_v1_1.0_224_quant.tflite");
let model_data: &[u8] =
include_bytes!("models/mobilenet_v1_1.0_224/mobilenet_v1_1.0_224_quant.tflite");
let labels = include_str!("models/mobilenet_v1_1.0_224/labels_mobilenet_quant_v1_224.txt");
let graph = GraphBuilder::new(GraphEncoding::TensorflowLite, ExecutionTarget::CPU).build_from_bytes(&[model_data])?;
let graph = GraphBuilder::new(GraphEncoding::TensorflowLite, ExecutionTarget::CPU)
.build_from_bytes(&[model_data])?;
let mut ctx = graph.init_execution_context()?;
/*
let graph = unsafe {
Expand Down Expand Up @@ -94,14 +97,14 @@ async fn classify(req: Request<Body>) -> Result<Response<Body>, anyhow::Error> {
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
let make_svc = make_service_fn(|_| {
async move {
Ok::<_, Infallible>(service_fn(move |req| {
classify(req)
}))
}
});
let server = Server::bind(&addr).serve(make_svc);

let listener = TcpListener::bind(addr).await?;

let make_svc =
make_service_fn(
|_| async move { Ok::<_, Infallible>(service_fn(move |req| classify(req))) },
);
let server = Server::from_tcp(listener.into_std()?)?.serve(make_svc);
if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
Expand Down

0 comments on commit 5ff5900

Please sign in to comment.