forked from spignelon/virtual-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
138 lines (123 loc) · 4.72 KB
/
Copy pathmain.py
File metadata and controls
138 lines (123 loc) · 4.72 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import random
import time
import webbrowser
from gtts import gTTS
import playsound
import pyjokes
import speech_recognition as sr
import wikipedia
import screen_brightness_control as sbc
import alsaaudio
# Initialize voice recognizer
r = sr.Recognizer()
r.energy_threshold = 2837
r.dynamic_energy_threshold = True
def record_audio(ask=False):
if ask:
alina_speak(ask)
with sr.Microphone() as source:
audio = r.listen(source)
voice_data = ""
try:
voice_data = r.recognize_google(audio)
except sr.UnknownValueError:
alina_speak("Sorry, I did not get that.")
except sr.RequestError:
alina_speak("Sorry, my speech service is down.")
return voice_data
def alina_speak(audio_string):
tts = gTTS(text=audio_string, tld="com", lang="en", slow=False)
r = random.randint(1, 1000000)
audio_file = f"audio-{str(r)}.mp3"
tts.save(audio_file)
print(audio_string)
playsound.playsound(audio_file)
os.remove(audio_file)
def respond(voice_data):
voice_data = voice_data.lower()
if "wikipedia" in voice_data:
alina_speak("Searching wikipedia...")
query = voice_data.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=1)
alina_speak(results)
if "youtube" in voice_data:
search = record_audio("What do you want to search for?")
url = f"https://www.youtube.com/results?search_query={search}"
webbrowser.get().open(url)
alina_speak(f"Here is what I found for {search}")
if "what is your name" in voice_data:
alina_speak("My name is Alina.")
if "what time is it" in voice_data:
alina_speak(time.ctime())
if "search" in voice_data:
search = record_audio("What do you want to search for?")
url = f"https://www.google.com/search?q={search}"
webbrowser.get().open(url)
alina_speak(f"Here is what I found for {search}")
if "find location" in voice_data:
location = record_audio("What is the location?")
url = f"https://google.nl/maps/place/{location}/&"
webbrowser.get().open(url)
alina_speak(f"Here is the location of {location}.")
if "toss a coin" in voice_data:
coin_flip_with_random = "Heads" if random.random() > 0.5 else "Tails"
alina_speak(f"You got {coin_flip_with_random}!")
if "exit" in voice_data:
alina_speak("Goodbye!")
exit()
if "tell a joke" in voice_data:
joke = pyjokes.get_joke(language="en", category="neutral")
alina_speak(joke)
# Command to shut down the system
if "shutdown" in voice_data:
confirmation = record_audio(
"Do you really wish to shut down your system?")
confirmation = confirmation.lower()
if "yes" in confirmation or "yep" in confirmation:
alina_speak("Shutting down in 3 seconds")
time.sleep(3)
if os.name == "posix":
os.system("shutdown now")
if os.name == "nt":
os.system("shutdown /s /t 1")
# Command to change brightness
if "brightness" in voice_data:
current_brightness = sbc.get_brightness()[0]
if "increase" in voice_data:
alina_speak("Increasing Brightness")
new_brightness = min(100, current_brightness+10)
sbc.set_brightness(new_brightness)
if "decrease" in voice_data:
alina_speak("Decreasing Brightness")
new_brightness = max(0, current_brightness-10)
sbc.set_brightness(new_brightness)
if "%" in voice_data:
percentage = int(voice_data.split("%")[0].split(" ")[-1])
percentage = min(max(0, percentage), 100)
alina_speak(f"Setting brightness to {percentage}%")
sbc.set_brightness(percentage)
# Command to change volume
if "volume" in voice_data:
mixer = alsaaudio.Mixer()
current_volume = mixer.getvolume()[0]
if "increase" in voice_data:
alina_speak("Increasing Volume")
new_volume = min(100, current_volume+5)
mixer.setvolume(new_volume)
if "decrease" in voice_data:
alina_speak("Decreasing Volume")
new_volume = max(0, current_volume-5)
mixer.setvolume(new_volume)
if "%" in voice_data:
percentage = int(voice_data.split("%")[0].split(" ")[-1])
percentage = min(max(0, percentage), 100)
alina_speak(f"Setting volumem to {percentage}%")
mixer.setvolume(percentage)
if __name__ == "__main__":
# Delay for one second
time.sleep(1)
alina_speak("How can I help you?")
while True:
voice_data = record_audio()
respond(voice_data)