add tts max length for kokoro (+1 squashed commits)

Squashed commits:

[c1c6feaf] add tts max length for kokoro
This commit is contained in:
Concedo 2025-08-24 17:42:49 +08:00
parent 0e2b031159
commit b0a8d11584
2 changed files with 33 additions and 3 deletions

View file

@ -64,7 +64,7 @@ dry_seq_break_max = 128
extra_images_max = 4 extra_images_max = 4
# global vars # global vars
KcppVersion = "1.98" KcppVersion = "1.98.1"
showdebug = True showdebug = True
kcpp_instance = None #global running instance kcpp_instance = None #global running instance
global_memory = {"tunnel_url": "", "restart_target":"", "input_to_exit":False, "load_complete":False, "restart_override_config_target":""} global_memory = {"tunnel_url": "", "restart_target":"", "input_to_exit":False, "load_complete":False, "restart_override_config_target":""}

View file

@ -478,6 +478,32 @@ std::string trim_words(const std::string& input, const std::string& separator, s
return result.str(); return result.str();
} }
static std::string TruncateToFirstNumberWords(const std::string& input, int limit) {
static const std::regex wordRegex(R"(\b[\w'-]+\b)");
std::sregex_iterator words_begin(input.begin(), input.end(), wordRegex);
std::sregex_iterator words_end;
int count = 0;
std::size_t cutoffPos = std::string::npos;
if(limit<=0)
{
return "";
}
for (auto it = words_begin; it != words_end; ++it) {
++count;
if (count >= limit) {
// position AFTER the last matched word
cutoffPos = it->position() + it->length();
break;
}
}
if (cutoffPos == std::string::npos) {
// fewer than N words, return original
return input;
}
// Preserve everything up to and including the Nth word
return input.substr(0, cutoffPos);
}
static llama_context * ttc_ctx = nullptr; //text to codes ctx static llama_context * ttc_ctx = nullptr; //text to codes ctx
static llama_context * cts_ctx = nullptr; //codes to speech static llama_context * cts_ctx = nullptr; //codes to speech
@ -562,6 +588,7 @@ bool ttstype_load_model(const tts_load_model_inputs inputs)
} }
ttsdebugmode = inputs.debugmode; ttsdebugmode = inputs.debugmode;
tts_max_len = inputs.ttsmaxlen;
// tts init // tts init
if (is_ttscpp_file) { if (is_ttscpp_file) {
@ -577,8 +604,6 @@ bool ttstype_load_model(const tts_load_model_inputs inputs)
nthreads = inputs.threads; nthreads = inputs.threads;
tts_max_len = inputs.ttsmaxlen;
tts_model_params.use_mmap = false; tts_model_params.use_mmap = false;
tts_model_params.use_mlock = false; tts_model_params.use_mlock = false;
tts_model_params.n_gpu_layers = inputs.gpulayers; //offload if possible tts_model_params.n_gpu_layers = inputs.gpulayers; //offload if possible
@ -692,6 +717,11 @@ static tts_generation_outputs ttstype_generate_ttscpp(const tts_generation_input
} }
} }
if(tts_max_len>0)
{
prompt = TruncateToFirstNumberWords(prompt,tts_max_len);
}
if(ttsdebugmode==1 && !tts_is_quiet) if(ttsdebugmode==1 && !tts_is_quiet)
{ {
printf("\nUsing Speaker ID: %d, Voice: %s", speaker_seed, voiceused.c_str()); printf("\nUsing Speaker ID: %d, Voice: %s", speaker_seed, voiceused.c_str());