-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
COS-130 - feat: WASM support and yew example app
- Loading branch information
Showing
6 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SD_API_URL=https://example.com/api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target/ | ||
Cargo.lock | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |