Skip to content

Commit

Permalink
Replace the generated file to the generated folder
Browse files Browse the repository at this point in the history
Signed-off-by: Aisuko <[email protected]>
  • Loading branch information
Aisuko committed Oct 19, 2023
1 parent 1806dd7 commit 61bd269
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 100 deletions.
18 changes: 10 additions & 8 deletions backend/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ resolver = "2"
members = [
"bunker",
"burn",
"codegen",
]
30 changes: 30 additions & 0 deletions backend/rust/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# default are fmt and then check
.DEFAULT_GOAL := all

.PHONY: all
all: fmt check

.PHONY: fmt
fmt:
@echo "Formatting code..."
@cargo fmt --all -- --check

.PHONY: build
build:
@echo "Building..."
@cargo build --release

.PHONY: test
test:
@echo "Testing..."
@cargo test --all

.PHONY: check
check:
@echo "Checking..."
@cargo check --all

.PHONY: clean
clean:
@echo "Cleaning..."
@cargo clean
6 changes: 2 additions & 4 deletions backend/rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ macOS
brew install protobuf
```

### Generating the server side code

> Rust backend uses the same proto file with the other backends, so we should keep the same interface of the backend. So, the output file of backend.rs is in the /target folder and do not need to be managed by git.
### Cargo fmt all the code

```
make build
cargo fmt --all --check
```
7 changes: 3 additions & 4 deletions backend/rust/bunker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tonic = "0.10"
tonic = { version = "0.10", features = [] }
prost = "0.12"
tokio = { version = "1.0", features = ["rt-multi-thread", "macros", "sync", "time"] }
tokio-stream = "0.1"
Expand All @@ -16,6 +16,5 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
rand = "0.7"
async-trait = "0.1.74"

[build-dependencies]
tonic-build = "0.10"
tracing-subscriber = "0.3.17"
tracing = "0.1.39"
10 changes: 0 additions & 10 deletions backend/rust/bunker/build.rs

This file was deleted.

File renamed without changes.
9 changes: 5 additions & 4 deletions backend/rust/bunker/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod service;
/// Import the code() backend.rs was generated from backend.proto
pub mod pb {
include!("../generated/backend.rs");
}

pub mod pb{
include!("backend.rs");
}
pub mod service;
56 changes: 39 additions & 17 deletions backend/rust/bunker/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
use crate::pb::{HealthMessage, PredictOptions,Reply,ModelOptions, EmbeddingResult, GenerateImageRequest,TranscriptRequest,TranscriptResult,TtsRequest,TokenizationResponse,StatusResponse};
//! Contains the service trait for the bunker service.

use crate::pb::Result as PbResult;
use tonic::{Request, Response, Status};
use tokio_stream::wrappers::ReceiverStream;
use crate::pb::{
EmbeddingResult, GenerateImageRequest, HealthMessage, ModelOptions, PredictOptions, Reply,
StatusResponse, TokenizationResponse, TranscriptRequest, TranscriptResult, TtsRequest,
};
use async_trait::async_trait;

use tokio_stream::wrappers::ReceiverStream;
use tonic::{Request, Response, Status};

#[async_trait]
pub trait BackendService<T = ReceiverStream<Result<Reply, Status>>>{
async fn health(&self, request: Request<HealthMessage>) -> Result<Response<Reply>,Status>;
async fn predict(&self, request: Request<PredictOptions>) -> Result<Response<Reply>,Status>;
async fn load_model(&self, request: Request<ModelOptions>) -> Result<Response<PbResult>,Status>;
async fn predict_stream(&self, request: Request<PredictOptions>) -> Result<Response<T>,Status>; // https://github.com/rust-lang/rust/issues/29661
async fn embedding(&self, request: Request<PredictOptions>) -> Result<Response<EmbeddingResult>,Status>;
async fn generate_image(&self, request: Request<GenerateImageRequest>) -> Result<Response<PbResult>,Status>;
async fn audio_transcription(&self, request: Request<TranscriptRequest>) -> Result<Response<TranscriptResult>,Status>;
async fn tts(&self, request: Request<TtsRequest>) -> Result<Response<PbResult>,Status>;
async fn tokenize_string(&self, request: Request<PredictOptions>) -> Result<Response<TokenizationResponse>,Status>;
async fn status(&self, request: Request<HealthMessage>) -> Result<Response<StatusResponse>,Status>;

}
pub trait BackendService<T = ReceiverStream<Result<Reply, Status>>> {
async fn health(&self, request: Request<HealthMessage>) -> Result<Response<Reply>, Status>;
async fn predict(&self, request: Request<PredictOptions>) -> Result<Response<Reply>, Status>;
async fn load_model(
&self,
request: Request<ModelOptions>,
) -> Result<Response<PbResult>, Status>;
async fn predict_stream(&self, request: Request<PredictOptions>)
-> Result<Response<T>, Status>; // https://github.com/rust-lang/rust/issues/29661
async fn embedding(
&self,
request: Request<PredictOptions>,
) -> Result<Response<EmbeddingResult>, Status>;
async fn generate_image(
&self,
request: Request<GenerateImageRequest>,
) -> Result<Response<PbResult>, Status>;
async fn audio_transcription(
&self,
request: Request<TranscriptRequest>,
) -> Result<Response<TranscriptResult>, Status>;
async fn tts(&self, request: Request<TtsRequest>) -> Result<Response<PbResult>, Status>;
async fn tokenize_string(
&self,
request: Request<PredictOptions>,
) -> Result<Response<TokenizationResponse>, Status>;
async fn status(
&self,
request: Request<HealthMessage>,
) -> Result<Response<StatusResponse>, Status>;
}
16 changes: 4 additions & 12 deletions backend/rust/burn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ edition = "2021"

# import bunker here
bunker = { path = "../bunker" }

tonic = "0.10"
prost = "0.12"
tokio = { version = "1.0", features = ["rt-multi-thread", "macros", "sync", "time"] }
tokio-stream = "0.1"

async-stream = "0.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
rand = "0.7"
tracing = "0.1"
tracing-subscriber = "0.3"
tokio = "1.33.0"
async-trait = "0.1.74"
tonic = "0.10.2"
tokio-stream = "0.1.14"
81 changes: 40 additions & 41 deletions backend/rust/burn/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,89 +1,88 @@
use tokio_stream::wrappers::ReceiverStream;
use tonic::transport::Server;
use tonic::{async_trait, Request, Response, Status};
use tracing;
use tracing_subscriber;
use bunker::pb::{EmbeddingResult, GenerateImageRequest, HealthMessage, ModelOptions, PredictOptions, Reply, StatusResponse, TokenizationResponse, TranscriptRequest, TranscriptResult, TtsRequest};
use bunker::pb::Result as PbResult;
use bunker::pb::{
EmbeddingResult, GenerateImageRequest, HealthMessage, ModelOptions, PredictOptions, Reply,
StatusResponse, TokenizationResponse, TranscriptRequest, TranscriptResult, TtsRequest,
};
use bunker::service::BackendService;
use tokio_stream::wrappers::ReceiverStream;
use tonic::{Request, Response, Status};

use async_trait::async_trait;

// implement BackendService trait in bunker

struct BurnBackend;

#[async_trait]
impl BackendService<ReceiverStream<Result<Reply, Status>>> for BurnBackend{

async fn health(&self, request: Request<HealthMessage>) -> Result<Response<Reply>,Status> {
impl BackendService<ReceiverStream<Result<Reply, Status>>> for BurnBackend {
async fn health(&self, request: Request<HealthMessage>) -> Result<Response<Reply>, Status> {
// return a Result<Response<Reply>,Status>
let reply = Reply {
message: "OK".into(),
};
let res=Response::new(reply);
let res = Response::new(reply);
Ok(res)
}

async fn predict(&self, request: Request<PredictOptions>) -> Result<Response<Reply>,Status> {
async fn predict(&self, request: Request<PredictOptions>) -> Result<Response<Reply>, Status> {
todo!()
}

async fn load_model(&self, request: Request<ModelOptions>) -> Result<Response<PbResult>, Status> {
async fn load_model(
&self,
request: Request<ModelOptions>,
) -> Result<Response<PbResult>, Status> {
todo!()
}

async fn predict_stream(&self, request: Request<PredictOptions>) -> Result<Response<ReceiverStream<Result<Reply,Status>>>, Status> {
async fn predict_stream(
&self,
request: Request<PredictOptions>,
) -> Result<Response<ReceiverStream<Result<Reply, Status>>>, Status> {
todo!()
}

async fn embedding(&self, request: Request<PredictOptions>) -> Result<Response<EmbeddingResult>, Status> {
async fn embedding(
&self,
request: Request<PredictOptions>,
) -> Result<Response<EmbeddingResult>, Status> {
todo!()
}

async fn generate_image(&self, request: Request<GenerateImageRequest>) -> Result<Response<PbResult>, Status> {
async fn generate_image(
&self,
request: Request<GenerateImageRequest>,
) -> Result<Response<PbResult>, Status> {
todo!()
}

async fn audio_transcription(&self, request: Request<TranscriptRequest>) -> Result<Response<TranscriptResult>, Status> {
async fn audio_transcription(
&self,
request: Request<TranscriptRequest>,
) -> Result<Response<TranscriptResult>, Status> {
todo!()
}

async fn tts(&self, request: Request<TtsRequest>) -> Result<Response<PbResult>, Status> {
todo!()
}

async fn tokenize_string(&self, request: Request<PredictOptions>) -> Result<Response<TokenizationResponse>, Status> {
async fn tokenize_string(
&self,
request: Request<PredictOptions>,
) -> Result<Response<TokenizationResponse>, Status> {
todo!()
}

async fn status(&self, request: Request<HealthMessage>) -> Result<Response<StatusResponse>, Status> {
async fn status(
&self,
request: Request<HealthMessage>,
) -> Result<Response<StatusResponse>, Status> {
todo!()
}

}


#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

let subscriber = tracing_subscriber::fmt()
.compact()
.with_file(true)
.with_line_number(true)
.with_target(true)
.with_level(true)
.finish();

tracing::subscriber::set_global_default(subscriber)
.expect("setting default subscriber failed");

let addr = "[::1]:50052".parse().unwrap();

let backend = BackendService {};

let svc = BurnBackend::new(backend);

Server::builder().add_service(svc).serve(addr).await?;

Ok(())
todo!()
}
9 changes: 9 additions & 0 deletions backend/rust/codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "codegen"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
tonic-build = "0.10"
11 changes: 11 additions & 0 deletions backend/rust/codegen/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
tonic_build::configure()
.out_dir("../bunker/generated")
.build_server(true)
.build_client(false)
.compile(
&["../../../pkg/grpc/proto/backend.proto"],
&["../../../pkg/grpc/proto"],
)
.unwrap();
}
1 change: 1 addition & 0 deletions backend/rust/codegen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 61bd269

Please sign in to comment.