Skip to content

Add Java and Kotlin API for Cohere Transcribe - #3461

Merged
csukuangfj merged 4 commits into
k2-fsa:masterfrom
csukuangfj:jni
Apr 3, 2026
Merged

csukuangfj merged 4 commits into
k2-fsa:masterfrom
csukuangfj:jni

Conversation

@csukuangfj

@csukuangfj csukuangfj commented Apr 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • New Features

    • Added offline Cohere Transcribe support with language, punctuation, and ITN options.
    • Added Java and Kotlin example programs and runnable scripts to demonstrate non‑streaming offline transcription and measure performance.
  • Chores

    • CI workflows updated to run on the jni branch and include a job step to execute the Cohere Transcribe example; workflow workspace cleanup added.

csukuangfj and others added 3 commits April 2, 2026 18:09
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Apr 3, 2026
@coderabbitai

coderabbitai Bot commented Apr 3, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 297c440e-c546-4170-acd8-7b52d2f045e9

📥 Commits

Reviewing files that changed from the base of the PR and between 0b2b130 and 63616d8.

📒 Files selected for processing (1)
  • sherpa-onnx/csrc/session.cc

📝 Walkthrough

Walkthrough

Adds Cohere Transcribe offline model support across Java/Kotlin APIs, JNI/native parsing, examples and CI: new model config types, JNI parsing for cohereTranscribe, example programs and scripts to run them, exported JNI option symbols, and CI workflow updates to run the new examples on the jni branch.

Changes

Cohort / File(s) Summary
CI workflows
.github/workflows/jni.yaml, .github/workflows/run-java-test.yaml
Added jni branch to push triggers; run-java-test workflow now executes an extra Cohere Transcribe example step and removes generated sherpa-onnx-cohere-transcribe-* artifacts.
Java API — model config & build
sherpa-onnx/java-api/src/main/java/com/k2fsa/sherpa/onnx/OfflineCohereTranscribeModelConfig.java, sherpa-onnx/java-api/src/main/java/com/k2fsa/sherpa/onnx/OfflineModelConfig.java, sherpa-onnx/java-api/Makefile
Added OfflineCohereTranscribeModelConfig and integrated it into OfflineModelConfig (getter/setter); updated Makefile ordering to include the new config class.
Kotlin API
sherpa-onnx/kotlin-api/OfflineRecognizer.kt
Added OfflineCohereTranscribeModelConfig data class and a cohereTranscribe property on OfflineModelConfig.
JNI / native parsing
sherpa-onnx/jni/offline-recognizer.cc, sherpa-onnx/jni/sherpa-onnx-symbols.exp
Parsed cohereTranscribe fields (encoder, decoder, language, use_punct, use_itn) into native config; exported new JNI symbols for stream option methods (getOption, hasOption, setOption) for OfflineStream and OnlineStream.
Examples & runner scripts (Java)
java-api-examples/NonStreamingDecodeFileCohereTranscribe.java, java-api-examples/run-non-streaming-decode-file-cohere-transcribe.sh
Added a Java non-streaming example using the Cohere Transcribe offline config and a shell script that builds JNI/JAR if needed, downloads the model, and runs the example.
Examples & runner scripts (Kotlin)
kotlin-api-examples/test_offline_cohere_transcribe.kt, kotlin-api-examples/run.sh
Added Kotlin offline Cohere Transcribe test and updated run.sh to download the model, compile a test JAR, and execute the test.
Session options minor tweak
sherpa-onnx/csrc/session.cc
Converted ProfilingFilePrefix to use SHERPA_ONNX_TO_ORT_PATH(...) when enabling ORT profiling.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Example as Example (Java/Kotlin)
  participant API as Java/Kotlin API
  participant JNI as JNI bridge
  participant Native as Native C++ Runtime
  participant Model as Cohere Transcribe ONNX files

  Example->>API: build OfflineModelConfig (cohereTranscribe)
  API->>JNI: call native createOfflineRecognizer(config)
  JNI->>Native: translate Java config -> native GetOfflineConfig
  Native->>Model: load encoder/decoder ONNX, tokens
  Example->>API: create OfflineStream, setOption(language)
  Example->>API: acceptWaveform(samples, sample_rate)
  API->>JNI: recognizer.decode(stream) -> JNI -> Native
  Native->>Native: run decode with Cohere Transcribe models
  Native-->>JNI: return result
  JNI-->>API: wrap result
  API-->>Example: getResult(text, metrics)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 I hopped through code with eager paws,

