]> git.djapps.eu Git - pkg/ggml/sources/whisper.cpp/commitdiff
Change temp file name for server application (#1535)
authorFelix <redacted>
Wed, 22 Nov 2023 08:23:36 +0000 (09:23 +0100)
committerGitHub <redacted>
Wed, 22 Nov 2023 08:23:36 +0000 (09:23 +0100)
Avoid issue of removing file if it exists in the current working
directory

examples/server/server.cpp

index 1ea6d31d6c4dafb81d7250a502a99d663a5f2e1b..4a62f34d6775dd9dcaa9e858cc6d3033ac8ef404 100644 (file)
@@ -453,21 +453,23 @@ int main(int argc, char ** argv) {
         std::vector<float> pcmf32;               // mono-channel F32 PCM
         std::vector<std::vector<float>> pcmf32s; // stereo-channel F32 PCM
 
-        // write file to temporary file
-        std::ofstream temp_file{filename, std::ios::binary};
+        // write to temporary file
+        const std::string temp_filename = "whisper_server_temp_file.wav";
+        std::ofstream temp_file{temp_filename, std::ios::binary};
         temp_file << audio_file.content;
         temp_file.close();
 
         // read wav content into pcmf32
-        if (!::read_wav(filename, pcmf32, pcmf32s, params.diarize)) {
-            fprintf(stderr, "error: failed to read WAV file '%s'\n", filename.c_str());
+        if (!::read_wav(temp_filename, pcmf32, pcmf32s, params.diarize)) {
+            fprintf(stderr, "error: failed to read WAV file '%s'\n", temp_filename.c_str());
             const std::string error_resp = "{\"error\":\"failed to read WAV file\"}";
             res.set_content(error_resp, "application/json");
+            std::remove(temp_filename.c_str());
             whisper_mutex.unlock();
             return;
         }
         // remove temp file
-        std::remove(filename.c_str());
+        std::remove(temp_filename.c_str());
 
         printf("Successfully loaded %s\n", filename.c_str());