CoolFace
Apppublic

simoxxDev/myKokoro

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
app.py42 linesDownload Raw Back to root
1import streamlit as st2from kokoro import KPipeline3import base644import io5import torch6 7# Initialize Kokoro Pipeline8pipeline = KPipeline(lang_code='a')9 10# Streamlit UI11st.title("Kokoro-82 Text-to-Speech API ๐ŸŽ™๏ธ")12st.write("Use the API to turn text into voice.")13 14# Get user input15text = st.text_area("Enter text:")16voice = st.selectbox("Choose a voice:", [17    'af_heart', 'af_bella', 'af_nicole', 'af_sarah', 'af_sky',18    'am_adam', 'am_michael', 'bf_emma', 'bf_isabella', 'bm_george', 'bm_lewis'19])20 21# Function to generate speech from text22def generate_speech(text, voice):23    """Generates speech from text using Kokoro TTS"""24    generator = pipeline(text, voice=voice)25    audio_data = next(generator)[2]26 27    # Ensure tensor is converted to bytes28    if isinstance(audio_data, torch.Tensor):29        audio_data = audio_data.cpu().numpy().tobytes()30 31    # Save audio file32    audio_file = io.BytesIO(audio_data)33    return audio_file34 35# When the button is clicked, generate and play the speech36if st.button("Generate Speech"):37    audio_file = generate_speech(text, voice)38    st.audio(audio_file, format="audio/wav")39 40# Inform the user that the app is running41st.write("The app is deployed on Hugging Face Spaces.")42