Skip to content

Commit

Permalink
Merge pull request #1 from obs-ai/roy.prep_to_ship
Browse files Browse the repository at this point in the history
Prep to ship
  • Loading branch information
royshil authored Oct 24, 2023
2 parents be1403f + 91e32f0 commit 70e7564
Show file tree
Hide file tree
Showing 22 changed files with 352 additions and 151 deletions.
3 changes: 2 additions & 1 deletion .github/scripts/.build.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ ${_usage_host:-}"
-DCODESIGN_IDENTITY=${CODESIGN_IDENT:--}
)
cmake_build_args+=(--preset ${_preset} --parallel --config ${config} -- ONLY_ACTIVE_ARCH=NO -arch arm64 -arch x86_64)
# TODO: enable -arch arm64
cmake_build_args+=(--preset ${_preset} --parallel --config ${config} -- ONLY_ACTIVE_ARCH=NO -arch x86_64 -arch arm64)
cmake_install_args+=(build_macos --config ${config} --prefix "${project_root}/release/${config}")
local -a xcbeautify_opts=()
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
!CMakePresets.json
!LICENSE
!README.md
!patch_libobs.diff

# Exclude lock files
*.lock.json
Expand Down
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ if(ENABLE_QT)
endif()

add_subdirectory(src/ui)
target_sources(${CMAKE_PROJECT_NAME} PRIVATE src/translation-service/translation.cpp
src/plugin-main.c src/utils/config-data.cpp
src/translation-service/httpserver.cpp)
target_sources(${CMAKE_PROJECT_NAME} PRIVATE src/translation-service/translation.cpp src/plugin-main.c
src/utils/config-data.cpp src/translation-service/httpserver.cpp)

set_target_properties_plugin(${CMAKE_PROJECT_NAME} PROPERTIES OUTPUT_NAME ${_name})
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Current Features:
- Choice of CTranslate2 model file

Roadmap:
- Translation directly on OBS text sources
- Translation directly on OBS text sources, e.g. with a filter

