@@ -2,10 +2,12 @@ package main
22
33import (
44 "fmt"
5+ "io"
56 "os"
67
78 // Packages
89 otel "github.com/mutablelogic/go-client/pkg/otel"
10+ httpresponse "github.com/mutablelogic/go-server/pkg/httpresponse"
911 httpclient "github.com/mutablelogic/go-whisper/pkg/httpclient"
1012 schema "github.com/mutablelogic/go-whisper/pkg/schema"
1113)
@@ -18,13 +20,9 @@ type TranscribeCommands struct {
1820}
1921
2022type TranscribeCommand struct {
21- Model string `arg:"" name:"model" help:"Model ID to use for transcription"`
22- File string `arg:"" name:"file" help:"Audio file to transcribe"`
23- Language * string `name:"language" help:"Language code (e.g., 'en', 'es', 'fr')"`
24- Prompt * string `name:"prompt" help:"Initial prompt to guide transcription"`
25- Temperature * float64 `name:"temperature" help:"Temperature (0.0-1.0)"`
26- Diarize * bool `name:"diarize" help:"Enable speaker diarization"`
27- Format string `name:"format" help:"Output format: json, text, vtt, srt" default:"json"`
23+ TranslateCommand
24+ Diarize * bool `name:"diarize" help:"Enable speaker diarization"`
25+ Language * string `name:"language" help:"Language code (e.g., 'en', 'es', 'fr')"`
2826}
2927
3028///////////////////////////////////////////////////////////////////////////////
@@ -63,20 +61,16 @@ func (cmd *TranscribeCommand) Run(ctx *Globals) (err error) {
6361 }
6462
6563 // Set format
66- var format httpclient.FormatType
67- switch cmd .Format {
68- case "text" , string (httpclient .FormatText ):
69- format = httpclient .FormatText
70- case "vtt" , string (httpclient .FormatVTT ):
71- format = httpclient .FormatVTT
72- case "srt" , string (httpclient .FormatSRT ):
73- format = httpclient .FormatSRT
74- case "json" , string (httpclient .FormatJSON ):
75- format = httpclient .FormatJSON
76- default :
77- return fmt .Errorf ("unsupported format: %s" , cmd .Format )
64+ format , err := formatFromString (cmd .Format )
65+ if err != nil {
66+ return err
7867 }
79- opts = append (opts , httpclient .WithFormat (format ))
68+
69+ // Add real-time segment printing callback
70+ opts = append (opts , httpclient .WithSegmentCallback (func (seg * schema.Segment ) error {
71+ writeSegment (os .Stdout , seg , format )
72+ return nil
73+ }))
8074
8175 // Transcribe
8276 var result * schema.Transcription
@@ -85,38 +79,51 @@ func (cmd *TranscribeCommand) Run(ctx *Globals) (err error) {
8579 return err
8680 }
8781
88- // Print result based on format
82+ // If segments were not printed via streaming, print from result
83+ for _ , seg := range result .Segments {
84+ writeSegment (os .Stdout , seg , format )
85+ }
86+ writeTrailer (os .Stdout , format )
87+
88+ // Return success
89+ return nil
90+ }
91+
92+ // Method to write segment in specified format
93+ func writeSegment (w io.Writer , seg * schema.Segment , format httpclient.FormatType ) {
8994 switch format {
90- case httpclient .FormatJSON :
91- fmt .Println (result )
9295 case httpclient .FormatVTT :
93- if len (result .Segments ) > 0 {
94- // Client-side formatting from segments
95- fmt .Print ("WEBVTT\n \n " )
96- for _ , seg := range result .Segments {
97- if seg != nil {
98- seg .WriteVTT (os .Stdout , 0 )
99- }
100- }
101- } else {
102- // Server already formatted it
103- fmt .Print (result .Text )
104- }
96+ seg .WriteVTT (w , 0 )
10597 case httpclient .FormatSRT :
106- if len (result .Segments ) > 0 {
107- // Client-side formatting from segments
108- for _ , seg := range result .Segments {
109- if seg != nil {
110- seg .WriteSRT (os .Stdout , 0 )
111- }
112- }
113- } else {
114- // Server already formatted it
115- fmt .Print (result .Text )
116- }
98+ seg .WriteSRT (w , 0 )
99+ case httpclient .FormatJSON :
100+ seg .WriteJSON (w )
117101 default :
118- // For text and other formats, print the formatted text from server
119- fmt .Print (result .Text )
102+ seg .WriteText (w )
103+ }
104+ }
105+
106+ // Method to write a trailer in specified format
107+ func writeTrailer (w io.Writer , format httpclient.FormatType ) {
108+ switch format {
109+ case httpclient .FormatJSON :
110+ schema .WriteJSONTrailer (w )
111+ default :
112+ schema .WriteTextTrailer (w )
113+ }
114+ }
115+
116+ func formatFromString (format string ) (httpclient.FormatType , error ) {
117+ switch format {
118+ case "text" , string (httpclient .FormatText ):
119+ return httpclient .FormatText , nil
120+ case "vtt" , string (httpclient .FormatVTT ):
121+ return httpclient .FormatVTT , nil
122+ case "srt" , string (httpclient .FormatSRT ):
123+ return httpclient .FormatSRT , nil
124+ case "json" , string (httpclient .FormatJSON ):
125+ return httpclient .FormatJSON , nil
126+ default :
127+ return "" , httpresponse .ErrBadRequest .Withf ("unsupported format: %q" , format )
120128 }
121- return nil
122129}
0 commit comments