Spaces:
Sleeping
Sleeping
Commit ·
55b480b
1
Parent(s): 74175bb
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,12 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import json
|
| 5 |
-
from vinorm import TTSnorm
|
| 6 |
-
from phonemizer import phonemize
|
| 7 |
-
|
| 8 |
-
# 1. TẢI CẤU HÌNH
|
| 9 |
-
with open("config.json", "r", encoding="utf-8") as f:
|
| 10 |
-
config = json.load(f)
|
| 11 |
-
phoneme_id_map = config["phoneme_id_map"]
|
| 12 |
-
|
| 13 |
-
# 2. KHỞI TẠO MODEL
|
| 14 |
-
# Dùng tên file chính xác của bạn trên Hugging Face
|
| 15 |
-
sess = ort.InferenceSession("model.onnx", providers=['CPUExecutionProvider'])
|
| 16 |
-
|
| 17 |
-
def text_to_ids(text):
|
| 18 |
-
# Chuẩn hóa văn bản tiếng Việt
|
| 19 |
-
text = TTSnorm(text)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
# Thêm ký tự bắt đầu (^) thường có ID là 1 trong Piper
|
| 27 |
-
ids.extend(phoneme_id_map.get("^", [1]))
|
| 28 |
-
|
| 29 |
-
for p in phonemes:
|
| 30 |
-
if p in phoneme_id_map:
|
| 31 |
-
ids.extend(phoneme_id_map[p])
|
| 32 |
-
# Thêm ký tự trống (_) giữa các âm nếu model yêu cầu
|
| 33 |
-
ids.extend(phoneme_id_map.get("_", [0]))
|
| 34 |
-
|
| 35 |
-
# Thêm ký tự kết thúc ($) thường có ID là 2
|
| 36 |
-
ids.extend(phoneme_id_map.get("$", [2]))
|
| 37 |
-
return ids
|
| 38 |
-
|
| 39 |
-
def tts_predict(text, speed, noise, noise_w):
|
| 40 |
-
try:
|
| 41 |
-
ids = text_to_ids(text)
|
| 42 |
-
input_ids = np.array([ids], dtype=np.int64)
|
| 43 |
-
input_lens = np.array([len(ids)], dtype=np.int64)
|
| 44 |
-
|
| 45 |
-
# Scales: [noise, noise_w, length_scale] dựa trên ảnh Netron
|
| 46 |
-
scales = np.array([noise, noise_w, 1.0/speed], dtype=np.float32)
|
| 47 |
-
|
| 48 |
-
# Chạy model
|
| 49 |
-
outputs = sess.run(None, {
|
| 50 |
-
"input": input_ids,
|
| 51 |
-
"input_lengths": input_lens,
|
| 52 |
-
"scales": scales
|
| 53 |
-
})
|
| 54 |
-
|
| 55 |
-
# Làm phẳng dữ liệu 4D từ model thành 1D âm thanh
|
| 56 |
-
audio = outputs[0].flatten().astype(np.float32)
|
| 57 |
-
|
| 58 |
-
# Trả về kết quả với Sample Rate 22050 từ config của bạn
|
| 59 |
-
return "Tạo âm thanh thành công!", (22050, audio)
|
| 60 |
-
except Exception as e:
|
| 61 |
-
return f"Lỗi: {str(e)}", None
|
| 62 |
-
|
| 63 |
-
# GIAO DIỆN
|
| 64 |
-
demo = gr.Interface(
|
| 65 |
-
fn=tts_predict,
|
| 66 |
-
inputs=[
|
| 67 |
-
gr.Textbox(label="Nhập tiếng Việt", value="Xin chào bạn"),
|
| 68 |
-
gr.Slider(0.5, 2.0, 1.0, label="Tốc độ"),
|
| 69 |
-
gr.Slider(0.1, 1.0, 0.667, label="Noise Scale"),
|
| 70 |
-
gr.Slider(0.1, 1.0, 0.8, label="Noise W")
|
| 71 |
-
],
|
| 72 |
-
outputs=[gr.Textbox(label="Trạng thái"), gr.Audio(label="Audio")],
|
| 73 |
-
title="Vietnamese Piper TTS Fixed"
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
if __name__ == "__main__":
|
| 77 |
-
demo.launch()
|
|
|
|
| 1 |
+
# Thay đổi phần giao diện trong app.py
|
| 2 |
+
with gr.Blocks() as demo:
|
| 3 |
+
gr.Markdown("# 📂 Chuyển đổi SRT sang Audio")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Thành phần cho phép chọn file từ bộ nhớ máy/SD Card
|
| 6 |
+
file_input = gr.File(
|
| 7 |
+
label="Nhấn để chọn file .srt từ SD Card",
|
| 8 |
+
file_types=[".srt"],
|
| 9 |
+
type="filepath" # Quan trọng: Để lấy đường dẫn tệp tạm thời
|
| 10 |
+
)
|
| 11 |
|
| 12 |
+
# ... (Các slider tốc độ 1.30 và nút bấm như cũ)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|