mirror of
https://github.com/ggerganov/whisper.cpp.git
synced 2025-06-25 20:21:42 +02:00
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: https://github.com/ggml-org/whisper.cpp/issues/2860
This commit is contained in:
parent
0083335ba0
commit
c85b1ae84e
@ -23,26 +23,42 @@ import io.github.ggerganov.whispercpp.WhisperCpp;
|
|||||||
public class Example {
|
public class Example {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
WhisperCpp whisper = new WhisperCpp();
|
WhisperCpp whisper = new WhisperCpp();
|
||||||
// By default, models are loaded from ~/.cache/whisper/ and are usually named "ggml-${name}.bin"
|
|
||||||
// or you can provide the absolute path to the model file.
|
|
||||||
long context = whisper.initContext("base.en");
|
|
||||||
try {
|
try {
|
||||||
var whisperParams = whisper.getFullDefaultParams(WhisperSamplingStrategy.WHISPER_SAMPLING_GREEDY);
|
// By default, models are loaded from ~/.cache/whisper/ and are usually named "ggml-${name}.bin"
|
||||||
// custom configuration if required
|
// or you can provide the absolute path to the model file.
|
||||||
whisperParams.temperature_inc = 0f;
|
whisper.initContext("../ggml-base.en.bin");
|
||||||
|
WhisperFullParams.ByValue whisperParams = whisper.getFullDefaultParams(WhisperSamplingStrategy.WHISPER_SAMPLING_BEAM_SEARCH);
|
||||||
|
|
||||||
|
// custom configuration if required
|
||||||
|
//whisperParams.n_threads = 8;
|
||||||
|
whisperParams.temperature = 0.0f;
|
||||||
|
whisperParams.temperature_inc = 0.2f;
|
||||||
|
//whisperParams.language = "en";
|
||||||
|
|
||||||
|
float[] samples = readAudio(); // divide each value by 32767.0f
|
||||||
|
List<WhisperSegment> whisperSegmentList = whisper.fullTranscribeWithTime(whisperParams, samples);
|
||||||
|
|
||||||
|
for (WhisperSegment whisperSegment : whisperSegmentList) {
|
||||||
|
|
||||||
var samples = readAudio(); // divide each value by 32767.0f
|
long start = whisperSegment.getStart();
|
||||||
whisper.fullTranscribe(whisperParams, samples);
|
long end = whisperSegment.getEnd();
|
||||||
|
|
||||||
int segmentCount = whisper.getTextSegmentCount(context);
|
String text = whisperSegment.getSentence();
|
||||||
for (int i = 0; i < segmentCount; i++) {
|
|
||||||
String text = whisper.getTextSegment(context, i);
|
System.out.println("start: "+start);
|
||||||
System.out.println(segment.getText());
|
System.out.println("end: "+end);
|
||||||
|
System.out.println("text: "+text);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
whisper.freeContext(context);
|
whisper.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user