Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
virtual assistant
#1
So i'm brand new to coding and have a quick question regarding a virtual assistant i'm trying to make. when it come to using a wake word that activates the program i want it to wait a few seconds before it sees that nothing was said and requires the wake word again a kind of like amazons alexa does for example

when i active the program with the wake word if i dont say a command or anything that has already been programed right away it will say no speak able text found and essentially crash the program requiring me to re run it. but i guess i want it to do two things the first making it so i am able to wait a few second to give it a command with out it crashing. second would be if i use the wake word with out giving it a command such as the wake word on its own it wont crash the program and just go back to constantly listing for the wake word again
Reply
#2
It looks like a matter of program organization. Instead of crashing the program when it doesn't get an expected order, it could loop and go back to a base state where it is waiting for the wake word. You must prepare the program to receive unintelligible requests. I suggest writing activity diagrams and sequence diagrams.
Reply
#3
how would i do that sorry like i said im brand new to coding
Reply
#4
It would be easier to tell what the problem is if we could see your code. Just write out different responses that people may give, and run through your code to see how your program will respond. For example, one of the possible responses is saying nothing, in which case look at the code run through it yourself to see what will happen, and if you see where the program might fail.
Reply
#5
Also I don't think it is a project for a beginner. The structure is probably that of event driven programming. You could learn this by programming GUIs such as Tkinter.

One can find python projects such as B.E.N.J.I, "A digital assistant for your device that uses speech-recognition to obey your commands", or some PyQT projects that record the microphone. Studying these programs could give you some ideas.
Reply
#6
here is my entire project thanks in advance guys and i have taken the ip for the hue bridge off for the post so it might effect the order

# Imported librays
import speech_recognition as sr
import os
from gtts import gTTS
import datetime
import warnings
import calendar
import random
import wikipedia
import re
import webbrowser
from phue import Bridge

#hue lights
def rgb_to_xy(red, green, blue) -> object:

    red = pow((red + 0.055) / (1.0 + 0.055), 2.4) if red > 0.04045 else (red / 12.92)
    green = pow((green + 0.055) / (1.0 + 0.055), 2.4) if green > 0.04045 else (green / 12.92)
    blue = pow((blue + 0.055) / (1.0 + 0.055), 2.4) if blue > 0.04045 else (blue / 12.92)

    # convert rgb to xyz
    x = red * 0.649926 + green * 0.103455 + blue * 0.197109
    y = red * 0.234327 + green * 0.743075 + blue * 0.022598
    z = green * 0.053077 + blue * 1.035763

    # convert xyz to xy
    x = x / (x + y + z)
    y = y / (x + y + z)

    return [x, y]

# truned off (with comment for now) b = Bridge()
# If the app is not registered and the button is not pressed, press the button and call connect() (this only needs to be run a single time)
b.connect()
# Get the bridge state (This returns the full dictionary that you can explore)
b.get_api()
# ignore any warning messages
warnings.filterwarnings('ignore')

# record audio and return it as a string
def recordaudio():
    #record audio
    r = sr.Recognizer()  # creating recognizer
    # open microphone and start recording
    with sr.Microphone() as source:
        print('listening:')# for testing perpouses

        #r.pause_threshold = .7

        audio = r.listen(source)
    # use google speech recegnizor
    data = '' #the audio that is said
    try:
        data = r.recognize_google(audio)
        print('you said: '+data) # 'you said' + the data that was said
    except sr.UnknownValueError:
        print('google speech recognition could not understand the audio, unknown error')
    except sr.RequestError as e:
        print('request results from google speech recognition service error'+ e)
    return data

#function to get the AI response
def assistantResponse (text):
    print(text)
    #convert text to speech
    myobj = gTTS(text= text, lang='en', slow=False)
    #save converted audio to file
    myobj.save('assistant_response.mp3')
    #play mp3 file
    os.system('start assistant_response.mp3')

#what is your name
def name(text):
    WAKE_WORD = ['what is your name']
    text = text.lower()
    for phrase in WAKE_WORD:
        if phrase in text:
            return True
    return False

