Update app.py
Browse files
app.py
CHANGED
|
@@ -3,43 +3,46 @@ from transformers import pipeline
|
|
| 3 |
from huggingface_hub import list_models
|
| 4 |
import langcodes
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
all_models = list_models(filter="facebook/mms-tts", full=True)
|
| 8 |
lang_map = {}
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
if not code:
|
| 23 |
-
return "Unsupported language", None
|
| 24 |
model_id = f"facebook/mms-tts-{code}"
|
| 25 |
try:
|
| 26 |
tts = pipeline("text-to-speech", model=model_id)
|
| 27 |
out = tts(text)
|
| 28 |
-
return f"Model: {model_id}", (out["audio"], "output.wav")
|
| 29 |
except Exception as e:
|
| 30 |
-
return f"Error: {str(e)}", None
|
| 31 |
|
| 32 |
with gr.Blocks() as demo:
|
| 33 |
-
gr.Markdown("# 🌐 MMS-TTS: 1100+ Languages
|
|
|
|
| 34 |
with gr.Row():
|
| 35 |
-
language = gr.
|
| 36 |
-
choices=sorted(lang_map.keys()),
|
| 37 |
-
label="Choose a language",
|
| 38 |
-
interactive=True
|
| 39 |
-
)
|
| 40 |
text = gr.Textbox(label="Enter your text", placeholder="Type something here...")
|
|
|
|
| 41 |
with gr.Row():
|
| 42 |
btn = gr.Button("Speak")
|
|
|
|
| 43 |
with gr.Row():
|
| 44 |
out_text = gr.Textbox(label="Info")
|
| 45 |
out_audio = gr.Audio(label="Audio", type="filepath")
|
|
|
|
| 3 |
from huggingface_hub import list_models
|
| 4 |
import langcodes
|
| 5 |
|
| 6 |
+
# Tải danh sách mô hình MMS-TTS
|
|
|
|
| 7 |
lang_map = {}
|
| 8 |
|
| 9 |
+
def load_languages():
|
| 10 |
+
global lang_map
|
| 11 |
+
all_models = list_models(filter="facebook/mms-tts", full=True)
|
| 12 |
+
for model in all_models:
|
| 13 |
+
repo = model.modelId # ví dụ: facebook/mms-tts-kaz
|
| 14 |
+
code = repo.split("-")[-1]
|
| 15 |
+
try:
|
| 16 |
+
name = langcodes.Language.make(language=code).display_name().title()
|
| 17 |
+
except:
|
| 18 |
+
name = code.upper()
|
| 19 |
+
lang_map[name.lower()] = code # key là tên thường để dễ so sánh
|
| 20 |
+
|
| 21 |
+
load_languages()
|
| 22 |
+
|
| 23 |
+
def tts_fn(text, language_name):
|
| 24 |
+
lang_key = language_name.strip().lower()
|
| 25 |
+
code = lang_map.get(lang_key)
|
| 26 |
if not code:
|
| 27 |
+
return f"❌ Unsupported language name: '{language_name}'", None
|
| 28 |
model_id = f"facebook/mms-tts-{code}"
|
| 29 |
try:
|
| 30 |
tts = pipeline("text-to-speech", model=model_id)
|
| 31 |
out = tts(text)
|
| 32 |
+
return f"✅ Model: {model_id}", (out["audio"], "output.wav")
|
| 33 |
except Exception as e:
|
| 34 |
+
return f"❌ Error: {str(e)}", None
|
| 35 |
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("# 🌐 MMS-TTS: 1100+ Languages – Type Language Name")
|
| 38 |
+
|
| 39 |
with gr.Row():
|
| 40 |
+
language = gr.Textbox(label="Language name", placeholder="e.g., Kazakh, Bashkir, French...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
text = gr.Textbox(label="Enter your text", placeholder="Type something here...")
|
| 42 |
+
|
| 43 |
with gr.Row():
|
| 44 |
btn = gr.Button("Speak")
|
| 45 |
+
|
| 46 |
with gr.Row():
|
| 47 |
out_text = gr.Textbox(label="Info")
|
| 48 |
out_audio = gr.Audio(label="Audio", type="filepath")
|