-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestcode.py
More file actions
132 lines (98 loc) · 3.43 KB
/
Copy pathtestcode.py
File metadata and controls
132 lines (98 loc) · 3.43 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
import speech_recognition as sr
import webbrowser
import pyttsx3
import musicLibrary
import requests
import pygame
from openai import OpenAI
from gtts import gTTS
recognizer = sr.Recognizer()
engine = pyttsx3.init()
# newsapi = "8c0cc0e71e4b48b69581333ba2464649"
def sepak_old(text):
print("Jarvis:", text) # ✅ added for debug
engine.setProperty('rate', 150) # speed (lower = slower)
engine.setProperty('volume', 1) # max volume
engine.say(text)
engine.runAndWait()
# new speak function
def sepak(text):
tts = gTTS(text)
tts.save('temp.mp3')
# initalize pyagem mixer
pygame.mixer.init()
# load the mp3 files
pygame.mixer.music.load('temp.mp3')
# play the mp3 file
pygame.mixer.music.play()
# keep the progtam running until the musci stops playing
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
def aiproccess(command):
try:
# client = OpenAI(api_key="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")
response = client.responses.create(
model="gpt-5-nano",
input=command
)
# ✅ safer response handling
if hasattr(response, "output_text") and response.output_text:
return response.output_text
elif response.output:
return response.output[0].content[0].text
else:
return "I didn't understand."
except Exception as e:
print("AI Error:", e)
return "AI is not available right now."
def processCommand(c):
c = c.lower()
if "open google" in c:
webbrowser.open("https://www.google.com")
elif "open facebook" in c:
webbrowser.open("https://www.facebook.com")
elif "open youtube" in c:
webbrowser.open("https://www.youtube.com")
elif "open linkedin" in c:
webbrowser.open("https://www.linkedin.com")
elif c.startswith("play"):
parts = c.split(" ")
if len(parts) > 1:
song = parts[1]
link = musicLibrary.music.get(song)
if link:
webbrowser.open(link)
else:
sepak("Song not found")
elif "news" in c:
r = requests.get(
f"https://newsapi.org/v2/top-headlines?country=in&apiKey={newsapi}"
)
if r.status_code == 200:
articles = r.json().get("articles", [])
for article in articles[:5]:
sepak(article["title"])
else:
# ✅ EXACT FLOW YOU WANTED
output = aiproccess(c)
sepak(output)
if __name__ == '__main__':
sepak('initializing Jarvis.....')
while True:
try:
with sr.Microphone() as source:
print("Listening...")
audio = recognizer.listen(source, timeout=2, phrase_time_limit=2)
word = recognizer.recognize_google(audio)
print("Heard:", word)
if word.lower() == 'jarvis':
sepak('Yes')
with sr.Microphone() as source:
print("Jarvis Active...")
audio = recognizer.listen(source)
command = recognizer.recognize_google(audio)
print("command:", command)
# ✅ FLOW START
processCommand(command)
except Exception as e:
print("error:", e)