How to use multithreading with tkinter in this program - python

This code accepts user input as audio and responds accordingly and I want to use tkinter to display the conversation. I tried using using multithreading but it won't work.Please help me regarding how to use multithreading here.
Expected result :
The code should print the conversation on a window
And also listen to the user simultaneously
Here is my code
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
from tkinter import *
import threading
import time
root = Tk()
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# print(voices[0].id)
engine.setProperty('voice', voices[0].id)
def speak(audio):
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
t = Text(root)
t.grid(row=0, column=0,
columnspan=2, rowspan=2, padx=5, pady=5)
t.insert(END, 'Genos says: ' + wishme() + '\n')
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)
speak("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('mail#gmail.com', '###')
server.sendmail('mail#gmail.com', to, content)
server.close()
return
if __name__ == "__main__":
wishme()
# while True:
for _ in range(10):
query = takecommand().lower()
# Logic for executing task based query
if 'wikipedia' in query:
speak('searching Wikipedia....')
query = query.replace("wikipedia", "")
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 stackoverflow' 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 = "mail#gmail.com#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 'how are you' in query:
speak("I am fine sir. How are you?")
continue
elif 'i am fine' in query:
speak("that's good to know, how can I help you")
continue
elif 'goodbye' in query:
speak("bye Sir")
exit()
root.mainloop()

I couldn't test speech recognition but at least it displays text which it speeks.
I put all your loop in function and run in Thread. I also send queue to this thread soit can send text (or commands like \quit) to main thread which use after to periodically (200ms) get text from queue and display in Text
import os
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import smtplib
import time
import threading
import queue
import tkinter as tk
# --- functions ---
def speak(text):
engine.say(text)
engine.runAndWait()
def wishme(queue):
hour = datetime.datetime.now().hour
if 0 <= hour < 12:
text = "Good Morning sir"
elif 12 <= hour < 18:
text = "Good Afternoon sir"
else:
text = "Good Evening sir"
queue.put(f'{text}.')
speak(text)
queue.put("I am Genos. How can I Serve you?\n")
speak("I am Genos. How can I Serve you?")
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)
speak("Say that again please")
return "None"
return query
def sendEmail(to, content):
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login('mail#gmail.com', '###')
server.sendmail('mail#gmail.com', to, content)
server.close()
return
def my_loop(queue):
wishme(queue)
while True:
query = takecommand().lower()
# Logic for executing task based query
if 'wikipedia' in query:
queue.put('searching Wikipedia....')
speak('searching Wikipedia....')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=5)
queue.put("According to wikipedia" + str(results))
speak("According to wikipedia")
print(results)
speak(results)
elif 'open youtube' in query:
queue.put("opening youtube.com")
webbrowser.open("youtube.com")
elif 'open google' in query:
queue.put("opening google.com")
webbrowser.open("google.com")
elif 'open stackoverflow' in query:
queue.put("opening stackoverflow.com")
webbrowser.open("stackoverflow.com")
elif 'open mailbox' in query:
webbrowser.open("gmail.com");
elif 'play music' in query:
music_dir = 'D:\\SAHIL\\$ONGS_MJ'
songs = os.listdir(music_dir)
queue.put(f"playing music {songs[0]}")
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}", queue)
elif 'open code' in query:
codepath = "C:\\Users\\Sahil\\AppData\\Local\\Programs\\Microsoft VS Code Insiders\\Code - Insiders.exe"
queue.put(f"opening code {codepath}")
os.startfile(codepath)
elif 'email to sahil' in query:
try:
queue.put("What should i say ?")
speak("What should i say ?")
content = takecommand()
to = "mail#gmail.com#gmail.com"
sendEmail(to, content)
queue.put(f"Email has been sent: {content}")
speak("Email has been sent")
except Exception as e:
print(e)
queue.put(f"Could nott send the email: {e}")
speak("Could nott send the email ")
elif 'how are you' in query:
queue.put("I am fine sir. How are you?")
speak("I am fine sir. How are you?")
elif 'i am fine' in query:
queue.put("that's good to know, how can I help you")
speak("that's good to know, how can I help you")
elif 'goodbye' in query:
queue.put("bye Sir")
speak("bye Sir")
queue.put("\quit")
break # exit loop and thread will end
#exit()
def update_text():
if not queue.empty():
text = queue.get()
if text == '\quit':
root.destroy() # close window and stop `root.mainloop()`
return # don't run `after` again
else:
t.insert('end', text)
root.after(200, update_text)
# --- main ---
if __name__ == "__main__":
engine = pyttsx3.init('sapi5') # Windows
#engine = pyttsx3.init('espeak') # Linux
voices = engine.getProperty('voices')
# print(voices[0].id)
engine.setProperty('voice', voices[0].id)
# ---
root = tk.Tk()
t = tk.Text()
t.pack()
queue = queue.Queue()
update_text()
task = threading.Thread(target=my_loop, args=(queue,)) # it has to be `,` in `(queue,)` to create tuple with one value
task.start() # start thread
root.mainloop()
task.join() # wait for end of thread

Related

How i can fix my functions automatic repetition

So i have a voice assistant project i use speech recognition and pytsx3, the problem is the functions repetition, so this is my code
main.py:
import possibilities as ps
clear = lambda: os.system('cls')
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[3].id)
engine.setProperty("rate", 160)
def speak(audio):
engine.say(audio)
engine.runAndWait()
while True:
rec = sr.Recognizer()
with sr.Microphone() as mic:
print("Manini listen...")
audio = rec.listen(mic)
try:
text = rec.recognize_google(audio, language='en')
query = text.lower()
except Exception as e:
print("Manini Listen...")
if 'wikipedia' in query:
ps.wikipediaSer(query)
elif "say" in query:
ps.sayrep(query)
elif 'open youtube' in query:
ps.openyoutube()
possibilties.py:
def wikipediaSer(query):
speak('Searching Wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences = 2)
speak("According to Wikipedia")
print(results)
speak(results)
def sayrep(query):
say_text = query.replace("say", "")
speak(say_text)
def openyoutube():
speak("Here you go to Youtube\n")
webbrowser.open("youtube.com")
so when I execute the project and say a command Wikipedia par example, Wikipedia code repet 5 times. how I can fix this repetition please.

I tried to make a virtual assistant using python, earlier it was only printing listening. Now it is browsing myntra's website for every command given

'''
on executing the following code, when I am giving any instruction to this voice assistant :
it is not printing whatever i am saying
it is opening myntra's website for every instruction I am giving
'''
import pyttsx3 #text to speech
import speech_recognition as sr #recognizes speech
import wikipedia
import webbrowser
import datetime
import os
import smtplib
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id) #activates voice
def speak(audio):
# enables system to speak
engine.say(audio)
engine.runAndWait()
def wishme():
to take know the current time
hour = int(datetime.datetime.now().hour)
if hour >= 00 and hour < 12:
speak("good morning!")
elif hour >= 12 and hour < 18:
speak("good afternoon!")
else:
speak("good evening!")
speak("I am JARVIS sir , how may i help you ?")
def takeCommand():
'''It takes microphone input from user and returns string output'''
# I added r.adjust_for_ambient_noise(source) and reduced threshold frequency
r = sr.Recognizer() # to recognize the user's voice
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
r.energy_threshold = 50 #to listen low frequency voices
r.adjust_for_ambient_noise(source) #to avoid noise
audio = r.listen(source)
try:
print("Recognizing...")
# to recognize user's voice
query = r.recognize_google(audio , Language = "en-in")
print(f"user said: {query}\n")
except Exception as e:
print("Say that again please...")
return "None"
return query
if __name__ == "__main__":
# speak("Akshima is a good girl")
wishme()
while True:
query = takeCommand().lower()
# logic for executing tasks based on query
# maybe there is an error in this part of the code
if 'wikipedia' in query:
speak('Searching Wikipedia...')
query = query.replace("wikipedia", "")
result = wikipedia.summary(query, sentences=2)
speak("according to wikipedia")
print(result)
speak(result)
elif 'open youtube' in query:
# to open youtube
webbrowser.open("youtube.com")
elif 'open google' in query:
webbrowser.open("google.com") #to open google
elif 'play music' in query:
webbrowser.open("spotify.com")
elif 'open myntra' or 'open any online shopping site ' in query:
webbrowser.open("myntra.com")
elif 'open instagram' in query:
webbrowser.open("instagram.com")
elif 'chill' in query:
webbrowser.open("netflix.com")
elif "the time" in query:
# tell user the current time
strTime = datetime.datetime.now().srtftime("%H:%M:%S")
speak(f"The time is {strTime}\n")
elif 'open code' in query:
#to open vs code
path = "C:\\Users\\HP\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
os.startfile(path)

