artificial intelligence personal assistant sample code example

Example: ai virtual assistant

import os
import time
import webbrowser
from datetime import date
import playsound
import speech_recognition as sr
from gtts import gTTS
import wikipedia
from datetime import datetime

WAKE = 'Sam'


def speak111(text):
    tts = gTTS(text=text, lang="en")
    filename = "b.mp3"
    playsound.playsound(filename)


def speak1000(text):
    tts = gTTS(text=text, lang="en")
    filename = "bbvhdzdzhuif.mp3"
    tts.save(filename)
    playsound.playsound(filename)


def speak100(text):
    tts = gTTS(text=text, lang="en")
    filename = "bumakdg.mp3"
    playsound.playsound(filename)


def speak12(text):
    tts = gTTS(text=text, lang="en")
    filename = "dck.mp3"
    playsound.playsound(filename)


def speak2(text):
    tts = gTTS(text=text, lang="en")
    filenam = "vo.mp3"
    playsound.playsound(filenam)


def speak3(text):
    tts = gTTS(text=text, lang="en")
    filenam = "voi.mp3"
    playsound.playsound(filenam)


def speak4(text):
    tts = gTTS(text=text, lang="en")
    filenam = "voic.mp3"
    playsound.playsound(filenam)


def speak5(text):
    tts = gTTS(text=text, lang="en")
    filename = "voic.mp3"
    playsound.playsound(filename)


def speak1(text):
    tts = gTTS(text=text, lang="en")
    filename = "voice1.mp3"
    playsound.playsound(filename)


def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)
        except Exception as e:
            said = None
    return said


def speak(text):
    tts = gTTS(text=text, lang="en")
    filename = "v.mp3"
    tts.save(filename)
    playsound.playsound(filename)


while True:
    text = get_audio()
    if text is not None:
        if "hello" == text:
            speak("hello, how are you?")
            print("hello, how are you?")
        elif "what is your name" == text:
            speak2("My name is Sam")
            print("My name is Sam")
        elif 'open YouTube' == text:
            webbrowser.open_new_tab('https://www.youtube.com/')
        elif 'tell me the news' == text:
            webbrowser.open_new_tab('https://www.bbc.co.uk/newsround')
        elif 'open my maps' == text:
            webbrowser.open_new_tab('https://www.mymaths.co.uk/')
        elif 'open HW' == text:
            webbrowser.open_new_tab('https://www.satchelone.com/todos/issued')
        elif 'open mail' == text:
            webbrowser.open_new_tab('https://outlook.office365.com/mail/inbox')
        elif 'what is the weather' == text or 'tell me the weather' == text or 'tell me the weather in barkingside' == text:
            webbrowser.open_new_tab(
                'https://www.google.com/search?q=weather&oq=weather&aqs=chrome..69i57j0j69i59l2j0l4.2886j1j7&sourceid=chrome&ie=UTF-8')
        elif 'open' == text:
            webbrowser.open('https://www.google.com/search?q=gmail')
        elif 'tell me the time' == text:
            now = datetime.now()
            current_time = now.strftime("%H:%M")
            print("Current Time =", current_time)
            speak('the time is' + current_time)
        elif 'tell me the date' == text or 'what is the time' == text:
            today = date.today()
            print("Today's date:", today)
        elif 'Google' in text:
            speak12('searching Google')
            print('searching Google')
            text = text.replace(' on Google', '')
            webbrowser.open_new_tab('https://www.google.com/search?q={}'.format(text))
        elif 'YouTube' in text:
            speak111('searching Youtube')
            print('searching Youtube')
            text = text.replace('on YouTube', '')
            webbrowser.open_new_tab('https://www.Youtube.com/search?q={}'.format(text))
        elif 'play' in text:
            speak111('searching Youtube')
            print('searching Youtube')
            text = text.replace('play', '')
            webbrowser.open_new_tab('https://www.Youtube.com/search?q={}'.format(text))
        elif 'Wikipedia' in text:
            speak3('searching wikipedia')
            print('searching wikipedia')
            text = text.replace('wikipedia', '')
            results = wikipedia.summary(text, sentences=2)
            speak4(results)
            print(results)