tts / app.py
darshankr's picture
Update app.py
d6dc2d5 verified
import gradio as gr
import subprocess
import os
# Define paths to the TTS model and vocoder files (relative to the Indic-TTS folder)
MODEL_PATH = "models/v1/hi/fastpitch/best_model.pth"
CONFIG_PATH = "models/v1/hi/fastpitch/config.json"
VOCODER_PATH = "models/v1/hi/hifigan/best_model.pth"
VOCODER_CONFIG_PATH = "models/v1/hi/hifigan/config.json"
OUTPUT_FILE = "output.mp4"
INDIC_TTS_FOLDER = "Indic-TTS" # Folder where the TTS system is located
def generate_speech(text: str) -> str:
"""
Navigate to the Indic-TTS folder, run the TTS synthesis command, and return to the original directory.
Returns the path to the output audio file or an error message.
"""
original_dir = os.getcwd() # Save the current working directory
try:
# Change to the Indic-TTS directory
os.chdir(INDIC_TTS_FOLDER)
# Construct the command for speech synthesis
command = [
"python3", "-m", "TTS.bin.synthesize",
"--text", text,
"--model_path", MODEL_PATH,
"--config_path", CONFIG_PATH,
"--vocoder_path", VOCODER_PATH,
"--vocoder_config_path", VOCODER_CONFIG_PATH,
"--speaker_idx", "female",
"--out_path", OUTPUT_FILE
]
# Run the command
result = subprocess.run(command, capture_output=True, text=True)
# Check for errors
if result.returncode != 0:
raise Exception(f"Error: {result.stderr}")
# Return the full path to the generated output file
return os.path.join(os.getcwd(), OUTPUT_FILE)
except Exception as e:
return str(e)
finally:
# Change back to the original directory
os.chdir(original_dir)
# Create the Gradio interface
interface = gr.Interface(
fn=generate_speech,
inputs=gr.Textbox(label="Enter Text", placeholder="Type some text to synthesize..."),
outputs=gr.File(label="Download Speech"),
title="Hindi Speech Synthesis",
description="Enter text in Hindi and generate speech using the FastPitch TTS model."
)
# Launch the app
if __name__ == "__main__":
interface.launch()