Skip to content

Commit

Permalink
refactor: Add external model option to translation settings
Browse files Browse the repository at this point in the history
  • Loading branch information
royshil committed May 16, 2024
1 parent 86fc468 commit 4c36124
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
2 changes: 2 additions & 0 deletions data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ buffered_output="Buffered output (Experimental)"
translate_model="Translation Model"
Whisper-Based-Translation="Whisper-Based Translation"
sentence_psum_accept_thresh="Sentence prob. threshold"
external_model_folder="External model folder"
load_external_model="Load external model"
1 change: 1 addition & 0 deletions src/transcription-filter-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ struct transcription_filter_data {
// translation context
struct translation_context translation_ctx;
std::string translation_model_index;
std::string translation_model_path_external;

TokenBufferThread captions_monitor;

Expand Down
32 changes: 31 additions & 1 deletion src/transcription-filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ void transcription_filter_update(void *data, obs_data_t *s)
gf->translation_output = obs_data_get_string(s, "translate_output");
gf->suppress_sentences = obs_data_get_string(s, "suppress_sentences");
gf->translation_model_index = obs_data_get_string(s, "translate_model");
gf->translation_model_path_external =
obs_data_get_string(s, "translation_model_path_external");

if (new_translate != gf->translate) {
if (new_translate) {
Expand Down Expand Up @@ -800,6 +802,29 @@ obs_properties_t *transcription_filter_properties(void *data)
model_info.first.c_str());
}
}
// add external model option
obs_property_list_add_string(prop_translate_model, MT_("load_external_model"),
"!!!external!!!");
// add callback to handle the external model file selection
obs_property_t *translation_model_path_external = obs_properties_add_path(

Check failure on line 809 in src/transcription-filter.cpp

View workflow job for this annotation

GitHub Actions / Build Project 🧱 / Build for macOS 🍏 (arm64)

unused variable 'translation_model_path_external' [-Werror,-Wunused-variable]
translation_group, "translation_model_path_external", MT_("external_model_folder"),
OBS_PATH_DIRECTORY, "CT2 Model folder", NULL);
// Hide the external model file selection input
obs_property_set_visible(obs_properties_get(ppts, "translation_model_path_external"),
false);
// Add a callback to the model list to handle the external model file selection
obs_property_set_modified_callback(prop_translate_model, [](obs_properties_t *props,
obs_property_t *property,
obs_data_t *settings) {
UNUSED_PARAMETER(property);
// If the selected model is the external model, show the external model file selection
// input
const char *new_model_path = obs_data_get_string(settings, "translate_model");
const bool is_external = (strcmp(new_model_path, "!!!external!!!") == 0);
obs_property_set_visible(
obs_properties_get(props, "translation_model_path_external"), is_external);
return true;
});
// add target language selection
obs_property_t *prop_tgt = obs_properties_add_list(
translation_group, "translate_target_language", MT_("target_language"),
Expand Down Expand Up @@ -840,6 +865,11 @@ obs_properties_t *transcription_filter_properties(void *data)
obs_property_set_visible(obs_properties_get(props, prop),
translate_enabled);
}
const bool is_external = (strcmp(obs_data_get_string(settings, "translate_model"),
"!!!external!!!") == 0);
obs_property_set_visible(obs_properties_get(props,
"translation_model_path_external"),
is_external && translate_enabled);
return true;
});

Expand All @@ -855,7 +885,7 @@ obs_properties_t *transcription_filter_properties(void *data)
{"whisper_params_group", "log_words", "caption_to_stream", "buffer_size_msec",
"overlap_size_msec", "step_by_step_processing", "min_sub_duration",
"process_while_muted", "buffered_output", "vad_enabled", "log_level",
"suppress_sentences"}) {
"suppress_sentences", "sentence_psum_accept_thresh"}) {
obs_property_set_visible(obs_properties_get(props, prop_name.c_str()),
show_hide);
}
Expand Down
12 changes: 12 additions & 0 deletions src/translation/translation-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ void start_translation(struct transcription_filter_data *gf)
{
obs_log(LOG_INFO, "Starting translation...");

if (gf->translation_model_index == "!!!external!!!") {
obs_log(LOG_INFO, "External model selected.");
if (gf->translation_model_path_external.empty()) {
obs_log(LOG_ERROR, "External model path is empty.");
gf->translate = false;
return;
}
std::string model_file_found = gf->translation_model_path_external;
build_and_enable_translation(gf, model_file_found);
return;
}

const ModelInfo &translation_model_info = models_info[gf->translation_model_index];
std::string model_file_found = find_model_folder(translation_model_info);
if (model_file_found == "") {
Expand Down
2 changes: 1 addition & 1 deletion src/translation/translation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int build_translation_context(struct translation_context &translation_ctx)
obs_log(LOG_INFO, "Building translation context from '%s'...", local_model_path.c_str());
// find the SPM file in the model folder
std::string local_spm_path = find_file_in_folder_by_regex_expression(
local_model_path, "(sentencepiece|spm).*?\\.model");
local_model_path, "(sentencepiece|spm|spiece).*?\\.model");

try {
obs_log(LOG_INFO, "Loading SPM from %s", local_spm_path.c_str());
Expand Down

0 comments on commit 4c36124

Please sign in to comment.