Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update server-warp to server-axum & Update server-tflite/Cargo.toml #22

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ jobs:
exit 1
fi

- name: Warp example
- name: Axum example
run: |
cd server-warp
cd server-axum
cargo build --target wasm32-wasi --release
wasmedgec target/wasm32-wasi/release/wasmedge_warp_server.wasm wasmedge_warp_server.wasm
nohup wasmedge wasmedge_warp_server.wasm &
wasmedgec target/wasm32-wasi/release/wasmedge_axum_server.wasm wasmedge_axum_server.wasm
nohup wasmedge wasmedge_axum_server.wasm &
echo $! > wasmedge.pid
sleep 15
resp=$(curl http://localhost:8080/echo -X POST -d "WasmEdge")
Expand Down
3 changes: 3 additions & 0 deletions server-axum/.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"]
15 changes: 15 additions & 0 deletions server-axum/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "wasmedge_axum_server"
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]
axum = "0.6"
bytes = "1"
futures-util = "0.3.30"
tokio = { version = "1", features = ["rt", "macros", "net", "time", "io-util"]}
File renamed without changes.
35 changes: 35 additions & 0 deletions server-axum/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use bytes::Bytes;
use futures_util::StreamExt;

use axum::{extract::BodyStream, routing::get, routing::post, Router};
use tokio::net::TcpListener;

#[tokio::main(flavor = "current_thread")]
async fn main() {
// build our application with a route
let app = Router::new()
.route("/", get(help))
.route("/echo", post(echo));

// run it
let addr = "0.0.0.0:8080";
let tcp_listener = TcpListener::bind(addr).await.unwrap();
println!("listening on {}", addr);
axum::Server::from_tcp(tcp_listener.into_std().unwrap())
.unwrap()
.serve(app.into_make_service())
.await
.unwrap();
}

async fn help() -> &'static str {
"Try POSTing data to /echo such as: `curl localhost:8080/echo -XPOST -d 'hello world'`\n"
}

async fn echo(mut stream: BodyStream) -> Bytes {
if let Some(Ok(s)) = stream.next().await {
s
} else {
Bytes::new()
}
}
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
9 changes: 0 additions & 9 deletions server-warp/Cargo.toml

This file was deleted.

20 changes: 0 additions & 20 deletions server-warp/src/main.rs

This file was deleted.

Loading