Spaces:
Sleeping
Sleeping
import gradio as gr | |
from TTS.api import TTS | |
import tempfile | |
import docx | |
# Voice models dictionary with metadata on whether they support multi-speaker | |
VOICE_MODELS = { | |
"LJSpeech (Standard Female)": { | |
"model_name": "tts_models/en/ljspeech/vits", | |
"multi_speaker": False | |
}, | |
"VCTK (Multi-speaker English)": { | |
"model_name": "tts_models/en/vctk/vits", | |
"multi_speaker": True | |
} | |
} | |
# Embedded short speaker metadata (from your CSV) | |
SPEAKER_METADATA = { | |
"225": {"age": 23, "gender": "F", "accent": "English"}, | |
"226": {"age": 22, "gender": "M", "accent": "English"}, | |
"227": {"age": 38, "gender": "M", "accent": "English"}, | |
"228": {"age": 22, "gender": "F", "accent": "English"}, | |
"229": {"age": 23, "gender": "F", "accent": "English"}, | |
"230": {"age": 22, "gender": "F", "accent": "English"}, | |
"231": {"age": 23, "gender": "F", "accent": "English"}, | |
"232": {"age": 23, "gender": "M", "accent": "English"}, | |
"233": {"age": 23, "gender": "F", "accent": "English"}, | |
"234": {"age": 22, "gender": "F", "accent": "Scottish"} | |
# Add more as needed | |
} | |
# Pre-format speaker dropdown choices | |
SPEAKER_CHOICES = [ | |
(sid, f"p{sid} ({data['gender']}, {data['accent']}, {data['age']} yrs)") | |
for sid, data in SPEAKER_METADATA.items() | |
] | |
# Model cache | |
MODEL_CACHE = {} | |
def load_tts_model(model_key): | |
if model_key in MODEL_CACHE: | |
return MODEL_CACHE[model_key] | |
model_info = VOICE_MODELS[model_key] | |
tts = TTS(model_name=model_info["model_name"], gpu=False) | |
MODEL_CACHE[model_key] = tts | |
return tts | |
def extract_text_from_docx(file): | |
doc = docx.Document(file) | |
return "\n".join([para.text for para in doc.paragraphs if para.text.strip()]) | |
def generate_audio(voice_key, speaker_id, docx_file): | |
text = extract_text_from_docx(docx_file) | |
tts = load_tts_model(voice_key) | |
kwargs = {} | |
if VOICE_MODELS[voice_key]["multi_speaker"]: | |
kwargs["speaker"] = speaker_id | |
output_path = tempfile.mktemp(suffix=".wav") | |
tts.tts_to_file(text=text, file_path=output_path, **kwargs) | |
return output_path | |
def update_speaker_visibility(voice_key): | |
visible = VOICE_MODELS[voice_key]["multi_speaker"] | |
return gr.update(visible=visible) | |
with gr.Blocks() as demo: | |
gr.Markdown("## DOCX to Speech with Speaker Selection") | |
with gr.Row(): | |
voice_dropdown = gr.Dropdown( | |
choices=list(VOICE_MODELS.keys()), | |
value="LJSpeech (Standard Female)", | |
label="Select Voice" | |
) | |
speaker_dropdown = gr.Dropdown( | |
choices=SPEAKER_CHOICES, | |
label="Select Speaker", | |
visible=False | |
) | |
docx_input = gr.File(label="Upload .docx File", file_types=[".docx"]) | |
generate_btn = gr.Button("Generate Audio") | |
audio_output = gr.Audio(label="Output Audio") | |
voice_dropdown.change(fn=update_speaker_visibility, inputs=voice_dropdown, outputs=speaker_dropdown) | |
generate_btn.click( | |
fn=generate_audio, | |
inputs=[voice_dropdown, speaker_dropdown, docx_input], | |
outputs=audio_output | |
) | |
demo.launch() | |