Skip to content

Commit 172ef9c

Browse files
committed
add a partial commit implementation
supports: - small files - git lfs files - git lfs multipart files - model repos does not yet support: - dataset repos, spaces - file copies - file deletions - folder uploads - much of anything else
1 parent a66bdcf commit 172ef9c

File tree

12 files changed

+2544
-414
lines changed

12 files changed

+2544
-414
lines changed

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ ureq = { version = "2.8.0", optional = true, features = [
3636
"json",
3737
"socks-proxy",
3838
] }
39+
sha2 = "0.10"
40+
base64 = "0.22.1"
41+
hex = "0.4.3"
42+
regex = "1.11.1"
43+
lazy_static = "1.5.0"
44+
sha1 = "0.10.6"
45+
urlencoding = "2.1.3"
3946

4047
[features]
4148
default = ["default-tls", "tokio", "ureq"]
@@ -68,6 +75,8 @@ ureq = [
6875
]
6976

7077
[dev-dependencies]
78+
env_logger = "0.11.5"
7179
hex-literal = "0.4.1"
80+
rand = "0.8.5"
7281
sha2 = "0.10"
7382
tokio-test = "0.4.2"

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## temporary todo
2+
3+
to run the test, do `RUST_LOG=trace HF_TOKEN=token_here HF_REPO=repo_here cargo run --example upload --release`
4+
5+
## real readme here
6+
17
This crates aims to emulate and be compatible with the
28
[huggingface_hub](https://github.com/huggingface/huggingface_hub/) python package.
39

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

21-
22-
# How to use
27+
# How to use
2328

2429
Add the dependency
2530

2631
```bash
2732
cargo add hf-hub # --features tokio
2833
```
34+
2935
`tokio` feature will enable an async (and potentially faster) API.
3036

3137
Use the crate:

examples/upload.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use std::time::Instant;
2+
3+
use hf_hub::{api::tokio::ApiBuilder, Repo};
4+
use rand::Rng;
5+
6+
const ONE_MB: usize = 1024 * 1024;
7+
8+
#[tokio::main]
9+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
10+
env_logger::init();
11+
let token =
12+
std::env::var("HF_TOKEN").map_err(|_| format!("HF_TOKEN environment variable not set"))?;
13+
let hf_repo = std::env::var("HF_REPO")
14+
.map_err(|_| format!("HF_REPO environment variable not set, e.g. apyh/gronk"))?;
15+
16+
let api = ApiBuilder::new().with_token(Some(token)).build()?;
17+
let repo = Repo::model(hf_repo);
18+
let api_repo = api.repo(repo);
19+
for (filepath, contents) in [
20+
(
21+
"tiny_file.txt",
22+
format!("im a tiny file {:?}", Instant::now())
23+
.as_bytes()
24+
.to_vec(),
25+
),
26+
("1m_file.txt", {
27+
let mut data = vec![0u8; ONE_MB];
28+
rand::thread_rng().fill(&mut data[..]);
29+
data
30+
}),
31+
("10m_file.txt", {
32+
let mut data = vec![0u8; 10 * ONE_MB];
33+
rand::thread_rng().fill(&mut data[..]);
34+
data
35+
}),
36+
("20m_file.txt", {
37+
let mut data = vec![0u8; 20 * ONE_MB];
38+
rand::thread_rng().fill(&mut data[..]);
39+
data
40+
}),
41+
] {
42+
let res = api_repo
43+
.upload_file(
44+
contents,
45+
filepath,
46+
None,
47+
format!("update {}", filepath).into(),
48+
false,
49+
)
50+
.await?;
51+
log::info!("Uploaded file {:?}", filepath);
52+
log::info!("{:?}", res);
53+
log::info!("Success!!");
54+
}
55+
Ok(())
56+
}

0 commit comments

Comments
 (0)