vietapk commited on
Commit
a889438
·
verified ·
1 Parent(s): 36df638

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -18
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import spaces
2
  import os
 
3
  from huggingface_hub import login
4
  import gradio as gr
5
  from cached_path import cached_path
@@ -18,23 +19,47 @@ from f5_tts.infer.utils_infer import (
18
  # Retrieve token from secrets
19
  hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
20
 
21
-
22
  # Log in to Hugging Face
23
  if hf_token:
24
  login(token=hf_token)
25
 
26
- def post_process(text):
 
 
 
27
  text = " " + text + " "
28
  text = text.replace(" . . ", " . ")
29
- text = " " + text + " "
30
  text = text.replace(" .. ", " . ")
31
- text = " " + text + " "
32
  text = text.replace(" , , ", " , ")
33
- text = " " + text + " "
34
  text = text.replace(" ,, ", " , ")
35
- text = " " + text + " "
36
  text = text.replace('"', "")
37
- return " ".join(text.split())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  # Load models
40
  vocoder = load_vocoder()
@@ -46,20 +71,26 @@ model = load_model(
46
  )
47
 
48
  @spaces.GPU
49
- def infer_tts(ref_audio_orig: str, gen_text: str, speed: float = 1.0, request: gr.Request = None):
50
-
51
  if not ref_audio_orig:
52
  raise gr.Error("Please upload a sample audio file.")
53
  if not gen_text.strip():
54
  raise gr.Error("Please enter the text content to generate voice.")
55
  if len(gen_text.split()) > 1000:
56
  raise gr.Error("Please enter text content with less than 1000 words.")
57
-
58
  try:
 
59
  ref_audio, ref_text = preprocess_ref_audio_text(ref_audio_orig, "")
60
- final_wave, final_sample_rate, spectrogram = infer_process(
61
- ref_audio, ref_text.lower(), post_process(TTSnorm(gen_text)).lower(), model, vocoder, speed=speed
 
 
62
  )
 
 
 
 
63
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_spectrogram:
64
  spectrogram_path = tmp_spectrogram.name
65
  save_spectrogram(spectrogram, spectrogram_path)
@@ -71,16 +102,17 @@ def infer_tts(ref_audio_orig: str, gen_text: str, speed: float = 1.0, request: g
71
  # Gradio UI
72
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
73
  gr.Markdown("""
74
- # 🎤 F5-TTS: Vietnamese Text-to-Speech Synthesis.
75
- # The model was trained with approximately 1000 hours of data on a RTX 3090 GPU.
76
- Enter text and upload a sample voice to generate natural speech.
77
  """)
78
 
79
  with gr.Row():
80
  ref_audio = gr.Audio(label="🔊 Sample Voice", type="filepath")
81
  gen_text = gr.Textbox(label="📝 Text", placeholder="Enter the text to generate voice...", lines=3)
82
 
83
- speed = gr.Slider(0.3, 2.0, value=1.0, step=0.1, label="⚡ Speed")
 
 
 
84
  btn_synthesize = gr.Button("🔥 Generate Voice")
85
 
86
  with gr.Row():
@@ -97,7 +129,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
97
  interactive=False
98
  )
99
 
100
- btn_synthesize.click(infer_tts, inputs=[ref_audio, gen_text, speed], outputs=[output_audio, output_spectrogram])
 
 
 
 
101
 
102
- # Run Gradio with share=True to get a gradio.live link
103
  demo.queue().launch()
 
1
  import spaces
2
  import os
3
+ import numpy as np
4
  from huggingface_hub import login
5
  import gradio as gr
6
  from cached_path import cached_path
 
19
  # Retrieve token from secrets
20
  hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
21
 
 
22
  # Log in to Hugging Face
23
  if hf_token:
24
  login(token=hf_token)
25
 
26
+ def post_process(text: str):
27
+ """
28
+ Chuẩn hóa text trước khi synthesize.
29
+ """
30
  text = " " + text + " "
31
  text = text.replace(" . . ", " . ")
 
32
  text = text.replace(" .. ", " . ")
 
33
  text = text.replace(" , , ", " , ")
 
34
  text = text.replace(" ,, ", " , ")
 
35
  text = text.replace('"', "")
36
+ return " ".join(text.split()).strip()
37
+
38
+ def synthesize_with_pauses(ref_audio, ref_text, text, model, vocoder, speed=1.0, volume=1.0, pause_duration=1.0):
39
+ """
40
+ Chia text theo dấu chấm, synthesize từng câu và ghép lại với khoảng im lặng.
41
+ """
42
+ processed_text = post_process(TTSnorm(text)).lower()
43
+ sentences = [s.strip() for s in processed_text.split(".") if s.strip()]
44
+
45
+ all_waves = []
46
+ sr = 22050 # sample rate mặc định (cập nhật sau từ infer_process)
47
+
48
+ for idx, sentence in enumerate(sentences):
49
+ wave, sr, _ = infer_process(ref_audio, ref_text.lower(), sentence, model, vocoder, speed=speed)
50
+ wave = np.clip(wave * volume, -1.0, 1.0)
51
+ all_waves.append(wave)
52
+
53
+ # Thêm im lặng giữa các câu (trừ câu cuối)
54
+ if idx < len(sentences) - 1:
55
+ silence = np.zeros(int(sr * pause_duration), dtype=np.float32)
56
+ all_waves.append(silence)
57
+
58
+ if all_waves:
59
+ final_wave = np.concatenate(all_waves)
60
+ else:
61
+ final_wave = np.array([], dtype=np.float32)
62
+ return final_wave, sr
63
 
64
  # Load models
65
  vocoder = load_vocoder()
 
71
  )
