Update app.py
Browse files
app.py
CHANGED
|
@@ -2,64 +2,42 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from huggingface_hub import list_models
|
| 4 |
import langcodes
|
| 5 |
-
import difflib
|
| 6 |
|
| 7 |
-
#
|
|
|
|
| 8 |
lang_map = {}
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def tts_fn(text, language):
|
| 29 |
-
|
| 30 |
-
code = lang_map.get(key)
|
| 31 |
-
|
| 32 |
if not code:
|
| 33 |
-
|
| 34 |
-
suggestions = difflib.get_close_matches(key, lang_map.keys(), n=5)
|
| 35 |
-
msg = f"❌ Unsupported language name: '{language}'"
|
| 36 |
-
if suggestions:
|
| 37 |
-
msg += f"\nDid you mean: {', '.join(suggestions)}?"
|
| 38 |
-
return msg, None
|
| 39 |
-
|
| 40 |
model_id = f"facebook/mms-tts-{code}"
|
| 41 |
-
|
| 42 |
try:
|
| 43 |
tts = pipeline("text-to-speech", model=model_id)
|
| 44 |
out = tts(text)
|
| 45 |
-
return f"✅
|
| 46 |
-
except Exception as e:
|
| 47 |
-
return f"❌ Pipeline error: {str(e)}", None
|
| 48 |
-
|
| 49 |
-
with gr.Blocks() as demo:
|
| 50 |
-
gr.Markdown("## 🌍 MMS-TTS (1100+ Languages)\nJust type the language name, no ISO needed!")
|
| 51 |
-
|
| 52 |
-
with gr.Row():
|
| 53 |
-
lang_input = gr.Textbox(label="Language name (e.g. English, Meadow Mari)")
|
| 54 |
-
text_input = gr.Textbox(label="Text")
|
| 55 |
-
|
| 56 |
-
with gr.Row():
|
| 57 |
-
speak_button = gr.Button("🔊 Speak")
|
| 58 |
-
|
| 59 |
-
with gr.Row():
|
| 60 |
-
result_text = gr.Textbox(label="Info")
|
| 61 |
-
result_audio = gr.Audio(label="Audio", type="filepath")
|
| 62 |
-
|
| 63 |
-
speak_button.click(fn=tts_fn, inputs=[text_input, lang_input], outputs=[result_text, result_audio])
|
| 64 |
-
|
| 65 |
-
demo.launch()
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from huggingface_hub import list_models
|
| 4 |
import langcodes
|
|
|
|
| 5 |
|
| 6 |
+
# Tự động lấy danh sách mô hình MMS-TTS
|
| 7 |
+
all_models = list_models(filter="facebook/mms-tts", full=True)
|
| 8 |
lang_map = {}
|
| 9 |
|
| 10 |
+
manual_map = {
|
| 11 |
+
"mhr": "Meadow Mari",
|
| 12 |
+
"mrj": "Hill Mari",
|
| 13 |
+
"cmn": "Mandarin Chinese",
|
| 14 |
+
"arb": "Arabic",
|
| 15 |
+
# thêm nếu cần
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
for model in all_models:
|
| 19 |
+
repo = model.modelId # ví dụ: facebook/mms-tts-kaz
|
| 20 |
+
code = repo.split("-")[-1]
|
| 21 |
+
try:
|
| 22 |
+
name = langcodes.Language.make(language=code).display_name()
|
| 23 |
+
except:
|
| 24 |
+
name = code
|
| 25 |
+
# Nếu langcodes không giải mã đúng, dùng thủ công
|
| 26 |
+
if name.lower() == code.lower() and code in manual_map:
|
| 27 |
+
name = manual_map[code]
|
| 28 |
+
lang_map[name.lower()] = code # dùng tên thường hết
|
| 29 |
+
|
| 30 |
+
# In ra để kiểm tra
|
| 31 |
+
print("🧠 Available language names:")
|
| 32 |
+
for lang in sorted(lang_map.keys()):
|
| 33 |
+
print("-", lang)
|
| 34 |
|
| 35 |
def tts_fn(text, language):
|
| 36 |
+
code = lang_map.get(language.lower())
|
|
|
|
|
|
|
| 37 |
if not code:
|
| 38 |
+
return f"❌ Unsupported language name: '{language}'", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
model_id = f"facebook/mms-tts-{code}"
|
|
|
|
| 40 |
try:
|
| 41 |
tts = pipeline("text-to-speech", model=model_id)
|
| 42 |
out = tts(text)
|
| 43 |
+
return f"✅ Model: {model_id}", (out["audio"], "output.wav
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|