1414 * limitations under the License.
1515 */
1616
17- package hfhub
17+ package huggingface
1818
1919import (
2020 "context"
@@ -29,11 +29,11 @@ import (
2929)
3030
3131const (
32- HuggingFaceBaseURL = "https://huggingface.co"
32+ huggingFaceBaseURL = "https://huggingface.co"
3333)
3434
35- // ParseModelURL parses a Hugging Face model URL and extracts the owner and repository name
36- func ParseModelURL (modelURL string ) (owner , repo string , err error ) {
35+ // parseModelURL parses a HuggingFace model URL and extracts the owner and repository name
36+ func parseModelURL (modelURL string ) (owner , repo string , err error ) {
3737 // Handle both full URLs and short-form repo names
3838 modelURL = strings .TrimSpace (modelURL )
3939
@@ -50,7 +50,7 @@ func ParseModelURL(modelURL string) (owner, repo string, err error) {
5050 // Expected format: https://huggingface.co/owner/repo
5151 parts := strings .Split (strings .Trim (u .Path , "/" ), "/" )
5252 if len (parts ) < 2 {
53- return "" , "" , fmt .Errorf ("invalid Hugging Face URL format, expected https://huggingface.co/owner/repo" )
53+ return "" , "" , fmt .Errorf ("invalid HuggingFace URL format, expected https://huggingface.co/owner/repo" )
5454 }
5555
5656 owner = parts [0 ]
@@ -73,45 +73,8 @@ func ParseModelURL(modelURL string) (owner, repo string, err error) {
7373 return owner , repo , nil
7474}
7575
76- // DownloadModel downloads a model from Hugging Face using the huggingface-cli
77- // It assumes the user is already logged in via `huggingface-cli login`
78- func DownloadModel (ctx context.Context , modelURL , destDir string ) (string , error ) {
79- owner , repo , err := ParseModelURL (modelURL )
80- if err != nil {
81- return "" , err
82- }
83-
84- repoID := fmt .Sprintf ("%s/%s" , owner , repo )
85-
86- // Check if huggingface-cli is available
87- if _ , err := exec .LookPath ("huggingface-cli" ); err != nil {
88- return "" , fmt .Errorf ("huggingface-cli not found in PATH. Please install it using: pip install huggingface_hub[cli]" )
89- }
90-
91- // Create destination directory if it doesn't exist
92- if err := os .MkdirAll (destDir , 0755 ); err != nil {
93- return "" , fmt .Errorf ("failed to create destination directory: %w" , err )
94- }
95-
96- // Construct the download path
97- downloadPath := filepath .Join (destDir , repo )
98-
99- // Use huggingface-cli to download the model
100- // The --local-dir-use-symlinks=False flag ensures files are copied, not symlinked
101- cmd := exec .CommandContext (ctx , "huggingface-cli" , "download" , repoID , "--local-dir" , downloadPath , "--local-dir-use-symlinks" , "False" )
102-
103- cmd .Stdout = os .Stdout
104- cmd .Stderr = os .Stderr
105-
106- if err := cmd .Run (); err != nil {
107- return "" , fmt .Errorf ("failed to download model using huggingface-cli: %w" , err )
108- }
109-
110- return downloadPath , nil
111- }
112-
113- // CheckHuggingFaceAuth checks if the user is authenticated with Hugging Face
114- func CheckHuggingFaceAuth () error {
76+ // checkHuggingFaceAuth checks if the user is authenticated with HuggingFace
77+ func checkHuggingFaceAuth () error {
11578 // Try to find the HF token
11679 token := os .Getenv ("HF_TOKEN" )
11780 if token != "" {
@@ -139,11 +102,11 @@ func CheckHuggingFaceAuth() error {
139102 }
140103 }
141104
142- return fmt .Errorf ("not authenticated with Hugging Face . Please run: huggingface-cli login" )
105+ return fmt .Errorf ("not authenticated with HuggingFace . Please run: huggingface-cli login" )
143106}
144107
145- // GetToken retrieves the Hugging Face token from environment or token file
146- func GetToken () (string , error ) {
108+ // getToken retrieves the HuggingFace token from environment or token file
109+ func getToken () (string , error ) {
147110 // First check environment variable
148111 token := os .Getenv ("HF_TOKEN" )
149112 if token != "" {
@@ -165,16 +128,16 @@ func GetToken() (string, error) {
165128 return strings .TrimSpace (string (data )), nil
166129}
167130
168- // DownloadFile downloads a single file from Hugging Face
169- func DownloadFile (ctx context.Context , owner , repo , filename , destPath string ) error {
170- token , err := GetToken ()
131+ // downloadFile downloads a single file from HuggingFace
132+ func downloadFile (ctx context.Context , owner , repo , filename , destPath string ) error {
133+ token , err := getToken ()
171134 if err != nil {
172- return fmt .Errorf ("failed to get Hugging Face token: %w" , err )
135+ return fmt .Errorf ("failed to get HuggingFace token: %w" , err )
173136 }
174137
175138 // Construct the download URL
176139 // Format: https://huggingface.co/{owner}/{repo}/resolve/main/{filename}
177- fileURL := fmt .Sprintf ("%s/%s/%s/resolve/main/%s" , HuggingFaceBaseURL , owner , repo , filename )
140+ fileURL := fmt .Sprintf ("%s/%s/%s/resolve/main/%s" , huggingFaceBaseURL , owner , repo , filename )
178141
179142 req , err := http .NewRequestWithContext (ctx , "GET" , fileURL , nil )
180143 if err != nil {
0 commit comments