examples : expose language detection probabilities to server example (#3044)

* feat: expose language detection probabilities to server.cpp

* feat: enhance language detection output in server.cpp

* Remove empty spaces.
This commit is contained in:
Sacha Arbonel 2025-04-28 18:25:45 +02:00 committed by GitHub
parent b7db9e7aac
commit f0171f0616
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -927,13 +927,25 @@ int main(int argc, char ** argv) {
} else if (params.response_format == vjson_format) { } else if (params.response_format == vjson_format) {
/* try to match openai/whisper's Python format */ /* try to match openai/whisper's Python format */
std::string results = output_str(ctx, params, pcmf32s); std::string results = output_str(ctx, params, pcmf32s);
// Get language probabilities
std::vector<float> lang_probs(whisper_lang_max_id() + 1, 0.0f);
const auto detected_lang_id = whisper_lang_auto_detect(ctx, 0, params.n_threads, lang_probs.data());
json jres = json{ json jres = json{
{"task", params.translate ? "translate" : "transcribe"}, {"task", params.translate ? "translate" : "transcribe"},
{"language", whisper_lang_str_full(whisper_full_lang_id(ctx))}, {"language", whisper_lang_str_full(whisper_full_lang_id(ctx))},
{"duration", float(pcmf32.size())/WHISPER_SAMPLE_RATE}, {"duration", float(pcmf32.size())/WHISPER_SAMPLE_RATE},
{"text", results}, {"text", results},
{"segments", json::array()} {"segments", json::array()},
{"detected_language", whisper_lang_str_full(detected_lang_id)},
{"detected_language_probability", lang_probs[detected_lang_id]},
{"language_probabilities", json::object()}
}; };
// Add all language probabilities
for (int i = 0; i <= whisper_lang_max_id(); ++i) {
if (lang_probs[i] > 0.001f) { // Only include non-negligible probabilities
jres["language_probabilities"][whisper_lang_str(i)] = lang_probs[i];
}
}
const int n_segments = whisper_full_n_segments(ctx); const int n_segments = whisper_full_n_segments(ctx);
for (int i = 0; i < n_segments; ++i) for (int i = 0; i < n_segments; ++i)
{ {