Python Speech Recognition not working when the file is converted to exe with pyinstaller

I created a Voice Assistant using speech recognition and GTTS modules in Python. The code works perfectly fine when run from script. But when I converted it using pyinstaller --onefile Speak.py(The name of the file) it stopped working. It is using my microphone but its not responding to anything. Suppose if I say hey google which is the wake up keyword it simply doesnt respond. I tried after a while but of no use. Can anyone please help me???
Code:
"from gtts import gTTS
import speech_recognition as sr
import os
import playsound
from datetime import datetime
import webbrowser
keeprunning = True
WAKING = "HEY GOOGLE"
def chec(cond, tex):
if cond in tex:
return True
else:
return False
def speak(text):
speaker = gTTS(text = text, lang = "en")
speaker.save("Voice.mp3")
playsound.playsound("voice.mp3")
os.remove("Voice.mp3")
def cleanify(text, wtremove, wtremove2=""):
try:
text = text.replace(wtremove)
except:
if not wtremove2 == "":
text = text.replace(wtremove2, "")
return text
#Main controlling part here(Oof!)
def Control(comnd):
if chec("SEARCH", comnd) or chec("GOOGLE", comnd):
comnd = cleanify(comnd, "SEARCH", "GOOGLE")
webbrowser.open(f"https://google.com/search?q={comnd.lower()}")
elif chec("WHO ARE YOU", comnd):
speak("I am your virtual assistant, google")
keepcontrol()
elif chec("TIME", comnd):
t = datetime.now()
t = time.strptime(t.strftime("%H:%S"), "%H:%M")
tt = time.strftime( "%I:%M %p", t )
speak(f"The current time is {tt}")
elif chec("NOTE", comnd) or chec("REMEMBER", comnd) or chec("JOT", comnd):
try:
text = comnd.replace("NOTE")
except:
try:
text = text.replace("REMEMBER")
except:
text = text.replace("JOT")
try:
with open("This_File_Name_Is_Very_Big_Because_I_dont_Want_to_overwrite_anyother_files_so_forgive_me_plz.txt", "rt") as file:
previous = file.read()
except:
with open("This_File_Name_Is_Very_Big_Because_I_dont_want_to_overwrite_any_other_files_so_forgive_me_plz.txt", "wt") as file:
ttw = f"""
•{text.lower()}
"""
file.write(previous + ttw)
previous = ""
with open("Notes.txt", "wt") as file:
ttw = previous + " •" + text
file.write(ttw)
speak("Got it! I noted it down")
elif chec("OPEN", comnd):
web = cleanify(comnd, "OPEN")
bruh = cleanify(web, ".com")
speak(f"Opening {bruh}")
print(comnd)
web = cleanify(comnd, "OPEN")
if web == "":
web = listen()
else:
if "https://" in web:
webbrowser.open(web.lower())
else:
web = "https://" + web
webbrowser.open(web.lower())
keepcontrol()
#Main controlling part ends
def listen():
r = sr.Recognizer()
with sr.Microphone() as source:
listen = r.listen(source)
try:
text = r.recognize_google(listen)
return text.upper()
except Exception as e:
if not e == "":
return ""
keepcontrol()
else:
speak("Sorry we couldnt catch that, please try again")
keepcontrol()
def keepcontrol():
while keeprunning:
text = listen()
if not text == None:
if text.count(WAKING) == 1:
speak("I am listening..")
text = listen()
if not text == None and not text == "":
Control(text)
else:
speak("Sorry i did not get that. Try again in a few seconds")
keepcontrol()
elif chec("QUIT", text):
exit()
elif chec("", text):
keepcontrol()
else:
speak("Sorry we didnt get that right. Please try again.")
speak("I am booting up...")
keepcontrol()
"
When you have converted it into exe,I think you have forgot to put files that you have used to build the assistant in the folder.
When your exe get converted, just simply copy those files(the files you have used) in the same folder in which you have kept your exe.
Hope it will work.

