Skip to content

Commit c85b1ae

Browse files
authored
bindings.java : update java example (#3281)
This commit updates the example in the README.md file as the current Java example code is not working. Resolves: #2860
1 parent 0083335 commit c85b1ae

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

bindings/java/README.md

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,42 @@ import io.github.ggerganov.whispercpp.WhisperCpp;
2323
public class Example {
2424

2525
public static void main(String[] args) {
26+
2627
WhisperCpp whisper = new WhisperCpp();
27-
// By default, models are loaded from ~/.cache/whisper/ and are usually named "ggml-${name}.bin"
28-
// or you can provide the absolute path to the model file.
29-
long context = whisper.initContext("base.en");
3028
try {
31-
var whisperParams = whisper.getFullDefaultParams(WhisperSamplingStrategy.WHISPER_SAMPLING_GREEDY);
32-
// custom configuration if required
33-
whisperParams.temperature_inc = 0f;
34-
35-
var samples = readAudio(); // divide each value by 32767.0f
36-
whisper.fullTranscribe(whisperParams, samples);
37-
38-
int segmentCount = whisper.getTextSegmentCount(context);
39-
for (int i = 0; i < segmentCount; i++) {
40-
String text = whisper.getTextSegment(context, i);
41-
System.out.println(segment.getText());
29+
// By default, models are loaded from ~/.cache/whisper/ and are usually named "ggml-${name}.bin"
30+
// or you can provide the absolute path to the model file.
31+
whisper.initContext("../ggml-base.en.bin");
32+
WhisperFullParams.ByValue whisperParams = whisper.getFullDefaultParams(WhisperSamplingStrategy.WHISPER_SAMPLING_BEAM_SEARCH);
33+
34+
// custom configuration if required
35+
//whisperParams.n_threads = 8;
36+
whisperParams.temperature = 0.0f;
37+
whisperParams.temperature_inc = 0.2f;
38+
//whisperParams.language = "en";
39+
40+
float[] samples = readAudio(); // divide each value by 32767.0f
41+
List<WhisperSegment> whisperSegmentList = whisper.fullTranscribeWithTime(whisperParams, samples);
42+
43+
for (WhisperSegment whisperSegment : whisperSegmentList) {
44+
45+
long start = whisperSegment.getStart();
46+
long end = whisperSegment.getEnd();
47+
48+
String text = whisperSegment.getSentence();
49+
50+
System.out.println("start: "+start);
51+
System.out.println("end: "+end);
52+
System.out.println("text: "+text);
53+
4254
}
55+
56+
} catch (IOException e) {
57+
e.printStackTrace();
4358
} finally {
44-
whisper.freeContext(context);
59+
whisper.close();
4560
}
61+
4662
}
4763
}
4864
```

0 commit comments

Comments
 (0)