@@ -2,8 +2,10 @@ package api
22
33import (
44 "context"
5+ "fmt"
56 "io"
67 "net/http"
8+ "slices"
79 "strings"
810 "time"
911
@@ -14,6 +16,7 @@ import (
1416 "github.com/mutablelogic/go-server/pkg/types"
1517 "github.com/mutablelogic/go-whisper"
1618 "github.com/mutablelogic/go-whisper/pkg/client/gowhisper"
19+ "github.com/mutablelogic/go-whisper/pkg/client/openai"
1720 "github.com/mutablelogic/go-whisper/pkg/schema"
1821 "github.com/mutablelogic/go-whisper/pkg/task"
1922)
@@ -34,6 +37,16 @@ func TranscribeFile(ctx context.Context, service *whisper.Whisper, w http.Respon
3437 return httpresponse .Error (w , httpresponse .ErrNotFound , req .Model )
3538 }
3639
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+
3750 // Start a translation task
3851 var result * schema.Transcription
3952 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
7184 }
7285
7386 // Response to client
74- return response (w , types . PtrString ( req . Format ) , result )
87+ return response (w , format , result )
7588}
7689
7790func 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
87100 return httpresponse .Error (w , httpresponse .ErrNotFound , req .Model )
88101 }
89102
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+
90113 // Cannot diarize when translating
91114 if req .Diarize != nil {
92115 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
124147 }
125148
126149 // Response to client
127- return response (w , types . PtrString ( req . Format ) , result )
150+ return response (w , format , result )
128151}
129152
130153func 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
145168 return nil
146169}
147170
148- const (
149- FormatJson = "json"
150- FormatVerboseJson = "verbose_json"
151- FormatText = "text"
152- FormatSrt = "srt"
153- FormatVtt = "vtt"
154- )
155-
156171func response (w http.ResponseWriter , format string , response * schema.Transcription ) error {
157172 switch strings .ToLower (format ) {
158- case FormatJson , FormatVerboseJson :
173+ case openai . FormatJson , openai . FormatVerboseJson :
159174 return httpresponse .JSON (w , http .StatusOK , 2 , response )
160- case FormatText , "" :
175+ case openai . FormatText , "" :
161176 return httpresponse .Write (w , http .StatusOK , types .ContentTypeTextPlain , func (w io.Writer ) (int , error ) {
162177 return w .Write ([]byte (response .Text ))
163178 })
164- case FormatSrt :
179+ case openai . FormatSrt :
165180 return httpresponse .Write (w , http .StatusOK , "application/x-subrip" , func (w io.Writer ) (int , error ) {
166181 for _ , seg := range response .Segments {
167182 task .WriteSegmentSrt (w , seg )
168183 }
169184 return 0 , nil
170185 })
171- case FormatVtt :
186+ case openai . FormatVtt :
172187 return httpresponse .Write (w , http .StatusOK , "text/vtt" , func (w io.Writer ) (int , error ) {
173188 if _ , err := w .Write ([]byte ("WEBVTT\n \n " )); err != nil {
174189 return 0 , err
0 commit comments