@@ -2,8 +2,10 @@ package api
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"io"
6
7
"net/http"
8
+ "slices"
7
9
"strings"
8
10
"time"
9
11
@@ -14,6 +16,7 @@ import (
14
16
"github.com/mutablelogic/go-server/pkg/types"
15
17
"github.com/mutablelogic/go-whisper"
16
18
"github.com/mutablelogic/go-whisper/pkg/client/gowhisper"
19
+ "github.com/mutablelogic/go-whisper/pkg/client/openai"
17
20
"github.com/mutablelogic/go-whisper/pkg/schema"
18
21
"github.com/mutablelogic/go-whisper/pkg/task"
19
22
)
@@ -34,6 +37,16 @@ func TranscribeFile(ctx context.Context, service *whisper.Whisper, w http.Respon
34
37
return httpresponse .Error (w , httpresponse .ErrNotFound , req .Model )
35
38
}
36
39
40
+ // Check the format
41
+ format := strings .TrimSpace (types .PtrString (req .Format ))
42
+ if format == "" {
43
+ format = openai .Formats [0 ] // Default to first format
44
+ } else if ! slices .Contains (openai .Formats , format ) {
45
+ return httpresponse .Error (w , httpresponse .ErrBadRequest .Withf ("Unsupported format: %q" , format ))
46
+ } else {
47
+ fmt .Println ("TranscribeFile: format:" , format )
48
+ }
49
+
37
50
// Start a translation task
38
51
var result * schema.Transcription
39
52
if err := service .WithModel (model , func (taskctx * task.Context ) error {
@@ -71,7 +84,7 @@ func TranscribeFile(ctx context.Context, service *whisper.Whisper, w http.Respon
71
84
}
72
85
73
86
// Response to client
74
- return response (w , types . PtrString ( req . Format ) , result )
87
+ return response (w , format , result )
75
88
}
76
89
77
90
func TranslateFile (ctx context.Context , service * whisper.Whisper , w http.ResponseWriter , r * http.Request ) error {
@@ -87,6 +100,16 @@ func TranslateFile(ctx context.Context, service *whisper.Whisper, w http.Respons
87
100
return httpresponse .Error (w , httpresponse .ErrNotFound , req .Model )
88
101
}
89
102
103
+ // Check the format
104
+ format := strings .TrimSpace (types .PtrString (req .Format ))
105
+ if format == "" {
106
+ format = openai .Formats [0 ] // Default to first format
107
+ } else if ! slices .Contains (openai .Formats , format ) {
108
+ return httpresponse .Error (w , httpresponse .ErrBadRequest .Withf ("Unsupported format: %q" , format ))
109
+ } else {
110
+ fmt .Println ("TranscribeFile: format:" , format )
111
+ }
112
+
90
113
// Cannot diarize when translating
91
114
if req .Diarize != nil {
92
115
return httpresponse .Error (w , httpresponse .ErrBadRequest , "Cannot diarize when translating" )
@@ -124,7 +147,7 @@ func TranslateFile(ctx context.Context, service *whisper.Whisper, w http.Respons
124
147
}
125
148
126
149
// Response to client
127
- return response (w , types . PtrString ( req . Format ) , result )
150
+ return response (w , format , result )
128
151
}
129
152
130
153
func segment (ctx context.Context , taskctx * task.Context , r io.Reader , fn func (seg * schema.Segment )) error {
@@ -145,30 +168,22 @@ func segment(ctx context.Context, taskctx *task.Context, r io.Reader, fn func(se
145
168
return nil
146
169
}
147
170
148
- const (
149
- FormatJson = "json"
150
- FormatVerboseJson = "verbose_json"
151
- FormatText = "text"
152
- FormatSrt = "srt"
153
- FormatVtt = "vtt"
154
- )
155
-
156
171
func response (w http.ResponseWriter , format string , response * schema.Transcription ) error {
157
172
switch strings .ToLower (format ) {
158
- case FormatJson , FormatVerboseJson :
173
+ case openai . FormatJson , openai . FormatVerboseJson :
159
174
return httpresponse .JSON (w , http .StatusOK , 2 , response )
160
- case FormatText , "" :
175
+ case openai . FormatText , "" :
161
176
return httpresponse .Write (w , http .StatusOK , types .ContentTypeTextPlain , func (w io.Writer ) (int , error ) {
162
177
return w .Write ([]byte (response .Text ))
163
178
})
164
- case FormatSrt :
179
+ case openai . FormatSrt :
165
180
return httpresponse .Write (w , http .StatusOK , "application/x-subrip" , func (w io.Writer ) (int , error ) {
166
181
for _ , seg := range response .Segments {
167
182
task .WriteSegmentSrt (w , seg )
168
183
}
169
184
return 0 , nil
170
185
})
171
- case FormatVtt :
186
+ case openai . FormatVtt :
172
187
return httpresponse .Write (w , http .StatusOK , "text/vtt" , func (w io.Writer ) (int , error ) {
173
188
if _ , err := w .Write ([]byte ("WEBVTT\n \n " )); err != nil {
174
189
return 0 , err
0 commit comments