Skip to content

Commit 0c21b4d

Browse files
author
litongmacos
committed
add get_result
1 parent 7f686cc commit 0c21b4d

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

common/utils.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#include <string>
77
#include <sstream>
88
#include <iomanip>
9+
#include <whisper.h>
10+
#include "../nlohmann/json.hpp"
11+
912

1013
std::string get_current_time() {
1114
auto now = std::chrono::system_clock::now();
@@ -23,4 +26,23 @@ std::string get_current_time() {
2326
long get_current_time_millis(){
2427
auto start = std::chrono::system_clock::now();
2528
return std::chrono::duration_cast<std::chrono::milliseconds>(start.time_since_epoch()).count();
26-
}
29+
}
30+
31+
nlohmann::json get_result(whisper_context *ctx) {
32+
nlohmann::json results = nlohmann::json(nlohmann::json::array());
33+
const int n_segments = whisper_full_n_segments(ctx);
34+
for (int i = 0; i < n_segments; ++i) {
35+
nlohmann::json segment;
36+
int64_t t0 = whisper_full_get_segment_t0(ctx, i);
37+
int64_t t1 = whisper_full_get_segment_t1(ctx, i);
38+
const char *sentence = whisper_full_get_segment_text(ctx, i);
39+
auto result = std::to_string(t0) + "-->" + std::to_string(t1) + ":" + sentence + "\n";
40+
printf("%s: result:%s\n", get_current_time().c_str(), result.c_str());
41+
segment["t0"] = t0;
42+
segment["t1"] = t1;
43+
segment["sentence"] = sentence;
44+
results.push_back(segment);
45+
}
46+
return results;
47+
}
48+

common/utils.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22

33
#include <iostream>
4+
#include <whisper.h>
5+
#include "../nlohmann/json.hpp"
46

57
std::string get_current_time();
6-
long get_current_time_millis();
8+
long get_current_time_millis();
9+
nlohmann::json get_result(whisper_context *ctx);

handler/inference_handler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,10 @@ void handleInference(const Request &request, Response &response, std::mutex &whi
410410
}
411411
// TODO add more output formats
412412
else {
413-
std::string results = output_str(ctx, params, pcmf32s);
413+
auto results = get_result(ctx);
414414
json jres = json{
415415
{"code",0},
416-
{"text", results}
416+
{"data", results}
417417
};
418418
response.set_content(jres.dump(-1, ' ', false, json::error_handler_t::replace),
419419
"application/json");

whisper_server_base_on_uwebsockets.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <sstream>
1111

1212
using namespace stream_components;
13-
nlohmann::json getResult(whisper_context *ctx);
1413
bool processAudio(WhisperService service, std::vector<float> pcm32, const whisper_local_stream_params& params);
1514

1615
int main(int argc, char **argv) {
@@ -155,7 +154,7 @@ int main(int argc, char **argv) {
155154
isOk= whisperService.process(pcm32.data(), pcm32.size());
156155
}
157156
if (isOk) {
158-
final_results = getResult(whisperService.ctx);
157+
final_results = get_result(whisperService.ctx);
159158
response["result"] = final_results;
160159
}
161160
ws->send(response.dump(), uWS::OpCode::TEXT);
@@ -236,22 +235,6 @@ bool processAudio(WhisperService whisperService, std::vector<float> pcm32, const
236235
}
237236
}
238237

239-
nlohmann::json getResult(whisper_context *ctx) {
240-
nlohmann::json results = nlohmann::json(nlohmann::json::array());
241-
const int n_segments = whisper_full_n_segments(ctx);
242-
for (int i = 0; i < n_segments; ++i) {
243-
nlohmann::json segment;
244-
int64_t t0 = whisper_full_get_segment_t0(ctx, i);
245-
int64_t t1 = whisper_full_get_segment_t1(ctx, i);
246-
const char *sentence = whisper_full_get_segment_text(ctx, i);
247-
auto result = std::to_string(t0) + "-->" + std::to_string(t1) + ":" + sentence + "\n";
248-
printf("%s: result:%s\n", get_current_time().c_str(), result.c_str());
249-
segment["t0"] = t0;
250-
segment["t1"] = t1;
251-
segment["sentence"] = sentence;
252-
results.push_back(segment);
253-
}
254-
return results;
255-
}
238+
256239

257240

0 commit comments

Comments
 (0)