Skip to content

Commit

Permalink
add coqui.ai
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianliechti committed May 16, 2024
1 parent 54d72a6 commit 016d517
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 8 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ providers:
#### Mimic 3
```docker
```shell
mkdir -p mimic3
chmod 777 mimic3
docker run -it -p 59125:59125 -v $(pwd)/models/mimic3:/home/mimic3/.local/share/mycroft/mimic3 mycroftai/mimic3
Expand All @@ -171,6 +171,24 @@ providers:
id: mimic-3
```
```shell
docker run --rm -it -p 5002:5002 --platform linux/amd64 --entrypoint /bin/bash ghcr.io/coqui-ai/tts-cpu
python3 TTS/server/server.py --list_models
python3 TTS/server/server.py --model_name tts_models/en/vctk/vits
```

#### Coqui

```yaml
providers:
- type: coqui
url: http://localhost:5002

models:
coqui-1:
id: coqui-1
```
#### LangChain / LangServe
Expand Down
10 changes: 10 additions & 0 deletions config/config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/adrianliechti/llama/pkg/provider/anthropic"
"github.com/adrianliechti/llama/pkg/provider/automatic1111"
"github.com/adrianliechti/llama/pkg/provider/azuretranslator"
"github.com/adrianliechti/llama/pkg/provider/coqui"
"github.com/adrianliechti/llama/pkg/provider/custom"
"github.com/adrianliechti/llama/pkg/provider/deepl"
"github.com/adrianliechti/llama/pkg/provider/groq"
Expand Down Expand Up @@ -162,6 +163,9 @@ func createProvider(cfg providerConfig, model string) (any, error) {
case "groq":
return groqProvider(cfg, model)

case "coqui":
return coquiProvider(cfg)

case "mimic":
return mimicProvider(cfg)

Expand Down Expand Up @@ -296,6 +300,12 @@ func groqProvider(cfg providerConfig, model string) (*groq.Client, error) {
return groq.New(options...)
}

func coquiProvider(cfg providerConfig) (*coqui.Client, error) {
var options []coqui.Option

return coqui.New(cfg.URL, options...)
}

func mimicProvider(cfg providerConfig) (*mimic.Client, error) {
var options []mimic.Option

Expand Down
2 changes: 1 addition & 1 deletion examples/anthropic-chat/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Anthropic Chat

```bash
```shell
export ANTHROPIC_API_KEY=sk-ant-......

docker compose up --force-recreate --remove-orphans
Expand Down
2 changes: 1 addition & 1 deletion examples/azure-chat/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Azure OpenAI Chat

```bash
```shell
export OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
export OPENAI_BASE_URL=https://xxxxxxxx.openai.azure.com

Expand Down
2 changes: 1 addition & 1 deletion examples/groq-chat/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Anthropic Chat

```bash
```shell
export GROQ_API_TOKEN=gsk_......

docker compose up --force-recreate --remove-orphans
Expand Down
2 changes: 1 addition & 1 deletion examples/huggingface-chat/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Anthropic Chat

```bash
```shell
export HF_TOKEN=hf_......

docker compose up --force-recreate --remove-orphans
Expand Down
4 changes: 2 additions & 2 deletions examples/kubernetes-chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

## Installation

```bash
```shell
kubectl create namespace llm-demo
kubectl apply -n llm-demo -k https://github.com/adrianliechti/llama/examples/kubernetes-chat/
kubectl port-forward service/chat 8501:80 -n llm-demo
```

## Demo Client

```bash
```shell
kubectl port-forward service/chat :80 -n llm-demo
```

Expand Down
2 changes: 1 addition & 1 deletion examples/openai-chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- Image Recognition (Vision)
- Audio Transcriptions (Whisper)

```bash
```shell
export OPENAI_API_KEY=sk-......

docker compose up --force-recreate --remove-orphans
Expand Down
35 changes: 35 additions & 0 deletions pkg/provider/coqui/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package coqui

import (
"errors"
"io"
"net/http"

"github.com/adrianliechti/llama/pkg/provider"
)

type Client struct {
provider.Synthesizer
}

func New(url string, options ...Option) (*Client, error) {
s, err := NewSynthesizer(url, options...)

if err != nil {
return nil, err
}

return &Client{
Synthesizer: s,
}, nil
}

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))
}
19 changes: 19 additions & 0 deletions pkg/provider/coqui/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package coqui

import (
"net/http"
)

type Config struct {
url string

client *http.Client
}

type Option func(*Config)

func WithClient(client *http.Client) Option {
return func(c *Config) {
c.client = client
}
}
76 changes: 76 additions & 0 deletions pkg/provider/coqui/synthesizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package coqui

import (
"context"
"errors"
"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(url string, options ...Option) (*Synthesizer, error) {
if url == "" {
return nil, errors.New("invalid url")
}

cfg := &Config{
url: url,

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, "/") + "/api/tts")

query := u.Query()

query.Set("text", content)
query.Set("speaker_id", "p376")

u.RawQuery = query.Encode()

req, _ := http.NewRequestWithContext(ctx, "GET", u.String(), nil)

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 + ".wav",
Content: resp.Body,
}, nil
}

0 comments on commit 016d517

Please sign in to comment.