{"id":31959545,"url":"https://github.com/huggingface/swift-transformers","last_synced_at":"2026-04-02T10:34:18.455Z","repository":{"id":187036537,"uuid":"639378200","full_name":"huggingface/swift-transformers","owner":"huggingface","description":"Swift Package to implement a transformers-like API in Swift","archived":false,"fork":false,"pushed_at":"2025-09-23T16:46:14.000Z","size":5290,"stargazers_count":1103,"open_issues_count":21,"forks_count":137,"subscribers_count":30,"default_branch":"main","last_synced_at":"2025-09-23T18:09:27.608Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/huggingface.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-05-11T10:56:48.000Z","updated_at":"2025-09-23T15:58:54.000Z","dependencies_parsed_at":"2023-12-04T21:26:17.506Z","dependency_job_id":"2b8857fb-1449-4973-9687-e6058623f50c","html_url":"https://github.com/huggingface/swift-transformers","commit_stats":null,"previous_names":["huggingface/swift-transformers"],"tags_count":25,"template":false,"template_full_name":null,"purl":"pkg:github/huggingface/swift-transformers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huggingface%2Fswift-transformers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huggingface%2Fswift-transformers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huggingface%2Fswift-transformers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huggingface%2Fswift-transformers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/huggingface","download_url":"https://codeload.github.com/huggingface/swift-transformers/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huggingface%2Fswift-transformers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279019320,"owners_count":26086711,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-10-14T15:32:26.053Z","updated_at":"2026-04-02T10:34:18.422Z","avatar_url":"https://github.com/huggingface.png","language":"Swift","funding_links":[],"categories":["Agentic AI ![](https://img.shields.io/badge/_-AGENTIC-22d3ee?style=flat-square\u0026logo=openai\u0026logoColor=white)","Infrastructure / Deployment of LLMs on Device"],"sub_categories":["Apple Silicon optimized (MLX / CoreML / ANE)","Deployment Frameworks"],"readme":"\u003cp align=\"center\"\u003e\n  \u003cpicture\u003e\n    \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"media/swift-t-banner.png\"\u003e\n    \u003csource media=\"(prefers-color-scheme: light)\" srcset=\"media/swift-t-banner.png\"\u003e\n    \u003cimg alt=\"Swift + Transformers\" src=\"media/swift-t-banner.png\" style=\"max-width: 100%;\"\u003e\n  \u003c/picture\u003e\n  \u003cbr/\u003e\n  \u003cbr/\u003e\n\u003c/p\u003e\n\n[![Unit Tests](https://github.com/huggingface/swift-transformers/actions/workflows/ci.yml/badge.svg)](https://github.com/huggingface/swift-transformers/actions/workflows/unit-tests.yml)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fhuggingface%2Fswift-transformers%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/huggingface/swift-transformers)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fhuggingface%2Fswift-transformers%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/huggingface/swift-transformers)\n\n`swift-transformers` is a collection of utilities to help adopt language models in Swift apps. \n\nThose familiar with the [`transformers`](https://github.com/huggingface/transformers) Python library will find a familiar yet idiomatic Swift API.\n\n## Rationale \u0026 Overview\n\nCheck out [our v1.0 release post](https://huggingface.co/blog/swift-transformers) and our [original announcement](https://huggingface.co/blog/swift-coreml-llm) for more context on why we built this library.\n\n## Examples\n\nThe most commonly used modules from `swift-transformers` are `Tokenizers` and `Hub`, which allow fast tokenization and\nmodel downloads from the Hugging Face Hub.\n\n### Tokenizing text + chat templating\n\nTokenizing text should feel very familiar to those who have used the Python `transformers` library:\n\n```swift\nlet tokenizer = try await AutoTokenizer.from(pretrained: \"deepseek-ai/DeepSeek-R1-Distill-Qwen-7B\")\nlet messages = [[\"role\": \"user\", \"content\": \"Describe the Swift programming language.\"]]\nlet encoded = try tokenizer.applyChatTemplate(messages: messages)\nlet decoded = tokenizer.decode(tokens: encoded)\n```\n\n\n### Tool calling\n\n`swift-transformers` natively supports formatting inputs for tool calling, allowing for complex interactions with language models:\n\n```swift\nlet tokenizer = try await AutoTokenizer.from(pretrained: \"mlx-community/Qwen2.5-7B-Instruct-4bit\")\n\nlet weatherTool = [\n    \"type\": \"function\",\n    \"function\": [\n        \"name\": \"get_current_weather\",\n        \"description\": \"Get the current weather in a given location\",\n        \"parameters\": [\n            \"type\": \"object\",\n            \"properties\": [\"location\": [\"type\": \"string\", \"description\": \"City and state\"]],\n            \"required\": [\"location\"]\n        ]\n    ]\n]\n\nlet tokens = try tokenizer.applyChatTemplate(\n    messages: [[\"role\": \"user\", \"content\": \"What's the weather in Paris?\"]],\n    tools: [weatherTool]\n)\n```\n\n\n### Hub downloads\n\nDownloading models to a user device _fast_ and _reliably_ is a core requirement of on-device ML. `swift-transformers` provides a simple API to\ndownload models from the Hugging Face Hub, with progress reporting, flaky connection handling, and more:\n\n```swift\nlet repo = Hub.Repo(id: \"mlx-community/Qwen2.5-0.5B-Instruct-2bit-mlx\")\nlet modelDirectory: URL = try await Hub.snapshot(\n    from: repo,\n    matching: [\"config.json\", \"*.safetensors\"],\n    progressHandler: { progress in\n        print(\"Download progress: \\(progress.fractionCompleted * 100)%\")\n    }\n)\nprint(\"Files downloaded to: \\(modelDirectory.path)\")\n```\n\n### CoreML Integration\n\nThe `Models` and `Generation` modules provide handy utilities when working with language models in CoreML. Check out our\nexample converting and running Mistral 7B using CoreML [here](https://github.com/huggingface/swift-transformers/tree/main/Examples).\n\nThe [modernization of Core ML](https://github.com/huggingface/swift-transformers/pull/257) and corresponding examples were primarily contributed by @joshnewnham, @1duo, @alejandro-isaza, @aseemw. Thank you 🙏\n\n### Offline CoreML tokenizers\n\nWhen you bundle a compiled CoreML model and tokenizer files with your app, you can skip any network requests by injecting\nthe tokenizer when constructing `LanguageModel`:\n\n```swift\nlet compiledURL: URL = ... // path to .mlmodelc\nlet tokenizerFolder: URL = ... // folder containing tokenizer_config.json and tokenizer.json\n\n// Construct the tokenizer from local files (inside an async context)\nlet tokenizer = try await AutoTokenizer.from(modelFolder: tokenizerFolder)\nlet model = try LanguageModel.loadCompiled(\n    url: compiledURL,\n    tokenizer: tokenizer\n)\n```\n\nMake sure the tokenizer assets come from the same Hugging Face repo as the original checkpoint or are compatible with the model you use. For the\nMistral example in `Examples/Mistral7B/`, you can fetch the tokenizer like this:\n\n```bash\nhuggingface-cli download \\\n  mistralai/Mistral-7B-Instruct-v0.3 \\\n  tokenizer.json tokenizer_config.json \\\n  --local-dir Examples/Mistral7B/local-tokenizer\n```\n\nIf the repo is gated, authenticate with `huggingface-cli login` first. Both initializers reuse the tokenizer\nyou pass in and never reach out to the Hugging Face Hub.\n\n## Usage via SwiftPM\n\nTo use `swift-transformers` with SwiftPM, you can add this to your `Package.swift`:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/huggingface/swift-transformers\", from: \"1.3.0\")\n]\n```\n\nAnd then, add the Transformers library as a dependency to your target:\n\n```swift\ntargets: [\n    .target(\n        name: \"YourTargetName\",\n        dependencies: [\n            .product(name: \"Transformers\", package: \"swift-transformers\")\n        ]\n    )\n]\n```\n\n### Optional Xet trait\n\n`swift-transformers` includes an `Xet` [package trait](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0450-swiftpm-package-traits.md)\nthat enables fast, parallel downloads from the Hugging Face Hub\nvia [swift-xet](https://github.com/huggingface/swift-xet).\nBecause Xet introduces additional transitive dependencies\n(including [AsyncHTTPClient](https://github.com/swift-server/async-http-client)),\nit is opt-in.\n\nOn Swift 6.1+, enable it in your `Package.swift`:\n\n```swift\ndependencies: [\n    .package(\n        url: \"https://github.com/huggingface/swift-transformers\",\n        from: \"1.3.0\",\n        traits: [\"Xet\"]\n    )\n]\n```\n\nOr from the command line:\n\n```bash\nswift build --traits Xet\nswift test --traits Xet\n```\n\nWhen the trait is not enabled,\nHub downloads use the default `URLSession`-based transport.\n\nOn Swift versions earlier than 6.1,\npackage traits are unavailable and Xet transport cannot be enabled.\n\n\u003e [!NOTE]\n\u003e Xcode doesn't yet provide a built-in way to declare package dependencies with traits.\n\u003e As a workaround, you can create an internal Swift package\n\u003e that re-exports `swift-transformers` with the `Xet` trait enabled,\n\u003e then add that package as a local dependency in your Xcode project.\n\n## Projects that use swift-transformers ❤️ \n\n- [WhisperKit](https://github.com/argmaxinc/WhisperKit): A Swift Package for state-of-the-art speech-to-text systems from [Argmax](https://github.com/argmaxinc)\n- [MLX Swift Examples](https://github.com/ml-explore/mlx-swift-examples): A Swift Package for integrating MLX models in Swift apps.\n\nUsing `swift-transformers` in your project? Let us know and we'll add you to the list!\n\n## Other Tools\n\n- [`swift-chat`](https://github.com/huggingface/swift-chat), a simple app demonstrating how to use this package.\n- [`exporters`](https://github.com/huggingface/exporters), a Core ML conversion package for transformers models, based on Apple's [`coremltools`](https://github.com/apple/coremltools).\n\n## Contributing \n\nSwift Transformers is a community project and we welcome contributions. Please\ncheck out [Issues](https://github.com/huggingface/swift-transformers/issues)\ntagged with `good first issue` if you are looking for a place to start!\n\nBefore submitting a pull request, please ensure your code:\n\n- Passes the test suite (`swift test`)\n- Passes linting checks (`swift format lint --recursive .`)\n\nTo format your code, run `swift format -i --recursive .`.\n\n## License\n\n[Apache 2](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuggingface%2Fswift-transformers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhuggingface%2Fswift-transformers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuggingface%2Fswift-transformers/lists"}