Skip to content

Commit

Permalink
v2.0 java (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
laves authored Nov 23, 2023
1 parent f6b26b8 commit 95a505b
Show file tree
Hide file tree
Showing 27 changed files with 467 additions and 124 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/java-demos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
java-version: ['11', '17', '19']

steps:
- uses: actions/checkout@v3

- name: Set up JDK 11
- name: Set up JDK ${{ matrix.java-version }}
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: ${{ matrix.java-version }}
distribution: 'temurin'

- name: Build
Expand Down
30 changes: 15 additions & 15 deletions .github/workflows/java-perf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ jobs:
os: [ubuntu-latest, macos-latest, windows-latest]
include:
- os: ubuntu-latest
init_performance_threshold_sec: 2.0
proc_performance_threshold_sec: 0.7
- os: windows-latest
init_performance_threshold_sec: 2.5
init_performance_threshold_sec: 4.0
proc_performance_threshold_sec: 0.9
- os: macos-latest
init_performance_threshold_sec: 2.4
init_performance_threshold_sec: 4.5
proc_performance_threshold_sec: 1.9
- os: windows-latest
init_performance_threshold_sec: 4.0
proc_performance_threshold_sec: 0.9

steps:
Expand All @@ -64,20 +64,20 @@ jobs:
machine: [rpi3-32, rpi3-64, rpi4-32, rpi4-64, jetson]
include:
- machine: rpi3-32
init_performance_threshold_sec: 7.6
proc_performance_threshold_sec: 3.3
init_performance_threshold_sec: 16.0
proc_performance_threshold_sec: 6.0
- machine: rpi3-64
init_performance_threshold_sec: 8.4
proc_performance_threshold_sec: 3.3
init_performance_threshold_sec: 14.0
proc_performance_threshold_sec: 5.5
- machine: rpi4-32
init_performance_threshold_sec: 5.7
proc_performance_threshold_sec: 2.1
init_performance_threshold_sec: 7.0
proc_performance_threshold_sec: 2.5
- machine: rpi4-64
init_performance_threshold_sec: 5.1
proc_performance_threshold_sec: 2.0
init_performance_threshold_sec: 7.0
proc_performance_threshold_sec: 2.5
- machine: jetson
init_performance_threshold_sec: 5.1
proc_performance_threshold_sec: 2.0
init_performance_threshold_sec: 7.0
proc_performance_threshold_sec: 2.6

steps:
- uses: actions/checkout@v3
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
java-version: ['11', '17', '19']

steps:
- uses: actions/checkout@v3

- name: Set up JDK 11
- name: Set up JDK ${{ matrix.java-version }}
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: ${{ matrix.java-version }}
distribution: 'temurin'

- name: Build
Expand Down
2 changes: 1 addition & 1 deletion binding/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {

ext {
PUBLISH_GROUP_ID = 'ai.picovoice'
PUBLISH_VERSION = '1.2.0'
PUBLISH_VERSION = '2.0.0'
PUBLISH_ARTIFACT_ID = 'leopard-java'
}

Expand Down
2 changes: 1 addition & 1 deletion binding/java/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
49 changes: 41 additions & 8 deletions binding/java/src/ai/picovoice/leopard/Leopard.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
package ai.picovoice.leopard;

import java.io.File;
import java.util.Arrays;
import java.util.List;

/**
* Class for the Leopard Speech-to-Text engine.
Expand All @@ -22,7 +22,9 @@ public class Leopard {

public static final String LIBRARY_PATH;
public static final String MODEL_PATH;
public static final String[] VALID_EXTENSIONS;
public static final List<String> VALID_EXTENSIONS;

private static String sdk = "java";

static {
LIBRARY_PATH = Utils.getPackagedLibraryPath();
Expand All @@ -39,19 +41,33 @@ public class Leopard {
* @param modelPath Absolute path to the file containing Leopard model parameters.
* @param libraryPath Absolute path to the native Leopard library.
* @param enableAutomaticPunctuation Set to `true` to enable automatic punctuation insertion.
* @param enableDiarization Set to `true` to enable speaker diarization, which allows Leopard to
* differentiate speakers as part of the transcription process. Word metadata
* will include a `speakerTag` to identify unique speakers.
* @throws LeopardException if there is an error while initializing Leopard.
*/
private Leopard(
String accessKey,
String modelPath,
String libraryPath,
boolean enableAutomaticPunctuation) throws LeopardException {
boolean enableAutomaticPunctuation,
boolean enableDiarization) throws LeopardException {
try {
System.load(libraryPath);
} catch (Exception exception) {
throw new LeopardException(exception);
}
handle = LeopardNative.init(accessKey, modelPath, enableAutomaticPunctuation);

LeopardNative.setSdk(Leopard.sdk);
handle = LeopardNative.init(
accessKey,
modelPath,
enableAutomaticPunctuation,
enableDiarization);
}

public static void setSdk(String sdk) {
Leopard.sdk = sdk;
}

/**
Expand Down Expand Up @@ -106,8 +122,8 @@ public LeopardTranscript processFile(String path) throws LeopardException {
return LeopardNative.processFile(handle, path);
} catch (LeopardInvalidArgumentException e) {
if (path.contains(".")) {
String extension = path.substring(path.lastIndexOf(".") + 1);
if (!Arrays.asList(VALID_EXTENSIONS).contains(extension)) {
String extension = path.substring(path.lastIndexOf(".") + 1).toLowerCase();
if (!VALID_EXTENSIONS.contains(extension)) {
throw new LeopardInvalidArgumentException(
String.format("Specified file with extension '%s' is not supported",
extension));
Expand Down Expand Up @@ -142,8 +158,8 @@ public static class Builder {
private String accessKey = null;
private String libraryPath = null;
private String modelPath = null;

private boolean enableAutomaticPunctuation = false;
private boolean enableDiarization = false;

/**
* Setter the AccessKey.
Expand Down Expand Up @@ -185,6 +201,18 @@ public Builder setEnableAutomaticPunctuation(boolean enableAutomaticPunctuation)
return this;
}

/**
* Setter for enabling speaker diarization.
*
* @param enableDiarization Set to `true` to enable speaker diarization, which allows Leopard to
* differentiate speakers as part of the transcription process. Word metadata
* will include a `speakerTag` to identify unique speakers.
*/
public Builder setEnableDiarization(boolean enableDiarization) {
this.enableDiarization = enableDiarization;
return this;
}

/**
* Creates an instance of Leopard Speech-to-Text engine.
*/
Expand Down Expand Up @@ -228,7 +256,12 @@ public Leopard build() throws LeopardException {
}
}

return new Leopard(accessKey, modelPath, libraryPath, enableAutomaticPunctuation);
return new Leopard(
accessKey,
modelPath,
libraryPath,
enableAutomaticPunctuation,
enableDiarization);
}
}
}
7 changes: 5 additions & 2 deletions binding/java/src/ai/picovoice/leopard/LeopardNative.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 Picovoice Inc.
Copyright 2022-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Expand All @@ -18,10 +18,13 @@ class LeopardNative {

static native String getVersion();

static native void setSdk(String sdk);

static native long init(
String accessKey,
String modelPath,
boolean enableAutomaticPunctuation) throws LeopardException;
boolean enableAutomaticPunctuation,
boolean enableDiarization) throws LeopardException;