Check out our other plugins:
- [Background Removal](https://github.com/royshil/obs-backgroundremoval) removes background from webcam without a green screen.
Expand All @@ -34,6 +34,12 @@ Check out our other plugins:
## Download
Check out the [latest releases](https://github.com/obs-ai/obs-polyglot/releases) for downloads and install instructions.

### Models
You need to download a CT2 model for the translation service to work.
Here are download links for models that are compatible with the plugin:
- https://forum.opennmt.net/t/nllb-200-with-ctranslate2/5090 NLLB-200 multilingual model (recommended)
- https://huggingface.co/models?sort=trending&search=ctranslate CTranslate2 models on HuggingFace

## Building

The plugin was built and tested on Mac OSX (Intel & Apple silicon), Windows and Linux.
Expand Down
164 changes: 105 additions & 59 deletions cmake/BuildCTranslate2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,114 @@
include(ExternalProject)
include(FetchContent)

# Build with OpenBLAS
set(OpenBLAS_URL "https://github.com/xianyi/OpenBLAS/releases/download/v0.3.24/OpenBLAS-0.3.24-x64.zip")
set(OpenBLAS_SHA256 "6335128ee7117ea2dd2f5f96f76dafc17256c85992637189a2d5f6da0c608163")
FetchContent_Declare(
openblas
URL ${OpenBLAS_URL}
URL_HASH SHA256=${OpenBLAS_SHA256})
FetchContent_MakeAvailable(openblas)
set(OpenBLAS_DIR ${openblas_SOURCE_DIR})
set(OPENBLAS_INCLUDE_DIR ${OpenBLAS_DIR}/include)
if(APPLE)

add_library(OpenBLAS STATIC IMPORTED)
set_target_properties(OpenBLAS PROPERTIES IMPORTED_LOCATION ${OpenBLAS_DIR}/lib/libopenblas.dll.a)
install(FILES ${OpenBLAS_DIR}/bin/libopenblas.dll DESTINATION "obs-plugins/64bit")
FetchContent_Declare(
ctranslate2_fetch
URL https://github.com/obs-ai/obs-ai-ctranslate2-dep/releases/download/1.0.0/libctranslate2-macos-Release-1.0.0.tar.gz
URL_HASH SHA256=8e55a6ed4fb17ac556ad0e020ddab619584e3ceb4c9497a816f819bd8fd36443)
FetchContent_MakeAvailable(ctranslate2_fetch)

set(CT2_VERSION "3.20.0")
set(CT2_URL "https://github.com/OpenNMT/CTranslate2.git")
add_library(ct2 INTERFACE)
target_link_libraries(ct2 INTERFACE "-framework Accelerate" ${ctranslate2_fetch_SOURCE_DIR}/lib/libctranslate2.a
${ctranslate2_fetch_SOURCE_DIR}/lib/libcpu_features.a)
set_target_properties(ct2 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${ctranslate2_fetch_SOURCE_DIR}/include)
target_compile_options(ct2 INTERFACE -Wno-shorten-64-to-32)

ExternalProject_Add(
ct2_build
DOWNLOAD_EXTRACT_TIMESTAMP true
GIT_REPOSITORY ${CT2_URL}
GIT_TAG v${CT2_VERSION}
GIT_PROGRESS 1
BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --config ${CMAKE_BUILD_TYPE}
CMAKE_GENERATOR ${CMAKE_GENERATOR}
INSTALL_COMMAND ${CMAKE_COMMAND} --install <BINARY_DIR> --config ${CMAKE_BUILD_TYPE}
CMAKE_ARGS -DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM}
-DCMAKE_OSX_DEPLOYMENT_TARGET=10.13
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES_}
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DBUILD_SHARED_LIBS=ON
-DWITH_CUDA=OFF
-DWITH_MKL=OFF
-DWITH_OPENBLAS=ON
-DOPENMP_RUNTIME=COMP
-DWITH_TESTS=OFF
-DWITH_EXAMPLES=OFF
-DWITH_TFLITE=OFF
-DWITH_TRT=OFF
-DWITH_PYTHON=OFF
-DWITH_SERVER=OFF
-DWITH_COVERAGE=OFF
-DWITH_PROFILING=OFF
-DBUILD_CLI=OFF
-DOPENBLAS_INCLUDE_DIR=${OPENBLAS_INCLUDE_DIR}
-DOPENBLAS_LIBRARY=${OpenBLAS_DIR}/lib/libopenblas.dll.a)
ExternalProject_Get_Property(ct2_build INSTALL_DIR)
else()
set(CT2_VERSION "3.20.0")
set(CT2_URL "https://github.com/OpenNMT/CTranslate2.git")

add_library(ct2::ct2 SHARED IMPORTED GLOBAL)
set_target_properties(
ct2::ct2 PROPERTIES IMPORTED_LOCATION
${INSTALL_DIR}/bin/${CMAKE_SHARED_LIBRARY_PREFIX}ctranslate2${CMAKE_SHARED_LIBRARY_SUFFIX})
set_target_properties(
ct2::ct2 PROPERTIES IMPORTED_IMPLIB
${INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}ctranslate2${CMAKE_STATIC_LIBRARY_SUFFIX})
if(WIN32)
# Build with OpenBLAS

install(FILES ${INSTALL_DIR}/bin/${CMAKE_SHARED_LIBRARY_PREFIX}ctranslate2${CMAKE_SHARED_LIBRARY_SUFFIX}
DESTINATION "obs-plugins/64bit")
set(OpenBLAS_URL "https://github.com/xianyi/OpenBLAS/releases/download/v0.3.24/OpenBLAS-0.3.24-x64.zip")
set(OpenBLAS_SHA256 "6335128ee7117ea2dd2f5f96f76dafc17256c85992637189a2d5f6da0c608163")
FetchContent_Declare(
openblas_fetch
URL ${OpenBLAS_URL}
URL_HASH SHA256=${OpenBLAS_SHA256})
FetchContent_MakeAvailable(openblas_fetch)
set(OpenBLAS_DIR ${openblas_fetch_SOURCE_DIR})
set(OPENBLAS_INCLUDE_DIR ${OpenBLAS_DIR}/include)

