Skip to content

Commit f0171f0

Browse files
authored
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.
1 parent b7db9e7 commit f0171f0

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

examples/server/server.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -926,14 +926,26 @@ int main(int argc, char ** argv) {
926926
res.set_content(ss.str(), "text/vtt");
927927
} else if (params.response_format == vjson_format) {
928928
/* try to match openai/whisper's Python format */
929-
std::string results = output_str(ctx, params, pcmf32s);
929+
std::string results = output_str(ctx, params, pcmf32s);
930+
// Get language probabilities
931+
std::vector<float> lang_probs(whisper_lang_max_id() + 1, 0.0f);
932+
const auto detected_lang_id = whisper_lang_auto_detect(ctx, 0, params.n_threads, lang_probs.data());
930933
json jres = json{
931934
{"task", params.translate ? "translate" : "transcribe"},
932935
{"language", whisper_lang_str_full(whisper_full_lang_id(ctx))},
933936
{"duration", float(pcmf32.size())/WHISPER_SAMPLE_RATE},
934937
{"text", results},
935-
{"segments", json::array()}
938+
{"segments", json::array()},
939+
{"detected_language", whisper_lang_str_full(detected_lang_id)},
940+
{"detected_language_probability", lang_probs[detected_lang_id]},
941+
{"language_probabilities", json::object()}
936942
};
943+
// Add all language probabilities
944+
for (int i = 0; i <= whisper_lang_max_id(); ++i) {
945+
if (lang_probs[i] > 0.001f) { // Only include non-negligible probabilities
946+
jres["language_probabilities"][whisper_lang_str(i)] = lang_probs[i];
947+
}
948+
}
937949
const int n_segments = whisper_full_n_segments(ctx);
938950
for (int i = 0; i < n_segments; ++i)
939951
{

0 commit comments

Comments
 (0)