Hub

Struct Hub 

Source
pub struct Hub { /* private fields */ }
Expand description

Manages interactions with GitHub repository releases and Hugging Face repositories

§Format Rules

  • GitHub Release

    • Use <tag>/<file> format.
    • Example: "yolo/v5-n-det.onnx".
  • Hugging Face

    • With Hub::from_hf(owner, repo): File paths are interpreted relative to the repo root.
      • Example: "sentencepiece.bpe.model", "onnx/tokenizer.json".
    • With Hub::default(): Paths with three segments are interpreted as <owner>/<repo>/<file>.
      • Example: "BAAI/bge-m3/sentencepiece.bpe.model".

§Examples

§GitHub Release Download

let mut hub = Hub::default();
// let mut hub = Hub::new(owner, repo);  // Optional: Specify owner and repo if not using default
let path = hub.try_fetch("images/bus.jpg")?; // <tag>/<file> format

§Hugging Face Download (Dedicated Hub)

let mut hub = Hub::from_hf("BAAI", "bge-m3")?;
let path = hub.try_fetch("sentencepiece.bpe.model")?; // Any format works
let path = hub.try_fetch("onnx/tokenizer.json")?; // Any format works

§Hugging Face Download (Temporary)

let mut hub = Hub::default().try_fetch("BAAI/bge-m3/tokenizer_config.json")?; // <owner>/<repo>/<file> format

§Errors

Methods in Hub return Result types. Errors may occur due to invalid paths, failed network requests, cache write failures, or mismatched file sizes during downloads.

Implementations§

Source§

impl Hub

Source

pub fn new(owner: &str, repo: &str) -> Self

Source

pub fn from_hf(_owner: &str, _repo: &str) -> Result<Self>

Source

pub fn try_fetch(&mut self, s: &str) -> Result<String>

Attempts to fetch a file from a local path, GitHub release, or Hugging Face repository.

The try_fetch method supports multiple scenarios:

  1. Local file: If the provided string is a valid file path, the file is returned without downloading.
  2. GitHub release URL: If the input matches a valid GitHub release URL, the corresponding file is downloaded.
  3. Hugging Face repository: If the hub is configured for HF or the path contains HF format, files are downloaded from HF.
  4. Default repository: If no explicit URL is provided, the method uses the default or configured repository.
§Parameters
  • s: A string representing the file to fetch. This can be:
    • A local file path.
    • A GitHub release URL (e.g., https://github.com/owner/repo/releases/download/tag/file).
    • A <tag>/<file> format for fetching from the default GitHub repository.
    • A HF repository file path (e.g., "sentencepiece.bpe.model" when using from_hf).
    • A temporary HF path format (e.g., "BAAI/bge-m3/sentencepiece.bpe.model").
§Returns
  • Result<String>: On success, returns the path to the fetched file.
§Errors
  • Returns an error if:
    • The file cannot be found locally.
    • The URL or tag is invalid.
    • Network operations fail after the maximum retry attempts.
    • HF repository access fails.
§Examples
let mut hub = Hub::default();

// Fetch a file from a local path
let local_path = hub.try_fetch("local/path/to/file").expect("File not found");

// Fetch a file from a GitHub release URL
let url_path = hub.try_fetch("https://github.com/owner/repo/releases/download/tag/file")
    .expect("Failed to fetch file");

// Fetch a file using the default GitHub repository
let default_repo_path = hub.try_fetch("yolo/v5-n-det.onnx").expect("Failed to fetch file");

// Method 1: Fetch from HF repository using dedicated hub
let mut hf_hub = Hub::from_hf("BAAI", "bge-m3")?;
let hf_path = hf_hub.try_fetch("sentencepiece.bpe.model").expect("Failed to fetch HF file");

// Method 2: Fetch from HF repository using temporary path (doesn't change hub's owner/repo)
let temp_hf_path = Hub::default().try_fetch("BAAI/bge-m3/sentencepiece.bpe.model")
    .expect("Failed to fetch HF file");
Source

pub fn tags(&self) -> Vec<String>

Source

pub fn files(&self, tag: &str) -> Vec<String>

Source

pub fn is_file_expired<P: AsRef<Path>>(file: P, ttl: &Duration) -> Result<bool>

Source

pub fn download<P: AsRef<Path> + Debug>( &self, src: &str, dst: P, message: Option<&str>, ) -> Result<()>

Download a file from a github release to a specified path with a progress bar

Source

pub fn is_valid_github_release_url( url: &str, ) -> Option<(String, String, String, String)>

Source

pub fn with_owner(self, owner: &str) -> Self

Source

pub fn with_repo(self, repo: &str) -> Self

Source

pub fn with_ttl(self, x: u64) -> Self

Source

pub fn with_max_attempts(self, x: u32) -> Self

Source

pub fn info(&self) -> Result<()>

Displays repository information including files and releases.

For Hugging Face repositories, shows commit SHA and file tree. For GitHub repositories, shows releases with file counts and provides usage examples.

Trait Implementations§

Source§

impl Debug for Hub

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Hub

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Hub

§

impl RefUnwindSafe for Hub

§

impl Send for Hub

§

impl Sync for Hub

§

impl Unpin for Hub

§

impl UnwindSafe for Hub

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, 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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