]> git.djapps.eu Git - pkg/ggml/sources/whisper.cpp/commitdiff
Server : Add support for .vtt format to Whisper server (#1578)
authorAleksander Andrzejewski <redacted>
Thu, 30 Nov 2023 23:44:26 +0000 (00:44 +0100)
committerGitHub <redacted>
Thu, 30 Nov 2023 23:44:26 +0000 (23:44 +0000)
- The code comes from examples/main
- The output mimetype is set to text/vtt

Example usage:
```shell
curl 127.0.0.1:8080/inference \
-H "Content-Type: multipart/form-data" \
-F file="@samples/jfk.wav" \
-F temperature="0.2" \
-F response-format="vtt"
```

examples/server/server.cpp

index 2d15d4cc8af6d8adc17ad82d76184f70c9a0452f..96f8608c43070baca5c522b47cea89fbeb3c3d59 100644 (file)
@@ -678,6 +678,29 @@ int main(int argc, char ** argv) {
                 ss << speaker << text << "\n\n";
             }
             res.set_content(ss.str(), "application/x-subrip");
+        } else if (params.response_format == vtt_format) {
+            std::stringstream ss;
+
+            ss << "WEBVTT\n\n";
+
+            const int n_segments = whisper_full_n_segments(ctx);
+            for (int i = 0; i < n_segments; ++i) {
+                const char * text = whisper_full_get_segment_text(ctx, i);
+                const int64_t t0 = whisper_full_get_segment_t0(ctx, i);
+                const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
+                std::string speaker = "";
+
+                if (params.diarize && pcmf32s.size() == 2)
+                {
+                    speaker = estimate_diarization_speaker(pcmf32s, t0, t1, true);
+                    speaker.insert(0, "<v Speaker");
+                    speaker.append(">");
+                }
+
+                ss << to_timestamp(t0) << " --> " << to_timestamp(t1) << "\n";
+                ss << speaker << text << "\n\n";
+            }
+            res.set_content(ss.str(), "text/vtt");
         }
         // TODO add more output formats
         else