replicate / replicate-swift

Swift client for Replicate
https://replicate.com
Apache License 2.0
157 stars 33 forks source link

Add `updateHandler` callback for `Prediction.wait` and `Client.run` methods #90

Closed mattt closed 1 month ago

mattt commented 1 month ago

updateHandler is a closure that executes with the updated prediction after each polling request to the API. It can be passed as a trailing closure. You can use it to provide feedback to the user about the progress of the prediction, or throw CancellationError to stop waiting for the prediction to finish.

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)
}

If the prediction is in a terminal state (e.g. succeeded, failed, or canceled), it's returned immediately and the closure is not executed.