File size: 1,091 Bytes
c75d7b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import torchaudio
import streamlit as st
from tortoise.api import TextToSpeech

# Initialize Tortoise TTS
tts = TextToSpeech()

# Function to generate speech from text
def text_to_speech(text, preset="fast"):
    st.write("Generating speech... This may take a while.")
    
    # Generate speech using the Tortoise TTS API
    generated = tts.tts_with_preset(text, preset=preset)
    
    # Save the generated audio as a wav file using torchaudio
    output_path = "output.wav"
    torchaudio.save(output_path, generated.squeeze(0).cpu(), sample_rate=24000)
    return output_path

# Streamlit interface
st.title("Text to Speech Converter")

# Take user input as text
text = st.text_input("Enter text you want to convert to speech", "Hello, welcome to the Tortoise TTS text-to-speech demonstration.")

# Button to generate speech
if st.button("Generate Speech"):
    output_path = text_to_speech(text)

    # Read and play the audio file in the Streamlit app
    audio_file = open(output_path, 'rb')
    audio_bytes = audio_file.read()
    st.audio(audio_bytes, format='audio/wav')