-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJarvis.py
More file actions
172 lines (145 loc) Β· 5.9 KB
/
Jarvis.py
File metadata and controls
172 lines (145 loc) Β· 5.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import speech_recognition as sr
import pyttsx3
import requests
import json
import re
import os
import subprocess
import webbrowser
# Configuration
API_KEY = "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"
API_URL = "https://openrouter.ai/api/v1/chat/completions"
HEADERS = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
"HTTP-Referer": "https://your-site-or-project.com",
"X-Title": "Jarvis Assistant"
}
# Initialize
engine = pyttsx3.init()
engine.setProperty("rate", 180)
recognizer = sr.Recognizer()
microphone = sr.Microphone()
def callback(recognizer, audio):
try:
command = recognizer.recognize_google(audio)
if command.lower() == "stop":
engine.stop()
print("Speech stopped by user")
except:
pass
stop_listening = recognizer.listen_in_background(microphone, callback)
def speak(text, allow_interruption=False):
if allow_interruption:
stop_listening(wait_for_stop=False)
stop_listening_new = recognizer.listen_in_background(microphone, callback)
engine.say(text)
engine.runAndWait()
if allow_interruption:
stop_listening_new(wait_for_stop=False)
def listen():
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source)
print("π€ Listening...")
audio = recognizer.listen(source)
try:
print("π Recognizing...")
query = recognizer.recognize_google(audio)
print("You said:", query)
return query
except:
return None
def clean_response(text):
# Remove escape characters
text = text.encode('utf-8').decode('unicode_escape') # Converts \n, \t, etc. to real characters
text = text.replace("\\", "") # Remove any remaining backslashes
# Remove markdown and special characters
text = re.sub(r'[*_#>\[\]{}|]', '', text)
text = re.sub(r'\s{2,}', ' ', text)
text = text.strip()
# Extract only the first sentence for speaking
sentences = re.split(r'(?<=[.!?]) +', text)
short_summary = sentences[0] if sentences else text
return short_summary
def chat_with_deepseek(prompt):
try:
data = {
"model": "deepseek/deepseek-r1-zero:free",
"messages": [
{
"role": "system",
"content": (
"You are Jarvis, an intelligent AI assistant. Speak clearly and briefly. "
"Always reply in a short, voice-friendly format. Avoid code, markdown, and long paragraphs."
)
},
{"role": "user", "content": prompt}
]
}
response = requests.post(API_URL, headers=HEADERS, data=json.dumps(data))
if response.status_code == 200:
result = response.json()
raw_answer = result["choices"][0]["message"]["content"]
print("π§ AI Raw Response:", raw_answer)
return clean_response(raw_answer)
else:
print("β API Error:", response.status_code, response.text)
return "Sorry, I couldn't get a response from the AI."
except Exception as e:
print("β Exception:", str(e))
return "There was a problem connecting to the AI."
# Control Functions
def shutdown():
speak("Shutting down the system.")
os.system("shutdown /s /t 1")
def open_chrome():
speak("Opening Chrome.")
path = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
subprocess.Popen([path])
def search_google(query):
speak(f"Searching Google for {query}")
url = f"https://www.google.com/search?q={query}"
webbrowser.open(url)
def open_folder(folder_name):
speak(f"Opening folder {folder_name}")
folder_path = os.path.join("C:\\Users\\YourUsername\\", folder_name) # β Change this to your actual Windows username
if os.path.exists(folder_path):
os.startfile(folder_path)
else:
speak("Sorry, I can't find that folder.")
# Main Loop
if __name__ == "__main__":
speak("Hello, I am Jarvis. Say 'Jarvis' to activate me.", allow_interruption=False)
while True:
print("π Waiting for wake word 'Jarvis'...")
wake_input = listen()
if wake_input and "jarvis" in wake_input.lower():
speak("Yes? What would you like me to do?", allow_interruption=False)
command = listen()
if command:
command_lower = command.lower()
if any(word in command_lower for word in ["exit", "quit", "stop", "bye"]):
speak("Goodbye! Have a great day.", allow_interruption=False)
break
elif "shutdown" in command_lower:
shutdown()
break
elif "open chrome" in command_lower:
open_chrome()
elif "search for" in command_lower or "google" in command_lower:
search_query = command_lower.replace("search for", "").replace("google", "").strip()
if search_query:
search_google(search_query)
else:
speak("What should I search for?")
elif "open folder" in command_lower:
folder = command_lower.replace("open folder", "").strip()
open_folder(folder)
else:
response = chat_with_deepseek(command)
if response:
speak(response, allow_interruption=True)
else:
speak("I couldn't understand the response.", allow_interruption=False)
else:
speak("Sorry, I didn't catch that.", allow_interruption=False)