It is not official toolkit for swift from Replicate I do it because I'd like to support guys who are creating open-source models for Replicate
Replicate is a service that lets you run machine learning models with a few lines of code, without needing to understand how machine learning works. This package is the swift layer between Replicate API and your application.
All API requests must be authenticated with a token. Get your API key
let url = URL(string: ReplicateAPI.Endpoint.baseURL)
let apiKey = "your API key"
api = ReplicateAPI(baseURL: url, apiKey: apiKey)
- owner - The name of the user or organization that owns the model
- name - The name of the model
let model = try await api.getModel(owner: item.owner, name: item.name)
Calling this operation starts a new prediction for the version and inputs you provide. As models can take several seconds or more to run, the output will not be available immediately. To get the final result of the prediction you should either provide a webhook URL to call when the results are ready, or wait a prediction result until it has one of the finished statuses.
guard let latest = model.latestVersion else {
throw Errors.latestVersionIsEmpty
}
/// In the case of success, output will be an object containing the
/// output of the model. Any files will be represented as URLs.
let output: [String]? = try await api.createPrediction(
version: latest.id,
input: input.params /// ["prompt": "an astronaut riding a horse on mars"]
).output
"output": [
"https://replicate.com/api/models/stability-ai/stable-diffusion/files/9c3b6fe4-2d37-4571-a17a-83951b1cb120/out-0.png"
]
Each time a prediction generates an output (note that predictions can generate multiple outputs) You can find input format for models at the model page for example
Processing a response errors cases when server cannot or will not process the request due to something that is perceived to be a client error
/// Get a collection of models
/// - Parameter collection_slug: The slug of the collection, like
/// super-resolution or image-restoration
/// - Returns: a collection of models
public func getCollections(collection_slug : String) async throws -> CollectionOfModels
/// Get a model
/// - Parameters:
/// - owner: Model owner
/// - name: Model name
public func getModel(owner: String, name: String) async throws -> Model
/// Create prediction
/// - Parameters:
/// - versionId: Version id
/// - input: Input data
/// - expect: Logic for awaiting a prediction check out ``ReplicateAPI.Expect``
/// - webhook: An HTTPS URL for receiving a webhook when the prediction has new output.
/// - Returns: Prediction result
public func createPrediction<Input: Encodable, Output: Decodable>(
version id : String,
input: Input,
expect: Expect = .yes(),
webhook: URL? = nil
) async throws -> Prediction<Output>
/// Get prediction
/// Returns the same response as the create a prediction operation
/// status will be one of ``Prediction.Status``
/// In the case of success, output will be an object containing the output
/// of the model. Any files will be represented as URLs.
/// - Parameter id: Prediction id
/// - Returns: Prediction
public func getPrediction<Output: Decodable>(
by id : String
) async throws -> Prediction<Output>
- You need to have Xcode 13 installed in order to have access to Documentation Compiler (DocC)
- Go to Product > Build Documentation or ⌃⇧⌘ D