File size: 1,433 Bytes
af8effb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"
        )