Spaces:
Running
Running
File size: 1,448 Bytes
6818fde |
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 46 |
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() |