minhanh1122 commited on
Commit
f7d6267
·
verified ·
1 Parent(s): 5eb05d4

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +53 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSpeechSeq2Seq
4
+ import soundfile as sf
5
+ import numpy as np
6
+ import tempfile
7
+
8
+
9
+ # Tên model TTS
10
+ MODEL_NAME = "hynt/F5-TTS-Vietnamese-100h"
11
+
12
+ # Load model & tokenizer
13
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
14
+ model = AutoModelForSpeechSeq2Seq.from_pretrained(
15
+ MODEL_NAME,
16
+ torch_dtype=torch.float32, # CPU Basic → dùng float32
17
+ )
18
+
19
+ # Đảm bảo chạy CPU
20
+ device = torch.device("cpu")
21
+ model = model.to(device)
22
+
23
+
24
+ def tts_generate(text):
25
+ if not text.strip():
26
+ return None
27
+
28
+ inputs = tokenizer(text, return_tensors="pt").to(device)
29
+
30
+ # Sinh audio
31
+ with torch.no_grad():
32
+ audio = model.generate(**inputs)
33
+
34
+ audio = audio.cpu().numpy().squeeze()
35
+
36
+ # Lưu tạm wav
37
+ tmp_path = tempfile.mktemp(suffix=".wav")
38
+ sf.write(tmp_path, audio, 22050)
39
+
40
+ return tmp_path
41
+
42
+
43
+ ### Gradio UI ###
44
+ with gr.Blocks(title="TTS Vietnamese Free") as demo:
45
+ gr.Markdown("# 🇻🇳 Text-to-Speech Vietnamese (Free Version)\nNhập văn bản tiếng Việt và nhấn **Sinh giọng nói**:")
46
+
47
+ text_in = gr.Textbox(lines=5, label="Văn bản")
48
+ audio_out = gr.Audio(label="Kết quả", type="filepath")
49
+ btn = gr.Button("🎤 Sinh giọng nói")
50
+
51
+ btn.click(tts_generate, inputs=text_in, outputs=audio_out)
52
+
53
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch
4
+ soundfile
5
+ numpy