Repository navigation
Add Pascal API for Cohere Transcribe #3463
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| { Copyright (c) 2026 Xiaomi Corporation } | ||
|
|
||
| { | ||
| This file shows how to use a non-streaming Cohere Transcribe model | ||
| to decode files. | ||
|
|
||
| You can download the model files from | ||
| https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models | ||
| } | ||
|
|
||
| program cohere_transcribe; | ||
|
|
||
| {$mode objfpc} | ||
|
|
||
| uses | ||
| sherpa_onnx, | ||
| DateUtils, | ||
| SysUtils; | ||
|
|
||
| var | ||
| Wave: TSherpaOnnxWave; | ||
| WaveFilename: AnsiString; | ||
|
|
||
| Config: TSherpaOnnxOfflineRecognizerConfig; | ||
| Recognizer: TSherpaOnnxOfflineRecognizer; | ||
| Stream: TSherpaOnnxOfflineStream; | ||
| RecognitionResult: TSherpaOnnxOfflineRecognizerResult; | ||
|
|
||
| Start: TDateTime; | ||
| Stop: TDateTime; | ||
|
|
||
| Elapsed: Single; | ||
| Duration: Single; | ||
| RealTimeFactor: Single; | ||
| begin | ||
| Initialize(Config); | ||
|
|
||
| Config.ModelConfig.CohereTranscribe.Encoder := './sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/encoder.int8.onnx'; | ||
| Config.ModelConfig.CohereTranscribe.Decoder := './sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/decoder.int8.onnx'; | ||
| Config.ModelConfig.CohereTranscribe.UsePunct := True; | ||
| Config.ModelConfig.CohereTranscribe.UseItn := True; | ||
| Config.ModelConfig.Tokens := './sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/tokens.txt'; | ||
| Config.ModelConfig.Provider := 'cpu'; | ||
| Config.ModelConfig.NumThreads := 1; | ||
| Config.ModelConfig.Debug := True; | ||
|
|
||
| WaveFilename := './sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/test_wavs/en.wav'; | ||
|
|
||
| Wave := SherpaOnnxReadWave(WaveFilename); | ||
|
|
||
| Recognizer := TSherpaOnnxOfflineRecognizer.Create(Config); | ||
| Stream := Recognizer.CreateStream(); | ||
| Stream.SetOption('language', 'en'); | ||
| Start := Now; | ||
|
|
||
| Stream.AcceptWaveform(Wave.Samples, Wave.SampleRate); | ||
| Recognizer.Decode(Stream); | ||
|
|
||
| RecognitionResult := Recognizer.GetResult(Stream); | ||
|
|
||
| Stop := Now; | ||
|
|
||
| Elapsed := MilliSecondsBetween(Stop, Start) / 1000; | ||
| Duration := Length(Wave.Samples) / Wave.SampleRate; | ||
| RealTimeFactor := Elapsed / Duration; | ||
|
|
||
| WriteLn(RecognitionResult.ToString); | ||
| WriteLn(Format('NumThreads %d', [Config.ModelConfig.NumThreads])); | ||
| WriteLn(Format('Elapsed %.3f s', [Elapsed])); | ||
| WriteLn(Format('Wave duration %.3f s', [Duration])); | ||
| WriteLn(Format('RTF = %.3f/%.3f = %.3f', [Elapsed, Duration, RealTimeFactor])); | ||
|
|
||
| FreeAndNil(Stream); | ||
| FreeAndNil(Recognizer); | ||
| end. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -ex | ||
|
|
||
| SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||
| SHERPA_ONNX_DIR=$(cd $SCRIPT_DIR/../.. && pwd) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: find . -type f -name "run-cohere-transcribe.sh" 2>/dev/nullRepository: k2-fsa/sherpa-onnx Length of output: 126 🏁 Script executed: cat -n ./pascal-api-examples/non-streaming-asr/run-cohere-transcribe.shRepository: k2-fsa/sherpa-onnx Length of output: 1874 Quote variable expansions to handle checkout paths with spaces. Line 6 and lines 36-37 use unquoted variable expansions that will break if the repository is checked out under a path containing spaces. Line 5 in the same script already demonstrates proper quoting practices for this scenario. 💡 Proposed fix-SHERPA_ONNX_DIR=$(cd $SCRIPT_DIR/../.. && pwd)
+SHERPA_ONNX_DIR=$(cd "$SCRIPT_DIR/../.." && pwd)
@@
fpc \
-dSHERPA_ONNX_USE_SHARED_LIBS \
- -Fu$SHERPA_ONNX_DIR/sherpa-onnx/pascal-api \
- -Fl$SHERPA_ONNX_DIR/build/install/lib \
+ "-Fu$SHERPA_ONNX_DIR/sherpa-onnx/pascal-api" \
+ "-Fl$SHERPA_ONNX_DIR/build/install/lib" \
./cohere_transcribe.pas🧰 Tools🪛 Shellcheck (0.11.0)[info] 6-6: Double quote to prevent globbing and word splitting. (SC2086) 🤖 Prompt for AI Agents |
||
|
|
||
| echo "SHERPA_ONNX_DIR: $SHERPA_ONNX_DIR" | ||
|
|
||
| if [[ ! -f ../../build/install/lib/libsherpa-onnx-c-api.dylib && ! -f ../../build/install/lib/libsherpa-onnx-c-api.so && ! -f ../../build/install/lib/sherpa-onnx-c-api.dll ]]; then | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # First, let's locate and examine the file
fd -t f "run-cohere-transcribe.sh" | head -5Repository: k2-fsa/sherpa-onnx Length of output: 124 🏁 Script executed: # Read the specific file to see the actual content
if [ -f "pascal-api-examples/non-streaming-asr/run-cohere-transcribe.sh" ]; then
cat -n "pascal-api-examples/non-streaming-asr/run-cohere-transcribe.sh"
fiRepository: k2-fsa/sherpa-onnx Length of output: 1874 🏁 Script executed: # Also check if there are similar scripts to understand the pattern
fd -t f "run-.*\.sh$" | grep -E "(non-streaming|streaming)" | head -10Repository: k2-fsa/sherpa-onnx Length of output: 616 🏁 Script executed: # Look for C API examples in other languages
fd -t f "run-.*\.sh$" | xargs grep -l "sherpa-onnx-c-api" | head -5Repository: k2-fsa/sherpa-onnx Length of output: 358 🏁 Script executed: # Check C examples specifically
fd -t f "run-.*\.sh$" c-api-examples/ 2>/dev/null | head -5Repository: k2-fsa/sherpa-onnx Length of output: 44 🏁 Script executed: # Look at another example of how library paths are handled
fd -t f "run-.*\.sh$" c-api-examples/ 2>/dev/null -exec grep -A 5 "export.*LIBRARY_PATH" {} + | head -30Repository: k2-fsa/sherpa-onnx Length of output: 44 🏁 Script executed: # Search for any case statements or Windows-specific handling in scripts
rg "MINGW|MSYS|CYGWIN|uname" --type shRepository: k2-fsa/sherpa-onnx Length of output: 44 🏁 Script executed: # Check if any scripts set PATH for libraries
rg "export PATH.*lib" --type shRepository: k2-fsa/sherpa-onnx Length of output: 44 🏁 Script executed: # Look at all pascal examples to see if they have similar issues
fd -t f "run-.*\.sh$" pascal-api-examples/ -exec cat -n {} + | grep -A 10 -B 2 "LIBRARY_PATH"Repository: k2-fsa/sherpa-onnx Length of output: 292 🏁 Script executed: # Look for PowerShell scripts or Windows batch files
fd -t f "\.(ps1|bat|cmd)$"Repository: k2-fsa/sherpa-onnx Length of output: 780 🏁 Script executed: # Read the pascal examples to see the pattern
cat pascal-api-examples/non-streaming-asr/run-dolphin-ctc.shRepository: k2-fsa/sherpa-onnx Length of output: 1504 🏁 Script executed: # Check if there's documentation about Windows support
fd -t f "README|WINDOWS|readme" | head -3 | xargs cat 2>/dev/null | head -50Repository: k2-fsa/sherpa-onnx Length of output: 2276 Add PATH export for Windows DLL discovery. Line 10 checks for Proposed fix-export LD_LIBRARY_PATH=$SHERPA_ONNX_DIR/build/install/lib:$LD_LIBRARY_PATH
-export DYLD_LIBRARY_PATH=$SHERPA_ONNX_DIR/build/install/lib:$DYLD_LIBRARY_PATH
+case "$(uname -s)" in
+ Darwin*)
+ export DYLD_LIBRARY_PATH="$SHERPA_ONNX_DIR/build/install/lib${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}"
+ ;;
+ MINGW*|MSYS*|CYGWIN*)
+ export PATH="$SHERPA_ONNX_DIR/build/install/lib:$PATH"
+ ;;
+ *)
+ export LD_LIBRARY_PATH="$SHERPA_ONNX_DIR/build/install/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
+ ;;
+esac🤖 Prompt for AI Agents |
||
| mkdir -p ../../build | ||
| pushd ../../build | ||
| cmake \ | ||
| -DCMAKE_INSTALL_PREFIX=./install \ | ||
| -DSHERPA_ONNX_ENABLE_PYTHON=OFF \ | ||
| -DSHERPA_ONNX_ENABLE_TESTS=OFF \ | ||
| -DSHERPA_ONNX_ENABLE_CHECK=OFF \ | ||
| -DBUILD_SHARED_LIBS=ON \ | ||
| -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ | ||
| .. | ||
|
|
||
| cmake --build . --target install --config Release | ||
| ls -lh lib | ||
| popd | ||
| fi | ||
|
|
||
| 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 | ||
|
|
||
| fpc \ | ||
| -dSHERPA_ONNX_USE_SHARED_LIBS \ | ||
| -Fu$SHERPA_ONNX_DIR/sherpa-onnx/pascal-api \ | ||
| -Fl$SHERPA_ONNX_DIR/build/install/lib \ | ||
| ./cohere_transcribe.pas | ||
|
|
||
| export LD_LIBRARY_PATH=$SHERPA_ONNX_DIR/build/install/lib:$LD_LIBRARY_PATH | ||
| export DYLD_LIBRARY_PATH=$SHERPA_ONNX_DIR/build/install/lib:$DYLD_LIBRARY_PATH | ||
|
|
||
| ./cohere_transcribe | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fail fast when the input WAV cannot be read.
SherpaOnnxReadWave()returnsSampleRate := 0on failure insherpa-onnx/pascal-api/sherpa_onnx.pas, Lines 1637-1643. Lines 56 and 64-65 then use that value immediately, so a missing/corrupt or emptyen.wavturns this example into anAcceptWaveformcall plus a divide-by-zero in the RTF math instead of a clear error.💡 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents