2

I have a virtual assistant with GUI. The desired behavior is:

  1. The assistant should keep listening until the user says exit.
  2. Conversation should be printed in the text field.
  3. the user says exit the program should terminate

The observed behavior is that the window appears after the user asks for a query and then it prints the conversation and then it freezes

Here is my code:

import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
from tkinter import *


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


def speak(audio):       
    e1 = Text(root)
    e1.grid(row=0, column=0)
    engine.say(audio)
    engine.runAndWait()
    t = Text(root)
    t.grid(row = 0, column = 0, 
       columnspan = 2, rowspan = 2, padx = 5, pady = 5) 
    t.insert(END, 'Genos says: '+ audio +'\n')



def wishme():
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        speak("Good Morning sir")

    elif hour>=12 and hour<18:
        speak("Good Afternoon sir")

    else:
        speak("Good Evening sir")    
    speak("I am Genos. How can I Serve you?")
    return

def takecommand():
     # it takes mic input from the user and return string output

    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 Exception as e:
        print(e)

        print("Say that again please")
        return "None"
    t1 = Text(root)
    t1.grid(row = 0, column = 2, 
       columnspan = 2, rowspan = 2, padx = 5, pady = 5)  
    t1.insert(END, 'user says: '+query+'\n')
    return query

def sendEmail(to, content):
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.ehlo()
    server.starttls()
    server.login('youremail@gmail.com','password')
    server.sendmail('youremail@gmail.com',to,content)
    server.close()
    return

if __name__ == "__main__":
root = Tk()

wishme()
#while True:
if 1:
    query = takecommand().lower()


#Logic for executing task based query 
    if 'wikipedia' in query:
        speak('searching Wikipedia....')
        query = query.replace("wikpedia", "")
        results = wikipedia.summary(query, sentences=5)
        speak("According to wikipedia")
        print(results)
        speak(results)

    elif 'open youtube' in query:
        webbrowser.open("youtube.com")

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

    elif 'open stackoverfolw' in query:
            webbrowser.open("stackoverflow.com")

    elif 'play music' in query:
        music_dir = 'D:\\SAHIL\\$ONGS_MJ'
        songs = os.listdir(music_dir)
        print(songs)
        os.startfile(os.path.join(music_dir, songs[0]))

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

    elif 'open code' in query:
        codepath = "C:\\Users\\Sahil\\AppData\\Local\\Programs\\Microsoft VS Code Insiders\\Code - Insiders.exe"
        os.startfile(codepath)

    elif 'email to sahil' in query:
        try:
            speak("What should i say ?")
            content = takecommand()
            to = "youremail@gmail.com"
            sendEmail(to, content)
            speak("Email has been sent")
        except Exception as e:
            print(e)
            speak("Could nott send the email ")

    elif 'open mailbox' in query:
        webbrowser.open("gmail.com");

    elif 'exit' in query:

        exit()     


root.mainloop()
Sahil
  • 39
  • 5

0 Answers0