-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
all local functions are now working with the raw C bindings, just nee…
…d to test the upload to server left
- Loading branch information
1 parent
8adeb23
commit 306df6a
Showing
11 changed files
with
251 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
# SurrealML Python Client | ||
|
||
The SurrealML Python client using the Rust `surrealml` library without any `PyO3` bindings. |
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ pub mod save_model; | |
pub mod load_cached_raw_model; | ||
pub mod to_bytes; | ||
pub mod meta; | ||
pub mod upload_model; |
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,86 @@ | ||
// Standard library imports | ||
use std::ffi::{CStr, CString}; | ||
use std::os::raw::c_char; | ||
|
||
// External crate imports | ||
use base64::encode; | ||
use hyper::{ | ||
Body, Client, Method, Request, Uri, | ||
header::{AUTHORIZATION, CONTENT_TYPE, HeaderValue}, | ||
}; | ||
use surrealml_core::storage::stream_adapter::StreamAdapter; | ||
|
||
// Local module imports | ||
use crate::utils::EmptyReturn; | ||
use crate::{empty_return_safe_eject, process_string_for_empty_return}; | ||
|
||
|
||
/// Uploads a model to a remote server. | ||
/// | ||
/// # Arguments | ||
/// * `file_path_ptr` - The path to the file to upload. | ||
/// * `url_ptr` - The URL to upload the file to. | ||
/// * `chunk_size` - The size of the chunks to upload the file in. | ||
/// * `ns_ptr` - The namespace to upload the file to. | ||
/// * `db_ptr` - The database to upload the file to. | ||
/// * `username_ptr` - The username to use for authentication. | ||
/// * `password_ptr` - The password to use for authentication. | ||
/// | ||
/// # Returns | ||
/// An empty return object indicating success or failure. | ||
#[no_mangle] | ||
pub extern "C" fn upload_model( | ||
file_path_ptr: *const c_char, | ||
url_ptr: *const c_char, | ||
chunk_size: usize, | ||
ns_ptr: *const c_char, | ||
db_ptr: *const c_char, | ||
username_ptr: *const c_char, | ||
password_ptr: *const c_char | ||
) -> EmptyReturn { | ||
// process the inputs | ||
let file_path = process_string_for_empty_return!(file_path_ptr, "file path"); | ||
let url = process_string_for_empty_return!(url_ptr, "url"); | ||
let ns = process_string_for_empty_return!(ns_ptr, "namespace"); | ||
let db = process_string_for_empty_return!(db_ptr, "database"); | ||
let username = match username_ptr.is_null() { | ||
true => None, | ||
false => Some(process_string_for_empty_return!(username_ptr, "username")) | ||
}; | ||
let password = match password_ptr.is_null() { | ||
true => None, | ||
false => Some(process_string_for_empty_return!(password_ptr, "password")) | ||
}; | ||
|
||
let client = Client::new(); | ||
|
||
let uri: Uri = empty_return_safe_eject!(url.parse()); | ||
let generator = empty_return_safe_eject!(StreamAdapter::new(chunk_size, file_path)); | ||
let body = Body::wrap_stream(generator); | ||
|
||
let part_req = Request::builder() | ||
.method(Method::POST) | ||
.uri(uri) | ||
.header(CONTENT_TYPE, "application/octet-stream") | ||
.header("surreal-ns", empty_return_safe_eject!(HeaderValue::from_str(&ns))) | ||
.header("surreal-db", empty_return_safe_eject!(HeaderValue::from_str(&db))); | ||
|
||
let req; | ||
if username.is_none() == false && password.is_none() == false { | ||
// unwraps are safe because we have already checked that the values are not None | ||
let encoded_credentials = encode(format!("{}:{}", username.unwrap(), password.unwrap())); | ||
req = empty_return_safe_eject!(part_req.header(AUTHORIZATION, format!("Basic {}", encoded_credentials)) | ||
.body(body)); | ||
} | ||
else { | ||
req = empty_return_safe_eject!(part_req.body(body)); | ||
} | ||
|
||
let tokio_runtime = empty_return_safe_eject!(tokio::runtime::Builder::new_current_thread().enable_io() | ||
.enable_time() | ||
.build()); | ||
tokio_runtime.block_on( async move { | ||
let _response = client.request(req).await.unwrap(); | ||
}); | ||
EmptyReturn::success() | ||
} |
Oops, something went wrong.