forked from ggerganov/whisper.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bindings : add java bindings (ggerganov#931)
* WIP - java bindings * updated README * failed attempt at JNI * fullTranscribe() test passes * tested on Ubuntu 20 * link to Java bindings
- Loading branch information
Showing
36 changed files
with
1,962 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,46 @@ | ||
*.o | ||
*.a | ||
.cache/ | ||
.coreml/ | ||
.test/ | ||
.vs/ | ||
.vscode/ | ||
.DS_Store | ||
|
||
build/ | ||
build-em/ | ||
build-debug/ | ||
build-release/ | ||
build-static/ | ||
build-cublas/ | ||
build-no-accel/ | ||
build-sanitize-addr/ | ||
build-sanitize-thread/ | ||
|
||
/main | ||
/stream | ||
/command | ||
/talk | ||
/talk-llama | ||
/bench | ||
/quantize | ||
|
||
arm_neon.h | ||
sync.sh | ||
libwhisper.a | ||
libwhisper.so | ||
compile_commands.json | ||
|
||
examples/arm_neon.h | ||
examples/whisper.objc/whisper.objc.xcodeproj/xcshareddata | ||
examples/whisper.objc/whisper.objc.xcodeproj/xcuserdata/ | ||
examples/whisper.objc/whisper.objc.xcodeproj/project.xcworkspace/xcuserdata | ||
|
||
extra/bench-gg.txt | ||
|
||
models/*.mlmodel | ||
models/*.mlmodelc | ||
models/*.mlpackage | ||
*.o | ||
*.a | ||
.cache/ | ||
.coreml/ | ||
.test/ | ||
.vs/ | ||
.vscode/ | ||
.DS_Store | ||
|
||
build/ | ||
build-em/ | ||
build-debug/ | ||
build-release/ | ||
build-static/ | ||
build-cublas/ | ||
build-no-accel/ | ||
build-sanitize-addr/ | ||
build-sanitize-thread/ | ||
|
||
/main | ||
/stream | ||
/command | ||
/talk | ||
/talk-llama | ||
/bench | ||
/quantize | ||
|
||
arm_neon.h | ||
sync.sh | ||
libwhisper.a | ||
libwhisper.so | ||
compile_commands.json | ||
|
||
examples/arm_neon.h | ||
examples/whisper.objc/whisper.objc.xcodeproj/xcshareddata | ||
examples/whisper.objc/whisper.objc.xcodeproj/xcuserdata/ | ||
examples/whisper.objc/whisper.objc.xcodeproj/project.xcworkspace/xcuserdata | ||
|
||
extra/bench-gg.txt | ||
|
||
models/*.mlmodel | ||
models/*.mlmodelc | ||
models/*.mlpackage | ||
bindings/java/.gradle/ | ||
bindings/java/.idea/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
|
||
project(whisper_java VERSION 1.4.2) | ||
|
||
# Set the target name and source file/s | ||
set(TARGET_NAME whisper_java) | ||
set(SOURCES src/main/cpp/whisper_java.cpp) | ||
|
||
# include <whisper.h> | ||
include_directories(../../) | ||
|
||
# Set the output directory for the DLL/shared library based on the platform as required by JNA | ||
if(WIN32) | ||
set(OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated/resources/main/win32-x86-64) | ||
elseif(UNIX AND NOT APPLE) | ||
set(OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated/resources/main/linux-x86-64) | ||
elseif(APPLE) | ||
set(OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated/resources/main/macos-x86-64) | ||
endif() | ||
|
||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIR}) | ||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIR}) | ||
|
||
# Create the whisper_java library | ||
add_library(${TARGET_NAME} SHARED ${SOURCES}) | ||
|
||
# Link against ../../build/Release/whisper.dll (or so/dynlib) | ||
target_link_directories(${TARGET_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../../../build/${CMAKE_BUILD_TYPE}) | ||
target_link_libraries(${TARGET_NAME} PRIVATE whisper) | ||
|
||
# Set the appropriate compiler flags for Windows, Linux, and macOS | ||
if(WIN32) | ||
target_compile_options(${TARGET_NAME} PRIVATE /W4 /D_CRT_SECURE_NO_WARNINGS) | ||
elseif(UNIX AND NOT APPLE) | ||
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra) | ||
elseif(APPLE) | ||
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra) | ||
endif() | ||
|
||
target_compile_definitions(${TARGET_NAME} PRIVATE WHISPER_SHARED) | ||
# add_definitions(-DWHISPER_SHARED) | ||
|
||
# Force CMake to save the libs to build/generated/resources/main/${os}-${arch} as required by JNA | ||
foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES}) | ||
string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG) | ||
set_target_properties(${TARGET_NAME} PROPERTIES | ||
RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${OUTPUT_DIR} | ||
LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${OUTPUT_DIR} | ||
ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${OUTPUT_DIR}) | ||
endforeach(OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Java JNI bindings for Whisper | ||
|
||
This package provides Java JNI bindings for whisper.cpp. They have been tested on: | ||
|
||
* <strike>Darwin (OS X) 12.6 on x64_64</strike> | ||
* Ubuntu on x86_64 | ||
* Windows on x86_64 | ||
|
||
The "low level" bindings are in `WhisperCppJnaLibrary` and `WhisperJavaJnaLibrary` which caches `whisper_full_params` and `whisper_context` in `whisper_java.cpp`. | ||
|
||
There are a lot of classes in the `callbacks`, `ggml`, `model` and `params` directories but most of them have not been tested. | ||
|
||
The most simple usage is as follows: | ||
|
||
```java | ||
import io.github.ggerganov.whispercpp.WhisperCpp; | ||
|
||
public class Example { | ||
|
||
public static void main(String[] args) { | ||
String modelpath; | ||
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. | ||
whisper.initContext("base.en"); | ||
|
||
long context = whisper.initContext(modelpath); | ||
try { | ||
whisper.fullTranscribe(context, samples); | ||
|
||
int segmentCount = whisper.getTextSegmentCount(context); | ||
for (int i = 0; i < segmentCount; i++) { | ||
String text = whisper.getTextSegment(context, i); | ||
System.out.println(segment.getText()); | ||
} | ||
} finally { | ||
whisper.freeContext(context); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Building & Testing | ||
|
||
In order to build, you need to have the JDK 8 or higher installed. Run the tests with: | ||
|
||
```bash | ||
git clone https://github.com/ggerganov/whisper.cpp.git | ||
cd whisper.cpp/bindings/java | ||
|
||
mkdir build | ||
pushd build | ||
cmake .. | ||
cmake --build . | ||
popd | ||
|
||
./gradlew build | ||
``` | ||
|
||
## License | ||
|
||
The license for the Go bindings is the same as the license for the rest of the whisper.cpp project, which is the MIT License. See the `LICENSE` file for more details. | ||
|
Oops, something went wrong.