-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50f7b5b
commit b8867bd
Showing
10 changed files
with
152 additions
and
8 deletions.
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
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,34 @@ | ||
[package] | ||
name = "ratchet-downloader" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
[dependencies] | ||
ratchet-loader = { path = "../ratchet-loader" } | ||
wasm-bindgen = "0.2.84" | ||
wasm-bindgen-futures = "0.4.39" | ||
js-sys = "0.3.64" | ||
gloo = "0.11.0" | ||
reqwest = "0.11.23" | ||
|
||
[dependencies.web-sys] | ||
features = [ | ||
'console', | ||
'Headers', | ||
'Request', | ||
'RequestInit', | ||
'RequestMode', | ||
'Response', | ||
'Window', | ||
'Navigator', | ||
'StorageManager', | ||
'CacheStorage' | ||
] | ||
version = "0.3.64" | ||
|
||
[dev-dependencies] | ||
wasm-bindgen-test.workspace = true | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] |
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,35 @@ | ||
use js_sys::{ArrayBuffer, Uint8Array, JSON}; | ||
|
||
use wasm_bindgen::{prelude::*, JsValue}; | ||
use wasm_bindgen_futures::JsFuture; | ||
use web_sys::{Request, RequestInit, RequestMode, Response}; | ||
|
||
fn to_error(value: JsValue) -> JsError { | ||
JsError::new( | ||
JSON::stringify(&value) | ||
.map(|js_string| { | ||
js_string | ||
.as_string() | ||
.unwrap_or(String::from("An unknown error occurred.")) | ||
}) | ||
.unwrap_or(String::from("An unknown error occurred.")) | ||
.as_str(), | ||
) | ||
} | ||
pub(crate) async fn fetch(url: &str) -> Result<Response, JsError> { | ||
let mut opts = RequestInit::new(); | ||
opts.method("GET"); | ||
opts.mode(RequestMode::Cors); | ||
|
||
let request = Request::new_with_str_and_init(&url, &opts).map_err(to_error)?; | ||
|
||
let window = web_sys::window().unwrap(); | ||
let resp_value = JsFuture::from(window.fetch_with_request(&request)) | ||
.await | ||
.map_err(to_error)?; | ||
|
||
assert!(resp_value.is_instance_of::<Response>()); | ||
let resp: Response = resp_value.dyn_into().unwrap(); | ||
|
||
Ok(resp) | ||
} |
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 @@ | ||
pub mod repo; |
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 @@ | ||
pub struct Repo { | ||
pub id: String, | ||
pub revision: String, | ||
pub repo_type: String, | ||
} | ||
|
||
impl Repo {} |
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,57 @@ | ||
#[cfg(test)] | ||
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure}; | ||
|
||
use gloo::console::error as log_error; | ||
use wasm_bindgen::{prelude::*, JsValue}; | ||
|
||
mod fetch; | ||
pub mod huggingface; | ||
|
||
#[cfg(test)] | ||
wasm_bindgen_test_configure!(run_in_browser); | ||
|
||
pub struct Model { | ||
url: String, | ||
} | ||
|
||
impl Model { | ||
fn from_hf(repo_id: String) -> Self { | ||
Self { | ||
url: format!("https://huggingface.co/{}/resolve/main", repo_id), | ||
} | ||
} | ||
|
||
fn from_hf_with_revision(repo_id: String, revision: String) -> Self { | ||
Self { | ||
url: format!("https://huggingface.co/{repo_id}/resolve/{revision}"), | ||
} | ||
} | ||
|
||
fn from_custom(url: String) -> Self { | ||
Self { url } | ||
} | ||
|
||
async fn get(&self, file_name: String) -> Result<(), JsError> { | ||
let file_url = format!("{}/{}", self.url, file_name); | ||
// let response = fetch::fetch(file_url.as_str()).await?; | ||
|
||
let res = reqwest::Client::new().get(file_url).send().await?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
#[wasm_bindgen_test] | ||
async fn pass() -> Result<(), JsValue> { | ||
use js_sys::JsString; | ||
|
||
let model = Model::from_hf("jantxu/ratchet-test".to_string()); | ||
let file = model | ||
.get("model.safetensors".to_string()) | ||
.await | ||
.map_err(|err| { | ||
log_error!(err); | ||
JsString::from("Failed to download file") | ||
}); | ||
Ok(()) | ||
} |
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 |
---|---|---|
@@ -1,2 +1,9 @@ | ||
line-count: | ||
cd ./crates/ratchet-core && scc -irs --exclude-file kernels | ||
cd ./crates/ratchet-core && scc -irs --exclude-file kernels | ||
install-pyo3: | ||
env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install --verbose 3.10.6 | ||
echo "Please PYO3_PYTHON to your .bashrc or .zshrc" | ||
wasm CRATE: | ||
RUSTFLAGS=--cfg=web_sys_unstable_apis wasm-pack build --target web -d `pwd`/target/pkg/{{CRATE}} --out-name {{CRATE}} ./crates/{{CRATE}} --release | ||
wasm-test CRATE: | ||
RUSTFLAGS=--cfg=web_sys_unstable_apis wasm-pack test --chrome `pwd`/crates/{{CRATE}} |