Open AI davinci does not produce any output (text or audio) - python

I have the following piece of code:
import openai
import pyttsx3
import speech_recognition as sr
from api_key import API_KEY
openai.api_key = API_KEY
engine = pyttsx3.init()
r = sr.Recognizer()
mic = sr.Microphone(device_index=1)
print(sr.Microphone.list_microphone_names())
conversation = ""
user_name = "Josode"
while True:
with mic as source:
print("\nlistening... speak clearly into mic.")
r.adjust_for_ambient_noise(source, duration=0.2)
audio = r.listen(source)
print("no longer listening.\n")
try:
user_input = r.recognize_google(audio)
except:
continue
prompt = user_name + ": " + user_input + "\n Ava:"
conversation += prompt
response = openai.Completion.create(engine="text-davinci-002", prompt=conversation, max_tokens=100)
response_str = response["choices"][0]["text"].replace("\n", "")
response_str = response_str.split(user_name + ": ", 1)[0].split("Ava: ", 1)[0]
conversation += response_str + "\n"
print(response_str)
engine.say(response_str)
engine.runAndWait()
When I run the file, I get just listening... speak clearly into mic.
no longer listening. with no output from davinci.
Also, the mic print is ['LG FULL HD', 'Ari Chan’s AirPods', 'Ari Chan’s AirPods', 'MacBook Pro Microphone', 'MacBook Pro Speakers']. I am using index 1
API key is correct and imported. I have an account at Open AI and can use playground without a problem.
Do you see something that I don't see? It should work

Most probably you are getting an exception in r.recognize_google(audio) so it forces continue again and again without any output, try to add something like this to debug it:
import traceback
...
try:
user_input = r.recognize_google(audio)
except:
print(traceback.format_exc())
continue

Related

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.

Basic Voice Assistance with Python

