Skip to content

Sample Android App for JNI library #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,42 @@ cache/
external/
test/dataset/

.vscode/

# Never exclude src files
!WhisperKit/**
!cli/**

# MacOS specific
.DS_Store

### Android ###
# IntelliJ IDEA project files
.idea/
*.iml
*.ipr
*.iws
out/

# Model folder (too large)
androidApp/app/src/main/assets/openai_whisper-tiny/

# Gradle files and caches
.gradle/
local.properties

# Native build outputs (e.g., C++/JNI)
.cxx/

# Heap dump files
*.hprof

# Log files
*.log

# test audio files
androidApp/app/src/main/assets/*.wav
androidApp/app/src/main/assets/*.m4a

# .so folder in the app (repeated files)
androidApp/app/src/main/jniLibs
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ but we are continuing to invest in Android and now welcome contributions from th
- [Installation](#installation)
- [Getting Started](#getting-started)
- [CLI Run and Test](#cli-run-and-test)
- [Running Android app](#running-android-app)
- [Contributing \& Roadmap](#contributing--roadmap)
- [License](#license)
- [Citation](#citation)
Expand Down Expand Up @@ -74,12 +75,13 @@ make env

2. Inside the Docker environment, build the `whisperkit-cli` CLI using (for Android and Linux):
```
make build [linux | qnn | gpu]
make build [linux | qnn | gpu | jni]
```

The QNN option builds WhisperKit with Qualcomm AI NPU support and the QNN TFLite delegate.
The 'gpu' option is the generic GPU backend for all Android devices from TFLite GPU delegate.
Linux builds are currently CPU-only.
The 'jni' option builds the .so file with JNI library to use on android (using QNN support).

3. Back on the host machine (outside Docker shell), push dependencies to the Android device:
```
Expand Down Expand Up @@ -110,7 +112,16 @@ For Linux:
make build linux
```

2. Manually run `whisperkit-cli`:
2. Run on Android with `run_on_android.sh` script:

Log in via adb shell:
```
adb shell
cd /sdcard/argmax/tflite
sh run_on_android.sh
```

3. Manually run `whisperkit-cli`:

Usage:

Expand All @@ -129,7 +140,7 @@ export LD_LIBRARY_PATH=/data/local/tmp/lib
whisperkit-cli transcribe --model-path /path/to/openai_whisper-base --audio-path /path/to/inputs/jfk_441khz.m4a
```

3. Sample execution output:
4. Sample execution output:
```
root@cf40510e9b93:/src/AXIE# ./build/linux/whisperkit-cli transcribe --model-path /src/AXIE/models/openai_whisper-small --audio-path /src/AXIE/test/jfk_441khz.m4a
SoC: generic CPU (x86, arm64, etc)
Expand All @@ -153,6 +164,22 @@ Transcription: And so, my fellow Americans, ask not what your country can do f
```
</details>

# Running Android App

<details>
<summary> (Click to expand) </summary>

1. Move model to assets folder
Download the models if you haven't done so as specified in the Installation section. Move specifically the "whisper_tiny" folder into the assets folder of the app.

2. Run the app with Android Studio

3. Things to consider.
QNN will only work if the SoC of your device is among the supported ones listed in the C++ code. The model may take a couple minutes to load when using QNN delegate.
After recording with the microphone, the input is saved into the MicInput.wav file, you can select it to transcribe your audio.

</details>

## Contributing

WhisperKit Android is currently in the v0.1 Beta stage. We are actively developing the project and welcome contributions from the community.
Expand Down
4 changes: 2 additions & 2 deletions WhisperKit/src/TranscribeTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extern "C" {
#define TRANSCRIBE_TASK_DEFAULT_LIB_DIR "/data/local/tmp/lib"
#define TRANSCRIBE_TASK_DEFAULT_CACHE_DIR "/data/user/0/com.whispertflite/cache"
#elif (QNN_DELEGATE || GPU_DELEGATE)
// for Android QNN or GPU delegatea
// for Android QNN or GPU delegate
#define TRANSCRIBE_TASK_TFLITE_ROOT_PATH "/sdcard/argmax/tflite"
#define TRANSCRIBE_TASK_DEFAULT_LIB_DIR "/data/local/tmp/lib"
#define TRANSCRIBE_TASK_DEFAULT_CACHE_DIR "/data/local/tmp/cache"
Expand Down Expand Up @@ -199,7 +199,7 @@ std::unique_ptr<std::string> Runtime::cmdexec(const char* cmd) {

bool Runtime::check_qcom_soc() {
vector<string> supported_socs{
"SM8650", "SM8550", "SM8450","SM8350"
"SM8650", "SM8550", "SM8450","SM8350", "SM7450"
};

auto soc = *cmdexec("getprop ro.soc.model");
Expand Down
72 changes: 72 additions & 0 deletions androidApp/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}

android {
signingConfigs {
release {
}
}
namespace 'com.whispertflite'
compileSdk 34

defaultConfig {
applicationId "com.whispertflite"
minSdk 26
targetSdk 34
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

// Specify the ABIs to exclude
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
}

buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion = '1.5.14'
}
// externalNativeBuild {
// cmake {
// path "src/main/cpp/WhisperKitAndroid/CMakeLists.txt"
// }
// }
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '11'
}
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.constraintlayout:constraintlayout:2.2.0'

// Main TensorFlow Lite library
implementation 'org.tensorflow:tensorflow-lite:2.14.0'
implementation 'org.tensorflow:tensorflow-lite-support:0.4.4'
implementation 'com.google.android.gms:play-services-tflite-gpu:16.4.0'
implementation 'com.google.android.gms:play-services-tflite-java:16.4.0'
implementation 'androidx.compose.ui:ui-android:1.7.6'
implementation 'androidx.compose.foundation:foundation-android:1.7.6'
implementation 'androidx.compose.material3:material3-android:1.3.1'
implementation 'androidx.activity:activity-compose:1.9.0'

}
21 changes: 21 additions & 0 deletions androidApp/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
35 changes: 35 additions & 0 deletions androidApp/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.whispertflite">

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="WhisperASR"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:extractNativeLibs="true"
tools:targetApi="31">
<uses-native-library
android:name="libcdsprpc.so"
android:required="false"/>
<activity android:name="com.whispertflite.MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>
<activity
android:name="org.libsdl.app.SDLActivity"
android:label="SDL"
android:launchMode="singleTask"
android:exported="false"
>

</activity>
</application>

</manifest>
Loading