Create a ChatGPT Voice Assistant in 8 Minutes (Python Tutorial)
Video Title: Create a ChatGPT Voice Assistant in 8 Minutes (Python Tutorial)
ever since chat GPT was released I have
the constant urge to ask Siri a question
that only chat GPT can answer so instead
I decided to create a gpt3 powered Voice
Assistant with python I’m going to show
how you can do the same and at the end
we will give some ideas on how to take
this program and make it into a software
as a service business we will be diving
into the code step by step and
explaining what each line of code is
doing so even if you’re new to Python
and AI you’ll still be able to follow
along first open your python environment
and create a new python file we will
Begin by importing the openai library
which will allow us to access the gpt3
API next we’ll import the pi TTS X3
Library which will allow us to convert
text to speech we will also be using the
speech underscore recognition library to
transcribe audio to text so let’s import
that as well now that we have imported
the necessary libraries we can set up
our open AI API key this will allow us
to access the gpt3 API in this line of
code replace the dummy API key with your
own open AI API key which you you can
get from the openai website for free
with our libraries imported and our API
key setup let’s set up our text to
speech engine here we create an instance
of the text-to-speech engine using the
init method this instance is stored in
the engine variable which we will use
later to generate speech from text now
let’s create a python function to
transcribe our voice commands into text
for our Python program to understand we
will transcribe audio to text using the
python speech recognition Library the
speech underscore recognition Library
provides a convenient way to transcribe
audio to text in Python start by
defining the transcribe audio to text
function this line defines a new
function called transcribe audio to text
and specifies that it takes a single
argument file name which represents the
name of the audio file that we want to
transcribe now let’s create an instance
of the recognizer class from the SR
speech recognition module this object is
a requirement to perform speech
recognition on the audio file next we
use the with statement to open the audio
file specified by file name using the
audio file class from the SR module then
we record the audio using the record
method of the recognizer object finally
let’s transcribe the recorded audio to
text using the recognize underscore
Google method of the recognizer object
if an error occurs during transcription
an exception will be raised and a
message indicating that there was an
error and the program is trying again
will be printed this handles unknown
speech errors from Google if our program
records unintelligible sounds and that’s
it this function takes an audio file as
input and Returns the transcribed text
moving forward we will create a function
to generate responses from the gpt3 API
this line defines a new function called
generate underscore response and
specifies that it takes a single
argument prompt The Prompt argument
represents the input text that we want
to use as a starting point for
generating a response using the open AI
gpt3 API now let’s use the open AI
completion create method to generate a
response based on the given prompt as
shown in the openai documentation for
gpt3 in this line we pass several
arguments to the open AI completion
create method to specify the parameters
of the response for example we specify
the engine as the gpt3 model by
inserting text DaVinci 3 then we set the
max tokens variable to control the
number of characters a response from
gpt3 is limited to 4000 is the max we
can do with the DaVinci 3 engine we can
set the limit lower if desired for
development to increase query speed and
then adjust it back to 4000 when we have
finished developing and are ready to
publish our program for now let’s just
set it to 4000 to see what the API is
capable of the temperature variable in
the open AI API is a parameter used to
control the creativity or randomness of
the generated text a temperature of 0.5
is often a good starting point as it
balances the trade-off between
predictability and creativity in the
generated text here we return the
generated response from the gpt3 API and
that’s all it we now have a function to
establish connection with open AI so we
can later call upon this function to
communicate with the gpt3 API
let’s create a simple function for
speaking our responses from the
assistant so it is 100 voice Interactive
first we will Define a function called
speak text which takes a text argument
this function will convert the text
argument to speech using the pi ttsx3
library in this function we use the
engine say method to specify the text to
be spoken and the engine run and wait
method to play the speech now that we
have set up all the prerequisite
functions let’s start structuring the
logic of how we want python to run this
script
let’s create a main function to do so
next we’ll add a while loop that will
run continuously until the program is
stopped the while true statement means
that the loop will run until we force
the program to stop in terminal this
will allow our program to listen answer
and then continue listening now let’s
add a message that will be displayed on
the screen to instruct the user to say
genius this line prints a message on the
screen that instructs the user to say
genius to start recording their question
genius will be our prompt command to
make our program start recording our
question we’ll use the SR microphone
class to access the microphone and
record audio
let’s create an instance of the SR
recognizer class that we can use to
transcribe audio to text let’s now
record audio using the listen method of
the recognizer object now we’ll
transcribe the recorded audio to text
using the recognize Google method
this line checks if the transcribed text
is genius
the lower method is used to convert the
text to lowercase to make the comparison
case insensitive
if the transcribed text is genius let’s
record more audio this code block
records more audio and saves it to a
file named input.wave The Print function
is used to display a message instructing
the user to say their question next
let’s transcribe the recorded audio to
text this line transcribes the recorded
audio to text using the transcribe audio
to text function
now let’s check if the transcription was
successful if the transcription was
successful the text variable will
contain the transcribed text next let’s
print the transcribed text now let’s
generate a response using the generate
response function next let’s print the
response finally let’s read the response
using text to speech Let’s wrap up our
triloop with this exception message
except statements are used in Python to
handle errors and give us the control of
how we want our program to respond to a
given error without accept statements
errors will simply cause a python script
to stop running
the basic idea behind using a try except
block is to attempt to execute a block
of code and if any errors occurs during
the execution of our try statement
python will respond how we tell it to in
the accept statement overall using try
accept statements can help make our
python programs more robust resilient
and easier to debug and that’s it this
is the complete main function that runs
the voice assistant finally we add the
following code to the end of the script
to run the main function
with these additions our personal
assistant is complete and ready to use
now let’s test out the script and see
how it works
genius
I just created a super dank Voice
Assistant with python and gpt3 how can I
make the Python program into a website
so everyone can access it
to make your Python program into a
website you’ll need to use a web
framework such as flask or Django these
Frameworks will allow you to create a
web application that can be hosted on
the internet and accessed by anyone with
an internet connection you’ll also need
to create a web interface for your voice
assistant so that users can interact
with it additionally you’ll need to set
up a server to host your application
once you have all of these components in
place you’ll be able to make your Python
program into a website
genius my python code just returned no
Pi TTS X3 module found why did it do
that
it is possible that the python code was
looking for a specific module that was
not found it could also be that the
module you were looking for was not
installed correctly or was not
compatible with your version of python
genius
write a diss song about how my
subscribers are superior to pancakes
that sounds like nle Choppa my
subscribers they’re always down to show
some love they don’t need Sarah
[Applause]
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Try the updated tutorial with GPT 3.5 Turbo, OpenAI Whisper and an open sourced Bing AI API:
https://youtu.be/aokn48vB0kc
LOL, I built this app thinking "oh I'll to the old one becuase it will be less complicated" I finally got this thing to work and my AI is dumb as a brick, good exercise but now I will build the newer version.
In case anyone is wondering as of today (2024) basically everything is outdated in this video unfortunately ;-;
Hopefully this can save some people from trying and failing.
so i'm having a slight problem when i say genius after starting the bot it comes back with: An error occurred: local variable 'filename' referenced before assignment. is there a fix for this?
import openai
from openai import OpenAI
import pyttsx3
import speech_recognition as sr
# Set your OpenAI API key
client = OpenAI(api_key="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")
# Initialize the text-to-speech engine
engine = pyttsx3.init()
def transcribe_audio_to_text(filename):
recognizer = sr.Recognizer()
with sr.AudioFile(filename) as source:
audio = recognizer.record(source)
try:
return recognizer.recognize_google(audio)
except sr.UnknownValueError:
print('Google Speech Recognition could not understand audio')
except sr.RequestError as e:
print(f'Could not request results from Google Speech Recognition service; {e}')
def generate_response(prompt):
response = client.chat.completions.create(
messages=[{"role": "user", "content": prompt}],
model="gpt-3.5-turbo",
)
# Access the content attribute directly with dot notation
return response.choices[0].message.content
def speak_text(text):
engine.say(text)
engine.runAndWait()
def main():
while True:
print("Say 'Genius' to start recording your question…")
with sr.Microphone() as source:
recognizer = sr.Recognizer()
audio = recognizer.listen(source)
try:
transcription = recognizer.recognize_google(audio)
if transcription.lower() == "genius":
print("Say your question…")
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
filename = "input.wav"
with open(filename, "wb") as f:
f.write(audio.get_wav_data())
text = transcribe_audio_to_text(filename)
if text:
print(f"You said: {text}")
response = generate_response(text)
print(f"GPT-3 says: {response}")
speak_text(response)
except Exception as e:
print(f"An error occurred: {e}")
if _name_ == "__main__":
main()
Man it's been a year! wow! what a year!
Great video austin but my program cannot access the voice of the microphone. Do I need to save my voice as a file for this program to recognize
Do you have to pay for tokens to use chatgpt
how do I change lang?
Debug your code:
– openai module not present even if you installed it? uninstall and reinstall python3, then pip install every library.
– pip install SpeechRecognition
– pip install setuptools
– DON'T FORGET TO INSERT YOUR API KEY!!! (because I did lol)
Now it should work BUT you'll still have to upgrade the code to get a fully functional assistant. Otherwise, if you miss the first audio recording opportunity, it just bugs in an infinite loop.
it compiles but after that doesnt do anythg…
code shows error
regarding response
why this error?
"(" was not closedPylance
here
response = openai.Completion.create(
Can u help me? The code is all correct but there is a huge error saying that google is not an attribute in the module recognizer
bro, can you give give them AI voice so it sounds better? Or that's not possible for now?
i got it to say "say "Genius" to start recording" but it still wont work due to gTTS not working, it says on line 66, col 35 that theres a problem and it cannot be defined
i tried to use gTTS and it cannot be found or used by imports
The imports aren’t working
Instead of typing all this all I did is ask Chat gpt to write me the code to do this then just pasting the API key in the code 😂
this is a great tutorial!. I really love it if you upgrade it. What i mean by upgrade is that, import the python programme in to any type of device such as arduino or raspberry pi ( If possible). Make it wireless.
with sr.Microphone() as source:
recognizer sr.Recognizer()
audio = recognizer.listen(source)
its highlightin "sr" as an error and when i run it it says invalid syntax, and when i try to pip install the library it says that its already installed
yeah and it's not free
will figure this out.
You need to buy Api key!
It is always saying an error occured: _enter_ what should I do
Hey, nice tutorial, but can you let me know if there is a way to change the voice of the model? If yes, how?
Thanks!
good afternoon, can you send me the codes? thenks
Hey, that's a tutorial. But i got an error and no idear how to solve it. Can you help me? I got this message over and over:
An error occurred: cannot access local variable 'recognizer' where it is not associated with a value
Say 'Jarvis' to start recording your question
Can you make it in different languages?
Lost me at open python, is there a GitHub?
engine pyttsx3.init() ^^^^^^^ syntaxerror: invalid syntax
i-also-hawe-pyttsx3-and-also-tried-3==2.17
pls-help
unfortunately didn't understood any thing. this tutorial is not for beginners .
give us the code source
why is everyone using chatgpt? i want to know how its done without chatgpt
BUt how can i install import open ai for chat 4.
Is dosnt work because there Is not instal of import openai in vscode
7:45 can u do that bruh? i would like to put my voice assistent in a webpage for everyone….