Replicate Swift 客户端

这是一个用于 Replicate 的 Swift 客户端。它允许你从 Swift 代码中运行模型,并在 Replicate 上执行各种其他操作。

要了解如何使用它,请查看我们关于使用 Replicate 构建 SwiftUI 应用程序的指南

用法

replicate.com/account 获取您的 API 令牌并将其传递给 Client(token:)

import Foundation
import Replicate

let replicate = Replicate.Client(token: <#token#>)

警告

不要将密钥存储在代码或任何其他与您的应用程序捆绑的资源中。相反,从 CloudKit 或其他服务器获取它们,并将它们存储在钥匙串中。

你可以运行一个模型并获取其输出

let output = try await replicate.run(
    "stability-ai/stable-diffusion-3",
    ["prompt": "a 19th century portrait of a gentleman otter"]
) { prediction in
    // Print the prediction status after each update
    print(prediction.status)
}

print(output)
// ["https://replicate.delivery/yhqm/bh9SsjWXY3pGKJyQzYjQlsZPzcNZ4EYOeEsPjFytc5TjYeNTA/R8_SD3_00001_.webp"]

或者按名称获取模型,并针对其最新版本创建预测

let model = try await replicate.getModel("stability-ai/stable-diffusion-3")
if let latestVersion = model.latestVersion {
    let prompt = "a 19th century portrait of a gentleman otter"
    let prediction = try await replicate.createPrediction(version: latestVersion.id,
                                                       input: ["prompt": "\(prompt)"],
                                                       wait: true)
    print(prediction.id)
    // "s654jhww3hrm60ch11v8t3zpkg"
    print(prediction.output)
    // ["https://replicate.delivery/yhqm/bh9SsjWXY3pGKJyQzYjQlsZPzcNZ4EYOeEsPjFytc5TjYeNTA/R8_SD3_00001_.webp"]
}

一些模型,例如 tencentarc/gfpgan,接收图像作为输入。要运行需要文件输入的模型,您可以传递互联网上可公开访问文件的 URL,或者使用 uriEncoded(mimeType:) 辅助方法从本地文件的内容创建 base64 编码的数据 URL。

let model = try await replicate.getModel("tencentarc/gfpgan")
if let latestVersion = model.latestVersion {
    let data = try! Data(contentsOf: URL(fileURLWithPath: "/path/to/image.jpg"))
    let mimeType = "image/jpeg"
    let prediction = try await replicate.createPrediction(version: latestVersion.id,
                                                       input: ["img": "\(data.uriEncoded(mimeType: mimeType))"])
    print(prediction.output)
    // ["https://replicate.delivery/mgxm/85f53415-0dc7-4703-891f-1e6f912119ad/output.png"]
}

您可以启动一个模型并在后台运行它

let model = replicate.getModel("kvfrans/clipdraw")

let prompt = "watercolor painting of an underwater submarine"
var prediction = replicate.createPrediction(version: model.latestVersion!.id,
                                         input: ["prompt": "\(prompt)"])
print(prediction.status)
// "starting"

try await prediction.wait(with: replicate)
print(prediction.status)
// "succeeded"

您可以取消正在运行的预测

let model = replicate.getModel("kvfrans/clipdraw")

let prompt = "watercolor painting of an underwater submarine"
var prediction = replicate.createPrediction(version: model.latestVersion!.id,
                                            input: ["prompt": "\(prompt)"])
print(prediction.status)
// "starting"

try await prediction.cancel(with: replicate)
print(prediction.status)
// "canceled"

您可以列出您运行的所有预测

var predictions: [Prediction] = []
var cursor: Replicate.Client.Pagination<Prediction>.Cursor?
let limit = 100

repeat {
    let page = try await replicate.getPredictions(cursor: cursor)
    predictions.append(contentsOf: page.results)
    cursor = page.next
} while predictions.count < limit && cursor != nil

Replicate 添加为依赖项

要在 Swift 项目中使用 Replicate 库,请将其添加到您的包和目标的依赖项中

let package = Package(
    // name, platforms, products, etc.
    dependencies: [
        // other dependencies
        .package(url: "https://github.com/replicate/replicate-swift", from: "0.24.0"),
    ],
    targets: [
        .target(name: "<target>", dependencies: [
            // other dependencies
            .product(name: "Replicate", package: "replicate-swift"),
        ]),
        // other targets
    ]
)