From 71f163623393cf81df1ef64f2778ae1c8a14dafc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Airenas=20Vai=C4=8Di=C5=ABnas?= Date: Fri, 25 Aug 2023 20:06:16 +0300 Subject: [PATCH] Return durations from AM if any --- internal/pkg/wrapservice/api/api.go | 17 +++++++++------- internal/pkg/wrapservice/api/internal.go | 2 +- internal/pkg/wrapservice/processor.go | 23 ++++++++++++++-------- internal/pkg/wrapservice/processor_test.go | 8 ++++---- internal/pkg/wrapservice/service.go | 5 ++--- internal/pkg/wrapservice/service_test.go | 10 +++++----- 6 files changed, 37 insertions(+), 28 deletions(-) diff --git a/internal/pkg/wrapservice/api/api.go b/internal/pkg/wrapservice/api/api.go index 66470ae..6bff0e6 100644 --- a/internal/pkg/wrapservice/api/api.go +++ b/internal/pkg/wrapservice/api/api.go @@ -1,14 +1,17 @@ package api -//Input is synthesizer input data +// Input is synthesizer input data type Input struct { - Text string `json:"text,omitempty"` - Speed float32 `json:"speedAlpha,omitempty"` - Voice string `json:"voice,omitempty"` - Priority int `json:"priority,omitempty"` + Text string `json:"text,omitempty"` + Speed float32 `json:"speedAlpha,omitempty"` + Voice string `json:"voice,omitempty"` + Priority int `json:"priority,omitempty"` } -//Result is synthesis result +// Result is synthesis result type Result struct { - Data string `json:"data,omitempty"` + Data string `json:"data,omitempty"` + Durations []int `json:"durations,omitempty"` + SilDuration int `json:"silDuration,omitempty"` + Error string `json:"error,omitempty"` } diff --git a/internal/pkg/wrapservice/api/internal.go b/internal/pkg/wrapservice/api/internal.go index 9f52168..8fbb1db 100644 --- a/internal/pkg/wrapservice/api/internal.go +++ b/internal/pkg/wrapservice/api/internal.go @@ -1,6 +1,6 @@ package api -//Params synthesis configuration params +// Params synthesis configuration params type Params struct { Text string Speed float32 diff --git a/internal/pkg/wrapservice/processor.go b/internal/pkg/wrapservice/processor.go index 265351b..4b0b890 100644 --- a/internal/pkg/wrapservice/processor.go +++ b/internal/pkg/wrapservice/processor.go @@ -11,13 +11,13 @@ import ( "github.com/pkg/errors" ) -//Processor does synthesis work +// Processor does synthesis work type Processor struct { amWrap processor.HTTPInvokerJSON vocWrap processor.HTTPInvokerJSON } -//NewProcessor creates new processor +// NewProcessor creates new processor func NewProcessor(amURL, vocURL string) (*Processor, error) { res := &Processor{} goapp.Log.Infof("AM URL: %s", amURL+"/model") @@ -56,23 +56,23 @@ func NewProcessor(amURL, vocURL string) (*Processor, error) { return res, nil } -//Work is main method -func (p *Processor) Work(params *api.Params) (string, error) { +// Work is main method +func (p *Processor) Work(params *api.Params) (*api.Result, error) { amIn := amInput{Text: params.Text, Speed: params.Speed, Voice: params.Voice, Priority: params.Priority} - var amOut output + var amOut amOutput err := p.amWrap.InvokeJSON(&amIn, &amOut) if err != nil { totalFailureMetrics.WithLabelValues("am", params.Voice).Add(1) - return "", errors.Wrap(err, "can't invoke AM") + return nil, errors.Wrap(err, "can't invoke AM") } vocIn := vocInput{Data: amOut.Data, Voice: params.Voice, Priority: params.Priority} var vocOut output err = p.vocWrap.InvokeJSON(&vocIn, &vocOut) if err != nil { totalFailureMetrics.WithLabelValues("vocoder", params.Voice).Add(1) - return "", errors.Wrap(err, "can't invoke Vocoder") + return nil, errors.Wrap(err, "can't invoke Vocoder") } - return vocOut.Data, nil + return &api.Result{Data: vocOut.Data, Durations: amOut.Durations, SilDuration: amOut.SilDuration}, nil } type amInput struct { @@ -92,6 +92,13 @@ type output struct { Data string `json:"data"` } +type amOutput struct { + Data string `json:"data"` + Durations []int `json:"durations,omitempty"` + SilDuration int `json:"silDuration,omitempty"` + Error string `json:"error,omitempty"` +} + func newBackoff() backoff.BackOff { res := backoff.NewExponentialBackOff() res.InitialInterval = time.Second * 2 diff --git a/internal/pkg/wrapservice/processor_test.go b/internal/pkg/wrapservice/processor_test.go index aedca1c..23a3f46 100644 --- a/internal/pkg/wrapservice/processor_test.go +++ b/internal/pkg/wrapservice/processor_test.go @@ -49,16 +49,16 @@ func TestInvokeProcessor(t *testing.T) { httpAMMock.On("InvokeJSON", mock.Anything, mock.Anything).Run( func(params mock.Arguments) { - *params[1].(*output) = output{Data: "specs"} + *params[1].(*amOutput) = amOutput{Data: "specs", Durations: []int{10, 12}, SilDuration: 15} }).Return(nil) httpVocMock.On("InvokeJSON", mock.Anything, mock.Anything).Run( func(params mock.Arguments) { *params[1].(*output) = output{Data: "audio"} }).Return(nil) - text, err := pr.Work(&api.Params{Text: "olia", Speed: 0.9, Voice: "voice", Priority: 10}) + res, err := pr.Work(&api.Params{Text: "olia", Speed: 0.9, Voice: "voice", Priority: 10}) assert.Nil(t, err) - assert.Equal(t, "audio", text) + assert.Equal(t, &api.Result{Data: "audio", Durations: []int{10, 12}, SilDuration: 15}, res) httpAMMock.AssertNumberOfCalls(t, "InvokeJSON", 1) cp1 := httpAMMock.Calls[0].Arguments[0] @@ -97,7 +97,7 @@ func TestInvokeProcessor_FailVoc(t *testing.T) { httpAMMock.On("InvokeJSON", mock.Anything, mock.Anything).Run( func(params mock.Arguments) { - *params[1].(*output) = output{Data: "audio"} + *params[1].(*amOutput) = amOutput{Data: "audio"} }).Return(nil) httpVocMock.On("InvokeJSON", mock.Anything, mock.Anything).Return(errors.New("haha")) _, err := pr.Work(&api.Params{Text: "olia", Speed: 1, Voice: "voice", Priority: 10}) diff --git a/internal/pkg/wrapservice/service.go b/internal/pkg/wrapservice/service.go index 8084933..10cd912 100644 --- a/internal/pkg/wrapservice/service.go +++ b/internal/pkg/wrapservice/service.go @@ -20,7 +20,7 @@ import ( type ( //WaveSynthesizer main sythesis processor WaveSynthesizer interface { - Work(params *api.Params) (string, error) + Work(params *api.Params) (*api.Result, error) } //Data is service operation data Data struct { @@ -92,12 +92,11 @@ func handleSynthesize(data *Data) func(echo.Context) error { return echo.NewHTTPError(http.StatusBadRequest, "No voice") } - resp, err := data.Processor.Work(&api.Params{Text: input.Text, Speed: input.Speed, Voice: input.Voice, Priority: input.Priority}) + res, err := data.Processor.Work(&api.Params{Text: input.Text, Speed: input.Speed, Voice: input.Voice, Priority: input.Priority}) if err != nil { goapp.Log.Error(errors.Wrap(err, "cannot process text")) return echo.NewHTTPError(http.StatusInternalServerError, "Cannot process text") } - res := &api.Result{Data: resp} c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().WriteHeader(http.StatusOK) return json.NewEncoder(c.Response()).Encode(res) diff --git a/internal/pkg/wrapservice/service_test.go b/internal/pkg/wrapservice/service_test.go index 8ce4d0b..39144cb 100644 --- a/internal/pkg/wrapservice/service_test.go +++ b/internal/pkg/wrapservice/service_test.go @@ -68,12 +68,12 @@ func TestWrongMethod(t *testing.T) { func Test_Returns(t *testing.T) { initTest(t) - synthesizerMock.On("Work", mock.Anything).Return("wav", nil) + synthesizerMock.On("Work", mock.Anything).Return(&api.Result{Data: "wav"}, nil) req := httptest.NewRequest("POST", "/synthesize", toReader(api.Input{Text: "olia", Speed: 0.9, Voice: "aa", Priority: 10})) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) resp := testCode(t, req, 200) bytes, _ := io.ReadAll(resp.Body) - assert.Contains(t, string(bytes), `"data":"wav"`) + assert.Equal(t, `{"data":"wav"}`, strings.TrimSpace(string(bytes))) synthesizerMock.AssertNumberOfCalls(t, "Work", 1) gPrms := mocks.To[*api.Params](synthesizerMock.Calls[0].Arguments[0]) @@ -85,7 +85,7 @@ func Test_Returns(t *testing.T) { func Test_Fail(t *testing.T) { initTest(t) - synthesizerMock.On("Work", mock.Anything).Return("", errors.New("haha")) + synthesizerMock.On("Work", mock.Anything).Return(nil, errors.New("haha")) req := httptest.NewRequest("POST", "/synthesize", toReader(api.Input{Text: "olia", Voice: "aa"})) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) testCode(t, req, 500) @@ -122,7 +122,7 @@ func testCode(t *testing.T, req *http.Request, code int) *httptest.ResponseRecor type mockWaveSynthesizer struct{ mock.Mock } -func (m *mockWaveSynthesizer) Work(in *api.Params) (string, error) { +func (m *mockWaveSynthesizer) Work(in *api.Params) (*api.Result, error) { args := m.Called(in) - return mocks.To[string](args.Get(0)), args.Error(1) + return mocks.To[*api.Result](args.Get(0)), args.Error(1) }