Add Kotlin and Java API for online punctuation models (#1936)

This commit is contained in:
Fangjun Kuang
2025-02-27 16:52:36 +08:00
committed by GitHub
parent 815ebac8f9
commit f5dfcf8d2f
16 changed files with 474 additions and 13 deletions

View File

@@ -5,7 +5,7 @@
// The model supports both English and Chinese.
import com.k2fsa.sherpa.onnx.*;
public class AddPunctuation {
public class OfflineAddPunctuation {
public static void main(String[] args) {
// please download the model from
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/punctuation-models

View File

@@ -0,0 +1,41 @@
// Copyright 2025 Xiaomi Corporation
// This file shows how to use a punctuation model to add punctuations to text.
//
// The model supports ONLY English.
import com.k2fsa.sherpa.onnx.*;
public class OnlineAddPunctuation {
public static void main(String[] args) {
// please download the model from
// https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-online-punct-en-2024-08-06.tar.bz2
String model = "./sherpa-onnx-online-punct-en-2024-08-06/model.int8.onnx";
String bpeVocab = "./sherpa-onnx-online-punct-en-2024-08-06/bpe.vocab";
OnlinePunctuationModelConfig modelConfig =
OnlinePunctuationModelConfig.builder()
.setCnnBilstm(model)
.setBpeVocab(bpeVocab)
.setNumThreads(1)
.setDebug(true)
.build();
OnlinePunctuationConfig config =
OnlinePunctuationConfig.builder().setModel(modelConfig).build();
OnlinePunctuation punct = new OnlinePunctuation(config);
String[] sentences =
new String[] {
"how are you doing fantastic thank you how about you",
"The African blogosphere is rapidly expanding bringing more voices online in the form of"
+ " commentaries opinions analyses rants and poetry",
};
System.out.println("---");
for (String text : sentences) {
String out = punct.addPunctuation(text);
System.out.printf("Input: %s\n", text);
System.out.printf("Output: %s\n", out);
System.out.println("---");
}
}
}

View File

@@ -34,4 +34,4 @@ fi
java \
-Djava.library.path=$PWD/../build/lib \
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \
./AddPunctuation.java
./OfflineAddPunctuation.java

View File

@@ -0,0 +1,37 @@
#!/usr/bin/env bash
set -ex
if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then
mkdir -p ../build
pushd ../build
cmake \
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DBUILD_SHARED_LIBS=ON \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=ON \
..
make -j4
ls -lh lib
popd
fi
if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
pushd ../sherpa-onnx/java-api
make
popd
fi
if [ ! -f ./sherpa-onnx-online-punct-en-2024-08-06/model.int8.onnx ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-online-punct-en-2024-08-06.tar.bz2
tar xvf sherpa-onnx-online-punct-en-2024-08-06.tar.bz2
rm sherpa-onnx-online-punct-en-2024-08-06.tar.bz2
fi
java \
-Djava.library.path=$PWD/../build/lib \
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \
./OnlineAddPunctuation.java