{"id":13695219,"url":"https://github.com/replicate/replicate-swift","last_synced_at":"2025-04-09T18:20:07.890Z","repository":{"id":64126709,"uuid":"543624057","full_name":"replicate/replicate-swift","owner":"replicate","description":"Swift client for Replicate","archived":false,"fork":false,"pushed_at":"2024-09-16T09:33:51.000Z","size":146,"stargazers_count":178,"open_issues_count":5,"forks_count":35,"subscribers_count":22,"default_branch":"main","last_synced_at":"2025-04-09T18:19:58.977Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://replicate.com","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/replicate.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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}},"created_at":"2022-09-30T13:51:03.000Z","updated_at":"2025-04-04T02:25:12.000Z","dependencies_parsed_at":"2023-11-16T15:32:03.912Z","dependency_job_id":"44ab1a02-2806-40e9-b5ab-dfcf493c362e","html_url":"https://github.com/replicate/replicate-swift","commit_stats":{"total_commits":92,"total_committers":5,"mean_commits":18.4,"dds":"0.14130434782608692","last_synced_commit":"efd033f4e421112b70e82221c813417e164aab7a"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/replicate%2Freplicate-swift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/replicate%2Freplicate-swift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/replicate%2Freplicate-swift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/replicate%2Freplicate-swift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/replicate","download_url":"https://codeload.github.com/replicate/replicate-swift/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248085324,"owners_count":21045139,"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","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":"2024-08-02T18:00:19.812Z","updated_at":"2025-04-09T18:20:07.852Z","avatar_url":"https://github.com/replicate.png","language":"Swift","funding_links":[],"categories":["Client libraries"],"sub_categories":[],"readme":"# Replicate Swift client\n\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Freplicate%2Freplicate-swift%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/replicate/replicate-swift)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Freplicate%2Freplicate-swift%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/replicate/replicate-swift)\n\nThis is a Swift client for [Replicate].\nIt lets you run models from your Swift code,\nand do various other things on Replicate.\n\nTo learn how to use it,\n[take a look at our guide to building a SwiftUI app with Replicate](https://replicate.com/docs/get-started/swiftui).\n\n## Usage\n\nGrab your API token from [replicate.com/account](https://replicate.com/account)\nand pass it to `Client(token:)`:\n\n```swift\nimport Foundation\nimport Replicate\n\nlet replicate = Replicate.Client(token: \u003c#token#\u003e)\n```\n\n\u003e [!WARNING]\n\u003e Don't store secrets in code or any other resources bundled with your app.\n\u003e Instead, fetch them from CloudKit or another server and store them in the keychain.\n\nYou can run a model and get its output:\n\n```swift\nlet output = try await replicate.run(\n    \"stability-ai/stable-diffusion-3\",\n    [\"prompt\": \"a 19th century portrait of a gentleman otter\"]\n) { prediction in\n    // Print the prediction status after each update\n    print(prediction.status)\n}\n\nprint(output)\n// [\"https://replicate.delivery/yhqm/bh9SsjWXY3pGKJyQzYjQlsZPzcNZ4EYOeEsPjFytc5TjYeNTA/R8_SD3_00001_.webp\"]\n```\n\nOr fetch a model by name and create a prediction against its latest version:\n\n```swift\nlet model = try await replicate.getModel(\"stability-ai/stable-diffusion-3\")\nif let latestVersion = model.latestVersion {\n    let prompt = \"a 19th century portrait of a gentleman otter\"\n    let prediction = try await replicate.createPrediction(version: latestVersion.id,\n                                                       input: [\"prompt\": \"\\(prompt)\"],\n                                                       wait: true)\n    print(prediction.id)\n    // \"s654jhww3hrm60ch11v8t3zpkg\"\n    print(prediction.output)\n    // [\"https://replicate.delivery/yhqm/bh9SsjWXY3pGKJyQzYjQlsZPzcNZ4EYOeEsPjFytc5TjYeNTA/R8_SD3_00001_.webp\"]\n}\n```\n\nSome models,\nlike [tencentarc/gfpgan](https://replicate.com/tencentarc/gfpgan),\nreceive images as inputs.\nTo run a model that takes a file input you can pass either\na URL to a publicly accessible file on the Internet\nor use the `uriEncoded(mimeType:) helper method to create\na base64-encoded data URL from the contents of a local file.\n\n```swift\nlet model = try await replicate.getModel(\"tencentarc/gfpgan\")\nif let latestVersion = model.latestVersion {\n    let data = try! Data(contentsOf: URL(fileURLWithPath: \"/path/to/image.jpg\"))\n    let mimeType = \"image/jpeg\"\n    let prediction = try await replicate.createPrediction(version: latestVersion.id,\n                                                       input: [\"img\": \"\\(data.uriEncoded(mimeType: mimeType))\"])\n    print(prediction.output)\n    // [\"https://replicate.delivery/mgxm/85f53415-0dc7-4703-891f-1e6f912119ad/output.png\"]\n}\n```\n\nYou can start a model and run it in the background:\n\n```swift\nlet model = replicate.getModel(\"kvfrans/clipdraw\")\n\nlet prompt = \"watercolor painting of an underwater submarine\"\nvar prediction = replicate.createPrediction(version: model.latestVersion!.id,\n                                         input: [\"prompt\": \"\\(prompt)\"])\nprint(prediction.status)\n// \"starting\"\n\ntry await prediction.wait(with: replicate)\nprint(prediction.status)\n// \"succeeded\"\n```\n\nYou can cancel a running prediction:\n\n```swift\nlet model = replicate.getModel(\"kvfrans/clipdraw\")\n\nlet prompt = \"watercolor painting of an underwater submarine\"\nvar prediction = replicate.createPrediction(version: model.latestVersion!.id,\n                                            input: [\"prompt\": \"\\(prompt)\"])\nprint(prediction.status)\n// \"starting\"\n\ntry await prediction.cancel(with: replicate)\nprint(prediction.status)\n// \"canceled\"\n```\n\nYou can list all the predictions you've run:\n\n```swift\nvar predictions: [Prediction] = []\nvar cursor: Replicate.Client.Pagination\u003cPrediction\u003e.Cursor?\nlet limit = 100\n\nrepeat {\n    let page = try await replicate.getPredictions(cursor: cursor)\n    predictions.append(contentsOf: page.results)\n    cursor = page.next\n} while predictions.count \u003c limit \u0026\u0026 cursor != nil\n```\n\n## Adding `Replicate` as a Dependency\n\nTo use the `Replicate` library in a Swift project,\nadd it to the dependencies for your package and your target:\n\n```swift\nlet package = Package(\n    // name, platforms, products, etc.\n    dependencies: [\n        // other dependencies\n        .package(url: \"https://github.com/replicate/replicate-swift\", from: \"0.24.0\"),\n    ],\n    targets: [\n        .target(name: \"\u003ctarget\u003e\", dependencies: [\n            // other dependencies\n            .product(name: \"Replicate\", package: \"replicate-swift\"),\n        ]),\n        // other targets\n    ]\n)\n```\n\n[Replicate]: https://replicate.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freplicate%2Freplicate-swift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freplicate%2Freplicate-swift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freplicate%2Freplicate-swift/lists"}