Run Python functions (a.k.a "predictors") locally in your iOS apps, with full GPU acceleration and zero dependencies.
Tip
Join our waitlist to bring your custom Python functions and run them on-device across Android, iOS, macOS, Linux, web, and Windows.
Note
Function requires iOS 15+.
Function is distributed as a SwiftPM package, and can be added as a dependency in an Xcode project or in a SwiftPM package:
In your project editor, open the Package Dependencies
tab, search
for https://github.com/fxnai/fxnios.git and add the package to your project:
Add the following dependency to your Package.swift
file:
let package = Package(
name: "MyAwesomeApp",
dependencies: [
.package(url: "https://github.com/fxnai/fxnios.git", from: "0.0.1"),
],
targets: [
.target(name: "MyAwesomeApp", dependencies: ["FunctionSwift"]),
]
)
Before creating predictions, you will need to create a Function account. Once you do, generate an access key:
Next, create an fxn.xcconfig
build configuration file and enter your access key:
# Function access key
FXN_ACCESS_KEY="<ACCESS KEY>"
Caution
Make sure that your fxn.xcconfig
file is excluded from source control by adding it to your .gitignore
file. Never share your access key.
First, create a Function client:
import FunctionSwift
// 💥 Create a Function client
let fxn = Function(accessKey: "...")
Then make a prediction:
// 🔥 Make a prediction
let prediction = try await fxn.predictions.create(
tag: "@fxn/greeting",
inputs: ["name": "Sam"]
)
Finally, use the prediction results:
// 🚀 Use the results
print("Prediction result: \(prediction.results![0]!)")
Function 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.
First, create an fxn.config.swift
file at the root of your target directory:
import FunctionSwift
let config = Function.Configuration(
// add all predictor tags to be embedded here
tags: [
"@fxn/greeting"
]
)
Next, right click on your project and run the Embed Predictors
command on your app target:
Function will download the prediction function as a dynamic framework, then configure Xcode to embed the framework into your app bundle.
Note
The Embed Predictors
script 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.
- Discover predictors to use in your apps.
- Join our Discord community.
- Check out our docs.
- Learn more about us on our blog.
- Reach out to us at [email protected].
Function is a product of NatML Inc.