-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech.py
82 lines (63 loc) · 2.28 KB
/
speech.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# one of these libraries for text to speech
# TODO : add deepmind wavenet tts for better quality
# TODO : also add paywall for wavenet
from gtts import gTTS
import os
# this is for linux
# sudo apt-get install mpg321
# this is for mac
# brew install mpg321
class TextToSpeech:
def __init__(self):
pass
@staticmethod
def speak(info):
'''
This will generate a complete sentence from all the information given
info: A dictionary containing all the information to be spoken
- current_time
- temperature
- humidity
- description
- deadline_progress
'''
project_name = 'begin quote,,, Become Rich and Ripped, end quote,'
text = f'''
Good Morning Alex! It's {info['current_time'].strftime('%H:%M')} am, and here's your morning update.
The current temperature is {info['temperature']}°F with a humidity of {info['humidity']}%.
As for the weather, it's {info['description']}.
Also, a quick project update - you've completed {round(info['deadline_progress']*100,2)}%
of your {project_name} project. I think ur falling behind a little ... you better start working or
you won't finish on time.
'''
try:
print('now generating audio')
tts = gTTS(text=text, lang="en", slow=False)
tts.save("text.mp3")
except Exception as e:
print(f"Error generating audio: {e}")
return # Do not proceed if there's an error
try:
os.system("mpg321 text.mp3")
except Exception as e:
print(f"Error playing audio: {e}")
finally:
os.remove("text.mp3") # Ensure cleanup even on error
if __name__ == "__main__":
import datetime
info = {
'current_time': datetime.datetime.now(),
'temperature': 75,
'humidity': 50,
'description': 'sunny',
'deadline_progress': 0.5342
}
tts = TextToSpeech()
# tts.speak(info)
print("done")
# testing threading
import threading
import time
threading.Thread(target=TextToSpeech.speak, args=(info,)).start()
time.sleep(1)
print('THIS SHOULD START RUNNING')