The ModelrunerClient is a robust and user-friendly Swift package designed for seamless integration of modelrunner AI models into Swift projects. This library, developed in pure Swift, provides developers with simple APIs to interact with AI models, suitable for both iOS and macOS platforms.
The ModelrunerClient library serves as a client for modelrunner AI models. Before using this library, ensure you've set up your models as per the quickstart guide.
This Swift client library is crafted as a lightweight layer atop Swift's networking standards like URLSession. It ensures hassle-free integration into your existing Swift codebase. The library is designed to address the nuances of Swift and Apple's ecosystem, ensuring smooth operation across different Apple platforms.
Note: Make sure to review the modelrunner getting started guide to acquire your credentials and register your functions.
-
Add
ModelrunerClientas a dependency in your Swift Package Manager. -
Set up the client instance:
import ModelrunerClient let modelrunner = ModelrunerClient.withCredentials(.keyPair("MODELRUNNER_KEY")) // You can also use a proxy to protect your credentials // let modelrunner = ModelrunerClient.withProxy("http://localhost:3333/api/modelrunner/proxy")
-
Use
modelrunner.subscribeto dispatch requests to the model API:let result = try await modelrunner.subscribe(to: "text-to-image", input: [ "prompt": "a cute shih-tzu puppy", "model_name": "stabilityai/stable-diffusion-xl-base-1.0", "image_size": "square_hd" ]) { update in print(update) }
Notes:
- Replace
text-to-imagewith a valid model id. Check modelrunner.ai/models for all available models. - It fully relies on
async/awaitfor asynchronous programming. - The result type in Swift will be a
[String: Any]and the entries depend on the API output schema. - The Swift client also supports typed inputs and outputs through
Codable.
Check the Sources/Samples folder for a handful of sample applications using the ModelrunerClient.
Open them with xed to quickly start playing with
xed Sources/Samples/ModelrunnerSampleAppContributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make to the Swift version of the client are greatly appreciated.
Distributed under the MIT License. See LICENSE for more information.