jhparmar commited on
Commit
4a13c18
1 Parent(s): 10f3b76

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +123 -123
main.py CHANGED
@@ -1,123 +1,123 @@
1
- import streamlit as st
2
- import time
3
- import speech_recognition as sr
4
- import pyttsx3
5
-
6
- # defining some global varibles
7
- global pause_rate
8
- global option
9
- global start_button
10
- global stop_button
11
- global text
12
- global gender
13
- global audio_name
14
-
15
- # Speech to text function
16
- def take_commands(pause_rate):
17
- r = sr.Recognizer()
18
- with sr.Microphone() as source:
19
- r.adjust_for_ambient_noise(source)
20
- st.write('Listening')
21
- print('Listening')
22
- r.pause_threshold = pause_rate
23
- audio = r.listen(source)
24
- try:
25
- st.write("PROCESSING VOICE")
26
- coundownrec(101)
27
- # for listening the command in indian english
28
- Query = r.recognize_google(audio, language='en-in')
29
- # print("the query is printed='", Query, "'")
30
- st.write(Query)
31
- except Exception as e:
32
- st.write('"Restart again and make sure you have strong internet connection"')
33
- return "None"
34
- return Query
35
-
36
- # Text to speech function
37
- def Speak(audio,gender,speed,audio_name):
38
- engine = pyttsx3.init()
39
- voices = engine.getProperty('voices')
40
- engine.setProperty('voice', voices[gender].id)
41
- engine.setProperty('rate',speed)
42
- engine.say(audio)
43
- if audio_name.endswith('3'):
44
- engine.save_to_file(audio,audio_name)
45
- engine.runAndWait()
46
- def coundownrec(t):
47
- st.write('Processing percentage')
48
- countbar = st.progress(0)
49
- for i in range(t):
50
- countbar.progress(i)
51
- time.sleep(0.01)
52
-
53
-
54
- # count down function
55
- def coundown(t):
56
- st.write('Start After Count Down Bar Is Full',)
57
- countbar = st.progress(0)
58
- for i in range(t):
59
- countbar.progress(i)
60
- time.sleep(0.01)
61
-
62
- # Streamlit app
63
- with st.sidebar:
64
- st.title('Speech To Text And Text To Speech App')
65
- st.image('app_files\speeckimage.png')
66
- option = st.radio('Options',['Speech to Text',"Text to Speech"])
67
- start_button = st.button('Start')
68
- stop_button = st.button('Stop')
69
- if stop_button:
70
- st.write('')
71
-
72
- # For speech to text
73
- if option == 'Speech to Text':
74
- st.write('Speech to text requires internet connection')
75
- pause_rate = st.number_input('Pause Rate',0.5,1.0)
76
- if start_button:
77
- coundown(101)
78
- take_commands(pause_rate)
79
-
80
- # For text to speech
81
- if option == 'Text to Speech':
82
- gender = st.radio('VOICE TO READ',['Male','Female'])
83
- save = st.radio('SAVE TEXT AS AUDIO',['Yes','No'])
84
- if save == 'Yes':
85
- audio_name = st.text_input('Audio Name',placeholder='save file as:')
86
- audio_name = audio_name+'.mp3'
87
- else:audio_name = ''
88
- print(audio_name)
89
- speed = st.number_input('Reading Speed',90,200)
90
- if gender == 'Male':
91
- gender = 0
92
- elif gender == "Female":
93
- gender = 1
94
- else: gender =0
95
- text = st.text_area('Text Input',placeholder='Type or paste your text here and click start')
96
- if start_button:
97
- coundown(101)
98
- st.title('Output Text')
99
- st.write(text)
100
- Speak(text,gender,speed,audio_name)
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
 
1
+ import streamlit as st
2
+ import time
3
+ import speech_recognition as sr
4
+ import pyttsx3
5
+
6
+ # defining some global varibles
7
+ global pause_rate
8
+ global option
9
+ global start_button
10
+ global stop_button
11
+ global text
12
+ global gender
13
+ global audio_name
14
+
15
+ # Speech to text function
16
+ def take_commands(pause_rate):
17
+ r = sr.Recognizer()
18
+ with sr.Microphone() as source:
19
+ r.adjust_for_ambient_noise(source)
20
+ st.write('Listening')
21
+ print('Listening')
22
+ r.pause_threshold = pause_rate
23
+ audio = r.listen(source)
24
+ try:
25
+ st.write("PROCESSING VOICE")
26
+ coundownrec(101)
27
+ # for listening the command in indian english
28
+ Query = r.recognize_google(audio, language='en-in')
29
+ # print("the query is printed='", Query, "'")
30
+ st.write(Query)
31
+ except Exception as e:
32
+ st.write('"Restart again and make sure you have strong internet connection"')
33
+ return "None"
34
+ return Query
35
+
36
+ # Text to speech function
37
+ def Speak(audio,gender,speed,audio_name):
38
+ engine = pyttsx3.init()
39
+ voices = engine.getProperty('voices')
40
+ engine.setProperty('voice', voices[gender].id)
41
+ engine.setProperty('rate',speed)
42
+ engine.say(audio)
43
+ if audio_name.endswith('3'):
44
+ engine.save_to_file(audio,audio_name)
45
+ engine.runAndWait()
46
+ def coundownrec(t):
47
+ st.write('Processing percentage')
48
+ countbar = st.progress(0)
49
+ for i in range(t):
50
+ countbar.progress(i)
51
+ time.sleep(0.01)
52
+
53
+
54
+ # count down function
55
+ def coundown(t):
56
+ st.write('Start After Count Down Bar Is Full',)
57
+ countbar = st.progress(0)
58
+ for i in range(t):
59
+ countbar.progress(i)
60
+ time.sleep(0.01)
61
+
62
+ # Streamlit app
63
+ with st.sidebar:
64
+ st.title('Speech To Text And Text To Speech App')
65
+ st.image('speeckimage.png')
66
+ option = st.radio('Options',['Speech to Text',"Text to Speech"])
67
+ start_button = st.button('Start')
68
+ stop_button = st.button('Stop')
69
+ if stop_button:
70
+ st.write('')
71
+
72
+ # For speech to text
73
+ if option == 'Speech to Text':
74
+ st.write('Speech to text requires internet connection')
75
+ pause_rate = st.number_input('Pause Rate',0.5,1.0)
76
+ if start_button:
77
+ coundown(101)
78
+ take_commands(pause_rate)
79
+
80
+ # For text to speech
81
+ if option == 'Text to Speech':
82
+ gender = st.radio('VOICE TO READ',['Male','Female'])
83
+ save = st.radio('SAVE TEXT AS AUDIO',['Yes','No'])
84
+ if save == 'Yes':
85
+ audio_name = st.text_input('Audio Name',placeholder='save file as:')
86
+ audio_name = audio_name+'.mp3'
87
+ else:audio_name = ''
88
+ print(audio_name)
89
+ speed = st.number_input('Reading Speed',90,200)
90
+ if gender == 'Male':
91
+ gender = 0
92
+ elif gender == "Female":
93
+ gender = 1
94
+ else: gender =0
95
+ text = st.text_area('Text Input',placeholder='Type or paste your text here and click start')
96
+ if start_button:
97
+ coundown(101)
98
+ st.title('Output Text')
99
+ st.write(text)
100
+ Speak(text,gender,speed,audio_name)
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+