Spaces:
Sleeping
Sleeping
Create text_speech_utils.py
Browse files- text_speech_utils.py +54 -0
text_speech_utils.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import sounddevice as sd
|
3 |
+
import audiofile as af
|
4 |
+
from scipy.io.wavfile import write
|
5 |
+
from gtts import gTTS
|
6 |
+
|
7 |
+
import multiprocessing
|
8 |
+
import pyttsx3
|
9 |
+
import keyboard
|
10 |
+
|
11 |
+
def say(text):
|
12 |
+
p = multiprocessing.Process(target=pyttsx3.speak, args=(text,))
|
13 |
+
p.start()
|
14 |
+
while p.is_alive():
|
15 |
+
if keyboard.is_pressed('enter'):
|
16 |
+
p.terminate()
|
17 |
+
else:
|
18 |
+
continue
|
19 |
+
p.join()
|
20 |
+
|
21 |
+
|
22 |
+
def record_audio(filename, sec, sr = 44100):
|
23 |
+
audio = sd.rec(int(sec * sr), samplerate=sr, channels=2, blocking=False)
|
24 |
+
sd.wait()
|
25 |
+
write(filename, sr, audio)
|
26 |
+
|
27 |
+
def record_audio_manual(filename, sr = 44100):
|
28 |
+
input(" ** Press enter to start recording **")
|
29 |
+
audio = sd.rec(int(10 * sr), samplerate=sr, channels=2)
|
30 |
+
input(" ** Press enter to stop recording **")
|
31 |
+
sd.stop()
|
32 |
+
write(filename, sr, audio)
|
33 |
+
|
34 |
+
def play_audio(filename):
|
35 |
+
signal, sr = af.read(filename)
|
36 |
+
sd.play(signal, sr)
|
37 |
+
|
38 |
+
def transcribe_audio(filename):
|
39 |
+
audio_file= open(filename, "rb")
|
40 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
41 |
+
audio_file.close()
|
42 |
+
return transcript
|
43 |
+
|
44 |
+
def translate_audio(filename):
|
45 |
+
audio_file= open(filename, "rb")
|
46 |
+
translation = openai.Audio.translate("whisper-1", audio_file)
|
47 |
+
audio_file.close()
|
48 |
+
return translation
|
49 |
+
|
50 |
+
def save_text_as_audio(text, audio_filename):
|
51 |
+
myobj = gTTS(text=text, lang='en', slow=False)
|
52 |
+
myobj.save(audio_filename)
|
53 |
+
|
54 |
+
|