Text-to-Speech
MLX
PyTorch
Portuguese
tts
pt-br
brazilian-portuguese
tinyml
esp32
esp32-s3
microcontroller
embedded
quantized
int8
4-bit precision
fargan
pingo
webassembly
javascript
Instructions to use semfence/pingo-ptbr-edge with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use semfence/pingo-ptbr-edge with MLX:
# Download the model from the Hub pip install huggingface_hub[hf_xet] hf download semfence/pingo-ptbr-edge --local-dir pingo-ptbr-edge
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
Download python/pingo_edge/frontend.py from semfence/pingo-ptbr-edge: direct link, hf CLI and curl.
- Browser
- Download file 2.25 kB
-
https://huggingface.co/semfence/pingo-ptbr-edge/resolve/main/python/pingo_edge/frontend.py
- Command line
-
hf download hf://semfence/pingo-ptbr-edge/python/pingo_edge/frontend.py
-
curl -L -o frontend.py https://huggingface.co/semfence/pingo-ptbr-edge/resolve/main/python/pingo_edge/frontend.py
2.25 kB
| """Text -> speech. Phonemization, sentence splitting and respelling are the same as in semfence/pingo-ptbr (speak.py); the backend is PyTorch or MLX. | |
| Requires: pip install git+https://github.com/SemFence/dizione-ptbr git+https://github.com/SemFence/tiny-voca-ptbr""" | |
| import numpy as np | |
| from .pronunciation import joined, spoken | |
| from tiny_voca.infer import finished, sentences, shorter | |
| from tiny_voca.text import PHONE_IDS, NothingToPronounce, phonemize | |
| VOICES = {"F_s4": 0, "M_sm4": 1} | |
| RATE, PEAK = 16000, 0.99 | |
| class Pingo: | |
| def __init__(self, backend="torch", path=None): | |
| kw = {"path": path} if path else {} | |
| if backend == "torch": | |
| from .acoustic_torch import AcousticTorch as A | |
| from .vocoder_torch import Vocoder as V | |
| elif backend == "mlx": | |
| import mlx.core as mx | |
| mx.set_default_device(mx.cpu) # tiny model, sequential steps: the CPU beats the GPU | |
| from .acoustic_mlx_edge import AcousticMLX as A | |
| from .vocoder_mlx import Vocoder as V | |
| else: | |
| raise ValueError("backend: torch or mlx") | |
| self.acoustic, self.vocoder, self.backend = A(**kw), V(**kw), backend | |
| def __call__(self, ph, voice, speed): | |
| mask = np.array([i in PHONE_IDS for i in ph.ids]) | |
| feats, _ = self.acoustic.features(list(ph.ids), list(ph.stress), voice, speed, mask) | |
| return self.vocoder(feats) | |
| def speak(backend, text, voice="F_s4", speed=1.0, respelling=True): | |
| waves = [] | |
| if respelling: text = spoken(text) | |
| def read(piece): | |
| try: ph = joined(phonemize(piece)) if respelling else phonemize(piece) | |
| except NothingToPronounce: return | |
| if len(ph.ids) <= 400: | |
| try: waves.append(np.asarray(backend(ph, VOICES[voice], speed))); return | |
| except ValueError: pass | |
| parts = shorter(piece) | |
| if not parts: raise ValueError(f"word too long: {piece[:40]!r}") | |
| for part in parts: read(part) | |
| for sentence in sentences(finished(text)): read(sentence) | |
| if not waves: raise NothingToPronounce("nothing to read") | |
| wave = np.concatenate(waves); peak = float(np.abs(wave).max()) | |
| return wave * (PEAK / peak) if peak > PEAK else wave | |