Skip to content

Commit d51bc4a

Browse files
author
litongjava
committed
response json
1 parent 06d94e2 commit d51bc4a

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

stream_components_service.cpp

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,33 +69,46 @@ bool WhisperService::process(const float *samples, int sample_count) {
6969
wparams.prompt_n_tokens = service_params.no_context ? 0 : prompt_tokens.size();
7070

7171
// *** Run the actual inference!!! ***
72-
//if (whisper_full(ctx, wparams, samples, sample_count) != 0) {
73-
if (whisper_full_parallel(ctx, wparams, samples, sample_count,service_params.n_processors) != 0) {
72+
if (whisper_full(ctx, wparams, samples, sample_count) != 0) {
7473
return false;
7574
}
76-
77-
// Now sure whether n_iter and n_new_line should have ever been there...
78-
// *** SUSPICIOUS what happens by removing them? Are they essential?
79-
//if (!use_vad && (n_iter % n_new_line) == 0) {
80-
if (!audio_params.use_vad) {
81-
//printf("\n");
82-
83-
// keep part of the audio for next iteration to try to mitigate word boundary issues
84-
// *** I don't know if we need this...
85-
//pcmf32_old = std::vector<float>(pcmf32.end() - n_samples_keep, pcmf32.end());
86-
87-
// Add tokens of the last full length segment as the prompt
88-
if (!service_params.no_context) {
89-
prompt_tokens.clear();
90-
91-
const int n_segments = whisper_full_n_segments(ctx);
92-
for (int i = 0; i < n_segments; ++i) {
93-
const int token_count = whisper_full_n_tokens(ctx, i);
94-
for (int j = 0; j < token_count; ++j) {
95-
prompt_tokens.push_back(whisper_full_get_token_id(ctx, i, j));
96-
}
97-
}
98-
}
99-
}
75+
//if (whisper_full_parallel(ctx, wparams, samples, sample_count,service_params.n_processors) != 0) {
76+
// error:ggml_metal_get_buffer: error: buffer is nil
77+
//return false;
78+
79+
80+
// Now sure whether n_iter and n_new_line should have ever been there...
81+
// *** SUSPICIOUS what happens by removing them? Are they essential?
82+
//if (!use_vad && (n_iter % n_new_line) == 0) {
83+
// if (!audio_params.use_vad) {
84+
//printf("\n");
85+
86+
// keep part of the audio for next iteration to try to mitigate word boundary issues
87+
// *** I don't know if we need this...
88+
//pcmf32_old = std::vector<float>(pcmf32.end() - n_samples_keep, pcmf32.end());
89+
90+
// Add tokens of the last full length segment as the prompt
91+
// if (!service_params.no_context) {
92+
// prompt_tokens.
93+
//
94+
// clear();
95+
//
96+
// const int n_segments = whisper_full_n_segments(ctx);
97+
// for (
98+
// int i = 0;
99+
// i < n_segments;
100+
// ++i) {
101+
// const int token_count = whisper_full_n_tokens(ctx, i);
102+
// for (
103+
// int j = 0;
104+
// j < token_count;
105+
// ++j) {
106+
// prompt_tokens.
107+
// push_back(whisper_full_get_token_id(ctx, i, j)
108+
// );
109+
// }
110+
// }
111+
// }
112+
// }
100113
return true;
101114
}

whisper_server_base_on_uwebsockets.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,19 @@ int main(int argc, char **argv) {
9898
printf("isOk:%d\n", isOk);
9999
if (isOk) {
100100
nlohmann::json response;
101-
std::string result;
101+
nlohmann::json results = nlohmann::json::array(); // create JSON Array
102102

103103
const int n_segments = whisper_full_n_segments(whisperService.ctx);
104-
printf("n_segments:%d\n", n_segments);
105104
for (int i = 0; i < n_segments; ++i) {
106-
const char *text = whisper_full_get_segment_text(whisperService.ctx, i);
107-
const int64_t t0 = whisper_full_get_segment_t0(whisperService.ctx, i);
108-
const int64_t t1 = whisper_full_get_segment_t1(whisperService.ctx, i);
109-
printf("%lld-->%lld:%s\n", t0, t1, text);
105+
nlohmann::json segment;
106+
segment["t0"] = whisper_full_get_segment_t0(whisperService.ctx, i);
107+
segment["t1"] = whisper_full_get_segment_t1(whisperService.ctx, i);
108+
segment["sentence"] = whisper_full_get_segment_text(whisperService.ctx, i);
109+
110+
results.push_back(segment);
110111
}
111-
response["result"] = result;
112+
113+
response["result"] = results;
112114
ws->send(response.dump(), uWS::OpCode::TEXT);
113115
}
114116
}

0 commit comments

Comments
 (0)