telee commited on
Commit
93aa3b8
·
1 Parent(s): db7f821

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pyttsx3
3
+ from gtts import gTTS
4
+ from io import BytesIO
5
+
6
+ LANG = "en"
7
+
8
+
9
+ # https://gtts.readthedocs.io/en/latest/
10
+ #
11
+ def tts_gtts(text):
12
+ mp3_fp = BytesIO()
13
+ tts = gTTS(text, lang=LANG)
14
+ tts.write_to_fp(mp3_fp)
15
+ return mp3_fp
16
+
17
+
18
+ # https://github.com/nateshmbhat/pyttsx3
19
+ # https://pyttsx3.readthedocs.io/en/latest/
20
+ #
21
+ def tts_pyttsx3(text, gender="F"):
22
+ mp3_fp = BytesIO()
23
+ engine = pyttsx3.init()
24
+ gender_idx = 1
25
+ if gender == "M":
26
+ gender_idx = 0
27
+ voices = engine.getProperty('voices') # details of current voice
28
+ engine.setProperty('voice', voices[gender_idx].id) # 0 for male and 1 for female
29
+ engine.save_to_file(text, mp3_fp)
30
+ engine.runAndWait()
31
+ return mp3_fp
32
+
33
+
34
+ def pronounce(text, gender=None):
35
+ if len(text) > 0:
36
+ data1 = tts_gtts(text)
37
+ st.text('gTTS (gender not supported):')
38
+ st.audio(data1, format="audio/wav", start_time=0)
39
+ if gender is None and gender is not None: # pyttsx3 not working with streamlit
40
+ data2 = tts_pyttsx3(text, gender)
41
+ st.text('pyttsx3:')
42
+ st.audio(data2, format="audio/wav", start_time=0)
43
+
44
+
45
+ def main():
46
+ st.title('TTS Demo')
47
+ # uploaded_file = st.file_uploader("Upload a recording of you saying the text in .wav format")
48
+ # if uploaded_file is not None:
49
+ # pass
50
+ text_input = st.text_input("", value="Input the text you are saying in your recording.")
51
+ gender_input = st.radio("Gender", ["M", "F"], captions=["", ""], horizontal=True)
52
+ if st.button("Pronounce"):
53
+ pronounce(text_input, gender_input)
54
+
55
+
56
+ if __name__ == "__main__":
57
+ main()