Joas Dev commited on
Commit
f001158
·
unverified ·
1 Parent(s): dc0917f

bindings.java : update java example (#3281)

Browse files

This commit updates the example in the README.md file as the current Java example code is not working.

Resolves: https://github.com/ggml-org/whisper.cpp/issues/2860

Files changed (1) hide show
  1. bindings/java/README.md +31 -15
bindings/java/README.md CHANGED
@@ -23,26 +23,42 @@ import io.github.ggerganov.whispercpp.WhisperCpp;
23
  public class Example {
24
 
25
  public static void main(String[] args) {
 
26
  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");
30
  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());
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  }
 
 
 
43
  } finally {
44
- whisper.freeContext(context);
45
  }
 
46
  }
47
  }
48
  ```
 
23
  public class Example {
24
 
25
  public static void main(String[] args) {
26
+
27
  WhisperCpp whisper = new WhisperCpp();
 
 
 
28
  try {
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
+
54
  }
55
+
56
+ } catch (IOException e) {
57
+ e.printStackTrace();
58
  } finally {
59
+ whisper.close();
60
  }
61
+
62
  }
63
  }
64
  ```