Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +32 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import VitsModel, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
import soundfile as sf
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Tải mô hình và tokenizer
|
| 8 |
+
model_id = "facebook/mms-tts-vie"
|
| 9 |
+
model = VitsModel.from_pretrained(model_id)
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
+
|
| 12 |
+
# Hàm chuyển văn bản thành giọng nói
|
| 13 |
+
def tts(text):
|
| 14 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 15 |
+
with torch.no_grad():
|
| 16 |
+
output = model(**inputs).waveform
|
| 17 |
+
audio = output.squeeze().cpu().numpy().astype("float32")
|
| 18 |
+
|
| 19 |
+
# Tạo thư mục lưu nếu chưa có
|
| 20 |
+
os.makedirs("outputs", exist_ok=True)
|
| 21 |
+
output_path = "outputs/output.wav"
|
| 22 |
+
sf.write(output_path, audio, samplerate=model.config.sampling_rate, format='WAV', subtype='PCM_16')
|
| 23 |
+
return output_path
|
| 24 |
+
|
| 25 |
+
# Giao diện Gradio
|
| 26 |
+
gr.Interface(
|
| 27 |
+
fn=tts,
|
| 28 |
+
inputs=gr.Textbox(label="Nhập văn bản tiếng Việt"),
|
| 29 |
+
outputs=gr.Audio(label="Kết quả âm thanh"),
|
| 30 |
+
title="Vietnamese TTS - Hugging Face",
|
| 31 |
+
description="Ứng dụng chuyển văn bản tiếng Việt thành giọng nói bằng mô hình MMS-TTS"
|
| 32 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
soundfile
|
| 4 |
+
gradio
|