Skip to content

Commit

Permalink
COS-130 - feat: WASM support and yew example app
Browse files Browse the repository at this point in the history
  • Loading branch information
jctosta committed Dec 19, 2022
1 parent fd6feef commit 858a2d4
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ image = "0.24.5"
reqwest = { version = "0.11.13", features = ["json"] }
serde = { version = "1.0.151", features = ["derive"] }
serde_json = "1.0.89"

[target.'cfg(any(target_arch = "x86_64", target_arch = "aarch64"))'.dependencies]
tokio = { version = "1.23.0", features = ["full"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2.83"

[[example]]
name = "clap-text"
path = "examples/clap/text2img.rs"
Expand All @@ -35,4 +40,4 @@ keywords = ["stable", "diffusion", "api", "client", "clap"]

[dev-dependencies]
clap = { version = "4.0.29", features = ["derive"] }
dotenv = "0.15.0"
dotenv = "0.15.0"
1 change: 1 addition & 0 deletions examples/yew-app/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SD_API_URL=https://example.com/api
3 changes: 3 additions & 0 deletions examples/yew-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target/
Cargo.lock
dist/
15 changes: 15 additions & 0 deletions examples/yew-app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "yew-app"
version = "0.1.0"
edition = "2021"

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

[dependencies]
yew = { version = "0.20.0", features = ["csr"] }
sd_client = { path = "../../" }
wasm-bindgen-futures = "0.4.33"
wasm-bindgen = { version = "0.2.83", features = ["serde-serialize"] }
web-sys = { version = "0.3.60" }
dotenv = "0.15.0"
load-dotenv = "0.1.2"
7 changes: 7 additions & 0 deletions examples/yew-app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Yew App</title>
</head>
</html>
54 changes: 54 additions & 0 deletions examples/yew-app/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use yew::prelude::*;
use sd_client::stable_diffusion::{
StableDiffusionClient, StableDiffusionParameters,
};
use wasm_bindgen_futures::spawn_local;
use web_sys::console;
use load_dotenv::load_dotenv;

load_dotenv!();

pub async fn call_sd() -> String {

let sd_api_url = std::env!("SD_API_URL").to_string();

let params = StableDiffusionParameters {
prompt: Some("A prompt".to_string()),
steps: Some(50),
..Default::default()
};

let sd_api = StableDiffusionClient::new(sd_api_url);

match sd_api.text2img(params).await {
Ok(res) => {
res.images[0].clone()
}
Err(err) => {
err.to_string().into()
}
}
}

#[function_component]
fn App() -> Html {

let app_title: String = "Yew Example".into();

// Spawn a task to call the SD API
// This is needed to call async api in a wasm context
spawn_local(async {
let res = call_sd().await;
console::log_1(&res.into());
});

html! {
<div>
<p>{ &app_title }</p>
</div>
}
}

fn main() {
yew::Renderer::<App>::new().render();
}

0 comments on commit 858a2d4

Please sign in to comment.