Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,51 @@
|
|
| 1 |
-
import
|
| 2 |
-
os.system("pip install --upgrade transformers==4.39.2")
|
| 3 |
-
from transformers import AutoModelForTextToSpeech, AutoProcessor
|
| 4 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
supported_languages.sort()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Giao diện Gradio
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
gr.
|
| 27 |
-
gr.
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
| 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 |
+
# Lấy danh sách các model MMS-TTS
|
| 9 |
+
def get_mms_tts_languages():
|
| 10 |
+
models = list_models(author="facebook", search="mms-tts-")
|
| 11 |
+
lang_map = {}
|
| 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 |
+
LANG_MAP = get_mms_tts_languages()
|
| 24 |
+
LANGUAGE_NAMES = list(LANG_MAP.keys())
|
|
|
|
| 25 |
|
| 26 |
+
# Sinh âm thanh từ văn bản
|
| 27 |
+
@torch.inference_mode()
|
| 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 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 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 |
+
demo.launch()
|