Skip to content

Commit

Permalink
Return durations from AM if any
Browse files Browse the repository at this point in the history
  • Loading branch information
airenas committed Aug 25, 2023
1 parent 44ea943 commit 71f1636
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 28 deletions.
17 changes: 10 additions & 7 deletions internal/pkg/wrapservice/api/api.go
Original file line number Diff line number Diff line change
@@ -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"`
}
2 changes: 1 addition & 1 deletion internal/pkg/wrapservice/api/internal.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package api

//Params synthesis configuration params
// Params synthesis configuration params
type Params struct {
Text string
Speed float32
Expand Down
23 changes: 15 additions & 8 deletions internal/pkg/wrapservice/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/wrapservice/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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})
Expand Down
5 changes: 2 additions & 3 deletions internal/pkg/wrapservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions internal/pkg/wrapservice/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}

0 comments on commit 71f1636

Please sign in to comment.