File size: 1,863 Bytes
527cad7
 
 
16aac4b
 
527cad7
16aac4b
527cad7
 
59a82b8
527cad7
 
59a82b8
16aac4b
59a82b8
 
527cad7
16aac4b
527cad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16aac4b
e0d22a9
527cad7
 
 
 
 
59a82b8
 
527cad7
 
 
 
 
 
59a82b8
527cad7
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
# Install necessary libraries
import os
import numpy as np
from speechbrain.pretrained import Tacotron2, HIFIGAN
from scipy.io.wavfile import write
import streamlit as st

# Load TTS and vocoder models
@st.cache_resource  # Cache the models to avoid reloading
def load_models():
    tacotron2 = Tacotron2.from_hparams(source="speechbrain/tts-tacotron2-ljspeech", savedir="tmp_tts")
    hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="tmp_vocoder")
    return tacotron2, hifi_gan

tacotron2, hifi_gan = load_models()

# Text-to-Speech function
def text_to_speech(text):
    try:
        # Generate mel spectrogram
        mel_output, _, _ = tacotron2.encode_text(text)

        # Generate waveform from mel spectrogram
        waveforms = hifi_gan.decode_batch(mel_output)

        # Convert waveform to numpy format
        waveform = waveforms.squeeze().cpu().numpy()

        # Normalize waveform to range [-1, 1]
        waveform = waveform / np.max(np.abs(waveform))

        # Save waveform to a .wav file
        output_path = "output.wav"
        write(output_path, 22050, (waveform * 32767).astype(np.int16))
        return output_path

    except Exception as e:
        st.error(f"Error during text-to-speech generation: {e}")
        return None

# Streamlit UI
st.title("Text-to-Speech Application")
st.write("Enter text below and convert it to speech!")

# Input field
text_input = st.text_area("Enter Text:", "Hello, welcome to the Text-to-Speech app!")

if st.button("Generate Speech"):
    if text_input.strip():
        output_audio = text_to_speech(text_input)
        if output_audio:
            st.audio(output_audio, format="audio/wav")
        else:
            st.error("Failed to generate audio. Please check the input text.")
    else:
        st.warning("Please enter some text to generate speech.")