voice: add voice catalog for Phonon (#19242)
* mlxrunner: import and list Phonon catalog voices * docs: document Phonon voices
This commit is contained in:
1 parent
547b98ea13
commit
675150d6ad
13 files changed
+188
-38
No files matched your search
@@ -821,6 +821,7 @@ type ShowResponse struct {
|
||||
ProjectorInfo map[string]any `json:"projector_info,omitempty"`
|
||||
Tensors []Tensor `json:"tensors,omitempty"`
|
||||
Capabilities []model.Capability `json:"capabilities,omitempty"`
|
||||
Voices []string `json:"voices,omitempty"`
|
||||
Manifests []ManifestSummary `json:"manifests,omitempty"`
|
||||
ModifiedAt time.Time `json:"modified_at,omitempty"`
|
||||
Requires string `json:"requires,omitempty"`
|
||||
|
||||
@@ -1511,6 +1511,15 @@ func showInfo(resp *api.ShowResponse, verbose bool, w io.Writer) error {
|
||||
})
|
||||
}
|
||||
|
||||
if len(resp.Voices) > 0 {
|
||||
tableRender("Voices", func() (rows [][]string) {
|
||||
for _, voice := range resp.Voices {
|
||||
rows = append(rows, []string{"", voice})
|
||||
}
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
if resp.ProjectorInfo != nil {
|
||||
tableRender("Projector", func() (rows [][]string) {
|
||||
arch, _ := resp.ProjectorInfo["general.architecture"].(string)
|
||||
|
||||
@@ -2860,6 +2860,35 @@ func TestShowInfoImageCapability(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestShowInfoSpeechVoices(t *testing.T) {
|
||||
var b bytes.Buffer
|
||||
err := showInfo(&api.ShowResponse{
|
||||
Details: api.ModelDetails{Family: "phonon", ParameterSize: "31M", QuantizationLevel: "F32"},
|
||||
Capabilities: []model.Capability{model.CapabilitySpeech},
|
||||
Voices: []string{"default", "Fairy Godmother", "Zoey"},
|
||||
}, false, &b)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expect := " Model\n" +
|
||||
" architecture phonon \n" +
|
||||
" parameters 31M \n" +
|
||||
" quantization F32 \n" +
|
||||
"\n" +
|
||||
" Capabilities\n" +
|
||||
" speech \n" +
|
||||
"\n" +
|
||||
" Voices\n" +
|
||||
" default \n" +
|
||||
" Fairy Godmother \n" +
|
||||
" Zoey \n" +
|
||||
"\n"
|
||||
if diff := cmp.Diff(expect, b.String()); diff != "" {
|
||||
t.Errorf("unexpected output (-want +got):\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushProgressMessage(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -141,6 +141,11 @@ func SafetensorsWeightFiles(dir string) ([]string, error) {
|
||||
if !slices.Contains(files, "default-voice.safetensors") {
|
||||
return nil, fmt.Errorf("Phonon checkpoint requires default-voice.safetensors")
|
||||
}
|
||||
voices, err := phononVoiceFiles(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files = append(files, voices...)
|
||||
}
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
|
||||
+61
-23
@@ -1,9 +1,13 @@
|
||||
package create
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/ollama/ollama/fs/safetensors"
|
||||
)
|
||||
@@ -14,34 +18,68 @@ func isPhononArchitecture(architecture string) bool {
|
||||
return architecture == gradiumPhononArchitecture
|
||||
}
|
||||
|
||||
// The publisher keeps its default speaker latents beside the model weights.
|
||||
// Import them as a separate tensor so the runner can project them with the
|
||||
// checkpoint's own speaker projection at load time.
|
||||
// The publisher keeps its default speaker latents beside the model weights
|
||||
// and any other voices in voices/NAME.safetensors. Import each as a separate
|
||||
// tensor so the runner can project them with the checkpoint's own speaker
|
||||
// projection at load time.
|
||||
func prepareGradiumPhononInventory(inv Inventory) (Inventory, error) {
|
||||
const filename = "default-voice.safetensors"
|
||||
reader, err := safetensors.OpenForExtraction(filepath.Join(inv.Dir, filename))
|
||||
if err != nil {
|
||||
return Inventory{}, fmt.Errorf("open Phonon default voice: %w", err)
|
||||
}
|
||||
defer reader.Close()
|
||||
if names := reader.ListTensors(); !slices.Equal(names, []string{"speaker_wavs"}) {
|
||||
return Inventory{}, fmt.Errorf("Phonon default voice must contain only speaker_wavs, got %v", names)
|
||||
}
|
||||
voice, err := reader.GetTensor("speaker_wavs")
|
||||
if err != nil {
|
||||
return Inventory{}, fmt.Errorf("read Phonon default voice: %w", err)
|
||||
}
|
||||
if voice.Dtype != "F32" || len(voice.Shape) != 3 || voice.Shape[0] != 1 || voice.Shape[1] != 512 || voice.Shape[2] <= 0 {
|
||||
return Inventory{}, fmt.Errorf("unsupported Phonon default voice: %s %v", voice.Dtype, voice.Shape)
|
||||
}
|
||||
if !inv.Has("flow_lm.condition_provider.conditioners.speaker_wavs.output_proj.weight") {
|
||||
return Inventory{}, fmt.Errorf("Phonon checkpoint has no speaker projection")
|
||||
}
|
||||
if inv.Has("voices.default") {
|
||||
return Inventory{}, fmt.Errorf("Phonon checkpoint already defines voices.default")
|
||||
files, err := phononVoiceFiles(inv.Dir)
|
||||
if err != nil {
|
||||
return Inventory{}, err
|
||||
}
|
||||
inv.Tensors["voices.default"] = SourceTensor{
|
||||
Name: "speaker_wavs", Dtype: voice.Dtype, Shape: voice.Shape, File: filename,
|
||||
if err := addPhononVoice(inv, "default", "default-voice.safetensors"); err != nil {
|
||||
return Inventory{}, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if err := addPhononVoice(inv, strings.TrimSuffix(path.Base(file), ".safetensors"), file); err != nil {
|
||||
return Inventory{}, err
|
||||
}
|
||||
}
|
||||
return inv, nil
|
||||
}
|
||||
|
||||
func addPhononVoice(inv Inventory, name, file string) error {
|
||||
reader, err := safetensors.OpenForExtraction(filepath.Join(inv.Dir, file))
|
||||
if err != nil {
|
||||
return fmt.Errorf("open Phonon %s voice: %w", name, err)
|
||||
}
|
||||
defer reader.Close()
|
||||
if names := reader.ListTensors(); !slices.Equal(names, []string{"speaker_wavs"}) {
|
||||
return fmt.Errorf("Phonon %s voice must contain only speaker_wavs, got %v", name, names)
|
||||
}
|
||||
voice, err := reader.GetTensor("speaker_wavs")
|
||||
if err != nil {
|
||||
return fmt.Errorf("read Phonon %s voice: %w", name, err)
|
||||
}
|
||||
if voice.Dtype != "F32" || len(voice.Shape) != 3 || voice.Shape[0] != 1 || voice.Shape[1] != 512 || voice.Shape[2] <= 0 {
|
||||
return fmt.Errorf("unsupported Phonon %s voice: %s %v", name, voice.Dtype, voice.Shape)
|
||||
}
|
||||
if inv.Has("voices." + name) {
|
||||
return fmt.Errorf("Phonon checkpoint already defines voices.%s", name)
|
||||
}
|
||||
inv.Tensors["voices."+name] = SourceTensor{
|
||||
Name: "speaker_wavs", Dtype: voice.Dtype, Shape: voice.Shape, File: file,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// phononVoiceFiles lists a checkpoint's voices/NAME.safetensors files.
|
||||
func phononVoiceFiles(dir string) ([]string, error) {
|
||||
entries, err := os.ReadDir(filepath.Join(dir, "voices"))
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var files []string
|
||||
for _, entry := range entries {
|
||||
if name, ok := strings.CutSuffix(entry.Name(), ".safetensors"); ok && name != "" && !entry.IsDir() {
|
||||
files = append(files, path.Join("voices", entry.Name()))
|
||||
}
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
+10
-4
@@ -26,11 +26,17 @@ func writeGradiumPhononSource(t *testing.T, dir string, voiceShape []int32) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGradiumPhononImportsPublisherVoice(t *testing.T) {
|
||||
func TestGradiumPhononImportsPublisherVoices(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeGradiumPhononSource(t, dir, []int32{1, 512, 2})
|
||||
if err := os.Mkdir(filepath.Join(dir, "voices"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
createTestSafetensors(t, filepath.Join(dir, "voices", "Zoey.safetensors"), []*st.TensorData{
|
||||
st.NewTensorDataFromBytes("speaker_wavs", "F32", []int32{1, 512, 2}, make([]byte, 4*512*2)),
|
||||
})
|
||||
files, err := SafetensorsWeightFiles(dir)
|
||||
if err != nil || !slices.Contains(files, "default-voice.safetensors") {
|
||||
if err != nil || !slices.Contains(files, "default-voice.safetensors") || !slices.Contains(files, "voices/Zoey.safetensors") {
|
||||
t.Fatalf("weight files = %v, %v", files, err)
|
||||
}
|
||||
store := newCaptureStore()
|
||||
@@ -45,8 +51,8 @@ func TestGradiumPhononImportsPublisherVoice(t *testing.T) {
|
||||
if !slices.Equal(got.ModelConfig.Capabilities, []string{"speech"}) || got.ModelConfig.Requires != phononMinOllamaVersion {
|
||||
t.Fatalf("model config = %+v", got.ModelConfig)
|
||||
}
|
||||
if store.blobs["voices.default"] == nil {
|
||||
t.Fatalf("missing default voice blob: %v", store.names())
|
||||
if store.blobs["voices.default"] == nil || store.blobs["voices.Zoey"] == nil {
|
||||
t.Fatalf("missing voice blobs: %v", store.names())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -325,7 +325,7 @@ Generates audio with [speech models](/capabilities/speech). The response streams
|
||||
|
||||
- [x] `model`
|
||||
- [x] `input` (up to 4,096 characters)
|
||||
- [x] `voice` (one of the model's voice names, such as `Freya`; omit it for the model's default)
|
||||
- [x] `voice` (one of the voices `ollama show` lists, in any case and with or without accents, such as `Freya`; omit it for the model's `default` voice)
|
||||
- [x] `response_format`
|
||||
- [x] `wav` (default)
|
||||
- [x] `pcm`
|
||||
|
||||
@@ -13,7 +13,7 @@ Start [Ollama](/quickstart) v0.40.9 or later, then download [Phonon](https://oll
|
||||
ollama pull phonon
|
||||
```
|
||||
|
||||
The default Phonon model uses Gradium's 31M English checkpoint and its included voice. The voice name is `default`.
|
||||
Phonon uses Gradium's 31M English checkpoint and comes with Gradium's English voices, such as `Zoey`, `Easton`, and `Freya`. Without a voice, it speaks with the checkpoint's own voice, `default`. `ollama show phonon` lists every voice. Voice names work in any case and with or without accents.
|
||||
|
||||
## Speak from the terminal
|
||||
|
||||
@@ -23,6 +23,9 @@ The default Phonon model uses Gradium's 31M English checkpoint and its included
|
||||
# Speak some text
|
||||
ollama run phonon "Hello from Ollama."
|
||||
|
||||
# Choose a voice
|
||||
ollama run phonon --voice Zoey "Hi, this is Zoey."
|
||||
|
||||
# Speak text from another program
|
||||
echo "Text from another program works too." | ollama run phonon
|
||||
|
||||
@@ -122,4 +125,4 @@ curl -sN http://localhost:11434/v1/audio/speech \
|
||||
- Options the model doesn't support, such as other formats, speeds, or `instructions`, return a `400` error instead of being ignored. An unknown voice returns a `400` error that lists the available voices.
|
||||
- Speech models only generate speech: chat, completion, and embedding requests to them return a `400` error.
|
||||
|
||||
`ollama show phonon` and `/api/show` list the model's capabilities as `speech`. See [OpenAI compatibility](/api/openai-compatibility) for every supported field.
|
||||
`ollama show phonon` and `/api/show` list the model's capabilities as `speech`, and its voices (`voices` in `/api/show`). See [OpenAI compatibility](/api/openai-compatibility) for every supported field.
|
||||
@@ -101,6 +101,8 @@ The state is piped in or given after the model name, as text or JSON. Each `--as
|
||||
```
|
||||
ollama run phonon "Hello from Ollama."
|
||||
|
||||
ollama run phonon --voice Zoey "Hi, this is Zoey."
|
||||
|
||||
ollama run phonon "Save this to a file." > speech.wav
|
||||
```
|
||||
|
||||
|
||||
@@ -9,6 +9,9 @@ import (
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"golang.org/x/text/unicode/norm"
|
||||
|
||||
"github.com/ollama/ollama/api"
|
||||
"github.com/ollama/ollama/llm"
|
||||
@@ -233,8 +236,8 @@ func (m *Model) LoadWeights(tensors map[string]*mlx.Array) error {
|
||||
}
|
||||
m.Voices[voice] = m.prefillVoice(t)
|
||||
}
|
||||
if len(m.Voices) == 0 {
|
||||
return fmt.Errorf("model has no voices")
|
||||
if m.Voices["default"] == nil {
|
||||
return fmt.Errorf("model has no default voice")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -247,9 +250,12 @@ func (m *Model) prefillVoice(prompt *mlx.Array) *Voice {
|
||||
}
|
||||
kv := mlx.ScopedEval(func() []*mlx.Array {
|
||||
m.Flow.forward(prompt, 0, caches)
|
||||
// Copy out the prompt's frames so the cache's padded buffers are freed.
|
||||
var kv []*mlx.Array
|
||||
for _, c := range caches {
|
||||
kv = append(kv, c.State()...)
|
||||
for _, s := range c.State() {
|
||||
kv = append(kv, mlx.Contiguous(s, false))
|
||||
}
|
||||
}
|
||||
return kv
|
||||
})
|
||||
@@ -273,13 +279,37 @@ func (m *Model) MaxContextLength() int { return 0 }
|
||||
|
||||
func (m *Model) VoiceNames() []string { return slices.Sorted(maps.Keys(m.Voices)) }
|
||||
|
||||
func (m *Model) Speak(ctx context.Context, req llm.SpeechRequest, emit func([]float32) error) error {
|
||||
name := req.Voice
|
||||
// voice finds a voice by name in any case and with or without accents, as
|
||||
// the publisher's runtime does. No name means the checkpoint's own voice,
|
||||
// default.
|
||||
func (m *Model) voice(name string) *Voice {
|
||||
if name == "" {
|
||||
name = m.VoiceNames()[0]
|
||||
name = "default"
|
||||
}
|
||||
voice, ok := m.Voices[name]
|
||||
if !ok {
|
||||
if v, ok := m.Voices[name]; ok {
|
||||
return v
|
||||
}
|
||||
for n, v := range m.Voices {
|
||||
if foldVoiceName(n) == foldVoiceName(name) {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func foldVoiceName(s string) string {
|
||||
var b strings.Builder
|
||||
for _, r := range norm.NFD.String(s) {
|
||||
if !unicode.Is(unicode.Mn, r) {
|
||||
b.WriteRune(unicode.ToLower(r))
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *Model) Speak(ctx context.Context, req llm.SpeechRequest, emit func([]float32) error) error {
|
||||
voice := m.voice(req.Voice)
|
||||
if voice == nil {
|
||||
return api.StatusError{StatusCode: http.StatusBadRequest, ErrorMessage: fmt.Sprintf("voice %q not found; available voices: %s", req.Voice, strings.Join(m.VoiceNames(), ", "))}
|
||||
}
|
||||
chunks, err := m.tok.chunks(req.Input)
|
||||
|
||||
@@ -53,6 +53,16 @@ func TestPrereleaseConfigRejected(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestVoiceLookup(t *testing.T) {
|
||||
def, zoey, kaua := &Voice{}, &Voice{}, &Voice{}
|
||||
m := &Model{Voices: map[string]*Voice{"default": def, "Zoey": zoey, "Kauã": kaua}}
|
||||
for name, want := range map[string]*Voice{"": def, "DEFAULT": def, "Zoey": zoey, "zoey": zoey, "kaua": kaua, "KAUÃ": kaua, "alloy": nil} {
|
||||
if got := m.voice(name); got != want {
|
||||
t.Errorf("voice(%q) = %p, want %p", name, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeNorm(t *testing.T) {
|
||||
mlxtest.Run(t, func(t *mlxtest.T) {
|
||||
values := []float32{1, 2, 3, 6}
|
||||
|
||||
@@ -1945,6 +1945,9 @@ func GetModelInfo(req api.ShowRequest) (*api.ShowResponse, error) {
|
||||
if !slices.Contains(resp.Capabilities, model.CapabilityDecision) {
|
||||
resp.Thinking = m.Thinking()
|
||||
}
|
||||
if slices.Contains(resp.Capabilities, model.CapabilitySpeech) {
|
||||
resp.Voices = speechVoices(mf)
|
||||
}
|
||||
|
||||
if summaries, err := manifestSummariesForShow(name, mf.SelectedDigest()); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
@@ -15,6 +16,7 @@ import (
|
||||
|
||||
"github.com/ollama/ollama/api"
|
||||
"github.com/ollama/ollama/llm"
|
||||
"github.com/ollama/ollama/manifest"
|
||||
"github.com/ollama/ollama/openai"
|
||||
"github.com/ollama/ollama/types/model"
|
||||
)
|
||||
@@ -144,6 +146,18 @@ func speechError(c *gin.Context, status int, message string) {
|
||||
c.AbortWithStatusJSON(status, openai.NewError(status, message))
|
||||
}
|
||||
|
||||
// speechVoices lists the voices create stored as voices.<name> tensors.
|
||||
func speechVoices(mf *manifest.Manifest) []string {
|
||||
var voices []string
|
||||
for _, layer := range mf.Layers {
|
||||
if name, ok := strings.CutPrefix(layer.Name, "voices."); ok && layer.MediaType == manifest.MediaTypeImageTensor {
|
||||
voices = append(voices, name)
|
||||
}
|
||||
}
|
||||
slices.SortFunc(voices, func(a, b string) int { return cmp.Compare(strings.ToLower(a), strings.ToLower(b)) })
|
||||
return voices
|
||||
}
|
||||
|
||||
// wavHeader describes 16-bit mono PCM of unknown length, as streamed WAV is.
|
||||
func wavHeader() []byte {
|
||||
h := make([]byte, 44)
|
||||
|
||||
Reference in new issue
Block a user