File size: 2,148 Bytes
3215d8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()