Upload 3 files
Browse files- app.py +47 -0
- requirements.txt +9 -0
- tts_infer.py +46 -0
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ------
|
| 2 |
+
# Simple Gradio app: paste Fon text, click SYNTH, hear audio.
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from tts_infer import FonTTS
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
MODEL_NAME = os.environ.get("FON_TTS_MODEL", "facebook/mms-tts-fon")
|
| 9 |
+
|
| 10 |
+
# Create TTS backend (loaded once)
|
| 11 |
+
backend = None
|
| 12 |
+
|
| 13 |
+
def get_backend():
|
| 14 |
+
global backend
|
| 15 |
+
if backend is None:
|
| 16 |
+
backend = FonTTS(model_name=MODEL_NAME)
|
| 17 |
+
return backend
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def synthesize_text(text, seed=42):
|
| 21 |
+
if not text or text.strip() == "":
|
| 22 |
+
return None
|
| 23 |
+
tts = get_backend()
|
| 24 |
+
audio, sr = tts.synthesize(text, seed=int(seed))
|
| 25 |
+
out_path = "last_output.wav"
|
| 26 |
+
tts.save_wav(audio, sr, out_path)
|
| 27 |
+
return out_path
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def create_ui():
|
| 31 |
+
with gr.Blocks(title="Fon — Text to Speech (MMS-TTS)") as demo:
|
| 32 |
+
gr.Markdown("# Fon (Fongbé) — Text → Speech\nPaste Fon text (use standard orthography) and press Synthesize.")
|
| 33 |
+
with gr.Row():
|
| 34 |
+
txt = gr.Textbox(lines=4, label="Fon text", placeholder="e.g. e nɔ do fɔngbe ganji")
|
| 35 |
+
seed = gr.Number(value=42, label="Random seed (optional)")
|
| 36 |
+
synth_btn = gr.Button("Synthesize")
|
| 37 |
+
audio_out = gr.Audio(label="Output audio", type="filepath")
|
| 38 |
+
synth_btn.click(fn=synthesize_text, inputs=[txt, seed], outputs=audio_out)
|
| 39 |
+
return demo
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def main():
|
| 43 |
+
demo = create_ui()
|
| 44 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ----------------
|
| 2 |
+
# Minimal Python dependencies
|
| 3 |
+
|
| 4 |
+
transformers>=4.33.0
|
| 5 |
+
accelerate
|
| 6 |
+
scipy
|
| 7 |
+
torch
|
| 8 |
+
soundfile
|
| 9 |
+
gradio
|
tts_infer.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -------------
|
| 2 |
+
# Small helper that loads the MMS VITS Fon model and exposes a function to synthesize text -> waveform
|
| 3 |
+
|
| 4 |
+
from transformers import VitsModel, AutoTokenizer
|
| 5 |
+
import torch
|
| 6 |
+
import numpy as np
|
| 7 |
+
import soundfile as sf
|
| 8 |
+
|
| 9 |
+
class FonTTS:
|
| 10 |
+
def __init__(self, model_name="facebook/mms-tts-fon", device=None):
|
| 11 |
+
self.model_name = model_name
|
| 12 |
+
self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
+
print(f"Loading model {model_name} on {self.device}...")
|
| 14 |
+
self.model = VitsModel.from_pretrained(model_name).to(self.device)
|
| 15 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 16 |
+
# model.config.sampling_rate is expected; fallback to 16k if missing
|
| 17 |
+
self.sr = getattr(self.model.config, "sampling_rate", 16000)
|
| 18 |
+
|
| 19 |
+
def synthesize(self, text, seed: int | None = None):
|
| 20 |
+
# Tokenize
|
| 21 |
+
inputs = self.tokenizer(text, return_tensors="pt").to(self.device)
|
| 22 |
+
if seed is not None:
|
| 23 |
+
torch.manual_seed(seed)
|
| 24 |
+
np.random.seed(seed)
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
out = self.model(**inputs).waveform
|
| 27 |
+
# Output from model may be a tensor on device
|
| 28 |
+
if isinstance(out, torch.Tensor):
|
| 29 |
+
audio = out.cpu().numpy()
|
| 30 |
+
else:
|
| 31 |
+
audio = np.array(out)
|
| 32 |
+
# Ensure 1D
|
| 33 |
+
audio = audio.squeeze()
|
| 34 |
+
return audio, self.sr
|
| 35 |
+
|
| 36 |
+
def save_wav(self, audio, sr, path):
|
| 37 |
+
sf.write(path, audio, sr)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
# quick smoke test
|
| 42 |
+
tts = FonTTS()
|
| 43 |
+
txt = "e nɔ do fɔngbe ganji"
|
| 44 |
+
audio, sr = tts.synthesize(txt)
|
| 45 |
+
tts.save_wav(audio, sr, "out_fon.wav")
|
| 46 |
+
print("Saved out_fon.wav")
|