Spaces:
Running
Running
import streamlit as st | |
import edge_tts | |
import asyncio | |
import tempfile | |
import os | |
from typing import Dict | |
async def text_to_speech(text: str, voice: str) -> str: | |
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") | |
communicate = edge_tts.Communicate(text, voice) | |
await communicate.save(output_file.name) | |
return output_file.name | |
async def list_voices() -> Dict[str, Dict]: | |
voices = await edge_tts.list_voices() | |
return {v['ShortName']: {'name': v['ShortName'], 'language': v['Locale']} for v in voices} | |
async def main(): | |
st.title("OpenSource Text-to-Speech App") | |
st.write("Convert text to speech using various voices") | |
# Get voices | |
voices = await list_voices() | |
# Text-to-Speech | |
st.header("Text-to-Speech") | |
text_input = st.text_area("Enter text to convert to speech:") | |
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 = await 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__': | |
asyncio.run(main()) |