1

I am trying to make a voice-assistant app using tkinter in Python and I'm having two issues with my code.

A snippet from my code:

def listen_to_me():
    msg2 = Message(root, text="Listening...", bg='yellow', font=('times', 14, 'italic'))
    msg2.pack
    msg2.place(x=200, y=220)

    with sr.Microphone() as source:

        audio = r.listen(source)

        global query

        query = r.recognize_google(audio, language='en-IN', show_all=True)
        if query:
            try:
                (f"[Me]: {query}")                                       
            except:
                engine.say("Sorry didn't quite catch that. Please repeat.")   


    return query



def reply():
    while True:
        global query

        # Logic for executing tasks based on query
        if 'wikipedia' in query:
            speak('Searching Wikipedia...')
            query = query.replace("wikipedia", "")
            query = query.replace("search", "")
            query = query.replace("for", "")
            results = wikipedia.summary(query, sentences=1)
            speak(f'According to Wikipedia, {results}')

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

        elif 'open stack overflow' in query:
            speak('Opening StackOverflow')
            webbrowser.open("https://stackoverflow.com")

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

        elif 'how are you' or 'what\'s up' in query:
            speak('I am doing jolly good, sir.')

Problem-1: I'm getting the output but it seems to be stuck in a loop:

[MyAssistant]: Good evening! I am Jarvis. How may I help you today?
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.

Problem-2: I want to convert my query to the lower-case. I've tried the following:

query = query.lower()

Error:

AttributeError: 'list' object has no attribute 'lower'

Thanks in advance!

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
DC007744
  • 123
  • 7
  • For the first issue, just remove `while True`, for the second one, it should be asked as a different question. – rizerphe Jun 19 '20 at 13:53
  • Does this answer your question? [use threads to preventing main event loop from “freezing”](https://stackoverflow.com/a/16747734/7414759) – stovfl Jun 19 '20 at 16:21
  • Does this answer your question? [While loop not working while using Tkinter](https://stackoverflow.com/questions/29849138/while-loop-not-working-while-using-tkinter) – 10 Rep Jun 19 '20 at 18:24

3 Answers3

2

For your first problem, the error is caused by the line while True. This simply sets in an infinite loop. You would want to structure this in a better way to avoid that in a loop, e.g. replace

while = True

with a

while = (conditional_statement)

Your second problem is unclear given that the output you showed does achieve what you want. But one of the ways you can do is to replace the key and values in the dictionary using a loop, e.g.

for key,val in query['alternative'].items():
    key = key.lower()
    key = val[0].lower()

Above is just an idea of what you have to do in order to replace your query with lower-case characters.

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • The first problem is solved. However, using the code that you suggested for the second problem shows this error - AttributeError: 'list' object has no attribute 'items'. What I intend to do is assign the recognized text to query (in listen_to_me) and use that query variable in reply() so as to enable the voice assistant to perform specific tasks on the basis of what I say (query). – DC007744 Jun 20 '20 at 05:53
  • It seems like your second problem is also solved. Great! – AzyCrw4282 Jun 20 '20 at 16:36
1

Solution to Problem-2:

query cannot simply be converted to string type using .lower() method. This is because recognize_google() (in SpeechRecognition module) is a list type and .lower() can only be applied to strings. In order to covert query into lowercase follow this method:

1. Create another string variable myStr (for instance)

2. Initialize myStr = query

3. Convert to lowercase myStr= myStr.lower()

4. Fianlly, return myStr

And for using that string in another function, simply initialize any_other_variable_name = myStr

DC007744
  • 123
  • 7
0

For the first one, you can simply add the while True statement to the bottom of the code and then below that, type run_name of the voice assistant. And also to stop the loop, just add a if statement like you have done with the others saying

elif 'stop' in command:
talk('Ok sir,have a great day!)
exit('name of your assistant that you have registered at the top')

I am not giving a answer for the second one because i am not that sure of that. And also @AzyCrw4282 had already given a clear and a perfect answer for that.

enter image description hereHere are some examples