File size: 1,098 Bytes
3da6ab0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83b10c7
3da6ab0
 
 
 
 
 
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
import os
os.system("pip install speechbrain torchaudio gradio")

import torchaudio
from speechbrain.pretrained import Tacotron2, HIFIGAN
import gradio as gr

# Load SpeechBrain models
print("Loading SpeechBrain models...")
tacotron2 = Tacotron2.from_hparams(source="speechbrain/tts-tacotron2-ljspeech", savedir="models/tacotron2")
hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="models/hifigan")
print("Models loaded! Ready to generate speech.")

# Generate speech from text
def text_to_speech(text):
    mel_output, _, _ = tacotron2.encode_text(text)
    waveforms = hifi_gan.decode_batch(mel_output)
    output_file = "output_speech.wav"
    torchaudio.save(output_file, waveforms.squeeze(1), 22050)
    return output_file  # Return the audio file

# Create Gradio UI
iface = gr.Interface(
    fn=text_to_speech,
    inputs=gr.Textbox(label="Text to speak"),
    outputs=gr.Audio(type="filepath", label="Generated Speech"),
    title="SpeechBrain TTS Demo",
    description="Enter text and get an AI-generated voice output!"
)

# Launch the app
iface.launch()