Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,7 @@ import requests
|
|
| 12 |
import base64
|
| 13 |
import wave
|
| 14 |
|
|
|
|
| 15 |
VOICES_FILE = Path(__file__).parent / "voices.json"
|
| 16 |
with open(VOICES_FILE, encoding="utf-8") as f:
|
| 17 |
voices_data = json.load(f)
|
|
@@ -19,10 +20,10 @@ with open(VOICES_FILE, encoding="utf-8") as f:
|
|
| 19 |
languages = sorted(voices_data.keys())
|
| 20 |
def get_genders(lang):
|
| 21 |
return sorted(voices_data.get(lang, {}).keys())
|
|
|
|
| 22 |
def get_edge_voices(lang, gender):
|
| 23 |
return [v["display_name"] for v in voices_data.get(lang, {}).get(gender, [])]
|
| 24 |
|
| 25 |
-
# Map Edge display_name → voice_code
|
| 26 |
edge_voice_map = {
|
| 27 |
v["display_name"]: v["voice_code"]
|
| 28 |
for block in voices_data.values()
|
|
@@ -30,12 +31,16 @@ edge_voice_map = {
|
|
| 30 |
for v in gender_block
|
| 31 |
}
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
]
|
| 37 |
def get_gemini_voices():
|
| 38 |
-
return
|
| 39 |
|
| 40 |
MAX_CHARS = 5000 # Edge-TTS giới hạn ~5000 ký tự
|
| 41 |
|
|
@@ -120,7 +125,6 @@ def run_gemini_tts(text, model, api_key, voice_name, out_path):
|
|
| 120 |
resp.raise_for_status()
|
| 121 |
b64 = resp.json()["candidates"][0]["content"]["parts"][0]["inlineData"]["data"]
|
| 122 |
pcm_data = base64.b64decode(b64)
|
| 123 |
-
# Lưu thành WAV rồi chuyển sang MP3
|
| 124 |
wav_path = out_path.replace('.mp3', '.wav')
|
| 125 |
with wave.open(wav_path, "wb") as wf:
|
| 126 |
wf.setnchannels(1)
|
|
@@ -145,7 +149,7 @@ def build_srt(segments, durations, srt_path):
|
|
| 145 |
f.write(f"{i}\n{start} --> {end}\n{seg.strip()}\n\n")
|
| 146 |
cum += dur
|
| 147 |
|
| 148 |
-
def generate_tts(text, engine, lang, gender, voice_name, rate, pitch, api_key, gemini_model):
|
| 149 |
if not text.strip():
|
| 150 |
return None, None
|
| 151 |
segments = split_text(text)
|
|
@@ -160,7 +164,7 @@ def generate_tts(text, engine, lang, gender, voice_name, rate, pitch, api_key, g
|
|
| 160 |
elif engine == "Google API":
|
| 161 |
if not api_key:
|
| 162 |
raise Exception("Bạn phải nhập API key Google Gemini TTS!")
|
| 163 |
-
run_gemini_tts(seg, gemini_model, api_key,
|
| 164 |
seg_files.append(seg_path)
|
| 165 |
durations.append(get_audio_duration(seg_path))
|
| 166 |
out_audio = "LyTranTTS.mp3"
|
|
@@ -169,7 +173,7 @@ def generate_tts(text, engine, lang, gender, voice_name, rate, pitch, api_key, g
|
|
| 169 |
build_srt(segments, durations, srt_file)
|
| 170 |
return out_audio, srt_file
|
| 171 |
|
| 172 |
-
def preview_voice(engine, lang, gender, voice_name, rate, pitch, api_key, gemini_model):
|
| 173 |
text = "Xin chào! Đây là ứng dụng chuyển văn bản thành giọng, nói được phát triển bởi Lý Trần." if "Việt" in lang else "Hello, This is a Text to Speech, Speech App developed by Ly Tran"
|
| 174 |
out = "LyTranTTS.mp3"
|
| 175 |
if engine == "Edge":
|
|
@@ -180,14 +184,18 @@ def preview_voice(engine, lang, gender, voice_name, rate, pitch, api_key, gemini
|
|
| 180 |
elif engine == "Google API":
|
| 181 |
if not api_key:
|
| 182 |
raise Exception("Bạn phải nhập API key Google Gemini TTS!")
|
| 183 |
-
run_gemini_tts(text, gemini_model, api_key,
|
| 184 |
return out
|
| 185 |
|
| 186 |
def on_engine_change(engine):
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
def on_language_change(lang):
|
| 193 |
genders = get_genders(lang)
|
|
@@ -207,7 +215,83 @@ def on_gender_change(lang, gender):
|
|
| 207 |
DEFAULT_LANG = "Đa Ngôn Ngữ"
|
| 208 |
DEFAULT_MODEL = "gemini-2.5-flash-preview-tts"
|
| 209 |
|
| 210 |
-
custom_css = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
with gr.Blocks(title="LyTranTTS - Phiên bản miễn phí", css=custom_css) as demo:
|
| 213 |
with gr.Row():
|
|
@@ -228,7 +312,7 @@ with gr.Blocks(title="LyTranTTS - Phiên bản miễn phí", css=custom_css) as
|
|
| 228 |
["Edge", "Google", "Google API"],
|
| 229 |
label="Chọn Engine TTS",
|
| 230 |
value="Edge",
|
| 231 |
-
info="Edge TTS hỗ trợ nhiều giọng đọc hơn, Google TTS đơn giản và nhanh, Google API dùng API key"
|
| 232 |
)
|
| 233 |
text_input = gr.Textbox(
|
| 234 |
label="Nhập văn bản cần chuyển đổi",
|
|
@@ -330,12 +414,12 @@ with gr.Blocks(title="LyTranTTS - Phiên bản miễn phí", css=custom_css) as
|
|
| 330 |
# Generate
|
| 331 |
gen_btn.click(
|
| 332 |
fn=generate_tts,
|
| 333 |
-
inputs=[text_input, engine, lang_dd, gender_dd, voice_dd, rate_sl, pitch_sl, api_key_input, gemini_model_input],
|
| 334 |
outputs=[out_audio, out_srt]
|
| 335 |
)
|
| 336 |
preview_btn.click(
|
| 337 |
fn=preview_voice,
|
| 338 |
-
inputs=[engine, lang_dd, gender_dd, voice_dd, rate_sl, pitch_sl, api_key_input, gemini_model_input],
|
| 339 |
outputs=[prev_audio]
|
| 340 |
)
|
| 341 |
|
|
|
|
| 12 |
import base64
|
| 13 |
import wave
|
| 14 |
|
| 15 |
+
# EDGE voices
|
| 16 |
VOICES_FILE = Path(__file__).parent / "voices.json"
|
| 17 |
with open(VOICES_FILE, encoding="utf-8") as f:
|
| 18 |
voices_data = json.load(f)
|
|
|
|
| 20 |
languages = sorted(voices_data.keys())
|
| 21 |
def get_genders(lang):
|
| 22 |
return sorted(voices_data.get(lang, {}).keys())
|
| 23 |
+
|
| 24 |
def get_edge_voices(lang, gender):
|
| 25 |
return [v["display_name"] for v in voices_data.get(lang, {}).get(gender, [])]
|
| 26 |
|
|
|
|
| 27 |
edge_voice_map = {
|
| 28 |
v["display_name"]: v["voice_code"]
|
| 29 |
for block in voices_data.values()
|
|
|
|
| 31 |
for v in gender_block
|
| 32 |
}
|
| 33 |
|
| 34 |
+
# Gemini voices (hardcoded)
|
| 35 |
+
GEMINI_VOICES = [
|
| 36 |
+
"Zephyr", "Puck", "Charon", "Kore", "Fenrir", "Leda", "Orus", "Aoede",
|
| 37 |
+
"Callirrhoe", "Autonoe", "Enceladus", "Iapetus", "Umbriel", "Algieba",
|
| 38 |
+
"Despina", "Erinome", "Algenib", "Rasalgethi", "Laomedeia", "Achernar",
|
| 39 |
+
"Alnilam", "Schedar", "Gacrux", "Pulcherrima", "Achird",
|
| 40 |
+
"Zubenelgenubi", "Vindemiatrix", "Sadachbia", "Sadaltager", "Sulafat"
|
| 41 |
]
|
| 42 |
def get_gemini_voices():
|
| 43 |
+
return GEMINI_VOICES
|
| 44 |
|
| 45 |
MAX_CHARS = 5000 # Edge-TTS giới hạn ~5000 ký tự
|
| 46 |
|
|
|
|
| 125 |
resp.raise_for_status()
|
| 126 |
b64 = resp.json()["candidates"][0]["content"]["parts"][0]["inlineData"]["data"]
|
| 127 |
pcm_data = base64.b64decode(b64)
|
|
|
|
| 128 |
wav_path = out_path.replace('.mp3', '.wav')
|
| 129 |
with wave.open(wav_path, "wb") as wf:
|
| 130 |
wf.setnchannels(1)
|
|
|
|
| 149 |
f.write(f"{i}\n{start} --> {end}\n{seg.strip()}\n\n")
|
| 150 |
cum += dur
|
| 151 |
|
| 152 |
+
def generate_tts(text, engine, lang, gender, voice_name, rate, pitch, api_key, gemini_model, gemini_voice):
|
| 153 |
if not text.strip():
|
| 154 |
return None, None
|
| 155 |
segments = split_text(text)
|
|
|
|
| 164 |
elif engine == "Google API":
|
| 165 |
if not api_key:
|
| 166 |
raise Exception("Bạn phải nhập API key Google Gemini TTS!")
|
| 167 |
+
run_gemini_tts(seg, gemini_model, api_key, gemini_voice, seg_path)
|
| 168 |
seg_files.append(seg_path)
|
| 169 |
durations.append(get_audio_duration(seg_path))
|
| 170 |
out_audio = "LyTranTTS.mp3"
|
|
|
|
| 173 |
build_srt(segments, durations, srt_file)
|
| 174 |
return out_audio, srt_file
|
| 175 |
|
| 176 |
+
def preview_voice(engine, lang, gender, voice_name, rate, pitch, api_key, gemini_model, gemini_voice):
|
| 177 |
text = "Xin chào! Đây là ứng dụng chuyển văn bản thành giọng, nói được phát triển bởi Lý Trần." if "Việt" in lang else "Hello, This is a Text to Speech, Speech App developed by Ly Tran"
|
| 178 |
out = "LyTranTTS.mp3"
|
| 179 |
if engine == "Edge":
|
|
|
|
| 184 |
elif engine == "Google API":
|
| 185 |
if not api_key:
|
| 186 |
raise Exception("Bạn phải nhập API key Google Gemini TTS!")
|
| 187 |
+
run_gemini_tts(text, gemini_model, api_key, gemini_voice, out)
|
| 188 |
return out
|
| 189 |
|
| 190 |
def on_engine_change(engine):
|
| 191 |
+
# Show/hide Gemini fields when engine changes
|
| 192 |
+
show_gemini = engine == "Google API"
|
| 193 |
+
return (
|
| 194 |
+
gr.update(visible=show_gemini), # api_key_input
|
| 195 |
+
gr.update(visible=show_gemini), # gemini_model_input
|
| 196 |
+
gr.update(visible=not show_gemini), # voice_dd (Edge voice)
|
| 197 |
+
gr.update(visible=show_gemini), # gemini_voice_dd
|
| 198 |
+
)
|
| 199 |
|
| 200 |
def on_language_change(lang):
|
| 201 |
genders = get_genders(lang)
|
|
|
|
| 215 |
DEFAULT_LANG = "Đa Ngôn Ngữ"
|
| 216 |
DEFAULT_MODEL = "gemini-2.5-flash-preview-tts"
|
| 217 |
|
| 218 |
+
custom_css = """
|
| 219 |
+
:root {
|
| 220 |
+
--primary: #4f46e5;
|
| 221 |
+
--secondary: #f9fafb;
|
| 222 |
+
--accent: #10b981;
|
| 223 |
+
--text: #1f2937;
|
| 224 |
+
--border: #e5e7eb;
|
| 225 |
+
}
|
| 226 |
+
body {
|
| 227 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 228 |
+
}
|
| 229 |
+
.gradio-container {
|
| 230 |
+
max-width: 900px !important;
|
| 231 |
+
margin: 0 auto;
|
| 232 |
+
background-color: white;
|
| 233 |
+
border-radius: 12px;
|
| 234 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
| 235 |
+
}
|
| 236 |
+
h1 {
|
| 237 |
+
color: var(--primary) !important;
|
| 238 |
+
font-weight: 700 !important;
|
| 239 |
+
margin-bottom: 0.5rem !important;
|
| 240 |
+
}
|
| 241 |
+
.description {
|
| 242 |
+
color: var(--text) !important;
|
| 243 |
+
margin-bottom: 1.5rem !important;
|
| 244 |
+
font-size: 1rem !important;
|
| 245 |
+
}
|
| 246 |
+
.input-section, .output-section {
|
| 247 |
+
background-color: var(--secondary);
|
| 248 |
+
padding: 1.5rem;
|
| 249 |
+
border-radius: 8px;
|
| 250 |
+
margin-bottom: 1.5rem;
|
| 251 |
+
border: 1px solid var(--border);
|
| 252 |
+
}
|
| 253 |
+
.input-section label, .output-section label {
|
| 254 |
+
font-weight: 600 !important;
|
| 255 |
+
color: var(--text) !important;
|
| 256 |
+
}
|
| 257 |
+
.tab {
|
| 258 |
+
background-color: white !important;
|
| 259 |
+
border-radius: 8px !important;
|
| 260 |
+
padding: 1rem !important;
|
| 261 |
+
}
|
| 262 |
+
button {
|
| 263 |
+
background-color: var(--primary) !important;
|
| 264 |
+
color: white !important;
|
| 265 |
+
border: none !important;
|
| 266 |
+
border-radius: 6px !important;
|
| 267 |
+
padding: 0.5rem 1rem !important;
|
| 268 |
+
font-weight: 500 !important;
|
| 269 |
+
transition: all 0.2s !important;
|
| 270 |
+
}
|
| 271 |
+
button:hover {
|
| 272 |
+
background-color: #4338ca !important;
|
| 273 |
+
transform: translateY(-1px) !important;
|
| 274 |
+
}
|
| 275 |
+
.preview-btn {
|
| 276 |
+
background-color: var(--accent) !important;
|
| 277 |
+
}
|
| 278 |
+
.preview-btn:hover {
|
| 279 |
+
background-color: #0d9b6e !important;
|
| 280 |
+
}
|
| 281 |
+
.slider-container {
|
| 282 |
+
margin-top: 1rem;
|
| 283 |
+
}
|
| 284 |
+
.slider-container label {
|
| 285 |
+
margin-bottom: 0.5rem;
|
| 286 |
+
display: block;
|
| 287 |
+
}
|
| 288 |
+
footer {
|
| 289 |
+
text-align: center;
|
| 290 |
+
margin-top: 2rem;
|
| 291 |
+
color: #6b7280;
|
| 292 |
+
font-size: 0.875rem;
|
| 293 |
+
}
|
| 294 |
+
"""
|
| 295 |
|
| 296 |
with gr.Blocks(title="LyTranTTS - Phiên bản miễn phí", css=custom_css) as demo:
|
| 297 |
with gr.Row():
|
|
|
|
| 312 |
["Edge", "Google", "Google API"],
|
| 313 |
label="Chọn Engine TTS",
|
| 314 |
value="Edge",
|
| 315 |
+
info="Edge TTS hỗ trợ nhiều giọng đọc hơn, Google TTS đơn giản và nhanh, Google API dùng API key Gemini"
|
| 316 |
)
|
| 317 |
text_input = gr.Textbox(
|
| 318 |
label="Nhập văn bản cần chuyển đổi",
|
|
|
|
| 414 |
# Generate
|
| 415 |
gen_btn.click(
|
| 416 |
fn=generate_tts,
|
| 417 |
+
inputs=[text_input, engine, lang_dd, gender_dd, voice_dd, rate_sl, pitch_sl, api_key_input, gemini_model_input, gemini_voice_dd],
|
| 418 |
outputs=[out_audio, out_srt]
|
| 419 |
)
|
| 420 |
preview_btn.click(
|
| 421 |
fn=preview_voice,
|
| 422 |
+
inputs=[engine, lang_dd, gender_dd, voice_dd, rate_sl, pitch_sl, api_key_input, gemini_model_input, gemini_voice_dd],
|
| 423 |
outputs=[prev_audio]
|
| 424 |
)
|
| 425 |
|