-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
147 lines (126 loc) · 4.9 KB
/
main.py
File metadata and controls
147 lines (126 loc) · 4.9 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
139
140
141
142
143
144
145
146
147
import speech_recognition as sr
import webbrowser
import pyttsx3
import requests
from googleapiclient.discovery import build
recognizer = sr.Recognizer()
engine = pyttsx3.init()
newsapi = "7dc0c60be8e143ac8742fe2c02b03762"
YOUTUBE_API_KEY = "enter_youtube_api_key"
def speak(text, min_words=50):
words = text.split()
if len(words) <= min_words:
short_text = text
else:
short_text = " ".join(words[:min_words])
for word in words[min_words:]:
short_text += " " + word
if word.endswith(('.', '!', '?')):
break
engine.say(short_text)
engine.runAndWait()
def aiProcess(command):
try:
response = requests.post(
"http://localhost:11434/api/generate",
json={
"model": "llama2",
"prompt": f"Answer briefly in 4-5 sentences: {command}",
"stream": False
}
)
return response.json()['response']
except Exception as e:
print("Ollama error:", e)
return "There was an error talking to the AI model"
def youtube_search(query):
"""Searches for a YouTube video and returns the URL of the first result."""
try:
# Build the YouTube API service object.
youtube_service = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
# Make the search request.
request = youtube_service.search().list(
q=query,
part="snippet",
maxResults=1,
type="video"
)
response = request.execute()
# Extract the video ID and construct the URL.
if response['items']:
video_id = response['items'][0]['id']['videoId']
video_url = f"https://www.youtube.com/watch?v={video_id}"
return video_url
else:
return None
except Exception as e:
print(f"Youtube error: {e}")
return None
def processCommand(c):
print(f"Processing command: {c}")
if "open google" in c.lower():
webbrowser.open("https://google.com")
elif "open facebook" in c.lower():
webbrowser.open("https://facebook.com")
elif "open youtube" in c.lower():
webbrowser.open("https://youtube.com")
elif "open linkedin" in c.lower():
webbrowser.open("https://linkedin.com")
elif "open spotify" in c.lower():
webbrowser.open("https://spotify.com")
elif c.lower().startswith("play"):
song_query = c.lower().replace("play", "").strip()
if song_query:
speak(f"Searching for {song_query} on YouTube.")
video_url = youtube_search(song_query)
if video_url:
webbrowser.open(video_url)
speak(f"Playing {song_query} on YouTube.")
else:
speak("I couldn't find that video on YouTube.")
else:
speak("Please tell me what you would like to play.")
elif "news" in c.lower() or "tell me news" in c.lower():
try:
url = f"https://newsapi.org/v2/top-headlines?country=us&apiKey={newsapi}"
print(f"Fetching news from: {url}")
r = requests.get(url)
if r.status_code == 200:
data = r.json()
articles = data.get('articles', [])
if not articles:
speak("No news articles found")
return
speak("Here are the latest news headlines")
for i, article in enumerate(articles[:5]):
if article['title']:
speak(f"Headline {i+1}: {article['title']}")
else:
speak("No title for this article")
else:
speak(f"Failed to fetch news. Status code: {r.status_code}")
except Exception as e:
print(f"News API error: {e}")
speak("Speak, I encountered an error while fetching the news")
else:
output = aiProcess(c)
speak(output)
if __name__ == "__main__":
speak("Initializing Nova....")
while True:
r = sr.Recognizer()
print("Recognizing...")
try:
with sr.Microphone() as source:
print("Listeninging...")
audio = r.listen(source, timeout=5, phrase_time_limit=5)
word = r.recognize_google(audio)
if(word.lower() == "nova"):
speak("Ya")
with sr.Microphone() as source:
print("Nova active...")
audio = r.listen(source, timeout=8, phrase_time_limit=10)
command = r.recognize_google(audio)
processCommand(command)
except Exception as e:
print("Error; {0}".format(e))