add_library(ct2 INTERFACE)
add_dependencies(ct2 ct2_build)
target_link_libraries(ct2 INTERFACE ct2::ct2)
set_target_properties(ct2::ct2 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)
add_library(openblas STATIC IMPORTED)
set_target_properties(openblas PROPERTIES IMPORTED_LOCATION ${OpenBLAS_DIR}/lib/libopenblas.dll.a)
install(FILES ${OpenBLAS_DIR}/bin/libopenblas.dll DESTINATION "obs-plugins/64bit")

set(CT2_OPENBLAS_CMAKE_ARGS -DOPENBLAS_INCLUDE_DIR=${OPENBLAS_INCLUDE_DIR}
-DOPENBLAS_LIBRARY=${OpenBLAS_DIR}/lib/libopenblas.dll.a -DWITH_OPENBLAS=ON)
else()
set(CT2_OPENBLAS_CMAKE_ARGS -DWITH_OPENBLAS=OFF)
endif()

if(UNIX)
if(APPLE)
set(CT2_CMAKE_PLATFORM_OPTIONS -DCMAKE_OSX_DEPLOYMENT_TARGET=10.13 -DBUILD_SHARED_LIBS=OFF -DWITH_ACCELERATE=ON
-DOPENMP_RUNTIME=NONE -DCMAKE_OSX_ARCHITECTURES=arm64)
else()
set(CT2_CMAKE_PLATFORM_OPTIONS -DBUILD_SHARED_LIBS=OFF -DOPENMP_RUNTIME=NONE -DCMAKE_POSITION_INDEPENDENT_CODE=ON)
endif()
set(CT2_LIB_INSTALL_LOCATION lib/${CMAKE_SHARED_LIBRARY_PREFIX}ctranslate2${CMAKE_STATIC_LIBRARY_SUFFIX})
else()
set(CT2_CMAKE_PLATFORM_OPTIONS -DBUILD_SHARED_LIBS=ON -DOPENMP_RUNTIME=COMP)
set(CT2_LIB_INSTALL_LOCATION bin/${CMAKE_SHARED_LIBRARY_PREFIX}ctranslate2${CMAKE_SHARED_LIBRARY_SUFFIX})
endif()

ExternalProject_Add(
ct2_build
GIT_REPOSITORY ${CT2_URL}
GIT_TAG v${CT2_VERSION}
GIT_PROGRESS 1
BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --config ${CMAKE_BUILD_TYPE}
CMAKE_GENERATOR ${CMAKE_GENERATOR}
INSTALL_COMMAND ${CMAKE_COMMAND} --install <BINARY_DIR> --config ${CMAKE_BUILD_TYPE}
BUILD_BYPRODUCTS <INSTALL_DIR>/${CT2_LIB_INSTALL_LOCATION}
CMAKE_ARGS -DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM}
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DWITH_CUDA=OFF
-DWITH_MKL=OFF
-DWITH_TESTS=OFF
-DWITH_EXAMPLES=OFF
-DWITH_TFLITE=OFF
-DWITH_TRT=OFF
-DWITH_PYTHON=OFF
-DWITH_SERVER=OFF
-DWITH_COVERAGE=OFF
-DWITH_PROFILING=OFF
-DBUILD_CLI=OFF
${CT2_OPENBLAS_CMAKE_ARGS}
${CT2_CMAKE_PLATFORM_OPTIONS})
ExternalProject_Get_Property(ct2_build INSTALL_DIR)

if(UNIX)
# Get cpu_features from the CTranslate2 build - only for x86_64 builds if(APPLE)
# ExternalProject_Get_Property(ct2_build BINARY_DIR) add_library(ct2::cpu_features STATIC IMPORTED GLOBAL)
# set_target_properties( ct2::cpu_features PROPERTIES IMPORTED_LOCATION
# ${BINARY_DIR}/third_party/cpu_features/RelWithDebInfo/libcpu_features.a) endif()

