-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9c9e91
commit 30a5422
Showing
8 changed files
with
190 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package elevenlabs | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type Config struct { | ||
url string | ||
|
||
token string | ||
model string | ||
|
||
client *http.Client | ||
} | ||
|
||
type Option func(*Config) | ||
|
||
func WithClient(client *http.Client) Option { | ||
return func(c *Config) { | ||
c.client = client | ||
} | ||
} | ||
|
||
func WithURL(url string) Option { | ||
return func(c *Config) { | ||
c.url = url | ||
} | ||
} | ||
|
||
func WithToken(token string) Option { | ||
return func(c *Config) { | ||
c.token = token | ||
} | ||
} | ||
|
||
func WithModel(model string) Option { | ||
return func(c *Config) { | ||
c.model = model | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package elevenlabs | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/adrianliechti/llama/pkg/provider" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
var _ provider.Synthesizer = (*Synthesizer)(nil) | ||
|
||
type Synthesizer struct { | ||
*Config | ||
} | ||
|
||
func NewSynthesizer(options ...Option) (*Synthesizer, error) { | ||
cfg := &Config{ | ||
url: "https://api.elevenlabs.io", | ||
|
||
client: http.DefaultClient, | ||
} | ||
|
||
for _, option := range options { | ||
option(cfg) | ||
} | ||
|
||
return &Synthesizer{ | ||
Config: cfg, | ||
}, nil | ||
} | ||
|
||
func (s *Synthesizer) Synthesize(ctx context.Context, content string, options *provider.SynthesizeOptions) (*provider.Synthesis, error) { | ||
if options == nil { | ||
options = new(provider.SynthesizeOptions) | ||
} | ||
|
||
u, _ := url.Parse(strings.TrimRight(s.url, "/") + "/v1/text-to-speech/" + s.model) | ||
|
||
body := map[string]any{ | ||
"text": content, | ||
"model_id": "eleven_multilingual_v2", | ||
} | ||
|
||
req, _ := http.NewRequestWithContext(ctx, "POST", u.String(), jsonReader(body)) | ||
req.Header.Set("xi-api-key", s.token) | ||
|
||
resp, err := s.client.Do(req) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, convertError(resp) | ||
} | ||
|
||
id := uuid.NewString() | ||
|
||
return &provider.Synthesis{ | ||
ID: id, | ||
|
||
Name: id + ".mp3", | ||
Content: resp.Body, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package elevenlabs | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
func convertError(resp *http.Response) error { | ||
data, _ := io.ReadAll(resp.Body) | ||
|
||
if len(data) == 0 { | ||
return errors.New(http.StatusText(resp.StatusCode)) | ||
} | ||
|
||
return errors.New(string(data)) | ||
} | ||
|
||
func jsonReader(v any) io.Reader { | ||
b := new(bytes.Buffer) | ||
|
||
enc := json.NewEncoder(b) | ||
enc.SetEscapeHTML(false) | ||
|
||
enc.Encode(v) | ||
return b | ||
} |