Skip to content

Commit a3a9ee8

Browse files
authored
Added request timeout support (#91)
1 parent dd15bac commit a3a9ee8

File tree

7 files changed

+80
-20
lines changed

7 files changed

+80
-20
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ require (
99
github.com/djthorpe/go-errors v1.0.3
1010
github.com/go-audio/audio v1.0.0
1111
github.com/go-audio/wav v1.1.0
12-
github.com/mutablelogic/go-client v1.3.3
12+
github.com/mutablelogic/go-client v1.3.4
1313
github.com/mutablelogic/go-media v1.8.3
14-
github.com/mutablelogic/go-server v1.5.17
14+
github.com/mutablelogic/go-server v1.5.18
1515
github.com/orcaman/writerseeker v0.0.0-20200621085525-1d3f536ff85e
1616
github.com/stretchr/testify v1.11.1
1717
go.opentelemetry.io/otel v1.39.0
@@ -24,7 +24,6 @@ require (
2424
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
2525
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2626
github.com/davecgh/go-spew v1.1.1 // indirect
27-
github.com/djthorpe/go-pg v1.0.7 // indirect
2827
github.com/go-asn1-ber/asn1-ber v1.5.8-0.20250403174932-29230038a667 // indirect
2928
github.com/go-audio/riff v1.0.0 // indirect
3029
github.com/go-ldap/ldap/v3 v3.4.12 // indirect
@@ -36,6 +35,7 @@ require (
3635
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
3736
github.com/jackc/pgx/v5 v5.8.0 // indirect
3837
github.com/jackc/puddle/v2 v2.2.2 // indirect
38+
github.com/mutablelogic/go-pg v1.1.1 // indirect
3939
github.com/pmezard/go-difflib v1.0.0 // indirect
4040
github.com/yinyin/go-ldap-schema-parser v0.0.0-20190716182935-542aadd3dcb5 // indirect
4141
go.opentelemetry.io/auto/sdk v1.2.1 // indirect

go.sum

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5Qvfr
3737
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
3838
github.com/djthorpe/go-errors v1.0.3 h1:GZeMPkC1mx2vteXLI/gvxZS0Ee9zxzwD1mcYyKU5jD0=
3939
github.com/djthorpe/go-errors v1.0.3/go.mod h1:HtfrZnMd6HsX75Mtbv9Qcnn0BqOrrFArvCaj3RMnZhY=
40-
github.com/djthorpe/go-pg v1.0.7 h1:xZJWSRpS0KRDHthHm4QH65tmBGndsXUrnnakkk1Pzcg=
41-
github.com/djthorpe/go-pg v1.0.7/go.mod h1:Tv8QLPZBlNTPy/+8MiDwGVfW7GWB/YRQ9uaf/3m4hLU=
4240
github.com/docker/docker v28.5.2+incompatible h1:DBX0Y0zAjZbSrm1uzOkdr1onVghKaftjlSWt4AFexzM=
4341
github.com/docker/docker v28.5.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
4442
github.com/docker/go-connections v0.6.0 h1:LlMG9azAe1TqfR7sO+NJttz1gy6KO7VJBh+pMmjSD94=
@@ -111,8 +109,8 @@ github.com/magiconair/properties v1.8.10 h1:s31yESBquKXCV9a/ScB3ESkOjUYYv+X0rg8S
111109
github.com/magiconair/properties v1.8.10/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
112110
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
113111
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
114-
github.com/moby/go-archive v0.1.0 h1:Kk/5rdW/g+H8NHdJW2gsXyZ7UnzvJNOy6VKJqueWdcQ=
115-
github.com/moby/go-archive v0.1.0/go.mod h1:G9B+YoujNohJmrIYFBpSd54GTUB4lt9S+xVQvsJyFuo=
112+
github.com/moby/go-archive v0.2.0 h1:zg5QDUM2mi0JIM9fdQZWC7U8+2ZfixfTYoHL7rWUcP8=
113+
github.com/moby/go-archive v0.2.0/go.mod h1:mNeivT14o8xU+5q1YnNrkQVpK+dnNe/K6fHqnTg4qPU=
116114
github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk=
117115
github.com/moby/patternmatcher v0.6.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
118116
github.com/moby/sys/sequential v0.6.0 h1:qrx7XFUd/5DxtqcoH1h438hF5TmOvzC/lspjy7zgvCU=
@@ -125,12 +123,14 @@ github.com/moby/term v0.5.2 h1:6qk3FJAFDs6i/q3W/pQ97SX192qKfZgGjCQqfCJkgzQ=
125123
github.com/moby/term v0.5.2/go.mod h1:d3djjFCrjnB+fl8NJux+EJzu0msscUP+f8it8hPkFLc=
126124
github.com/morikuni/aec v1.1.0 h1:vBBl0pUnvi/Je71dsRrhMBtreIqNMYErSAbEeb8jrXQ=
127125
github.com/morikuni/aec v1.1.0/go.mod h1:xDRgiq/iw5l+zkao76YTKzKttOp2cwPEne25HDkJnBw=
128-
github.com/mutablelogic/go-client v1.3.3 h1:iPxKSiqjrjBeKQrQP2szxy3q/LuPFteGdORgFbN38EI=
129-
github.com/mutablelogic/go-client v1.3.3/go.mod h1:FRk/jiFO0g7uoPhd3gZS1fOirnbAJaokKCISQ2Ffvpo=
126+
github.com/mutablelogic/go-client v1.3.4 h1:aIDA4B89ymfGkS142bDZpunovwNgpa0iMySk7BpPJfk=
127+
github.com/mutablelogic/go-client v1.3.4/go.mod h1:GZ5n/MLDju3Lk8+9fCrztSkRvODvT5t2KvecHe2kZs4=
130128
github.com/mutablelogic/go-media v1.8.3 h1:gAWbvPJpL73MLMZY5U9pe5eBKpiUXg4c7BKbDBMOif4=
131129
github.com/mutablelogic/go-media v1.8.3/go.mod h1:MkY+onBUz2IlUeWcQZY1sTvB/8mv+41asQZi66DdJ+w=
132-
github.com/mutablelogic/go-server v1.5.17 h1:NXuP8IWIM3DLlu/I375fK8pq+OIYHil3Xun1GGklUWM=
133-
github.com/mutablelogic/go-server v1.5.17/go.mod h1:HCX8WZtE3RXR4i+npBvCdILfnBelomDIe8/B68O/MA4=
130+
github.com/mutablelogic/go-pg v1.1.1 h1:21N1/I4+Od68ynlxRrSla3Y2OYB5vnw/HE/SG8J45BM=
131+
github.com/mutablelogic/go-pg v1.1.1/go.mod h1:qwdZHszWb7LySx+ILAeWkLC/rQxyCGirQ8FGHw8eBvI=
132+
github.com/mutablelogic/go-server v1.5.18 h1:UKpJQReabHFMz1U/gbOq/+Q0C0ZzklVBalvs5FFk9NQ=
133+
github.com/mutablelogic/go-server v1.5.18/go.mod h1:swZf3T0eGe9VEE0Ki37WknpN+XxTGj2Xn6EP7BJm9x0=
134134
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
135135
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
136136
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
@@ -145,8 +145,8 @@ github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt
145145
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
146146
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
147147
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
148-
github.com/shirou/gopsutil/v4 v4.25.11 h1:X53gB7muL9Gnwwo2evPSE+SfOrltMoR6V3xJAXZILTY=
149-
github.com/shirou/gopsutil/v4 v4.25.11/go.mod h1:EivAfP5x2EhLp2ovdpKSozecVXn1TmuG7SMzs/Wh4PU=
148+
github.com/shirou/gopsutil/v4 v4.25.12 h1:e7PvW/0RmJ8p8vPGJH4jvNkOyLmbkXgXW4m6ZPic6CY=
149+
github.com/shirou/gopsutil/v4 v4.25.12/go.mod h1:EivAfP5x2EhLp2ovdpKSozecVXn1TmuG7SMzs/Wh4PU=
150150
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
151151
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
152152
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

pkg/httpclient/opts.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"path/filepath"
66

77
// Packages
8+
client "github.com/mutablelogic/go-client"
89
schema "github.com/mutablelogic/go-whisper/pkg/schema"
910
)
1011

@@ -14,6 +15,7 @@ import (
1415
type opt struct {
1516
schema.TranscribeMultipartRequest
1617
format FormatType
18+
reqOpts []client.RequestOpt
1719
segmentCallback func(*schema.Segment) error
1820
progressCallback func(*schema.Event) error
1921
}
@@ -120,3 +122,13 @@ func WithProgressCallback(callback func(*schema.Event) error) Opt {
120122
return nil
121123
}
122124
}
125+
126+
///////////////////////////////////////////////////////////////////////////////
127+
// OPTIONS - REQUEST OPTIONS
128+
129+
func WithRequestOpts(opts ...client.RequestOpt) Opt {
130+
return func(o *opt) error {
131+
o.reqOpts = append(o.reqOpts, opts...)
132+
return nil
133+
}
134+
}

pkg/httpclient/transcribe.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ func (c *Client) Transcribe(ctx context.Context, model string, r io.Reader, opts
4848
return nil, fmt.Errorf("failed to create multipart request: %w", err)
4949
}
5050

51+
// Append path to request options
52+
reqOpts := append(opt.reqOpts, client.OptPath("transcribe"))
53+
5154
// If streaming callback provided, handle streaming
52-
var reqOpts []client.RequestOpt = []client.RequestOpt{
53-
client.OptPath("transcribe"),
54-
}
5555
var response schema.Transcription
5656
if opt.segmentCallback != nil {
5757
reqOpts = append(reqOpts, client.OptReqHeader("Accept", "text/event-stream"))

pkg/httpclient/translate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ func (c *Client) Translate(ctx context.Context, model string, r io.Reader, opts
5555
return nil, fmt.Errorf("failed to create multipart request: %w", err)
5656
}
5757

58+
// Set request options
59+
reqOpts := append(opt.reqOpts, client.OptPath("translate"))
60+
5861
// If streaming callback provided, handle streaming
59-
var reqOpts []client.RequestOpt = []client.RequestOpt{
60-
client.OptPath("translate"),
61-
}
6262
var response schema.Transcription
6363
if opt.segmentCallback != nil {
6464
reqOpts = append(reqOpts, client.OptReqHeader("Accept", "text/event-stream"))

pkg/schema/transcription.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ package schema
22

33
import (
44
"encoding/json"
5+
"io"
6+
"net/http"
7+
"slices"
58
"time"
9+
10+
// Packages
11+
client "github.com/mutablelogic/go-client"
12+
httpresponse "github.com/mutablelogic/go-server/pkg/httpresponse"
13+
types "github.com/mutablelogic/go-server/pkg/types"
614
)
715

816
//////////////////////////////////////////////////////////////////////////////
@@ -25,6 +33,8 @@ type TranscriptionSummary struct {
2533
Duration Timestamp `json:"duration,omitempty"`
2634
}
2735

36+
var _ client.Unmarshaler = (*Transcription)(nil)
37+
2838
//////////////////////////////////////////////////////////////////////////////
2939
// STRINGIFY
3040

@@ -62,3 +72,41 @@ func SecToTimestamp(sec float64) Timestamp {
6272
// Convert seconds to Timestamp
6373
return Timestamp(time.Duration(sec * float64(time.Second)))
6474
}
75+
76+
//////////////////////////////////////////////////////////////////////////////
77+
// UNMARSHAL
78+
79+
// Unmarshal implements the client.Unmarshaler interface for transcriptions,
80+
// which can accept JSON, SRT, VTT or plain text formats.
81+
func (t *Transcription) Unmarshal(header http.Header, reader io.Reader) error {
82+
mimetype, err := types.ParseContentType(header.Get(types.ContentTypeHeader))
83+
if err != nil {
84+
return err
85+
}
86+
switch {
87+
case slices.Contains(ContentTypeSRTVariants, mimetype):
88+
if data, err := io.ReadAll(reader); err != nil {
89+
return err
90+
} else {
91+
t.Text = string(data)
92+
return nil
93+
}
94+
case slices.Contains(ContentTypeVTTVariants, mimetype):
95+
if data, err := io.ReadAll(reader); err != nil {
96+
return err
97+
} else {
98+
t.Text = string(data)
99+
return nil
100+
}
101+
case mimetype == types.ContentTypeTextPlain:
102+
if data, err := io.ReadAll(reader); err != nil {
103+
return err
104+
} else {
105+
t.Text = string(data)
106+
return nil
107+
}
108+
default:
109+
// For other types, pass through to underlying JSON unmarshaller
110+
return httpresponse.ErrNotImplemented
111+
}
112+
}

third_party/whisper.cpp

0 commit comments

Comments
 (0)