0% found this document useful (0 votes)
50 views5 pages

Jarvis

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views5 pages

Jarvis

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

import pyttsx3

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 send_email(receiver_email, subject, body):


try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('[email protected]', 'your_password') # Use environment
variables for security
message = f"Subject: {subject}\n\n{body}"
server.sendmail('[email protected]', receiver_email, message)
server.close()
speak("Email has been sent successfully!")
except Exception as e:
print(e)
speak("Sorry, I am unable to send the email at the moment.")

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

if 'hello' in query or 'hi' in query:


speak("Hello, how can I assist you today?")

elif 'how are you' in query:


speak("I am functioning within normal parameters, thank you for
asking!")

elif 'open youtube' in query:


open_website("https://www.youtube.com")

elif 'open google' in query:


open_website("https://www.google.com")

elif 'open whatsapp' in query:


open_website("https://web.whatsapp.com")

elif 'search' in query:


search_query = query.replace("search", "")
open_website(f"https://www.google.com/search?q={search_query}")

elif 'wikipedia' in query:


search_wikipedia(query)

elif 'open stack overflow' in query:


open_website("https://stackoverflow.com")
elif 'what is the date' in query:
get_date()

elif 'what is the time' in query:


get_time()

elif 'weather' in query:


get_weather()

elif 'send email' in query:


try:
speak("What is the receiver's email address?")
receiver_email = take_command().lower()
if receiver_email == "none":
continue
speak("What is the subject of the email?")
subject = take_command()
if subject == "none":
continue
speak("What should I say in the email?")
body = take_command()
if body == "none":
continue
send_email(receiver_email, subject, body)
except Exception as e:
print(e)
speak("Sorry, I am unable to send the email at the moment.")

elif 'what is my name' in query:


speak("Your name is Moksh Arora.")

elif 'where am i studying' in query:


speak("You are studying at Dayananda Sagar University.")

elif 'exit' in query or 'bye' in query or 'stop' in query:


speak("Goodbye!")
break

elif 'jarvis can you make this thing' in query or 'create a model' in
query:
attempt_model_creation()

elif 'make' in query or 'create' in query:


search_web(query)

elif 'screen up' in query:


move_screen("up")

elif 'screen down' in query:


move_screen("down")

elif 'screen left' in query:


move_screen("left")

elif 'screen right' in query:


move_screen("right")

elif 'close chrome' in query:


close_application("chrome")
elif 'close whatsapp' in query:
close_application("whatsapp")

elif 'close' in query:


app_name = query.replace("close", "").strip()
close_application(app_name)

else:
speak("I am not able to understand that command. Let me search that for
you.")
search_web(query)

You might also like