Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoTokenizer, VitsModel
|
3 |
+
import gradio as gr
|
4 |
+
import soundfile as sf
|
5 |
+
import tempfile
|
6 |
+
|
7 |
+
# Load Meta's MMS-TTS model for Min Nan (zh-nan)
|
8 |
+
model = VitsModel.from_pretrained("facebook/mms-tts-nan")
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-nan")
|
10 |
+
|
11 |
+
# Synthesize speech from Min Nan (POJ) text input
|
12 |
+
def synthesize(text):
|
13 |
+
inputs = tokenizer(text, return_tensors="pt")
|
14 |
+
with torch.no_grad():
|
15 |
+
output = model(**inputs)
|
16 |
+
audio = output.waveform.squeeze().cpu().numpy()
|
17 |
+
|
18 |
+
# Use model-defined sampling rate for writing audio
|
19 |
+
sampling_rate = model.config.sampling_rate
|
20 |
+
tmp_wav = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
|
21 |
+
sf.write(tmp_wav.name, audio, samplerate=sampling_rate)
|
22 |
+
return tmp_wav.name
|
23 |
+
|
24 |
+
# Gradio app interface
|
25 |
+
demo = gr.Interface(
|
26 |
+
fn=synthesize,
|
27 |
+
inputs=gr.Textbox(
|
28 |
+
lines=3,
|
29 |
+
placeholder="Enter Min Nan (Taiwanese Hokkien) text in POJ format.\nExample: Lí hó! Góa sī lâng Tâi-oân.",
|
30 |
+
label="Min Nan Text (POJ format)"
|
31 |
+
),
|
32 |
+
outputs=gr.Audio(type="filepath", label="Synthesized Speech"),
|
33 |
+
title="Text-to-Speech (TTS) for Min Nan / Taiwanese Hokkien using Meta’s MMS-TTS Model (facebook/mms-tts-nan)",
|
34 |
+
description=(
|
35 |
+
"🗣️ This application uses Meta's multilingual speech model (MMS-TTS) to generate natural speech "
|
36 |
+
"from text written in Min Nan Chinese (zh-nan), also known as Taiwanese Hokkien. "
|
37 |
+
"Input text should be written using the Pe̍h-ōe-jī (POJ) romanization system. \n\n"
|
38 |
+
"Example input: Lí hó! Góa sī lâng Tâi-oân.\n\n"
|
39 |
+
"The output is a 16kHz WAV audio file synthesized using the VITS-based neural TTS model."
|
40 |
+
),
|
41 |
+
allow_flagging="never"
|
42 |
+
)
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
demo.launch()
|