Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForTextToSpeech, AutoProcessor
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Tải mô hình và processor
|
| 6 |
+
model = AutoModelForTextToSpeech.from_pretrained("facebook/mms-tts")
|
| 7 |
+
processor = AutoProcessor.from_pretrained("facebook/mms-tts")
|
| 8 |
+
|
| 9 |
+
# Lấy danh sách tất cả ngôn ngữ hỗ trợ
|
| 10 |
+
supported_languages = list(processor.feature_extractor.sampling_rate_map.keys())
|
| 11 |
+
supported_languages.sort()
|
| 12 |
+
|
| 13 |
+
# Hàm xử lý chuyển văn bản thành giọng nói
|
| 14 |
+
def tts_fn(text, language_code):
|
| 15 |
+
inputs = processor(text=text, language=language_code, return_tensors="pt")
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
output = model(**inputs)
|
| 18 |
+
audio = output.waveform.numpy()[0]
|
| 19 |
+
return (processor.feature_extractor.sampling_rate, audio)
|
| 20 |
+
|
| 21 |
+
# Giao diện Gradio
|
| 22 |
+
iface = gr.Interface(
|
| 23 |
+
fn=tts_fn,
|
| 24 |
+
inputs=[
|
| 25 |
+
gr.Textbox(label="Nhập văn bản"),
|
| 26 |
+
gr.Dropdown(choices=supported_languages, label="Chọn ngôn ngữ"),
|
| 27 |
+
],
|
| 28 |
+
outputs=gr.Audio(label="Kết quả đọc"),
|
| 29 |
+
title="MMS-TTS 1100+ Ngôn ngữ",
|
| 30 |
+
description="Dự án tạo TTS sử dụng mô hình facebook/mms-tts, hỗ trợ hơn 1100 ngôn ngữ khác nhau"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
iface.launch()
|