Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,17 @@
|
|
| 1 |
-
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import AutoProcessor, AutoModelForTextToSpeech, pipeline
|
| 4 |
-
from huggingface_hub import list_models
|
| 5 |
-
from iso639 import Lang
|
| 6 |
-
import re
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
for model in models:
|
| 13 |
-
match = re.search(r"mms-tts-([a-z0-9]{3})$", model.modelId)
|
| 14 |
-
if match:
|
| 15 |
-
iso = match.group(1)
|
| 16 |
-
try:
|
| 17 |
-
lang = Lang(iso)
|
| 18 |
-
lang_map[lang.name.title()] = iso
|
| 19 |
-
except:
|
| 20 |
-
lang_map[iso.upper()] = iso
|
| 21 |
-
return dict(sorted(lang_map.items()))
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def generate_tts(text, language_name):
|
| 29 |
-
iso_code = LANG_MAP[language_name]
|
| 30 |
-
model_id = f"facebook/mms-tts-{iso_code}"
|
| 31 |
|
| 32 |
-
|
| 33 |
-
model = AutoModelForTextToSpeech.from_pretrained(model_id).to("cpu")
|
| 34 |
-
|
| 35 |
-
tts_pipeline = pipeline("text-to-speech", model=model, tokenizer=processor, feature_extractor=processor)
|
| 36 |
-
output = tts_pipeline(text)
|
| 37 |
-
return output["audio"]
|
| 38 |
-
|
| 39 |
-
# Giao diện Gradio
|
| 40 |
-
with gr.Blocks() as demo:
|
| 41 |
-
gr.Markdown("## 🌍 MMS-TTS: 1100+ Ngôn ngữ TTS từ Meta")
|
| 42 |
-
with gr.Row():
|
| 43 |
-
language = gr.Dropdown(label="Chọn ngôn ngữ", choices=LANGUAGE_NAMES, value="Vietnamese")
|
| 44 |
-
text = gr.Textbox(label="Nhập văn bản", placeholder="Xin chào thế giới")
|
| 45 |
-
output = gr.Audio(label="Giọng nói")
|
| 46 |
-
|
| 47 |
-
btn = gr.Button("Phát")
|
| 48 |
-
btn.click(generate_tts, inputs=[text, language], outputs=output)
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
-
|
|
|
|
| 1 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
def main():
|
| 4 |
+
# Dùng model Facebook MMS TTS đa ngôn ngữ
|
| 5 |
+
model_name = "facebook/mms-tts-1b"
|
| 6 |
+
tts = pipeline("text-to-speech", model=model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
text = "Hello world, this is a multi-language TTS test."
|
| 9 |
+
audio = tts(text)
|
| 10 |
|
| 11 |
+
with open("output.wav", "wb") as f:
|
| 12 |
+
f.write(audio["wav"])
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
print("TTS thành công, file output.wav đã lưu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
if __name__ == "__main__":
|
| 17 |
+
main()
|