add_library(ct2::ct2 STATIC IMPORTED GLOBAL)
else()
add_library(ct2::ct2 SHARED IMPORTED GLOBAL)
set_target_properties(
ct2::ct2 PROPERTIES IMPORTED_IMPLIB
${INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}ctranslate2${CMAKE_STATIC_LIBRARY_SUFFIX})
install(FILES ${INSTALL_DIR}/${CT2_LIB_INSTALL_LOCATION} DESTINATION "obs-plugins/64bit")
endif()
add_dependencies(ct2::ct2 ct2_build)
set_target_properties(ct2::ct2 PROPERTIES IMPORTED_LOCATION ${INSTALL_DIR}/${CT2_LIB_INSTALL_LOCATION})

add_library(ct2 INTERFACE)
if(APPLE)
target_link_libraries(
ct2
INTERFACE
"-framework Accelerate /Users/roy_shilkrot/Downloads/obs-ai-ctranslate2-dep/CTranslate2-3.20.0/release/universal/RelWithDebInfo/lib/libctranslate2.a"
)
else()
target_link_libraries(ct2 INTERFACE ct2::ct2)
endif()
set_target_properties(ct2::ct2 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)

endif()
3 changes: 1 addition & 2 deletions cmake/BuildCppHTTPLib.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

set(CppHTTPLib_URL "https://github.com/yhirose/cpp-httplib/archive/refs/tags/v0.14.1.tar.gz")
set(CppHTTPLib_HASH SHA256=2D4FB5544DA643E5D0A82585555D8B7502B4137EB321A4ABBB075E21D2F00E96)

Expand All @@ -10,5 +9,5 @@ FetchContent_Declare(
URL_HASH ${CppHTTPLib_HASH})
FetchContent_MakeAvailable(cpphttplib)

add_library(cpphttplib INTERFACE)
add_library(cpphttplib INTERFACE EXCLUDE_FROM_ALL)
target_include_directories(cpphttplib INTERFACE ${cpphttplib_SOURCE_DIR})
81 changes: 48 additions & 33 deletions cmake/BuildSentencepiece.cmake
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
# build sentencepiece from "https://github.com/google/sentencepiece.git"

include(ExternalProject)

set(SP_URL
"https://github.com/google/sentencepiece.git"
CACHE STRING "URL of sentencepiece repository")

ExternalProject_Add(
sentencepiece_build
DOWNLOAD_EXTRACT_TIMESTAMP true
GIT_REPOSITORY ${SP_URL}
GIT_TAG v0.1.99
BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --config ${CMAKE_BUILD_TYPE}
CMAKE_GENERATOR ${CMAKE_GENERATOR}
INSTALL_COMMAND ${CMAKE_COMMAND} --install <BINARY_DIR> --config ${CMAKE_BUILD_TYPE}
CMAKE_ARGS -DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} -DCMAKE_OSX_DEPLOYMENT_TARGET=10.13
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES_} -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})
ExternalProject_Get_Property(sentencepiece_build INSTALL_DIR)

add_library(libsentencepiece SHARED IMPORTED GLOBAL)
set_target_properties(
libsentencepiece
PROPERTIES IMPORTED_LOCATION
${INSTALL_DIR}/bin/${CMAKE_SHARED_LIBRARY_PREFIX}sentencepiece${CMAKE_SHARED_LIBRARY_SUFFIX})
set_target_properties(
libsentencepiece
PROPERTIES IMPORTED_IMPLIB
${INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}sentencepiece${CMAKE_STATIC_LIBRARY_SUFFIX})

add_library(sentencepiece INTERFACE)
add_dependencies(sentencepiece sentencepiece_build)
target_link_libraries(sentencepiece INTERFACE libsentencepiece)
set_target_properties(libsentencepiece PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)
if(APPLE)

include(FetchContent)

FetchContent_Declare(
sentencepiece_fetch
URL https://github.com/obs-ai/obs-ai-ctranslate2-dep/releases/download/1.0.0/libsentencepiece-macos-Release-1.0.0.tar.gz
URL_HASH SHA256=67f58a8e97c14db1bc69becd507ffe69326948f371bf874fe919157d7d65aff4)
FetchContent_MakeAvailable(sentencepiece_fetch)
add_library(sentencepiece INTERFACE)
target_link_libraries(sentencepiece INTERFACE ${sentencepiece_fetch_SOURCE_DIR}/lib/libsentencepiece.a)
set_target_properties(sentencepiece PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${sentencepiece_fetch_SOURCE_DIR}/include)

