Spaces:
Sleeping
Sleeping
File size: 16,202 Bytes
bda4ba4 e50d013 c0cc079 5e75590 b816a52 bda4ba4 ef4db28 1ce989a ef4db28 4de4aab ef4db28 6c4b49c ef4db28 6c4b49c ef4db28 1561c93 ef4db28 e50d013 ef4db28 b816a52 ef4db28 885153e 5ac76d6 885153e 5ac76d6 ef4db28 5ac76d6 885153e ef4db28 8829e6c ef4db28 8829e6c ef4db28 8829e6c ef4db28 72772b4 ef4db28 72772b4 ef4db28 1ce989a ef4db28 6c4b49c ef4db28 6c4b49c ef4db28 6c4b49c ef4db28 1ce989a ef4db28 9b4a9ad ef4db28 1ce989a ef4db28 1ce989a ef4db28 e1c7ef8 ef4db28 e1c7ef8 ef4db28 0a0cb99 ef4db28 e1c7ef8 ef4db28 d293a11 ef4db28 6c4b49c ef4db28 ba9d64e ef4db28 ba9d64e ef4db28 8829e6c ef4db28 885153e ef4db28 e50d013 ef4db28 e50d013 ef4db28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
import gradio as gr
import torch
import tempfile
import os
import numpy as np
import soundfile as sf
# Import our model factory
from src.models.factory import ModelFactory
# Patch torch.load to always use CPU
original_torch_load = torch.load
def patched_torch_load(f, map_location=None, **kwargs):
if map_location is None:
map_location = 'cpu'
return original_torch_load(f, map_location=map_location, **kwargs)
torch.load = patched_torch_load
# Get model descriptions
MODEL_DESCRIPTIONS = ModelFactory.get_model_descriptions()
# Models dictionary for UI display
MODELS = {
"ResembleAI/chatterbox": "Chatterbox",
"KittenML/KittenTTS": "KittenTTS",
"piper-tts": "Piper (no voice cloning)",
"SYSTRAN/faster-whisper": "Faster Whisper",
"hexgrad/kokoro": "Kokoro-82M",
"nari-labs/Dia-1.6B": "Dia TTS",
}
# Initialize model instances
tts_models = ModelFactory.get_tts_models()
stt_models = ModelFactory.get_stt_models()
# Initialize the models that need immediate initialization
for model_name in ["ResembleAI/chatterbox", "KittenML/KittenTTS"]:
if model_name in tts_models:
tts_models[model_name].initialize()
# Initialize the STT model
whisper_model = stt_models.get("SYSTRAN/faster-whisper")
if whisper_model:
whisper_model.initialize()
# Helper function to get Kokoro voices
def get_kokoro_voices(language_code):
"""
Get available voices for a specific Kokoro language code
Based on: https://huggingface.co/hexgrad/Kokoro-82M/blob/main/VOICES.md
"""
voice_map = {
# American English (a)
"a": [
"af_heart", "af_alloy", "af_aoede", "af_bella", "af_jessica",
"af_kore", "af_nicole", "af_nova", "af_river", "af_sarah",
"af_sky", "am_adam", "am_echo", "am_eric", "am_fenrir",
"am_liam", "am_michael", "am_onyx", "am_puck", "am_santa"
],
# British English (b)
"b": [
"bf_alice", "bf_emma", "bf_isabella", "bf_lily",
"bm_daniel", "bm_fable", "bm_george", "bm_lewis"
],
# Spanish (e)
"e": ["ef_dora", "em_alex", "em_santa"],
# French (f)
"f": ["ff_siwis"],
# Hindi (h)
"h": ["hf_alpha", "hf_beta", "hm_omega", "hm_psi"],
# Italian (i)
"i": ["if_sara", "im_nicola"],
# Japanese (j)
"j": ["jf_alpha", "jf_gongitsune", "jf_nezumi", "jf_tebukuro", "jm_kumo"],
# Brazilian Portuguese (p)
"p": ["pt_heart", "pt_sun", "pt_moon", "pt_star", "pt_cloud"],
# Mandarin Chinese (z)
"z": [
"zf_xiaobei", "zf_xiaoni", "zf_xiaoxiao", "zf_xiaoyi",
"zm_yunjian", "zm_yunxi", "zm_yunxia", "zm_yunyang"
]
}
return voice_map.get(language_code, ["af_heart"]) # Default to American English voices
# UI Functions for TTS Models
def tts_chatterbox(text, language, audio_prompt=None):
"""UI function for Chatterbox TTS"""
model = tts_models.get("ResembleAI/chatterbox")
if not model:
return None, "Model not available"
try:
audio_path = model.generate_speech(text, language=language, audio_prompt=audio_prompt)
return audio_path, ""
except Exception as e:
return None, f"Error: {str(e)}"
def tts_kittentts(text, audio_prompt=None):
"""UI function for KittenTTS"""
model = tts_models.get("KittenML/KittenTTS")
if not model:
return None, "Model not available"
try:
audio_path = model.generate_speech(text, audio_prompt=audio_prompt)
return audio_path, ""
except Exception as e:
return None, f"Error: {str(e)}"
def tts_piper(text, language, voice):
"""UI function for Piper TTS"""
model = tts_models.get("piper-tts")
if not model:
return None, "Model not available"
try:
model.initialize() # Ensure voices are scanned
audio_path = model.generate_speech(text, language=language, voice=voice)
return audio_path, ""
except Exception as e:
return None, f"Error: {str(e)}"
def tts_kokoro(text, language_code, voice_name):
"""UI function for Kokoro TTS"""
model = tts_models.get("hexgrad/kokoro")
if not model:
return None, "Model not available"
try:
# Initialize the model if not already initialized
if not model._initialized:
model.initialize()
# Generate speech (voice_name is kept for interface consistency but not used by Kokoro)
audio_path = model.generate_speech(text, lang_code=language_code, voice_name=voice_name)
# Check if audio file was actually created
if audio_path and os.path.exists(audio_path):
return audio_path, ""
else:
return None, "Error: Audio file was not generated"
except Exception as e:
return None, f"Error: {str(e)}"
def tts_dia(text, audio_prompt=None):
"""UI function for Dia TTS"""
model = tts_models.get("nari-labs/Dia-1.6B")
if not model:
return None, "Model not available"
try:
model.initialize() # Ensure model is loaded
audio_path = model.generate_speech(text, audio_prompt=audio_prompt)
return audio_path, ""
except Exception as e:
return None, f"Error: {str(e)}"
# UI Function for STT Model
def stt_whisper(audio_path, language=None):
"""UI function for Faster Whisper STT"""
model = stt_models.get("SYSTRAN/faster-whisper")
if not model:
return "Model not available"
try:
transcription = model.transcribe(audio_path, language=language)
return transcription
except Exception as e:
return f"Error: {str(e)}"
# Gradio UI Components
def create_tts_tab():
"""Create the TTS tab for the Gradio interface"""
with gr.Tab("Text-to-Speech"):
gr.Markdown("## Text-to-Speech Models")
with gr.Tabs():
# Chatterbox Tab
with gr.Tab("Chatterbox"):
with gr.Row():
with gr.Column():
chatterbox_text = gr.Textbox(
label="Text to speak",
placeholder="Enter text here...",
lines=5
)
chatterbox_language = gr.Dropdown(
choices=["English", "Chinese"],
value="English",
label="Language"
)
chatterbox_audio_prompt = gr.Audio(
label="Voice reference (optional)",
type="filepath"
)
chatterbox_submit = gr.Button("Generate Speech")
with gr.Column():
chatterbox_output = gr.Audio(label="Generated Speech")
chatterbox_error = gr.Textbox(label="Error", visible=False)
chatterbox_submit.click(
tts_chatterbox,
inputs=[chatterbox_text, chatterbox_language, chatterbox_audio_prompt],
outputs=[chatterbox_output, chatterbox_error]
)
# KittenTTS Tab
with gr.Tab("KittenTTS"):
with gr.Row():
with gr.Column():
kittentts_text = gr.Textbox(
label="Text to speak",
placeholder="Enter text here...",
lines=5
)
kittentts_audio_prompt = gr.Audio(
label="Voice reference (optional)",
type="filepath"
)
kittentts_submit = gr.Button("Generate Speech")
with gr.Column():
kittentts_output = gr.Audio(label="Generated Speech")
kittentts_error = gr.Textbox(label="Error", visible=False)
kittentts_submit.click(
tts_kittentts,
inputs=[kittentts_text, kittentts_audio_prompt],
outputs=[kittentts_output, kittentts_error]
)
# Piper Tab
with gr.Tab("Piper"):
with gr.Row():
with gr.Column():
piper_text = gr.Textbox(
label="Text to speak",
placeholder="Enter text here...",
lines=5
)
# Initialize Piper model to get voices
piper_model = tts_models.get("piper-tts")
if piper_model:
piper_model.initialize()
languages = piper_model.get_supported_languages()
else:
languages = ["English"]
piper_language = gr.Dropdown(
choices=languages,
value="English",
label="Language"
)
def update_piper_voices(language):
if piper_model:
voices = piper_model.get_available_voices(language)
return gr.update(choices=voices, value=voices[0] if voices else None)
return gr.update(choices=[], value=None)
piper_voice = gr.Dropdown(
label="Voice",
choices=[]
)
piper_language.change(
update_piper_voices,
inputs=[piper_language],
outputs=[piper_voice]
)
piper_submit = gr.Button("Generate Speech")
with gr.Column():
piper_output = gr.Audio(label="Generated Speech")
piper_error = gr.Textbox(label="Error", visible=False)
piper_submit.click(
tts_piper,
inputs=[piper_text, piper_language, piper_voice],
outputs=[piper_output, piper_error]
)
# Kokoro Tab
with gr.Tab("Kokoro"):
with gr.Row():
with gr.Column():
kokoro_text = gr.Textbox(
label="Text to speak",
placeholder="Enter text here...",
lines=5
)
kokoro_language = gr.Dropdown(
choices=[
"American English (a)", "British English (b)",
"Spanish (e)", "French (f)", "Hindi (h)",
"Italian (i)", "Japanese (j)",
"Brazilian Portuguese (p)", "Mandarin Chinese (z)"
],
value="American English (a)",
label="Language"
)
def get_lang_code(language):
return language.split("(")[-1].split(")")[0].strip()
def update_kokoro_voices(language):
lang_code = get_lang_code(language)
voices = get_kokoro_voices(lang_code)
return gr.update(choices=voices, value=voices[0] if voices else None)
kokoro_voice = gr.Dropdown(
label="Voice",
choices=get_kokoro_voices("a"),
value="af_heart"
)
kokoro_language.change(
update_kokoro_voices,
inputs=[kokoro_language],
outputs=[kokoro_voice]
)
kokoro_submit = gr.Button("Generate Speech")
with gr.Column():
kokoro_output = gr.Audio(label="Generated Speech")
kokoro_error = gr.Textbox(label="Error", visible=False)
kokoro_submit.click(
lambda text, lang, voice: tts_kokoro(text, get_lang_code(lang), voice),
inputs=[kokoro_text, kokoro_language, kokoro_voice],
outputs=[kokoro_output, kokoro_error]
)
# Dia Tab
with gr.Tab("Dia"):
with gr.Row():
with gr.Column():
dia_text = gr.Textbox(
label="Text to speak",
placeholder="Enter text here...",
lines=5
)
dia_audio_prompt = gr.Audio(
label="Voice reference (optional)",
type="filepath"
)
dia_submit = gr.Button("Generate Speech")
with gr.Column():
dia_output = gr.Audio(label="Generated Speech")
dia_error = gr.Textbox(label="Error", visible=False)
dia_submit.click(
tts_dia,
inputs=[dia_text, dia_audio_prompt],
outputs=[dia_output, dia_error]
)
def create_stt_tab():
"""Create the STT tab for the Gradio interface"""
with gr.Tab("Speech-to-Text"):
gr.Markdown("## Speech-to-Text Models")
with gr.Tabs():
# Faster Whisper Tab
with gr.Tab("Faster Whisper"):
with gr.Row():
with gr.Column():
whisper_audio = gr.Audio(
label="Audio to transcribe",
type="filepath"
)
whisper_language = gr.Dropdown(
choices=["Auto-detect", "English", "Chinese", "Spanish", "French", "German", "Japanese"],
value="Auto-detect",
label="Language (optional)"
)
whisper_submit = gr.Button("Transcribe")
with gr.Column():
whisper_output = gr.Textbox(
label="Transcription",
lines=5
)
whisper_submit.click(
lambda audio, lang: stt_whisper(audio, None if lang == "Auto-detect" else lang),
inputs=[whisper_audio, whisper_language],
outputs=[whisper_output]
)
# Create the Gradio interface
def create_interface():
"""Create the main Gradio interface"""
with gr.Blocks(title="TTS & STT Gallery") as demo:
gr.Markdown("# TTS & STT Model Gallery")
gr.Markdown("Explore different Text-to-Speech and Speech-to-Text models")
with gr.Tabs():
create_tts_tab()
create_stt_tab()
return demo
# Launch the app
if __name__ == "__main__":
demo = create_interface()
demo.launch()
|