Skip to content

muna-ai/muna-swift

Repository files navigation

Muna for Swift

Muna logo

Dynamic JSON Badge

Run AI models anywhere.

Note

Muna requires iOS 15+.

Installing Muna

Muna is distributed as a SwiftPM package, and can be added as a dependency in an Xcode project or in a SwiftPM package:

In Xcode

In your project editor, open the Package Dependencies tab, search for https://github.com/muna-ai/muna-swift.git and add the package to your project:

Xcode2

In Swift Package Manager

Add the following dependency to your Package.swift file:

let package = Package(
    name: "MySwiftPackage",
    dependencies: [
        .package(url: "https://github.com/muna-ai/muna-swift.git", from: "0.0.1"),
    ],
    targets: [
        .target(name: "MySwiftPackage", dependencies: ["Muna"]),
    ]
)

Retrieving your Access Key

Before creating predictions, you will need to create a Muna account. Once you do, generate an access key:

generate access key

Making a Prediction

First, create a Muna client:

import Muna

// 💥 Create a Muna client
let muna = Muna(accessKey: "...")

Then make a prediction:

// 🔥 Make a prediction
let prediction = try await muna.predictions.create(
    tag: "@fxn/greeting",
    inputs: ["name": "Sam"]
)

Finally, use the prediction results:

// 🚀 Use the results
print("Prediction result: \(prediction.results![0]!)")

Embedding Predictors

Muna normally works by downloading and executing prediction functions at runtime. But because iOS requires strict sandboxing, you must download and embed predictors at build-time instead.

Specifying Predictors to Embed

First, create an muna.config.swift file at the root of your target directory:

import Muna

let config = Muna.Configuration(
    // add all predictor tags to be embedded here
    tags: [
        "@fxn/greeting"
    ]
)

Embedding the Predictors

You can embed the predictors using the context menu by right-clicking on your project and selecting the Embed Predictors command:

Embed

You can also use the Swift CLI:

# Embed predictors
swift package --allow-writing-to-package-directory muna-embed

Note

Embedding predictors requires internet and file system access to download and embed the prediction function into your Xcode project.

Important

After embedding, Xcode might prompt you to either reload the project from disk or keep the current version in memory. Always reload your project from disk.

Useful Links

Muna is a product of NatML Inc.

About

Run AI models anywhere. https://muna.ai/explore

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages