Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import edge_tts
|
3 |
+
import asyncio
|
4 |
+
import tempfile
|
5 |
+
import os
|
6 |
+
from typing import Dict
|
7 |
+
|
8 |
+
def text_to_speech(text: str, voice: str) -> str:
|
9 |
+
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
10 |
+
|
11 |
+
async def generate_speech():
|
12 |
+
communicate = edge_tts.Communicate(text, voice)
|
13 |
+
await communicate.save(output_file.name)
|
14 |
+
|
15 |
+
asyncio.run(generate_speech())
|
16 |
+
return output_file.name
|
17 |
+
|
18 |
+
def list_voices() -> Dict[str, Dict]:
|
19 |
+
voices = edge_tts.list_voices()
|
20 |
+
return {v['ShortName']: {'name': v['ShortName'], 'language': v['Locale']} for v in voices}
|
21 |
+
|
22 |
+
def main():
|
23 |
+
st.title("OpenSource Text-to-Speech App")
|
24 |
+
st.write("Convert text to speech using various voices")
|
25 |
+
|
26 |
+
# Text-to-Speech
|
27 |
+
st.header("Text-to-Speech")
|
28 |
+
text_input = st.text_area("Enter text to convert to speech:")
|
29 |
+
voices = list_voices()
|
30 |
+
selected_voice = st.selectbox("Select a voice:", list(voices.keys()))
|
31 |
+
|
32 |
+
if st.button("Generate Speech"):
|
33 |
+
if not text_input:
|
34 |
+
st.error("Please enter some text.")
|
35 |
+
else:
|
36 |
+
with st.spinner("Generating speech..."):
|
37 |
+
output_file = text_to_speech(text_input, selected_voice)
|
38 |
+
st.audio(output_file, format='audio/mp3')
|
39 |
+
os.unlink(output_file) # Delete the temporary file
|
40 |
+
|
41 |
+
# List Available Voices
|
42 |
+
st.header("Available Voices")
|
43 |
+
st.dataframe(voices)
|
44 |
+
|
45 |
+
if __name__ == '__main__':
|
46 |
+
main()
|