Jarvis
Jarvis
import speech_recognition as sr
import webbrowser
import wikipedia
import datetime
import smtplib
import requests
import json
import pyautogui
import os # Added for system operations
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def take_command():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"User said: {query}\n")
except sr.UnknownValueError:
print("Could not understand audio")
speak("I am not able to understand that command.")
return "None"
except sr.RequestError as e:
print(f"Could not request results from Google Speech Recognition service;
{e}")
speak("I am not able to understand that command.")
return "None"
return query
def open_website(url):
speak(f"Opening {url}")
webbrowser.open(url)
def search_wikipedia(query):
speak("Searching Wikipedia...")
query = query.replace("wikipedia", "")
try:
results = wikipedia.summary(query, sentences=2)
speak("According to Wikipedia")
speak(results)
except wikipedia.exceptions.DisambiguationError:
speak("There are multiple results. Please be more specific.")
except wikipedia.exceptions.PageError:
speak("No information found. Please try another query.")
def get_date():
now = datetime.datetime.now()
date = now.strftime("%A, %B %d, %Y")
speak(f"Today's date is {date}")
def get_time():
now = datetime.datetime.now()
time = now.strftime("%I:%M %p")
speak(f"The current time is {time}")
def get_weather():
api_key = 'your_api_key_here' # Ensure to securely manage your API key
base_url = "http://api.openweathermap.org/data/2.5/weather?"
city_name = "New York" # Replace with user's city or implement location
detection
complete_url = f"{base_url}q={city_name}&appid={api_key}&units=metric"
response = requests.get(complete_url)
data = response.json()
if data["cod"] != "404":
main = data["main"]
temperature = main["temp"]
weather_desc = data["weather"][0]["description"]
speak(f"The temperature in {city_name} is {temperature} degrees Celsius
with {weather_desc}")
else:
speak("City not found. Please try again.")
def create_model():
speak("I will guide you to a website where you can create a model.")
open_website("https://teachablemachine.withgoogle.com/")
def attempt_model_creation():
try:
create_model()
except Exception as e:
print(e)
speak("Sorry, I cannot create a model for you at the moment.")
def search_web(query):
try:
speak(f"Searching the web for {query}")
webbrowser.open(f"https://www.google.com/search?q={query}")
except Exception as e:
print(e)
speak("Sorry, I cannot search the web for you at the moment.")
def move_screen(direction):
if direction == "up":
pyautogui.scroll(100)
elif direction == "down":
pyautogui.scroll(-100)
elif direction == "left":
pyautogui.hscroll(100)
elif direction == "right":
pyautogui.hscroll(-100)
def close_application(app_name):
if "chrome" in app_name.lower():
os.system("taskkill /f /im chrome.exe")
speak("Chrome has been closed.")
elif "whatsapp" in app_name.lower():
os.system("taskkill /f /im whatsapp.exe")
speak("WhatsApp has been closed.")
else:
speak(f"Sorry, I cannot close {app_name}.")
if __name__ == "__main__":
while True:
query = take_command().lower()
if query == "hi jarvis":
speak("Hello, I am your assistant Jarvis. How can I assist you today?")
break
while True:
query = take_command().lower()
if query == "none":
continue
elif 'jarvis can you make this thing' in query or 'create a model' in
query:
attempt_model_creation()
else:
speak("I am not able to understand that command. Let me search that for
you.")
search_web(query)