Spaces:
Running
Running
import streamlit as st | |
import edge_tts | |
import asyncio | |
import tempfile | |
import os | |
from typing import Dict | |
def text_to_speech(text: str, voice: str) -> str: | |
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") | |
async def generate_speech(): | |
communicate = edge_tts.Communicate(text, voice) | |
await communicate.save(output_file.name) | |
asyncio.run(generate_speech()) | |
return output_file.name | |
def list_voices() -> Dict[str, Dict]: | |
voices = edge_tts.list_voices() | |
return {v['ShortName']: {'name': v['ShortName'], 'language': v['Locale']} for v in voices} | |
def main(): | |
st.title("OpenSource Text-to-Speech App") | |
st.write("Convert text to speech using various voices") | |
# Text-to-Speech | |
st.header("Text-to-Speech") | |
text_input = st.text_area("Enter text to convert to speech:") | |
voices = list_voices() | |
selected_voice = st.selectbox("Select a voice:", list(voices.keys())) | |
if st.button("Generate Speech"): | |
if not text_input: | |
st.error("Please enter some text.") | |
else: | |
with st.spinner("Generating speech..."): | |
output_file = text_to_speech(text_input, selected_voice) | |
st.audio(output_file, format='audio/mp3') | |
os.unlink(output_file) # Delete the temporary file | |
# List Available Voices | |
st.header("Available Voices") | |
st.dataframe(voices) | |
if __name__ == '__main__': | |
main() |