pantelnm commited on
Commit
7651f4d
1 Parent(s): f9d14f8

Update app.py

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