Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add uploading support to the library! #72

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ ureq = { version = "2.8.0", optional = true, features = [
"json",
"socks-proxy",
] }
sha2 = "0.10"
base64 = "0.22.1"
hex = "0.4.3"
regex = "1.11.1"
lazy_static = "1.5.0"
sha1 = "0.10.6"
urlencoding = "2.1.3"

[features]
default = ["default-tls", "tokio", "ureq"]
Expand Down Expand Up @@ -68,6 +75,8 @@ ureq = [
]

[dev-dependencies]
env_logger = "0.11.5"
hex-literal = "0.4.1"
rand = "0.8.5"
sha2 = "0.10"
tokio-test = "0.4.2"
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## temporary todo

to run the test, do `RUST_LOG=trace HF_TOKEN=token_here HF_REPO=repo_here cargo run --example upload --release`

## real readme here

This crates aims to emulate and be compatible with the
[huggingface_hub](https://github.com/huggingface/huggingface_hub/) python package.

Expand All @@ -18,14 +24,14 @@ However allowing new features or creating new features might be denied by lack o
time. We're focusing on what we currently internally need. Hopefully that subset is already interesting
to more users.


# How to use
# How to use

Add the dependency

```bash
cargo add hf-hub # --features tokio
```

`tokio` feature will enable an async (and potentially faster) API.

Use the crate:
Expand Down
83 changes: 83 additions & 0 deletions examples/upload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use std::time::Instant;

use hf_hub::{
api::tokio::{ApiBuilder, ApiError},
Repo,
};
use rand::Rng;

const ONE_MB: usize = 1024 * 1024;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let token = std::env::var("HF_TOKEN")
.map_err(|_| "HF_TOKEN environment variable not set".to_string())?;
let hf_repo = std::env::var("HF_REPO")
.map_err(|_| "HF_REPO environment variable not set, e.g. apyh/gronk".to_string())?;

let api = ApiBuilder::new().with_token(Some(token)).build()?;
let repo = Repo::model(hf_repo);
let api_repo = api.repo(repo);

let exists = api_repo.exists().await;
if !exists {
return Err(ApiError::GatedRepoError("repo does not exist".to_string()).into());
} else {
log::info!("repo exists!");
}

let is_writable = api_repo.is_writable().await;
if !is_writable {
return Err(ApiError::GatedRepoError("repo is not writable".to_string()).into());
} else {
log::info!("repo is writable!");
}

let files = [
(
format!("im a tiny file {:?}", Instant::now())
.as_bytes()
.to_vec(),
"tiny_file.txt",
),
(
{
let mut data = vec![0u8; ONE_MB];
rand::thread_rng().fill(&mut data[..]);
data
},
"1m_file.txt",
),
(
{
let mut data = vec![0u8; 10 * ONE_MB];
rand::thread_rng().fill(&mut data[..]);
data
},
"10m_file.txt",
),
(
{
let mut data = vec![0u8; 20 * ONE_MB];
rand::thread_rng().fill(&mut data[..]);
data
},
"20m_file.txt",
),
];
let res = api_repo
.upload_files(
files
.into_iter()
.map(|(data, path)| (data.into(), path.into()))
.collect(),
None,
"update multiple files!".to_string().into(),
false,
)
.await?;
log::info!("commit result: {:?}", res);
log::info!("Success!!");
Ok(())
}
45 changes: 19 additions & 26 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
url = "github:oxalica/rust-overlay";
};
};
outputs =
{
self,
crate2nix,
nixpkgs,
flake-utils,
rust-overlay,
}:
outputs = {
self,
crate2nix,
nixpkgs,
flake-utils,
rust-overlay,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
system: let
cargoNix = crate2nix.tools.${system}.appliedCargoNix {
name = "hf-hub";
src = ./.;
Expand All @@ -30,10 +28,8 @@
];
};
hf-hub = cargoNix.rootCrate.build;
in
{
in {
devShells = with pkgs; rec {

default = pure;

pure = mkShell {
Expand All @@ -43,19 +39,16 @@
};

impure = mkShell {
buildInputs =
[
openssl.dev
pkg-config
(rust-bin.stable.latest.default.override {
extensions = [
"rust-analyzer"
"rust-src"
];
})
];

inputsFrom = [ ];
buildInputs = [
openssl.dev
pkg-config
(rust-bin.stable.latest.default.override {
extensions = [
"rust-analyzer"
"rust-src"
];
})
];

postShellHook = ''
export PATH=$PATH:~/.cargo/bin
Expand Down
Loading