-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonAssistant.py
More file actions
116 lines (95 loc) · 4.19 KB
/
PythonAssistant.py
File metadata and controls
116 lines (95 loc) · 4.19 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
import speech_recognition as sr
import pyttsx3
import webbrowser
from datetime import datetime
import subprocess
import platform
import os
# Initialize the speech recognizer
r = sr.Recognizer()
# Initialize the text-to-speech engine
engine = pyttsx3.init()
# Function to listen to user's voice command
def get_Command():
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=5)
print("Listening...")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
text = text.lower()
print("You said: ", text)
return text
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print(
f"Could not request results from Google Speech Recognition service; {e}")
# Function to speak the response
def speak(text):
engine.say(text)
engine.runAndWait()
# Intro Screen
print("===================================== Virtual Assistant =====================================")
print("* *")
print("* *")
print("==================================================================== By: Tarun Singh Chauhan =====")
# Main loop
while True:
command = get_Command()
if command:
if command == "hello":
print("Hello! How can I assist you?")
speak("Hello! How can I assist you?")
elif command == "who are you":
print("I am your virtual assistant.")
speak("I am your virtual assistant.")
elif command == "what can you do" or command == "help" or command == "introduce yourself":
print("I can open Google, open Terminal, open Visual Studio Code, open Spotify, open Whatsapp, open YouTube, search for anything on Google, tell you the current time and say goodbye.")
speak("I can open Google, open Terminal, open Visual Studio Code, open Spotify, open Whatsapp, open YouTube, search for anything on Google, tell you the current time and say goodbye.")
elif command == "goodbye":
print("Goodbye!")
speak("Goodbye!")
break
elif command == "open google":
webbrowser.open("https://www.google.com")
print("Opening Google.")
speak("Opening Google.")
elif "open terminal" in command:
print("Opening Terminal.")
speak("Opening Terminal.")
if platform.system() == "Windows":
os.system("start cmd /k")
elif platform.system() == "Linux":
subprocess.Popen(["gnome-terminal"])
elif "open vs code" in command:
subprocess.Popen(["code"])
print("Opening Visual Studio Code.")
speak("Opening Visual Studio Code.")
elif "open spotify" in command:
subprocess.Popen(["spotify"])
print("Opening Spotify.")
speak("Opening Spotify.")
elif "whatsapp" in command:
webbrowser.open("https://web.whatsapp.com")
print("Opening Whatsapp.")
speak("Opening Whatsapp.")
elif "open youtube" in command:
webbrowser.open("https://www.youtube.com")
print("Opening YouTube.")
speak("Opening YouTube.")
elif "search for" in command:
query = command.replace("search for", "").strip()
webbrowser.open(f"https://www.google.com/search?q={query}")
print(f"Searching for {query}.")
speak(f"Searching for {query}.")
elif "what time is it" in command or command == "time":
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
speak(f"The time is {now}.")
print(f"The time is {now}.")
else:
print("Sorry, I don't understand that command.")
print("Opening Google.")
webbrowser.open("https://www.google.com")
speak("Sorry, I don't understand that command.")
speak("Opening Google.")