Python Voice Assistant

PYTHON PROGRAMMING LANGUAGE PROJECT 23. 

JARVIS AI

code: 

# This is Voice Assestaint or VA combined with a ai tool
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
from googlesearch import search
import random
from pydub import AudioSegment
from pydub.playback import play



startsong = AudioSegment.from_wav("start.wav")


engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)


def speak(audio):
    engine.say(audio)
    engine.runAndWait()


def wishMe():

    hour = datetime.datetime.now().hour
    if 0 <= hour < 12:
        play(startsong), speak("Good Morning Sir")

    elif 12 <= hour < 18:
        # play(startsong)
        play(startsong), speak("Good Afternoon Sir")
    else:
       # play(startsong)
        play(startsong), speak("Good Evening Sir")

    speak("I 'am Jarvis. How can i help you.")


def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 4
        audio = r.listen(source, phrase_time_limit=6)
        print("Pass1")

    try:
        print("Recognizing...")
        query = r.recognize_google(audio, language='en-in')
        print("pass 2")
        print(f"Boss said: {query}\n")

    except Exception as e:
        print("Sorry Boss, I couldn't understand that.")
        speak("Sorry Boss , I couldn't understand that. Please type your command.")
        query = input("Enter your command: ")

    return query


def playMusic(directory):
    music_dir = directory
    songs = os.listdir(music_dir)
    random.shuffle(songs)  # Shuffle the list of songs
    for song in songs:
        if song.endswith(".mp4" or ".mp3"):
            song_path = os.path.join(music_dir, song)
            os.startfile(song_path)
            break  # Play the first song and exit the loop


if __name__ == "__main__":
    wishMe()
    while True:
        query = takeCommand().lower()

        if 'wikipedia' in query:
            speak('Searching Wikipedia...')
            query = query.replace("wikipedia", "")
            try:
                results = wikipedia.summary(query, sentences=10)
                speak("According to Wikipedia")
                print(results)
                speak(results)
            except wikipedia.exceptions.PageError:
                speak("Sorry, I could not find any relevant information.")

        elif 'open youtube' in query:
            speak("Opening, youtube Boss")
            webbrowser.open("https://www.youtube.com")

        elif 'open google' in query:
            webbrowser.open("https://www.google.com")

        elif 'open stackoverflow' in query:
            webbrowser.open("https://www.stackoverflow.com")

        elif 'open aternos' in query:
            webbrowser.open("https://aternos.org/server/")
            speak("Opening Aternos Sir")

        elif 'open instagram' in query:
            webbrowser.open("https://www.instagram.com")
            speak("Opening Instagram Sir")

        elif 'my instagram' in query:
            webbrowser.open("https://www.instagram.com/sr.ijan__/")
            speak("Opening your instagram Profile sir")

        elif 'open tiktok' in query:
            webbrowser.open("https://www.tiktok.com")
            speak("opening tiktok sir")

        elif 'my tiktok' in query:
            webbrowser.open("https://www.tiktok.com/@lfg_sik")
            speak("opening your tiktok profile sir")

        elif 'the time' in query:
            strTime = datetime.datetime.now().strftime("%H:%M:%S")
            speak(f"Sir, the time is {strTime}")

        elif 'code' in query:
            codePath = "C:\\Program Files\\Microsoft VS Code\\Code.exe"
            os.startfile(codePath)

        elif 'episode 1 of doctor stranger' in query:
            episodePath = "D:\Dramas\Doctor Stranger\episode1.mp4"
            os.startfile(episodePath)

        elif 'episode 1 part 2 of doctor stranger' in query:
            episodePath = "D:\Dramas\Doctor Stranger\episode1part2.mp4"
            os.startfile(episodePath)

        elif 'episode 1 part 3 of doctor stranger' in query:
            episodePath = "D:\Dramas\Doctor Stranger\episode1part3.mp4"
            os.startfile(episodePath)

        elif 'episode 1 part 4 of doctor stranger' in query:
            episodePath = "D:\Dramas\Doctor Stranger\episode1part4.mp4"
            os.startfile(episodePath)

        elif '1 part 2 of doctor stranger' in query:
            episodePath = "D:\Dramas\Doctor Stranger\episode1part2.mp4"
            os.startfile(episodePath)

        elif 'search for' in query:
            # Perform a Google search with the query
            speak("Searching Google...")
            query = query.replace("search for", "")
            search_results = list(search(query, num=1, stop=1))

            if search_results:
                webbrowser.open(search_results[0])
                speak("Here is what I found on Google.")
            else:
                speak("Sorry, I couldn't find any relevant information on Google.")

        elif 'stop jarvis' in query:
            exit()

        elif 'who are you' in query:
            speak("I 'am Jarvis Sir. ")
        elif 'hi' in query:
            speak("Hi Boss, how are you")  

        elif 'i am fine' in query:
            speak("good to hear that. What Should i do sir")    

        elif 'play music' or 'next song' or 'another music ' or 'play random song' in query:
            # Specify the path to your music directory
            music_directory = 'D:\Downloads (videos)'
            playMusic(music_directory)
            speak("Playing random music.")

        elif 'brave' or 'Brave' or 'Brave browser' in query:
            bravepath = "C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"
            os.startfile(bravepath)
            speak("opening brave boss")

        elif 'chat gpt' or 'Chat GPT' or 'Chat gpt' or 'Chat G P T' or 'open ai ' in query:
            webbrowser.open_new("https://www.chat.openai.com")


Note: This program is still uncompleted. it will be completed in few month. I have made the Frontend for it. I will try my best to make it as a GUI compatible program soon. if you want add simple gui use pygui or pygame modules to make gui. Also you should make some changes in the directory of some places, like music , Browser location. It is not necessary to use the drama location part you can remove it.

USE ELIF FUNCTION TO MAKE IT DO ANYTHING. 

IF YOU NEED TO ADD ANY FEATURES JUST WRITE THE FUNCTION ALONG WITH OTHER DEF. 


to run it please install the following python library. 


Pyttsx3 : pip install pytts3

voice recognition: Pip install voice_recognizer

Wikipedia: pip install wikipedia

Pydub : Pip install pydub

webbrowser ; IT MAY BE INSTALLED DEFAULTLY. THEN ALSO  USE THE FOLLOWING CODE pip install webbrowser. 



The intregation of chatgpt is still in incompleted stage the code will be updated weekly alogng with download files. 


Download the jarvis va with other projects:

GoogleDrive: Download


Video:


SOON><



DEMO It may not work in some cases so copy the code and install the modules in Vscode to make it function


 PLESE NOTE THAT THIS PROGRAM IS NOT FULLY DEVELOPED BY ME. IT IS A MODIFIED VERSON WHICH IS MORE ACCESSABLE TO ALL OF YOU TO MODIFY IT FREELY. YOU CAN ADD MORE FEATURES IN IT. UNLIKE THE REAL ONE. 


Popular Posts