else()

set(SP_URL
"https://github.com/google/sentencepiece.git"
CACHE STRING "URL of sentencepiece repository")

set(SP_CMAKE_OPTIONS -DSPM_ENABLE_SHARED=OFF)
set(SENTENCEPIECE_INSTALL_LIB_LOCATION lib/${CMAKE_STATIC_LIBRARY_PREFIX}sentencepiece${CMAKE_STATIC_LIBRARY_SUFFIX})

include(ExternalProject)

ExternalProject_Add(
sentencepiece_build
GIT_REPOSITORY ${SP_URL}
GIT_TAG v0.1.99
BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --config ${CMAKE_BUILD_TYPE}
CMAKE_GENERATOR ${CMAKE_GENERATOR}
INSTALL_COMMAND ${CMAKE_COMMAND} --install <BINARY_DIR> --config ${CMAKE_BUILD_TYPE}
BUILD_BYPRODUCTS <INSTALL_DIR>/${SENTENCEPIECE_INSTALL_LIB_LOCATION}
CMAKE_ARGS -DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} ${SP_CMAKE_OPTIONS})
ExternalProject_Get_Property(sentencepiece_build INSTALL_DIR)

add_library(libsentencepiece STATIC IMPORTED GLOBAL)
add_dependencies(libsentencepiece sentencepiece_build)
set_target_properties(libsentencepiece PROPERTIES IMPORTED_LOCATION
${INSTALL_DIR}/${SENTENCEPIECE_INSTALL_LIB_LOCATION})

add_library(sentencepiece INTERFACE)
add_dependencies(sentencepiece libsentencepiece)
target_link_libraries(sentencepiece INTERFACE libsentencepiece)
target_include_directories(sentencepiece INTERFACE ${INSTALL_DIR}/include)

endif()
10 changes: 9 additions & 1 deletion cmake/common/buildspec_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,19 @@ function(_setup_obs_studio)
set(_cmake_version "2.0.0")
elseif(OS_MACOS)
set(_cmake_generator "Xcode")
set(_cmake_arch "-DCMAKE_OSX_ARCHITECTURES:STRING='arm64;x86_64'")
# TODO: enable arm64; currently fails
set(_cmake_arch "-DCMAKE_OSX_ARCHITECTURES:STRING='x86_64;arm64'")
set(_cmake_extra "-DCMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}")
set(_cmake_version "3.0.0")
endif()

message(STATUS "Patch libobs")
execute_process(
COMMAND patch --forward "libobs/CMakeLists.txt" "${CMAKE_CURRENT_SOURCE_DIR}/patch_libobs.diff"
RESULT_VARIABLE _process_result
WORKING_DIRECTORY "${dependencies_dir}/${_obs_destination}")
message(STATUS "Patch - done")

message(STATUS "Configure ${label} (${arch})")
execute_process(
COMMAND
Expand Down
2 changes: 1 addition & 1 deletion cmake/macos/compilerconfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ else()
# Enable color diagnostics for AppleClang
set(CMAKE_COLOR_DIAGNOSTICS ON)
# Set universal architectures via CMake flag for non-Xcode generators
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")

# Enable compiler and build tracing (requires Ninja generator)
if(ENABLE_COMPILER_TRACE AND CMAKE_GENERATOR STREQUAL "Ninja")
Expand Down
12 changes: 12 additions & 0 deletions patch_libobs.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/libobs/CMakeLists.txt b/libobs/CMakeLists.txt
index d2e2671..d7797c6 100644
--- a/libobs/CMakeLists.txt
+++ b/libobs/CMakeLists.txt
@@ -287,6 +287,7 @@ set(public_headers
util/base.h
util/bmem.h
util/c99defs.h
+ util/config-file.h
util/darray.h
util/profiler.h
util/sse-intrin.h
Loading

0 comments on commit 70e7564

Please sign in to comment.