Skip to content

Commit 9ee19ba

Browse files
committed
[feature]: add hearbeat + change msg format
1 parent 381735c commit 9ee19ba

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ ifdef WHISPER_SERVER_SSL
283283
MK_LDFLAGS += -lssl -lcrypto
284284
endif
285285

286-
MK_LDFLAGS += -lrdkafka -lrdkafka++ -lonnxruntime
286+
MK_LDFLAGS += -lrdkafka -lrdkafka++ -lonnxruntime -lcurl
287287

288288
ifdef WHISPER_DISABLE_LOGS
289289
MK_CPPFLAGS += -DLOG_DISABLE_LOGS

examples/stream/protocol.hpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include <unistd.h>
88
#include <limits.h>
99

10+
#include <chrono>
11+
#include <format>
12+
1013
const std::string DEFAULT_BOOTSTRAP_SERVERS = "aci.crolard.fr:9092";
1114
const std::string DEFAULT_KAFKA_TOPIC = "records-topic";
1215

@@ -66,19 +69,13 @@ void producer_send_message(RdKafka::Producer* producer, const std::string& json_
6669
} // A producer application should continually serve the delivery report queue // by calling poll() at frequent intervals. producer->poll(0);
6770
}
6871

69-
void build_and_send_message(RdKafka::Producer* producer, const std::string& topic, const char* text) {
72+
void build_and_send_message(RdKafka::Producer* producer, const std::string& topic, const char* text, const int32_t node_id) {
7073
const std::string text_str = text;
7174

72-
char hostname[HOST_NAME_MAX];
73-
char username[LOGIN_NAME_MAX];
74-
gethostname(hostname, HOST_NAME_MAX);
75-
getlogin_r(username, LOGIN_NAME_MAX);
76-
77-
7875
json j;
79-
j["user"] = std::string(username);
80-
j["hostname"] = std::string(hostname);
81-
j["timestamp"] = std::to_string(std::chrono::system_clock::now().time_since_epoch().count());
76+
const auto now = std::chrono::system_clock::now();
77+
j["node_id"] = node_id;
78+
j["timestamp"] = std::format("{:%FT%TZ}", now);
8279
j["text"] = text_str;
8380

8481
std::string json_str = j.dump();

examples/stream/stream.cpp

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
#include <vector>
1616
#include <tuple>
1717
#include <fstream>
18+
#include <thread>
19+
20+
#include <curl/curl.h>
1821

1922
#include "protocol.hpp"
2023

2124
#include "vad_iterator.hpp"
2225

23-
constexpr int64_t VAD_WINDOW_SIZE = 2048;
26+
constexpr int64_t VAD_WINDOW_SIZE = 1536;
2427
constexpr size_t VAD_TEST_FRAME_MS = 512;
2528

2629
// command-line parameters
@@ -47,8 +50,11 @@ struct whisper_params {
4750
bool flash_attn = false;
4851
bool remote = false;
4952

53+
int32_t id = 0;
54+
5055
std::string language = "en";
5156
std::string model = "models/ggml-base.en.bin";
57+
std::string location = "41.40338,2.17403";
5258
std::string bootstrap_servers = DEFAULT_BOOTSTRAP_SERVERS;
5359
std::string topic = DEFAULT_KAFKA_TOPIC;
5460
std::string fname_out;
@@ -87,6 +93,8 @@ static bool whisper_params_parse(int argc, char ** argv, whisper_params & params
8793
else if (arg == "-sa" || arg == "--save-audio") { params.save_audio = true; }
8894
else if (arg == "-ng" || arg == "--no-gpu") { params.use_gpu = false; }
8995
else if (arg == "-fa" || arg == "--flash-attn") { params.flash_attn = true; }
96+
else if (arg == "-loc" || arg == "--location") { params.location = argv[++i]; }
97+
else if (arg == "-id" || arg == "--id") { params.id = std::stoi(argv[++i]); }
9098

9199
else {
92100
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
@@ -127,6 +135,29 @@ void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & para
127135
fprintf(stderr, "\n");
128136
}
129137

138+
bool send_webhook_request(const std::string &node_id, const std::string &data) {
139+
CURL *curl;
140+
CURLcode res;
141+
std::string url = "https://aci.crolard.fr/api/webhook/" + node_id + "/" + data;
142+
143+
curl = curl_easy_init();
144+
if (curl) {
145+
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
146+
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
147+
148+
res = curl_easy_perform(curl);
149+
if (res != CURLE_OK) {
150+
return false;
151+
} else {
152+
return true;
153+
}
154+
155+
curl_easy_cleanup(curl);
156+
} else {
157+
return false;
158+
}
159+
}
160+
130161
int main(int argc, char ** argv) {
131162
whisper_params params;
132163

@@ -249,6 +280,19 @@ int main(int argc, char ** argv) {
249280
auto t_last = std::chrono::high_resolution_clock::now();
250281
const auto t_start = t_last;
251282

283+
284+
std::thread aliveThread([&]{
285+
while(true) {
286+
json data;
287+
data["localisation"] = params.location;
288+
// escape the string to it can be sent on a URL
289+
std::string json = data.dump();
290+
std::string escaped = curl_easy_escape(nullptr, json.c_str(), json.size());
291+
send_webhook_request(std::to_string(params.id), escaped);
292+
std::this_thread::sleep_for(std::chrono::seconds(3));
293+
}
294+
});
295+
252296
// main audio loop
253297
while (is_running) {
254298
if (params.save_audio) {
@@ -261,7 +305,6 @@ int main(int argc, char ** argv) {
261305
break;
262306
}
263307

264-
265308
if(params.remote) {
266309
producer->poll(0);
267310
}
@@ -307,7 +350,7 @@ int main(int argc, char ** argv) {
307350
const auto t_diff = std::chrono::duration_cast<std::chrono::milliseconds>(t_now - t_last).count();
308351

309352
if (t_diff < VAD_WINDOW_SIZE) {
310-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
353+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
311354
continue;
312355
}
313356

@@ -329,13 +372,11 @@ int main(int argc, char ** argv) {
329372
inference_avg = inference_avg / inferences.size();
330373

331374
bool vad_output = inference_avg > params.vad_thold;
332-
//bool vad_simple = ::vad_simple(pcmf32_new, WHISPER_SAMPLE_RATE, 1000, params.vad_thold, params.freq_thold, false);
333375

334376
if (vad_output) {
335377
audio.get(params.length_ms, pcmf32);
336378
} else {
337-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
338-
379+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
339380
continue;
340381
}
341382

@@ -394,9 +435,8 @@ int main(int argc, char ** argv) {
394435
for (int i = 0; i < n_segments; ++i) {
395436
const char * text = whisper_full_get_segment_text(ctx, i);
396437

397-
const char first_character = text[0];
398-
if(params.remote && first_character != '[') {
399-
build_and_send_message(producer, params.topic, text);
438+
if(params.remote) {
439+
build_and_send_message(producer, params.topic, text, params.id);
400440
}
401441

402442
if (params.no_timestamps) {

0 commit comments

Comments
 (0)