@@ -10,14 +10,13 @@ using json = nlohmann::json;
1010struct whisper_print_user_data {
1111 const whisper_params *params;
1212
13- const std::vector<std::vector<float >> *pcmf32s;
13+ const std::vector <std::vector<float >> *pcmf32s;
1414 int progress_prev;
1515};
1616
17-
1817// Terminal color map. 10 colors grouped in ranges [0.0, 0.1, ..., 0.9]
1918// Lowest is red, middle is yellow, highest is green.
20- const std::vector<std::string> k_colors = {
19+ const std::vector <std::string> k_colors = {
2120 " \033 [38;5;196m" , " \033 [38;5;202m" , " \033 [38;5;208m" , " \033 [38;5;214m" , " \033 [38;5;220m" ,
2221 " \033 [38;5;226m" , " \033 [38;5;190m" , " \033 [38;5;154m" , " \033 [38;5;118m" , " \033 [38;5;82m" ,
2322};
@@ -28,7 +27,7 @@ int timestamp_to_sample(int64_t t, int n_samples) {
2827}
2928
3029std::string
31- estimate_diarization_speaker (std::vector<std::vector<float >> pcmf32s, int64_t t0, int64_t t1, bool id_only = false ) {
30+ estimate_diarization_speaker (std::vector <std::vector<float >> pcmf32s, int64_t t0, int64_t t1, bool id_only = false ) {
3231 std::string speaker = " " ;
3332 const int64_t n_samples = pcmf32s[0 ].size ();
3433
@@ -133,7 +132,7 @@ void whisper_print_segment_callback(struct whisper_context *ctx, struct whisper_
133132}
134133
135134std::string
136- output_str (struct whisper_context *ctx, const whisper_params ¶ms, std::vector<std::vector<float >> pcmf32s) {
135+ output_str (struct whisper_context *ctx, const whisper_params ¶ms, std::vector <std::vector<float >> pcmf32s) {
137136 std::stringstream result;
138137 const int n_segments = whisper_full_n_segments (ctx);
139138 for (int i = 0 ; i < n_segments; ++i) {
@@ -163,7 +162,7 @@ void whisper_print_progress_callback(struct whisper_context * /*ctx*/, struct wh
163162}
164163
165164void getReqParameters (const Request &req, whisper_params ¶ms) {
166- // user model configu.has_fileion
165+ // user model configu.has_fileion
167166 if (req.has_file (" offset-t" )) {
168167 params.offset_t_ms = std::stoi (req.get_file_value (" offset-t" ).content );
169168 }
@@ -185,16 +184,22 @@ void getReqParameters(const Request &req, whisper_params ¶ms) {
185184 if (req.has_file (" temerature" )) {
186185 params.userdef_temp = std::stof (req.get_file_value (" temperature" ).content );
187186 }
188- if (req.has_file (" audio_format" )) {
189- params.audio_format = req.get_file_value (" audio_format" ).content ;
187+ if (req.has_file (" audio-format" )) {
188+ params.audio_format = req.get_file_value (" audio-format" ).content ;
189+ }
190+ if (req.has_file (" language" )) {
191+ params.language = req.get_file_value (" language" ).content ;
192+ }
193+ if (req.has_file (" diarize" )) {
194+ params.diarize = true ;
195+ }
196+ if (req.has_file (" translate" )) {
197+ params.translate = true
190198 }
191199}
192200
193-
194- void getReqParameters (const Request &request, whisper_params ¶ms);
195-
196201bool read_audio_file (std::string audio_format, std::string filename, std::vector<float > &pcmf32,
197- std::vector<std::vector<float >> &pcmf32s, bool diarize) {
202+ std::vector <std::vector<float >> &pcmf32s, bool diarize) {
198203
199204 // read audio content into pcmf32
200205 if (audio_format == " mp3" ) {
@@ -217,7 +222,7 @@ bool read_audio_file(std::string audio_format, std::string filename, std::vector
217222}
218223
219224bool run (std::mutex &whisper_mutex, whisper_params ¶ms, whisper_context *ctx, std::string filename,
220- const std::vector<std::vector<float >> &pcmf32s, std::vector<float > pcmf32) {
225+ const std::vector <std::vector<float >> &pcmf32s, std::vector<float > pcmf32) {
221226 // print system information
222227 {
223228 fprintf (stderr, " \n " );
@@ -367,13 +372,17 @@ void handleInference(const Request &request, Response &response, std::mutex &whi
367372
368373 // audio arrays
369374 std::vector<float > pcmf32; // mono-channel F32 PCM
370- std::vector<std::vector<float >> pcmf32s; // stereo-channel F32 PCM
375+ std::vector <std::vector<float >> pcmf32s; // stereo-channel F32 PCM
371376
372377 // write file to temporary file
373378 std::ofstream temp_file{filename, std::ios::binary};
374379 temp_file << audio_file.content ;
375380
376381 bool isOK = read_audio_file (params.audio_format , filename, pcmf32, pcmf32s, params.diarize );
382+
383+ // remove temp file
384+ std::remove (filename.c_str ());
385+
377386 if (!isOK) {
378387 json json_obj = {
379388 {" code" , -1 },
@@ -384,9 +393,6 @@ void handleInference(const Request &request, Response &response, std::mutex &whi
384393 return ;
385394 }
386395
387- // remove temp file
388- std::remove (filename.c_str ());
389-
390396 printf (" Successfully loaded %s\n " , filename.c_str ());
391397
392398 bool isOk = run (whisper_mutex, params, ctx, filename, pcmf32s, pcmf32);
@@ -395,8 +401,7 @@ void handleInference(const Request &request, Response &response, std::mutex &whi
395401 if (params.response_format == text_format) {
396402 std::string results = output_str (ctx, params, pcmf32s);
397403 response.set_content (results.c_str (), " text/html" );
398- }
399- else {
404+ } else {
400405 auto results = get_result (ctx);
401406 json jres = json{
402407 {" code" , 1 },
@@ -414,3 +419,31 @@ void handleInference(const Request &request, Response &response, std::mutex &whi
414419 response.set_content (json_string, " application/json" );
415420 }
416421}
422+
423+ void handle_events (const Request &request, Response &response, std::mutex &whisper_mutex, whisper_params ¶ms,
424+ whisper_context *ctx, char *arg_audio_file) {
425+ // set CORS hander
426+ response.set_header (" Access-Control-Allow-Origin" , " *" );
427+ response.set_header (" Access-Control-Allow-Methods" , " POST, GET, OPTIONS" );
428+ response.set_header (" Access-Control-Allow-Headers" , " Content-Type" );
429+ // first check user requested fields of the request
430+ if (!request.has_file (" file" )) {
431+ fprintf (stderr, " error: no 'file' field in the request\n " );
432+ json jres = json{
433+ {" code" , -1 },
434+ {" msg" , " no 'file' field in the request" }
435+ };
436+ auto json_string = jres.dump (-1 , ' ' , false , json::error_handler_t ::replace);
437+ response.set_content (json_string, " application/json" );
438+ return ;
439+ }
440+ auto audio_file = request.get_file_value (" file" );
441+ std::string filename{audio_file.filename };
442+ printf (" %s: Received filename: %s \n " , get_current_time ().c_str (), filename.c_str ());
443+ // check non-required fields
444+ getReqParameters (request, params);
445+ params.stream = true
446+ printf (" %s: audio_format:%s \n " , get_current_time ().c_str (), params.audio_format .c_str ());
447+ std::unique_lock<std::mutex> lock (queue_mutex);
448+
449+ }
0 commit comments