pub struct ApiRepo { /* private fields */ }
Expand description
Shorthand for accessing things within a particular repo
You can inspect repos with ApiRepo::info
or download files with ApiRepo::download
Implementations§
Source§impl ApiRepo
impl ApiRepo
Sourcepub fn url(&self, filename: &str) -> String
pub fn url(&self, filename: &str) -> String
Get the fully qualified URL of the remote filename
let api = Api::new().unwrap();
let url = api.model("gpt2".to_string()).url("model.safetensors");
assert_eq!(url, "https://huggingface.co/gpt2/resolve/main/model.safetensors");
Sourcepub async fn get(&self, filename: &str) -> Result<PathBuf, ApiError>
pub async fn get(&self, filename: &str) -> Result<PathBuf, ApiError>
This will attempt the fetch the file locally first, then [Api.download
]
if the file is not present.
let api = Api::new().unwrap();
let local_filename = api.model("gpt2".to_string()).get("model.safetensors").await.unwrap();
Sourcepub async fn download(&self, filename: &str) -> Result<PathBuf, ApiError>
pub async fn download(&self, filename: &str) -> Result<PathBuf, ApiError>
Downloads a remote file (if not already present) into the cache directory to be used locally. This functions require internet access to verify if new versions of the file exist, even if a file is already on disk at location.
let api = Api::new().unwrap();
let local_filename = api.model("gpt2".to_string()).download("model.safetensors").await.unwrap();
Sourcepub async fn download_with_progress<P: Progress + Clone + Send + Sync + 'static>(
&self,
filename: &str,
progress: P,
) -> Result<PathBuf, ApiError>
pub async fn download_with_progress<P: Progress + Clone + Send + Sync + 'static>( &self, filename: &str, progress: P, ) -> Result<PathBuf, ApiError>
This function is used to download a file with a custom progress function.
It uses the Progress
trait and can be used in more complex use
cases like downloading a showing progress in a UI.
use hf_hub::api::tokio::{Api, Progress};
#[derive(Clone)]
struct MyProgress{
current: usize,
total: usize
}
impl Progress for MyProgress{
async fn init(&mut self, size: usize, _filename: &str){
self.total = size;
self.current = 0;
}
async fn update(&mut self, size: usize){
self.current += size;
println!("{}/{}", self.current, self.total)
}
async fn finish(&mut self){
println!("Done !");
}
}
let api = Api::new().unwrap();
let progress = MyProgress{ current: 0, total : 0};
let local_filename = api.model("gpt2".to_string()).download_with_progress("model.safetensors", progress).await.unwrap();
Sourcepub async fn info(&self) -> Result<RepoInfo, ApiError>
pub async fn info(&self) -> Result<RepoInfo, ApiError>
Get information about the Repo
let api = Api::new().unwrap();
api.model("gpt2".to_string()).info();
Sourcepub fn info_request(&self) -> RequestBuilder
pub fn info_request(&self) -> RequestBuilder
Get the raw reqwest::RequestBuilder
with the url and method already set
let api = Api::new().unwrap();
api.model("gpt2".to_owned())
.info_request()
.query(&[("blobs", "true")])
.send()
.await;