-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvAssistant.py
More file actions
120 lines (100 loc) · 4.08 KB
/
vAssistant.py
File metadata and controls
120 lines (100 loc) · 4.08 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
import speech_recognition as sr
import pyttsx3
import webbrowser
import requests
import musiclibrary
from openai import OpenAI, completions
# Initialize speech recognition and text-to-speech engine
recognizer = sr.Recognizer()
engine = pyttsx3.init()
newsapi = "260f9618674d45bcb17b80292ffd9908"
# Function to speak text
def speak(text):
engine.say(text)
engine.runAndWait()
def AIprocess(Command):
client = OpenAI( api_key="<Your Key Here>",
)
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a virtual assistant named Alex skilled in general tasks like Alexa and Google Cloud"},
{"role": "user", "content": "command"}
]
)
return completion.choices[0].message.content
# Function to process commands
def processCommand(c):
c = c.lower()
print(f"Processing command: {c}")
if "open google" in c:
print("Opening Google...")
webbrowser.open("https://google.com")
elif "open facebook" in c:
print("Opening Facebook...")
webbrowser.open("https://facebook.com")
elif "open youtube" in c:
print("Opening YouTube...")
webbrowser.open("https://youtube.com")
elif "open linkedin" in c:
print("Opening LinkedIn...")
webbrowser.open("https://linkedin.com")
elif c.startswith("play"):
song = c.split(" ", 1)[-1]
if song in musiclibrary.music:
link = musiclibrary.music[song]
webbrowser.open(link)
speak(f"Playing {song}")
else:
speak(f"Sorry, I couldn't find the song {song}.")
elif "news" in c:
try:
print(f"Fetching news for command: {c}")
r = requests.get(f"https://newsapi.org/v2/everything?q=world&apiKey={newsapi}")
print(f"Status code: {r.status_code}")
print(f"Response: {r.text}")
if r.status_code == 200:
data = r.json()
articles = data.get('articles', [])
if articles:
speak("Here are the top news headlines:")
for article in articles[:3]:
speak(article['title'])
else:
speak("No news articles found.")
else:
speak("Failed to fetch news.")
except Exception as e:
speak(f"An error occurred while fetching news: {e}")
else:
output = AIprocess(c)
speak(output)
# Main Program
if __name__ == "__main__":
speak("Hello, I am your virtual assistant 'Alex'. How can I help you today?")
while True:
with sr.Microphone() as source:
print("Listening for 'Alex'...")
try:
audio = recognizer.listen(source, timeout=10, phrase_time_limit=5)
command = recognizer.recognize_google(audio)
print(f"You said: {command}")
# Check for Alex trigger
if "alex" in command.lower():
speak("Yes, Alex here! What can I do for you?")
# Listen for the actual command after Alex trigger
print("Listening for your command...")
audio = recognizer.listen(source, timeout=10, phrase_time_limit=5)
user_command = recognizer.recognize_google(audio)
print(f"Command received: {user_command}")
processCommand(user_command)
else:
print("No trigger word detected.")
except sr.WaitTimeoutError:
print("Listening timed out. Please speak again.")
except sr.UnknownValueError:
print("Sorry, I didn't catch that.")
except sr.RequestError as e:
print(f"Request failed: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")