|
| 1 | +/* |
| 2 | + * Copyright 2025 The CNAI Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package hfhub |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "net/http" |
| 24 | + "net/url" |
| 25 | + "os" |
| 26 | + "os/exec" |
| 27 | + "path/filepath" |
| 28 | + "strings" |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + HuggingFaceBaseURL = "https://huggingface.co" |
| 33 | +) |
| 34 | + |
| 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) { |
| 37 | + // Handle both full URLs and short-form repo names |
| 38 | + modelURL = strings.TrimSpace(modelURL) |
| 39 | + |
| 40 | + // Remove trailing slashes |
| 41 | + modelURL = strings.TrimSuffix(modelURL, "/") |
| 42 | + |
| 43 | + // If it's a full URL, parse it |
| 44 | + if strings.HasPrefix(modelURL, "http://") || strings.HasPrefix(modelURL, "https://") { |
| 45 | + u, err := url.Parse(modelURL) |
| 46 | + if err != nil { |
| 47 | + return "", "", fmt.Errorf("invalid URL: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + // Expected format: https://huggingface.co/owner/repo |
| 51 | + parts := strings.Split(strings.Trim(u.Path, "/"), "/") |
| 52 | + if len(parts) < 2 { |
| 53 | + return "", "", fmt.Errorf("invalid Hugging Face URL format, expected https://huggingface.co/owner/repo") |
| 54 | + } |
| 55 | + |
| 56 | + owner = parts[0] |
| 57 | + repo = parts[1] |
| 58 | + } else { |
| 59 | + // Handle short-form like "owner/repo" |
| 60 | + parts := strings.Split(modelURL, "/") |
| 61 | + if len(parts) != 2 { |
| 62 | + return "", "", fmt.Errorf("invalid model identifier, expected format: owner/repo") |
| 63 | + } |
| 64 | + |
| 65 | + owner = parts[0] |
| 66 | + repo = parts[1] |
| 67 | + } |
| 68 | + |
| 69 | + if owner == "" || repo == "" { |
| 70 | + return "", "", fmt.Errorf("owner and repository name cannot be empty") |
| 71 | + } |
| 72 | + |
| 73 | + return owner, repo, nil |
| 74 | +} |
| 75 | + |
| 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 | + fmt.Printf("Downloading model %s to %s...\n", repoID, downloadPath) |
| 107 | + |
| 108 | + if err := cmd.Run(); err != nil { |
| 109 | + return "", fmt.Errorf("failed to download model using huggingface-cli: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + fmt.Printf("Successfully downloaded model to %s\n", downloadPath) |
| 113 | + |
| 114 | + return downloadPath, nil |
| 115 | +} |
| 116 | + |
| 117 | +// CheckHuggingFaceAuth checks if the user is authenticated with Hugging Face |
| 118 | +func CheckHuggingFaceAuth() error { |
| 119 | + // Try to find the HF token |
| 120 | + token := os.Getenv("HF_TOKEN") |
| 121 | + if token != "" { |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + // Check if the token file exists |
| 126 | + homeDir, err := os.UserHomeDir() |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("failed to get user home directory: %w", err) |
| 129 | + } |
| 130 | + |
| 131 | + tokenPath := filepath.Join(homeDir, ".huggingface", "token") |
| 132 | + if _, err := os.Stat(tokenPath); err == nil { |
| 133 | + return nil |
| 134 | + } |
| 135 | + |
| 136 | + // Try using whoami command |
| 137 | + if _, err := exec.LookPath("huggingface-cli"); err == nil { |
| 138 | + cmd := exec.Command("huggingface-cli", "whoami") |
| 139 | + if err := cmd.Run(); err == nil { |
| 140 | + return nil |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return fmt.Errorf("not authenticated with Hugging Face. Please run: huggingface-cli login") |
| 145 | +} |
| 146 | + |
| 147 | +// GetToken retrieves the Hugging Face token from environment or token file |
| 148 | +func GetToken() (string, error) { |
| 149 | + // First check environment variable |
| 150 | + token := os.Getenv("HF_TOKEN") |
| 151 | + if token != "" { |
| 152 | + return token, nil |
| 153 | + } |
| 154 | + |
| 155 | + // Then check the token file |
| 156 | + homeDir, err := os.UserHomeDir() |
| 157 | + if err != nil { |
| 158 | + return "", fmt.Errorf("failed to get user home directory: %w", err) |
| 159 | + } |
| 160 | + |
| 161 | + tokenPath := filepath.Join(homeDir, ".huggingface", "token") |
| 162 | + data, err := os.ReadFile(tokenPath) |
| 163 | + if err != nil { |
| 164 | + return "", fmt.Errorf("failed to read token file: %w", err) |
| 165 | + } |
| 166 | + |
| 167 | + return strings.TrimSpace(string(data)), nil |
| 168 | +} |
| 169 | + |
| 170 | +// DownloadFile downloads a single file from Hugging Face |
| 171 | +func DownloadFile(ctx context.Context, owner, repo, filename, destPath string) error { |
| 172 | + token, err := GetToken() |
| 173 | + if err != nil { |
| 174 | + return fmt.Errorf("failed to get Hugging Face token: %w", err) |
| 175 | + } |
| 176 | + |
| 177 | + // Construct the download URL |
| 178 | + // Format: https://huggingface.co/{owner}/{repo}/resolve/main/{filename} |
| 179 | + fileURL := fmt.Sprintf("%s/%s/%s/resolve/main/%s", HuggingFaceBaseURL, owner, repo, filename) |
| 180 | + |
| 181 | + req, err := http.NewRequestWithContext(ctx, "GET", fileURL, nil) |
| 182 | + if err != nil { |
| 183 | + return fmt.Errorf("failed to create request: %w", err) |
| 184 | + } |
| 185 | + |
| 186 | + // Add authorization header |
| 187 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) |
| 188 | + |
| 189 | + client := &http.Client{} |
| 190 | + resp, err := client.Do(req) |
| 191 | + if err != nil { |
| 192 | + return fmt.Errorf("failed to download file: %w", err) |
| 193 | + } |
| 194 | + defer resp.Body.Close() |
| 195 | + |
| 196 | + if resp.StatusCode != http.StatusOK { |
| 197 | + return fmt.Errorf("failed to download file, status code: %d", resp.StatusCode) |
| 198 | + } |
| 199 | + |
| 200 | + // Create destination directory |
| 201 | + destDir := filepath.Dir(destPath) |
| 202 | + if err := os.MkdirAll(destDir, 0755); err != nil { |
| 203 | + return fmt.Errorf("failed to create destination directory: %w", err) |
| 204 | + } |
| 205 | + |
| 206 | + // Create the destination file |
| 207 | + outFile, err := os.Create(destPath) |
| 208 | + if err != nil { |
| 209 | + return fmt.Errorf("failed to create destination file: %w", err) |
| 210 | + } |
| 211 | + defer outFile.Close() |
| 212 | + |
| 213 | + // Copy the content |
| 214 | + _, err = io.Copy(outFile, resp.Body) |
| 215 | + if err != nil { |
| 216 | + return fmt.Errorf("failed to write file: %w", err) |
| 217 | + } |
| 218 | + |
| 219 | + return nil |
| 220 | +} |
0 commit comments