This is a React Native application for ONNX Runtime via Expo platform. The demo app demonstrates how to accomplish simple tasks such as loading onnx models and creating inference sessions, etc. in a react native expo application.
- Install Node.js
- Install Expo-CLI
npm install -g expo-cli
- Install Yarn (Recommended)
npm install -g yarn
Run the example expo app as-is for validating local React Native environment setup.
Steps:
-
Run
yarn installto set up JavaScript dependencies.yarn install
-
Install NPM
onnxruntime-react-nativepackage.expo install onnxruntime-react-native@latest
-
Run the following command in
<PROJECT_ROOT>to generate Android and iOS project.sh expo prebuildThe generated Android and iOS project code will be updated automatically. -
Build and run the app.
Run the following command to build and launch the app:
- In
<PROJECT_ROOT>, run the following command to launch for Android:
expo run:android
- In
<PROJECT_ROOT>, run the following command to launch for iOS:
expo run:ios
- In
The following steps were done in this sample for using onxnruntime-react-native. These can be replicated as a reference when setting up your own react native expo application.
-
NPM
onnxruntime-react-nativepackage.Note: By default, we install the current latest release version of ORT react native npm package(Recommended). Can also update to dev version npm package if necessary.
-
Prepare the model.
-
Model files are usually placed under
<PROJECT_ROOT>/assets.In this sample application, a simple ONNX MNIST model (
mnist.onnx) is provided by default. -
Add file
metro.config.jsunder<PROJECT_ROOT>. This file adds the extension.onnxto the bundler's asset extension list, which allows the bundler to include the model into assets.metro.config.jsfile in this sample application looks like:const { getDefaultConfig } = require('@expo/metro-config'); const defaultConfig = getDefaultConfig(__dirname); defaultConfig.resolver.assetExts.push('onnx'); module.exports = defaultConfig;
-
-
Generate Android and iOS directories native code to run your React Native app.
In this sample project, it generates the native code automatically by using package
onnxruntime-react-nativeas an Expo plugin. (Recommended approach.)-
In
<PROJECT_ROOT>/app.json, add the following line to sectionexpo:"plugins": ["onnxruntime-react-native"],Note: The plugin is added by default in
app.jsonin the sample. -
Run the following command in
<PROJECT_ROOT>to generate Android and iOS project: More info about Expo CLI Prebuild.expo prebuild
The generated Android and iOS project code will be updated automatically.
[Optional] Set up manually.
-
First run
expo prebuildto generate Android and iOS Native code.Note: In this tutorial we use
ai.onnxruntime.example.reactnative.basicusageas Android package name/iOS bundle identifier. Expo requires a manual configuration on package name and bundle identifier. -
[Android]: Add
onnxruntime-react-nativeas Gradle dependencies.In
<PROJECT_ROOT>/android/app/build.gradle, add the following line to sectiondependencies:implementation project(':onnxruntime-react-native') -
[iOS]: Add
onnxruntime-react-nativeas CocoaPods dependencies.In
<PROJECT_ROOT>/ios/Podfile, add the following line to sectiontarget 'OrtReactNativeBasicUsage':pod 'onnxruntime-react-native', :path => '../node_modules/onnxruntime-react-native'
Run the following command in
<PROJECT_ROOT>/iosto install pod:pod install
-
If running model containing ort-extensions custom operators, you may want to enable onnxruntime-extensions library in your react-native app.
See instructions here about enabling the option in your package.json file: Enable ONNX Runtime Extensions for React Native
NOTE: If you are interested in creating a new react native expo project from scratch, refer to instructions: https://docs.expo.dev/get-started/create-a-project/