#creating a function for wake word(s) or phrase
def wakeWord(text):
    WAKE_WORDS = ['eta', 'ada', 'hey ada', 'hi ada', 'aida', 'haida',] #list of wake words
    text = text.lower() #converting text to all lower case words
    #check to see if the users command or text contains a wake word/phrase
    for phrase in WAKE_WORDS:
        if phrase in text:
            return True
    #if the wake word isnt found in the text from the loop it returns false
    return False

# a function to get current date
def getDate():
    now = datetime.datetime.now()
    my_date = datetime.datetime.today()
    weekday = calendar.day_name[my_date.weekday()]#e.g. Friday
    dayNum = now.day
    #a list of months
    month_names = ['January', 'February', 'March', 'April', 'may', 'June', 'July', 'August', 'September',
                   'October', 'November','December']
    #a list of ordinal numbers
    ordinalNumbers = ['1st', '2nd' '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th', '11th', '12th',
                    '13th', '14th', '15th', '16th', '17th', '18th', '19th', '20th', '21st', '22nd', '23rd',
                    '24th', '25th', '26th', '27th', '28th', '29th', '30th', '31st']
    return 'Today is '+weekday+' '+ month_names[7]+' the '+ ordinalNumbers[20]+'. '

# a function to return a random greeting response
def greeting(text):
    GREETING_INPUTS = ['hi', 'hey', 'hello', 'greetings',]
    GREETING_RESPONSES = ['hi', 'hello how are you', 'hello', 'hi how are you']
    #if the users input is a greeting then return a randomly chosen greeting response
    for word in text.split():
        if word.lower() in GREETING_INPUTS:
            return random.choice(GREETING_RESPONSES) +'.'
    #if no greeting is detected then return an empty string
    return ''

# a function to get a first and last name form the text
def getPerson(text):
    wordlist = text.split() #spliting text into a list of words
    for i in range (0, len(wordlist)):
        if i + 3 <= len(wordlist) - 1 and wordlist[i].lower() == 'who' and wordlist[i+1].lower() =='is':
            return wordlist[i+2] + ' '+ wordlist[i+3]

#always listen to wake word
while True:
    #record the audio
    text = recordaudio()
    response = ''
    #start check for wake word or phrase
    if(wakeWord(text) or name(text) == True): #added or name(text)
        #check for greeting by user
        response = response + greeting(text)
        #name
        if ('what is your name' in text):
            response = 'eta'
        #date
        if('date' in text):
            get_date = getDate()
            response = response + ' '+get_date
        #time
        if('time' in text):
            now = datetime.datetime.now()
            meridiem =''
            if now.hour >=12:
                meridiem = 'p.m.'
                hour = now.hour - 12
            else:
                meridiem = 'a.m.'
                hour = now.hour
            # convert minuite to proper string
            if now.minute < 10:
                minute = '0'+str(now.minute)
            else:
                minute = str(now.minute)
            response = response +' '+'it is '+str(hour)+ ':'+ minute+ ' '+meridiem+' .'
        #person 'who'
        if('who is' in text):
            Person = getPerson(text)
            wiki = wikipedia.summary(Person, sentences=2)
            response = response +' '+ wiki
        #how are you
        if ('how are you' in text):
            response = 'very well thank you'
    #webpages
        #google
        if ('open Google' in text):
            response = 'google opening'
            webbrowser.open("google.com")
        #youtube
        if ('open YouTube' in text):
            response = 'YouTube opening'
            webbrowser.open("youtube.com")

        if ('lights red' in text):
            response = 'okay'
            xy = rgb_to_xy(1.0, 0.28627, 0.95686)
            lights = b.get_light_objects('00:17:88:01:03:81:86:92')

            b.get_light_id_by_name('00:17:88:01:03:81:86:92')
            b.set_light('00:17:88:01:03:81:86:92', 'on', True)
            b.set_light('00:17:88:01:03:81:86:92', 'bri', 1)
            lights = b.get_light_objects()

            for light in lights:
                light.brightness = 255
                light.xy = [.9, .1]




        #have the assisstant respond back having audio and the text form response
        assistantResponse(response)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Virtual Assistant code is looping back in python ShishirModi 1 2,556 Oct-13-2020, 08:04 AM
Last Post: Nickd12
  voice assistant python based pyaudio error hardik 6 5,128 Oct-06-2017, 07:29 PM
Last Post: hardik

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020