HyperAIHyperAI

Command Palette

Search for a command to run...

Introducing swift-huggingface: A Robust Swift Client for Hugging Face with Secure Auth, Resumable Downloads, and Python Cache Compatibility

Introducing swift-huggingface, a new Swift package that delivers a complete and modern client for the Hugging Face Hub. Available today as a standalone package, it will soon replace the current HubApi implementation in swift-transformers, bringing significant improvements in reliability, performance, and developer experience. The project was built in response to community feedback following the release of swift-transformers 1.0. Users highlighted issues with authentication, unreliable downloads, lack of progress tracking, and poor integration with the broader Python ecosystem. swift-huggingface addresses each of these challenges with a ground-up redesign. One of the key advancements is the flexible TokenProvider system, which makes authentication clear and secure. Developers can choose the appropriate token source depending on the environment: For development, the client automatically detects tokens from environment variables like HF_TOKEN or HUGGING_FACE_HUB_TOKEN, or from standard locations such as ~/.cache/huggingface/token. In CI/CD pipelines, tokens can be passed explicitly via .static("hf_xxx"). For production apps, tokens are securely stored in the Keychain using .keychain(service: "com.myapp", account: "hf_token"). This approach mirrors the behavior of the Python huggingface_hub library, ensuring consistency across platforms. For user-facing applications, swift-huggingface includes a full OAuth 2.0 implementation. The HuggingFaceAuthenticationManager handles sign-in via the system browser, secure token storage in the Keychain, automatic refresh, and safe sign-out. Once authenticated, the token can be seamlessly used with the HubClient, with no need for manual management. Downloads are now robust and reliable. The package supports progress tracking, resume capability, and intelligent caching. Large model downloads can be monitored in real time: let progress = Progress(totalUnitCount: 0) Task { for await _ in progress.publisher(for: .fractionCompleted).values { print("Download: (Int(progress.fractionCompleted * 100))%") } } let fileURL = try await client.downloadFile( at: "model.safetensors", from: "microsoft/phi-2", to: destinationURL, progress: progress ) If interrupted, downloads can be resumed using saved resume data. For entire model repositories, downloadSnapshot handles all files with intelligent metadata tracking—only downloading changed files on subsequent calls. A major breakthrough is the shared cache with Python. swift-huggingface uses the same cache structure as the Python library, located at ~/.cache/huggingface/hub. This allows seamless sharing of cached models between Swift and Python workflows. The cache respects standard environment variables like HF_HUB_CACHE and HF_HOME, and uses file locking to prevent race conditions. Beyond downloads, swift-huggingface offers a full suite of Hub interactions: listing trending models, retrieving model details, managing collections, and accessing discussions. It also integrates with Hugging Face Inference Providers, enabling direct access to hundreds of machine learning models for tasks like text-to-image generation. let response = try await client.textToImage( model: "black-forest-labs/FLUX.1-schnell", prompt: "A serene Japanese garden with cherry blossoms", provider: .hfInference, width: 1024, height: 1024, numImages: 1, guidanceScale: 7.5, numInferenceSteps: 50, seed: 42 ) try response.image.write(to: URL(fileURLWithPath: "generated.png")) Looking ahead, swift-huggingface is being integrated into swift-transformers via an ongoing pull request. The team is also working on faster downloads using the Xet storage backend, which enables chunk-based deduplication and improved performance for large models. Developers can add swift-huggingface to their projects using Swift Package Manager: dependencies: [ .package(url: "https://github.com/huggingface/swift-huggingface.git", from: "0.4.0") ] The team welcomes feedback and contributions. If you're using Swift for AI and have struggled with model downloads, try it out and let them know how it works for you. Your input will help shape the next steps.

Related Links