import streamlit as st from transformers import pipeline import soundfile as sf import tempfile # Streamlit page setup st.set_page_config(page_title="Hugging Face Text-to-Speech", page_icon="🎤", layout="centered") st.title("🎤 Text to Speech Converter (Hugging Face)") # Text input from user text = st.text_area("Enter the text you want to convert to speech:", height=150) # Choose model model_name = st.selectbox( "Select a Hugging Face TTS model:", ["suno/bark-small", "facebook/speecht5_tts", "espnet/kan-bayashi_ljspeech_vits"], index=0 ) if st.button("Convert to Speech"): if text.strip() == "": st.warning("Please enter some text.") else: st.info("Generating speech... please wait ⏳") tts = pipeline("text-to-speech", model=model_name) output = tts(text) # Get audio and sampling rate audio = output["audio"] sr = output["sampling_rate"] # Save audio to a temporary file with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile: sf.write(tmpfile.name, audio, sr) audio_path = tmpfile.name st.success("✅ Conversion complete!") st.audio(audio_path, format="audio/wav") st.download_button( label="📥 Download Audio", data=open(audio_path, "rb").read(), file_name="tts_output.wav", mime="audio/wav" )