I am a newbie in Python and I am developing a basic Voice Assistance with my teachers on Raspberry Pi 3. The code in this project is collected and fixed by me. However, as a newbie there are many questions I cant answer. Can you help me, please?
I want to develop an assistance which can search on Google, Play music on YouTube and show out the weather forecast. The AI.py is my main file, Google_Search.py is used for searching and YouTube_Search.py is used for playing music.
When I ask for searching the AI.py will import the Google_Search.py file to search for my requests. It is similar with YouTube file.
The problem is the Google_Search.py and YouTube_search.py just keep running and don't stop or allow me to keep working with the AI.py file. I want to run these two files while I still can run with the AI.py file to stop or re-open them again. These are the code, Please help me out. Thanks so much.
This is AI.py file
import speech_recognition
import pyaudio
import pyttsx3
import time
import os
import webbrowser, sys, imp
import requests, json
import vlc
import pafy
from datetime import date
from datetime import datetime
from googlesearch import search
robot_ear = speech_recognition.Recognizer()
robot_mouth = pyttsx3.init()
robot_brain = ""
api_key = "82328b3addcd3f984da6c1e74cf4c7c9"
base_url = "http://api.openweathermap.org/data/2.5/weather?"
city_name = "Melbourne,au"
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
response = requests.get(complete_url)
x = response.json()
while True:
with speech_recognition.Microphone() as mic:
print("Robot: I'm Listening")
audio = robot_ear.adjust_for_ambient_noise(mic)
audio = robot_ear.listen(mic, phrase_time_limit=3)
print("Robot: ...")
try:
you = robot_ear.recognize_google(audio, language = "en")
except:
you = ""
print("You: " + you)
if you == "":
robot_brain = "I can't hear you, please try again"
elif "hello" in you:
robot_brain = "Hello Minh"
elif "play some music" in you:
import YouTube_Search
robot_brain = "Music is playing"
elif "stop music" in you:
os.system("pkill YouTube_Search")
robot_brain = "Music is stoped"
elif "search something" in you:
robot_brain = "What do you want me to search for?"
import Google_Search
imp.reload(Google_Search)
time.sleep(5)
elif "stop searching" in you:
os.system("pkill chromium")
robot_brain = "Google search is closed"
elif "weather today" in you:
if x["cod"] != "404":
y = x["main"]
current_pressure = y["pressure"]
current_humidiy = y["humidity"]
z = x["weather"]
weather_description = z[0]["description"]
robot_brain = (" Temperature: " +
str(current_temperature) + " °F" +
"\n atmospheric pressure: " +
str(current_pressure) + " hPa" +
"\n humidity: " +
str(current_humidiy) + " %" +
"\n weather today: " +
str(weather_description))
elif "bye" in you:
robot_brain = "Bye Minh"
print("Robot: " + robot_brain)
robot_mouth.say(robot_brain)
robot_mouth.runAndWait()
break
else:
robot_brain = "I'm learning"
print("Robot: " + robot_brain)
robot_mouth.say(robot_brain)
robot_mouth.runAndWait()
This is the Google_Search.py file
import speech_recognition
import os, sys
import webbrowser
robot_brain = ""
robot_ear = speech_recognition.Recognizer()
with speech_recognition.Microphone() as mic:
#print("Robot: What do you want me to search for?")
audio = robot_ear.adjust_for_ambient_noise(mic)
audio = robot_ear.listen(mic, phrase_time_limit=2)
try:
you = robot_ear.recognize_google(audio, language = 'en-US')
except:
you = ""
search_terms = [you]
# ... construct your list of search terms ...
for term in search_terms:
url = "https://www.google.com/search?q={}".format(term)
webbrowser.open_new_tab(url)
robot_brain = ("Here is what I found for") + (" ") + str(you)
print("Robot: " + robot_brain)
os.system("pkill Google_Search")
This is the YouTube_Search.py
import vlc
import pafy
import time
import urllib.request
import urllib.parse
import re
import speech_recognition
robot_ear = speech_recognition.Recognizer()
with speech_recognition.Microphone() as mic:
print("Robot: What do you want me to search for?")
audio = robot_ear.adjust_for_ambient_noise(mic)
audio = robot_ear.listen(mic, phrase_time_limit=2)
try:
you = robot_ear.recognize_google(audio, language = 'en-US')
except:
you = ""
search = you
query_string = urllib.parse.urlencode({"search_query" : search})
html_content = urllib.request.urlopen("http://www.youtube.com/results?search_query="+query_string)
search_results = re.findall(r"watch\?v=(\S{11})", html_content.read().decode())
#print("http://www.youtube.com/watch?v=" + search_results[0])
url = "http://www.youtube.com/watch?v=" + search_results[0]
video = pafy.new(url)
best = video.getbest()
playurl = best.url
Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new(playurl)
Media.get_mrl()
player.set_media(Media)
player.play()
The import keyword in Python is used to load other Python source code files in to the current interpreter session.
When you import a file, that file will be run first. If you want to call the program in your imported file, you should use a function and call that. For example:
test.py
def myFunction():
print("Hello")
main.py
import test
test.myFunction()

Create a Loop of SpeechRecognition

I am creating a voice assistant in python and I would like to know how I can make a loop so that every time the assistant responds, the microphone is activated so I can ask him again
#Use Google TTS for say something
def speak(text):
tts = gTTS(text=text, lang="es", slow=False)
filename = "voiceww.mp3"
tts.save(filename)
playsound.playsound(filename)
#Use mic for get my voice
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Di algo')
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
said = ""
#Print the answer in console
try:
#print('Listo')
said = r.recognize_google(audio, language='es')
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
#Here the function activates
text = get_audio()
#This is a example question
if 'hello computer' in text:
speak('Hi! How are you?')
#This is where I want to turn on the microphone again, process my voice and say the answer and so on to infinity
#Use Google TTS for say something
def speak(text):
tts = gTTS(text=text, lang="es", slow=False)
filename = "voiceww.mp3"
tts.save(filename)
playsound.playsound(filename)
#Use mic for get my voice
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Di algo')
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
said = ""
#Print the answer in console
try:
#print('Listo')
said = r.recognize_google(audio, language='es')
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
This is how you keep your microphone turned on:
while True:
# Here the function activates
text = get_audio().lower
# These are examples
if 'hello computer' in text:
speak('Hi! How are you?')
elif "good morning" in text:
speak("Hey good morning")

