huggingface / swift-transformers

Swift Package to implement a transformers-like API in Swift
Apache License 2.0
638 stars 63 forks source link

Option to use `~/.cache/huggingface/hub` for downloaded models #102

Open DePasqualeOrg opened 2 months ago

DePasqualeOrg commented 2 months ago

The Python transformers library downloads models to ~/.cache/huggingface/hub. This allows reuse of previously downloaded models by multiple apps. Currently, calling HubApi.shapshot in swift-transformers downloads the models to a specified directory, with a different directory structure than the one used by Python transformers. As more apps get built with these libraries, it would be great if swift-transformers and Python transformers could share models in the user's home directory on macOS. (If I understand correctly, this isn't possible on iOS, although apps within the same app group can save models to the app group's shared directory.)

DePasqualeOrg commented 1 month ago

Users of my app, which uses MLX and swift-transformers, are requesting to be able to reuse models downloaded from other apps, and the Hugging Face cache directory seems like a natural choice.

https://x.com/Andreadful_/status/1813156367324057611

https://x.com/vincentbosch/status/1813131165437448646

@pcuenca, any idea how this could be approached, given that swift-transformers and the Python transformers library use a different directory structure for storing the models?

terribilissimo commented 1 month ago

Andreadful_ here. Of course the solution you laid out above would be the neatest, but I could live with just being able to set a directory of choice for swift-transformers' models (that is, I wouldn't mind too much if swift-transformers requires its own directory structure and downloads duplicates of the same models put into the HF cache by python-transformers). With HF python transformers it's quite easy, you just have to set the HF_HOME env var. Question: where does swift-transformers download the models by default?? Asking for Mac OS, not iOS.

DePasqualeOrg commented 1 month ago

@terribilissimo, you can find it here. It is already possible to define a download directory: https://github.com/huggingface/swift-transformers/blob/main/Sources/Hub/HubApi.swift

Ideally swift-transformers should be able to share models with the Python transformers library. I can imagine people will be building macOS apps with the Python and Swift implementations, and currently they use different conventions for saving the model files and so cannot share models.