static native void delete(long object);

Expand Down
21 changes: 20 additions & 1 deletion binding/java/src/ai/picovoice/leopard/LeopardTranscript.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static class Word {
private final float confidence;
private final float startSec;
private final float endSec;
private final int speakerTag;

/**
* Constructor.
Expand All @@ -65,12 +66,21 @@ public static class Word {
* @param confidence Transcription confidence. It is a number within [0, 1].
* @param startSec Start of word in seconds.
* @param endSec End of word in seconds.
* @param speakerTag The speaker tag is `-1` if diarization is not enabled during initialization;
* otherwise, it's a non-negative integer identifying unique speakers, with `0` reserved
* for unknown speakers
*/
public Word(String word, float confidence, float startSec, float endSec) {
public Word(
String word,
float confidence,
float startSec,
float endSec,
int speakerTag) {
this.word = word;
this.confidence = confidence;
this.startSec = startSec;
this.endSec = endSec;
this.speakerTag = speakerTag;
}

/**
Expand Down Expand Up @@ -108,5 +118,14 @@ public float getStartSec() {
public float getEndSec() {
return endSec;
}

/**
* Getter for the speaker tag.
*
* @return Speaker tag.
*/
public int getSpeakerTag() {
return speakerTag;
}
}
}
8 changes: 5 additions & 3 deletions binding/java/src/ai/picovoice/leopard/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -232,8 +234,8 @@ public static String getPackagedLibraryPath() {
}
}

public static String[] getValidFileExtensions() {
return new String[]{
public static List<String> getValidFileExtensions() {
return Arrays.asList(
"3gp",
"flac",
"m4a",
Expand All @@ -243,6 +245,6 @@ public static String[] getValidFileExtensions() {
"opus",
"vorbis",
"wav",
"webm"};
"webm");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 Picovoice Inc.
Copyright 2022-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Expand All @@ -20,4 +20,8 @@ public LeopardActivationException(Throwable cause) {
public LeopardActivationException(String message) {
super(message);
}

public LeopardActivationException(String message, String[] messageStack) {
super(message, messageStack);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 Picovoice Inc.
Copyright 2022-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Expand All @@ -20,4 +20,8 @@ public LeopardActivationLimitException(Throwable cause) {
public LeopardActivationLimitException(String message) {
super(message);
}

public LeopardActivationLimitException(String message, String[] messageStack) {
super(message, messageStack);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 Picovoice Inc.
Copyright 2022-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Expand All @@ -20,4 +20,8 @@ public LeopardActivationRefusedException(Throwable cause) {
public LeopardActivationRefusedException(String message) {
super(message);
}

public LeopardActivationRefusedException(String message, String[] messageStack) {
super(message, messageStack);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 Picovoice Inc.
Copyright 2022-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Expand All @@ -20,4 +20,8 @@ public LeopardActivationThrottledException(Throwable cause) {
public LeopardActivationThrottledException(String message) {
super(message);
}

public LeopardActivationThrottledException(String message, String[] messageStack) {
super(message, messageStack);
}
}
Loading

0 comments on commit 95a505b

Please sign in to comment.