Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

It's a swift layer between Replicate API and your swiftui application. Replicate toolkit for running text to image model, stable difusion model, ML makeovers, super resolution, diffusion api library for swift, client for using diffusion in real-time applications Integration example swiftui

License

Notifications You must be signed in to change notification settings

swiftuiux/replicate-kit-swift

Repository files navigation

Replicate toolkit for swift

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

SwiftUI example for the package

Replicate kit example app

The concept

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.

How to use

Authentication

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)

Get a model

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

Create prediction and get result

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

Handling errors

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 The concept

Replicate API

 /// 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>

The concept

Documentation(API)

  • You need to have Xcode 13 installed in order to have access to Documentation Compiler (DocC)
  • Go to Product > Build Documentation or ⌃⇧⌘ D

The concept

About

It's a swift layer between Replicate API and your swiftui application. Replicate toolkit for running text to image model, stable difusion model, ML makeovers, super resolution, diffusion api library for swift, client for using diffusion in real-time applications Integration example swiftui

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

AltStyle によって変換されたページ (->オリジナル) /