File size: 1,523 Bytes
f84c58e
e7a21bd
f84c58e
 
e7a21bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech
import torch
import soundfile as sf
from datasets import load_dataset

# Initialize the processor and model
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")

# Load the vocoder
vocoder = torch.hub.load("s3prl/s3prl", "mb_melgan")

# Initialize session state
if 'text' not in st.session_state:
    st.session_state['text'] = "Hello, my dog is cooler than you!"

# Function to update session state
def update_text():
    st.session_state['text'] = st.text_area("Text", st.session_state['text'])

st.title("Text-to-Speech with SpeechT5")
st.write("Enter the text you want to convert to speech:")

# Use session state to store text
update_text()

if st.button("Generate Speech"):
    st.write("Generating speech...")

    # Process the input text
    inputs = processor(text=st.session_state['text'], return_tensors="pt")

    # Generate speech
    speech = model.generate_speech(inputs["input_ids"], speaker_embeddings=None)

    # Use the vocoder to convert the generated speech to audio
    with torch.no_grad():
        audio = vocoder(speech)

    # Save the audio to a file
    sf.write("output.wav", audio.cpu().numpy(), samplerate=16000)

    # Provide a download link for the generated speech
    st.audio("output.wav", format="audio/wav")
    st.write("Speech generation complete. You can listen to the generated speech above.")