Skip to content
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

java bindings #931

Merged
merged 6 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 46 additions & 43 deletions .gitignore
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/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Supported platforms:
- [x] Mac OS (Intel and Arm)
- [x] [iOS](examples/whisper.objc)
- [x] [Android](examples/whisper.android)
- [x] [Java](bindings/java/README.md)
- [x] Linux / [FreeBSD](https://github.com/ggerganov/whisper.cpp/issues/56#issuecomment-1350920264)
- [x] [WebAssembly](examples/whisper.wasm)
- [x] Windows ([MSVC](https://github.com/ggerganov/whisper.cpp/blob/master/.github/workflows/build.yml#L117-L144) and [MinGW](https://github.com/ggerganov/whisper.cpp/issues/168)]
Expand Down
124 changes: 124 additions & 0 deletions bindings/java/.idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions bindings/java/CMakeLists.txt
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)
63 changes: 63 additions & 0 deletions bindings/java/README.md
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.

Loading