How to give input using speech recognition in a element in python

import speech_recognition as sr
import pyttsx3
import string
import random
#Text To Speech
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
#print(voices)
engine.setProperty('voice',voices[0].id)
engine.setProperty('rate', 145) #you can replace it to incease or decrease dound speed default(200)
def speak(audio): #here audio is var which contain text
engine.say(audio)
engine.runAndWait()
#now convert audio to text
def takecom():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listning....")
audio = r.listen(source)
try:
print("Recognising....")
text = r.recognize_google(audio,language='en-in')
print(text)
except Exception:
speak("error...")
print("Network connection error")
return "none"
return text
#for main function
if __name__ == "__main__":
while True:
query = takecom().lower()
if 'create password' in query or 'c' in query :
if __name__ == "__main__":
s1 = string.ascii_lowercase
s2 = string.ascii_uppercase
s3 = string.digits
s4 = string.punctuation
speak('what do you want to keep the length of the password type here')
plen =int(input('what is the length of the password')) #p
s=[]
s.extend(list(s1))
s.extend(list(s2))
s.extend(list(s3))
s.extend(list(s4))
print("Your password is:")
print("".join(random.sample(s,plen)))
speak("".join(random.sample(s,plen)))
elif query == 'none':
continue
elif 'exit' in query or 'abort' in query or 'stop' in query or 'bye' in query or 'quit' in query:
ex_exit = 'ok byy'
speak(ex_exit)
exit()
When i run this code every thing work fine but it ask to write the length of the password and when i write to length in it then the code proceed but i don't want to write anything in it is there any way so that i can give a voice input in the function plen (#p) to do the program work with the help of voice command.
I am using python 3.8
Oh god finally i got answer of my question by my own.
let's see the answer
In the line plen =int(input('what is the length of the password')) #p
we need to change this code to plen =int(takecom()) show that the word which we will speak will get directly converted into input in it and the code will work properly with voice command.
Here is the final code
import speech_recognition as sr
import pyttsx3
import string
import random
#Text To Speech
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
#print(voices)
engine.setProperty('voice',voices[0].id)
engine.setProperty('rate', 145) #you can replace it to incease or decrease dound speed default(200)
def speak(audio): #here audio is var which contain text
engine.say(audio)
engine.runAndWait()
#now convert audio to text
def takecom():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listning....")
audio = r.listen(source)
try:
print("Recognising....")
text = r.recognize_google(audio,language='en-in')
print(text)
except Exception:
speak("error...")
print("Network connection error")
return "none"
return text
#for main function
if __name__ == "__main__":
while True:
query = takecom().lower()
if 'create password' in query or 'c' in query :
if __name__ == "__main__":
s1 = string.ascii_lowercase
s2 = string.ascii_uppercase
s3 = string.digits
s4 = string.punctuation
speak('what do you want to keep the length of the password type here')
plen =int(takecom()) #p
s=[]
s.extend(list(s1))
s.extend(list(s2))
s.extend(list(s3))
s.extend(list(s4))
print("Your password is:")
print("".join(random.sample(s,plen)))
speak("".join(random.sample(s,plen)))
elif query == 'none':
continue
elif 'exit' in query or 'abort' in query or 'stop' in query or 'bye' in query or 'quit' in query:
ex_exit = 'ok byy'
speak(ex_exit)
exit()
just change ↓
plen =int(input('what is the length of the password')) #p
to ↓
plen =int(takecom()) #p

Why does the code terminates after once the user asks query

This is a code for a virtual assistant this code terminates after the user speaks once. I need this code to iterate till the user says exit. I tried using 'while True' after 'if name == "main":
wishme()' it runs, but in that case it does not show the tkinter window. Can you help me please?
Here is my code
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
from tkinter import *
root = Tk()
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('your email#gmail.com','password')
server.sendmail('your email#gmail.com',to,content)
server.close()
return
if __name__ == "__main__":
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 stackoverflow' 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 = "your email11#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()

Categories

Resources