72
 
73
  @spaces.GPU
74
+ def infer_tts(ref_audio_orig: str, gen_text: str, speed: float = 1.0, volume: float = 1.0, pause: float = 1.0, request: gr.Request = None):
 
75
  if not ref_audio_orig:
76
  raise gr.Error("Please upload a sample audio file.")
77
  if not gen_text.strip():
78
  raise gr.Error("Please enter the text content to generate voice.")
79
  if len(gen_text.split()) > 1000:
80
  raise gr.Error("Please enter text content with less than 1000 words.")
81
+
82
  try:
83
+ # Tiền xử lý sample voice
84
  ref_audio, ref_text = preprocess_ref_audio_text(ref_audio_orig, "")
85
+
86
+ # Synthesize với ngắt nghỉ
87
+ final_wave, final_sample_rate = synthesize_with_pauses(
88
+ ref_audio, ref_text, gen_text, model, vocoder, speed=speed, volume=volume, pause_duration=pause
89
  )
90
+
91
+ # Tạo spectrogram (dùng đoạn text đầy đủ để hiển thị, nhưng không tái synthesize)
92
+ _, _, spectrogram = infer_process(ref_audio, ref_text.lower(), post_process(TTSnorm(gen_text)).lower(), model, vocoder, speed=speed)
93
+
94
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_spectrogram:
95
  spectrogram_path = tmp_spectrogram.name
96
  save_spectrogram(spectrogram, spectrogram_path)
 
102
  # Gradio UI
103
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
104
  gr.Markdown("""
105
+ # 🎤 Chương trình chuyển đổi text thành giọng nói.
 
 
106
  """)
107
 
108
  with gr.Row():
109
  ref_audio = gr.Audio(label="🔊 Sample Voice", type="filepath")
110
  gen_text = gr.Textbox(label="📝 Text", placeholder="Enter the text to generate voice...", lines=3)
111
 
112
+ speed = gr.Slider(0.3, 2.0, value=0.95, step=0.01, label="⚡ Speed")
113
+ volume = gr.Slider(0.1, 2.0, value=1.0, step=0.01, label="🔊 Volume")
114
+ pause = gr.Slider(0.0, 3.0, value=1.1, step=0.01, label="⏸ Pause between sentences (seconds)")
115
+
116
  btn_synthesize = gr.Button("🔥 Generate Voice")
117
 
118
  with gr.Row():
 
129
  interactive=False
130
  )
131
 
132
+ btn_synthesize.click(
133
+ infer_tts,
134
+ inputs=[ref_audio, gen_text, speed, volume, pause],
135
+ outputs=[output_audio, output_spectrogram]
136
+ )
137
 
138
+ # Run Gradio
139
  demo.queue().launch()