-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_main.py
More file actions
116 lines (92 loc) · 2.99 KB
/
01_main.py
File metadata and controls
116 lines (92 loc) · 2.99 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
import speech_recognition as sr
import webbrowser
import pyttsx3
import musicLibrary
import requests
from gtts import gTTS
import pygame
import os
import google.genai as genai
#Pip install pocketsphinx
recognizer = sr.Recognizer()
engine = pyttsx3 .init()
newsapi = "f97d71eaa7ef4f298c0e8f2f58d78067"
def speak (text):
engine.say(text)
engine.runAndWait()
def aiprocess(command):
try:
client = genai.Client(api_key="Enter-Your-API-Key-Here")
response = client.models.generate_content(
model="gemini-3-flash-preview",
contents=command,
)
reply = response.text
print("Jarvis:", reply)
speak_gtts(reply)
return reply
except Exception as e:
print("AI Error:", e)
speak_gtts("Sorry, I could not process that.")
return None
def speak_gtts(text):
tts = gTTS(text)
tts.save("temp.mp3")
pygame.mixer.init()
pygame.mixer.music.load("temp.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.wait(100)
pygame.mixer.music.unload()
os.remove("temp.mp3")
def processcommand(c):
if "open google" in c.lower():
webbrowser.open("http://google.com")
elif "open facebook" in c.lower():
webbrowser.open("http://facebook.com")
elif "open youtube" in c.lower():
webbrowser.open("http://youtube.com")
elif "open linkedin" in c.lower():
webbrowser.open("https://linkedin.com")
elif c.lower().startswith("play"):
song = c.lower().split(" ")[1]
link = musicLibrary.music[song]
webbrowser.open(link)
elif "news" in c.lower():
r = requests.get(f"https://newsapi.org/v2/top-headlines?country=us&apiKey={newsapi}")
if r.status_code == 200:
#parse the json response
data = r.json()
#Extract the articles
articles = data.get('articles' , [])
#print the headlines
for article in articles:
speak_gtts(article['title'])
else:
aiprocess(c)
if __name__ == "__main__":
speak("Initializing Jarvis...")
while True:
#Listen to the wake word 'Jarvis'
#obtain audio from the microphone
r = sr.Recognizer()
print("recognize....")
try:
with sr.Microphone()as source:
print("Listening.....")
audio = r.listen(source , timeout=2 , phrase_time_limit=1)
word = r.recognize_google(audio)
if (word.lower() == "jarvis"):
speak_gtts("Ya")
#Listen for command
try:
with sr.Microphone()as source:
print("Jarvis Active...")
audio = r.listen(source)
command = r.recognize_google(audio)
print(f"Recognized command: {command}")
processcommand(command)
except Exception as e:
print(f"Command recognition error: {e}")
except Exception as e:
print ("Error; {0}".format(e))