Models unpacked and JNI laws.
Java, Kotlin, native meet—
Cohere Transcribe hums so sweet.
A carrot-sized PR: builds complete! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly summarizes the main change: adding Java and Kotlin API support for Cohere Transcribe model. This accurately reflects the core functionality added across multiple files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for the Cohere Transcribe model across the Java and Kotlin APIs. Key changes include the addition of the OfflineCohereTranscribeModelConfig class, updated JNI bindings to handle the new configuration, and new example scripts and source files for both languages. The review feedback suggests improving the maintainability of the shell scripts by using variables for model names and cleaning up the Kotlin compilation command by removing an unused source file.

Comment on lines +28 to +33
if [ ! -f ./sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/encoder.int8.onnx ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
tar xvf sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
rm sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
ls -lh sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01
fi

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The model name sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01 is repeated multiple times. To improve maintainability, it would be better to store it in a variable and reuse it. This makes it easier to update the model version in the future.

Suggested change
if [ ! -f ./sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/encoder.int8.onnx ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
tar xvf sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
rm sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
ls -lh sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01
fi
MODEL_NAME="sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01"
if [ ! -f ./${MODEL_NAME}/encoder.int8.onnx ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/${MODEL_NAME}.tar.bz2
tar xvf ${MODEL_NAME}.tar.bz2
rm ${MODEL_NAME}.tar.bz2
ls -lh ${MODEL_NAME}
fi

Comment on lines +741 to +745
if [ ! -f ./sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/encoder.int8.onnx ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
tar xvf sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
rm sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
fi

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The model name sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01 is repeated. To improve maintainability, consider defining it as a local variable at the beginning of the function. This will make it easier to update in the future.

Suggested change
if [ ! -f ./sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/encoder.int8.onnx ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
tar xvf sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
rm sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
fi
local model_name="sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01"
if [ ! -f ./${model_name}/encoder.int8.onnx ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/${model_name}.tar.bz2
tar xvf ${model_name}.tar.bz2
rm ${model_name}.tar.bz2
fi

Comment on lines +748 to +756
kotlinc-jvm -include-runtime -d $out_filename \
test_offline_cohere_transcribe.kt \
FeatureConfig.kt \
QnnConfig.kt \
HomophoneReplacerConfig.kt \
OfflineRecognizer.kt \
OfflineStream.kt \
WaveReader.kt \
faked-asset-manager.kt

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The file faked-asset-manager.kt is included in the compilation but does not seem to be used by test_offline_cohere_transcribe.kt. To keep the build dependencies clean, it's better to remove unused files from the compilation list.

Suggested change
kotlinc-jvm -include-runtime -d $out_filename \
test_offline_cohere_transcribe.kt \
FeatureConfig.kt \
QnnConfig.kt \
HomophoneReplacerConfig.kt \
OfflineRecognizer.kt \
OfflineStream.kt \
WaveReader.kt \
faked-asset-manager.kt
kotlinc-jvm -include-runtime -d $out_filename \
test_offline_cohere_transcribe.kt \
FeatureConfig.kt \
QnnConfig.kt \
HomophoneReplacerConfig.kt \
OfflineRecognizer.kt \
OfflineStream.kt \
WaveReader.kt

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
kotlin-api-examples/test_offline_cohere_transcribe.kt (1)

26-28: Use an explicit locale for numeric formatting.

Lines 26–28 use String.format() without an explicit locale, causing numeric output (decimal separator, grouping) to vary based on the system's default locale. This can lead to inconsistent results across environments.

Suggested patch
+import java.util.Locale
...
-  println(String.format("-- elapsed : %.3f seconds", timeElapsedSeconds))
-  println(String.format("-- audio duration: %.3f seconds", audioDuration))
-  println(String.format("-- real-time factor (RTF): %.3f", realTimeFactor))
+  println(String.format(Locale.US, "-- elapsed : %.3f seconds", timeElapsedSeconds))
+  println(String.format(Locale.US, "-- audio duration: %.3f seconds", audioDuration))
+  println(String.format(Locale.US, "-- real-time factor (RTF): %.3f", realTimeFactor))
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@kotlin-api-examples/test_offline_cohere_transcribe.kt` around lines 26 - 28,
The String.format calls used in println in test_offline_cohere_transcribe.kt
produce locale-dependent numeric output; update those String.format invocations
(the three lines that print timeElapsedSeconds, audioDuration, and
realTimeFactor) to pass an explicit Locale (e.g., Locale.US or Locale.ROOT) as
the first argument so decimal separators and formatting are consistent across
environments.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@java-api-examples/run-non-streaming-decode-file-cohere-transcribe.sh`:
- Line 36: The JVM library path argument is unquoted in the shell script causing
word-splitting/globbing for paths with spaces; update the invocation that sets
-Djava.library.path (in run-non-streaming-decode-file-cohere-transcribe.sh) to
quote the value (e.g., "-Djava.library.path=$PWD/../build/lib") so the entire
path is treated as a single argument.

---

Nitpick comments:
In `@kotlin-api-examples/test_offline_cohere_transcribe.kt`:
- Around line 26-28: The String.format calls used in println in
test_offline_cohere_transcribe.kt produce locale-dependent numeric output;
update those String.format invocations (the three lines that print
timeElapsedSeconds, audioDuration, and realTimeFactor) to pass an explicit
Locale (e.g., Locale.US or Locale.ROOT) as the first argument so decimal
separators and formatting are consistent across environments.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a4bbc366-39f9-463f-8157-8030754bffbe

📥 Commits

Reviewing files that changed from the base of the PR and between d4465bc and 0b2b130.

📒 Files selected for processing (12)
  • .github/workflows/jni.yaml
  • .github/workflows/run-java-test.yaml
  • java-api-examples/NonStreamingDecodeFileCohereTranscribe.java
  • java-api-examples/run-non-streaming-decode-file-cohere-transcribe.sh
  • kotlin-api-examples/run.sh
  • kotlin-api-examples/test_offline_cohere_transcribe.kt
  • sherpa-onnx/java-api/Makefile
  • sherpa-onnx/java-api/src/main/java/com/k2fsa/sherpa/onnx/OfflineCohereTranscribeModelConfig.java
  • sherpa-onnx/java-api/src/main/java/com/k2fsa/sherpa/onnx/OfflineModelConfig.java
  • sherpa-onnx/jni/offline-recognizer.cc
  • sherpa-onnx/jni/sherpa-onnx-symbols.exp
  • sherpa-onnx/kotlin-api/OfflineRecognizer.kt

fi

java \
-Djava.library.path=$PWD/../build/lib \

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
rg -n '\-Djava\.library\.path=\$PWD/\.\./build/lib' java-api-examples/run-non-streaming-decode-file-cohere-transcribe.sh -C1

Repository: k2-fsa/sherpa-onnx

Length of output: 173


Quote the JVM library path value.

Line 36 expands an unquoted path, which can split or glob in bash if the path contains spaces or special characters.

Suggested patch
-  -Djava.library.path=$PWD/../build/lib \
+  -Djava.library.path="$PWD/../build/lib" \
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
-Djava.library.path=$PWD/../build/lib \
-Djava.library.path="$PWD/../build/lib" \
🧰 Tools
🪛 Shellcheck (0.11.0)

[info] 36-36: Double quote to prevent globbing and word splitting.

(SC2086)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@java-api-examples/run-non-streaming-decode-file-cohere-transcribe.sh` at line
36, The JVM library path argument is unquoted in the shell script causing
word-splitting/globbing for paths with spaces; update the invocation that sets
-Djava.library.path (in run-non-streaming-decode-file-cohere-transcribe.sh) to
quote the value (e.g., "-Djava.library.path=$PWD/../build/lib") so the entire
path is treated as a single argument.

@csukuangfj
csukuangfj merged commit 99206ad into k2-fsa:master Apr 3, 2026
18 of 27 checks passed
@csukuangfj
csukuangfj deleted the jni branch April 3, 2026 02:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant