Skip to content

Commit 7bbb7e1

Browse files
make hf api key optional (kubeflow#1963)
Signed-off-by: Adysen Rothman <85646824+adysenrothman@users.noreply.github.com>
1 parent 579e15e commit 7bbb7e1

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

catalog/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ The HuggingFace catalog source allows you to discover and import models from the
8282
8383
#### 1. Set Your API Key
8484
85-
The HuggingFace provider requires an API key for authentication. By default, the service reads the API key from the `HF_API_KEY` environment variable:
85+
Setting a Hugging Face API key is optional. Hugging Face requires an API key for authentication for full access to data of models that are private and/or gated. If an API key is NOT set, private models will be entirely unavailable and gated models will have limited metadata. By default, the service reads the API key from the `HF_API_KEY` environment variable:
8686

8787
```bash
8888
export HF_API_KEY="your-huggingface-api-key-here"

catalog/internal/catalog/hf_catalog.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -633,21 +633,31 @@ func newHFModelProvider(ctx context.Context, source *Source, reldir string) (<-c
633633
}
634634
apiKey := os.Getenv(apiKeyEnvVar)
635635
if apiKey == "" {
636-
return nil, fmt.Errorf("missing %s environment variable for HuggingFace catalog", apiKeyEnvVar)
636+
glog.Infof("No API key configured for Hugging Face. Only public models and limited data for gated models will be available.")
637637
}
638638
p.apiKey = apiKey
639639

640640
// Parse base URL (optional, defaults to huggingface.co)
641641
// This allows tests to use mock servers by providing a custom URL
642642
p.baseURL = defaultHuggingFaceURL
643643
if url, ok := source.Properties[urlKey].(string); ok && url != "" {
644-
p.baseURL = strings.TrimSuffix(url, "/") // Remove trailing slash if present
644+
p.baseURL = strings.TrimSuffix(url, "/")
645645
}
646646

647-
// Validate credentials before proceeding
648-
if err := p.validateCredentials(ctx); err != nil {
649-
glog.Errorf("HuggingFace catalog credential validation failed: %v", err)
650-
return nil, fmt.Errorf("failed to validate HuggingFace catalog credentials: %w", err)
647+
if p.apiKey != "" {
648+
hasValidPrefix := strings.HasPrefix(p.apiKey, "hf_")
649+
if !hasValidPrefix {
650+
// API key is set but doesn't have expected prefix, warn and continue without authentication
651+
glog.Infof("API key does not have expected 'hf_' prefix. Only public models and limited data for gated models will be available.")
652+
p.apiKey = "" // Clear invalid key to prevent its use
653+
}
654+
if hasValidPrefix {
655+
// Validate credentials only if API key has correct format
656+
if err := p.validateCredentials(ctx); err != nil {
657+
glog.Errorf("HuggingFace catalog credential validation failed: %v", err)
658+
return nil, fmt.Errorf("failed to validate HuggingFace catalog credentials: %w", err)
659+
}
660+
}
651661
}
652662

653663
// Use top-level IncludedModels from Source as the list of models to fetch

0 commit comments

Comments
 (0)