Unable to get instructions in audio format and not able to play audio file after first one

Jarvis should say every instruction comes on screen but here first statement is spoken & none other than that gets audio voice. And while saving the audio only the first statement gets stored as audio file. how to delete the previous audio file after process & save new audio file as instruction.How to get print statements in audio form, like there's a print statement ("Say something!") , it should be there on the screen as well as audio of same should be spoken.
import speech_recognition as sr
from time import ctime
import time
import os
from gtts import gTTS
from pygame import mixer
mixer.init()
def speak(audioString):
print(audioString)
tts = gTTS(text=audioString, lang='en')
tts.save("audio.mp3")
os.system("mpg321 audio.mp3")
def speak_out():
mixer.music.load('audio.mp3')
mixer.music.play()
time.sleep(2)
def recordAudio():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
data = ""
try:
data = r.recognize_google(audio)
print("You said: " + data)
except sr.UnknownValueError:
print("I could not understand")
except sr.RequestError as e:
print("Server error. Check your internet connection: {0}".format(e))
return data
def jarvis(data):
if "how are you" in data:
speak("I am fine")
if "what time is it" in data:
speak(ctime())
if 'mail' in data:
speak('who is recipient')
recipient = speak()
if 'John' in data:
speak('What should I say')
content = recordAudio()
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('abc#gmail.com','xyz')
mail.sendmail('Person name', 'mail_id', content)
mail.close()
speak('Email sent')
time.sleep(2)
speak("Hi Ajay, what can I do for you?")
speak_out()
while 1:
data = recordAudio()
jarvis(data)

Speech recognizing in a loop

I am trying to write a speech recognition code which takes voice from the microphone and process till a "Stop" is said. The code works for the first voice but then it gives an error. The code which I wrote is below:
import speech_recognition as sr
import webbrowser
r = sr.Recognizer()
with sr.Microphone() as source:
while True:
audio = r.listen(source)
print("You said " + r.recognize(audio))
if r.recognize(audio)=="Facebook":
webbrowser.open('https://www.facebook.com')
if r.recognize(audio)=="Google":
webbrowser.open('https://www.google.co.uk')
if r.recognize(audio)=="Stop":
break
The error which I am getting is :
You said Facebook
Traceback (most recent call last):
File "C:/Work/Scripts/SpeechRecognition/Speech.py", line 9, in <module>
print("You said " + r.recognize(audio)) # listen for the first phrase and extract it into audio data
File "C:\Users\roradhak.NDS-UK\AppData\Roaming\Python\Python27\site-packages\speech_recognition\__init__.py", line 324, in recognize
raise LookupError("Speech is unintelligible")
LookupError: Speech is unintelligible
Process finished with exit code 1
I think the problem might be that you call recognize many times on the same input and the first call has already consumed the audio. You can introduce one extra variable + add try/catch to handle the LookupError.
with sr.Microphone() as source:
while True:
audio = r.listen(source)
try:
result = r.recognize(audio)
print("You said " + result)
words = result.lower()
if words=="facebook":
webbrowser.open('https://www.facebook.com')
if words=="google":
webbrowser.open('https://www.google.co.uk')
if words=="stop":
break
except LookupError:
print("Please, speak more clearly")
You need to catch the exception:
def recognize(audio):
try:
return r.recognize(audio)
except LookupError, e:
print e
return ''
Then:
with sr.Microphone() as source:
while True:
audio = r.listen(source)
words = recognize(audio)
print("You said " + words)
if words == "Facebook":
webbrowser.open('https://www.facebook.com')
elif words =="Google":
webbrowser.open('https://www.google.co.uk')
elif words == "Stop":
break

Categories

Resources