HaileyIsNotAPig's picture
Create app.py
aef1a8d verified
Raw History Blame Contribute Delete
1.82 kB
import streamlit as st
import os
from TTS.api import TTS
from pydub import AudioSegment
import io
import base64
# Initialize TTS model
@st.cache_resource
def load_tts_model():
return TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=True)
tts = load_tts_model()
st.title("French Text-to-Speech Converter")
# Text input
text_input = st.text_area("Enter your French text here:", height=150)
# Speed slider
speed = st.slider("Speech speed", 0.5, 1.5, 0.7, 0.1)
# Generate button
if st.button("Generate Speech"):
if text_input:
# Create a progress bar
progress_bar = st.progress(0)
status_text = st.empty()
# Update progress
status_text.text("Generating speech...")
progress_bar.progress(25)
# Generate speech
output_path = "output.mp3"
tts.tts_to_file(text=text_input,
file_path=output_path,
speaker_wav="female.wav",
speed=speed,
language="fr")
progress_bar.progress(75)
status_text.text("Processing audio...")
# Read generated audio file
audio = AudioSegment.from_mp3(output_path)
# Create a download button
buffer = io.BytesIO()
audio.export(buffer, format="mp3")
b64 = base64.b64encode(buffer.getvalue()).decode()
href = f'<a href="data:audio/mp3;base64,{b64}" download="generated_speech.mp3">Download MP3</a>'
progress_bar.progress(100)
status_text.text("Done!")
st.markdown(href, unsafe_allow_html=True)
# Play audio in the browser
st.audio(buffer, format="audio/mp3")
# Clean up
os.remove(output_path)
else:
st.warning("Please enter some text to convert to speech.")