Skip to content

Commit

Permalink
v2.1 Android - Demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ErisMik committed Dec 4, 2024
1 parent 553f761 commit d1ba476
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ android {
def testDataFile = file('../../../../resources/.test/test_data.json')
def parsedJson = new groovy.json.JsonSlurper().parseText(testDataFile.text)
def languages = []
parsedJson.tests.parameters.each { a ->
parsedJson.tests.language_tests.each { a ->
languages.add(a.language)
}

Expand Down
2 changes: 1 addition & 1 deletion demo/android/CheetahDemo/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
.externalNativeBuild
release
test_resources
cheetah_params.pv
*.pv
11 changes: 0 additions & 11 deletions demo/android/CheetahDemo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,3 @@ Launch the demo on your phone using Android Studio.

1. Press the record button.
2. Start talking. The transcription will appear in the textbox above.

## Running the Instrumented Unit Tests

Ensure you have an Android device connected or simulator running. Then run the following from the terminal:

```console
cd demo/android/CheetahDemo
./gradlew connectedAndroidTest -PpvTestingAccessKey="YOUR_ACCESS_KEY_HERE"
```

The test results are stored in `cheetah-demo-app/build/reports`.
6 changes: 6 additions & 0 deletions demo/android/CheetahDemo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ buildscript {
repositories {
google()
mavenCentral()
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1352/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:7.4.2'
Expand All @@ -16,6 +19,9 @@ allprojects {
repositories {
google()
mavenCentral()
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1352/'
}
}
}

Expand Down
55 changes: 48 additions & 7 deletions demo/android/CheetahDemo/cheetah-demo-app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import groovy.json.JsonSlurper

apply plugin: 'com.android.application'

android {
compileSdkVersion defaultTargetSdkVersion

Expand All @@ -17,6 +20,44 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

def testDataFile = file('../../../../resources/.test/test_data.json')
def parsedJson = new JsonSlurper().parseText(testDataFile.text)
def languages = []
parsedJson.tests.language_tests.each { a ->
languages.add(a.language)
}

flavorDimensions "language"
productFlavors {
en {
getIsDefault().set(true)
}

languages.each { language ->
"$language" {
applicationIdSuffix ".$language"

}
}

all { flavor ->
delete fileTree("$projectDir/src/main/assets") {
exclude '**/.gitkeep'
}
task("${flavor.name}CopyParams", type: Copy) {
if (flavor.name != 'en') {
from("$projectDir/../../../../lib/common/")
include("cheetah_params_${flavor.name}.pv")
into("$projectDir/src/main/assets/models")
} else {
from("$projectDir/../../../../lib/common/")
include("cheetah_params.pv")
into("$projectDir/src/main/assets/models")
}
}
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
Expand All @@ -32,14 +73,14 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'ai.picovoice:cheetah-android:2.0.0'
implementation 'ai.picovoice:cheetah-android:2.1.0'
implementation 'ai.picovoice:android-voice-processor:1.0.2'
}

task copyParams(type: Copy) {
from("${rootDir}/../../../lib/common")
include('cheetah_params.pv')
into("${rootDir}/cheetah-demo-app/src/main/assets")
afterEvaluate {
android.productFlavors.all {
flavor ->
tasks."merge${flavor.name.capitalize()}DebugAssets".dependsOn "${flavor.name}CopyParams"
tasks."merge${flavor.name.capitalize()}ReleaseAssets".dependsOn "${flavor.name}CopyParams"
}
}

preBuild.dependsOn(copyParams)
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.util.Objects;

import ai.picovoice.android.voiceprocessor.VoiceProcessor;
import ai.picovoice.android.voiceprocessor.VoiceProcessorException;
import ai.picovoice.cheetah.Cheetah;
Expand Down Expand Up @@ -54,12 +56,20 @@ protected void onCreate(Bundle savedInstanceState) {
transcriptTextView.setMovementMethod(new ScrollingMovementMethod());

try {
cheetah = new Cheetah.Builder()
Cheetah.Builder builder = new Cheetah.Builder()
.setAccessKey(ACCESS_KEY)
.setModelPath(MODEL_FILE)
.setEndpointDuration(1f)
.setEnableAutomaticPunctuation(true)
.build(getApplicationContext());
.setEnableAutomaticPunctuation(true);

String model;
if (Objects.equals(BuildConfig.FLAVOR, "en")) {
model = "cheetah_params.pv";
} else {
model = "cheetah_params_" + BuildConfig.FLAVOR + ".pv";
}
builder.setModelPath("models/" + model);

cheetah = builder.build(getApplicationContext());
} catch (CheetahInvalidArgumentException e) {
displayError(e.getMessage());
} catch (CheetahActivationException e) {
Expand Down

0 comments on commit d1ba476

Please sign in to comment.