hf_hub::api::tokio

Struct ApiRepo

Source
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

Source

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");
Source

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();
Source

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();
Source

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();
Source

pub async fn info(&self) -> Result<RepoInfo, ApiError>

Get information about the Repo

let api = Api::new().unwrap();
api.model("gpt2".to_string()).info();
Source

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;

Trait Implementations§

Source§

impl Debug for ApiRepo

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T