How to Create Jarvis AI Assistant | Like Iron Man

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Friday write a haiku about programmers minds and codes they write crafting G programmers create in this video we're going to be creating our own artificial intelligence using open AI that we can actually talk to and it will respond and talk back to us this project is incredibly easy and I'm going to explain all of the code to you we're going to be able to do this really quickly and it's only going to take about 25 lines of code I think so if you're interested in having your own voice assistant that goes Way Beyond the capabilities of a Siri or Alexa then this video is going to be for you and then you can actually take this program that we're going to write and you can move it over to a Windows Mac Linux or even a Raspberry Pi if you would like to go ahead and check this out the links for the pycharm download is going to be in the description it'll take you straight to this page and if you are on Windows you can come over here from Windows or Linux and you can download it if you are running a Mac with a Silicon Valley chip you can click this right here and it'll automatically start the download you'll just click next next install and it will install for you and I already have it installed down here so we can go ahead and open this up you can and create a new project I'm just going to use this project right here and when it is all set up it will look like this actually it will look like this without that if you want to open a terminal you can just click that button down there that's what I had clicked and we can minimize this because we're not ready for that just yet we're going to need to get an API key from chat GTP or open AI so what you can do is just come over here and say chat GPT API key and you'll be brought to this one right here the one that says platform.openai.com so you can click on this you will need to sign up if you do not have an account yet and I already have an account so I'm going to go ahead and log in and I'll bring you back at the main home page and once you're logged in I'm not really sure what page you're going to be brought to this is the one that I was brought to you can just click on the API Keys down here and for those of you who just created it you get six months with something like 10 000 requests so you got a lot of requests you don't have to worry about running out and you come down to the API key and you're going to click create a secret key and you're going to click create and a lot of you are going to be saying why would you put your API key out I will just delete this API key once this video is done so don't try to use it or you can try to use it and it's not going to do you any good but I'm going to delete this and then once you have your secret key you need to copy it and save it somewhere because once you close out of this with done there's no way to see this API key anymore it's gone so you click done and you're not gonna be able to see that API key anymore so I'm going to come back over here to our pycharm and we are going to just delete all of this and I'm going to paste in that API key and then I'm going to comment it out and I think it's control it is command question mark and it should come out our API key but actually what we can do instead of just commenting it out is just come down here and we'll just say API underscore key equals and then we'll have this API key stored in a variable which we are going to need and the reason this is underlined is because it needs quotes so we'll just go ahead and throw some quotes around it and we're going to import some modules now so we're going to import OS we're going to import time we will import high audio and we'll also import play sound we're going to use these to play our sound and then we're going to import gtts from gtts like this this is just a Google Text-to-Speech so it's going to take what we say Google's going to run it through our text which we say it's going to take it to speech and then it's going to say it back to us and then we're going to import open AI as our in order to use our API key and this is going to be our AI for us so now we are going to need it to actually import one more thing we need to import speech recognition and I think I have that spelled right and we're going to import it as Sr just like this now you're going to notice that these are squiggly lined and it is because we need to import these modules and so we're going to use pip if you don't know if you have Pip you can just open up a terminal by clicking this down here you can just type in PIP and hit enter and if it works then you have Pip if not this also works with pip3 you can just type in PIP 3 and if you get this output then it works for you and pip should work for you because you just installed pycharm and it should install pip for you so you should have Pip or pip3 depending on a pie charm when you installed it or you might have both like I do or on a Windows you can install Pi audio by typing in PIP install Pi audio like this and if that doesn't work pretty I guess have that typed out wrong and if that doesn't work for you you can come back and you can type in PIP install pip win and then you're going to run pip win to install Pi audio so it will look like this pip win install Pi audio and that should work for you so if you're on a Mac this is going to be a little more challenging you're probably going to have to go out and install Brew or Homebrew but we can also try that and you can try pip install Pi audio and if this doesn't work for you and you're on a Mac or you're on Linux you might need to run pip install Port audio like this and then if you're not able to to install Port audio you're gonna have to go out to Homebrew and install Homebrew and I'll show you how to install that now what you'll do is you'll come out here and you're going to copy this right here and we'll come back to our text editor and open that up we will paste in what we had just copied you will run this and we're going to type in our password and that should run we want to run it and it's going to tell us that we need to put this in our path okay what you'll need to do is scroll up to where you see this next steps right here and it's going to tell you to run these two commands and what you can do is just highlight this entire thing right here from the echo all the way down here to this last quote and you will copy that and you will paste it into your terminal and you will hit enter and it will go ahead and put Brew in the Pat in your path and then you'll be able to use Homebrew to finish the installation of Pi audio and poor audio and we're going to install Port audio by just typing in Brew install for audio just like this should be able to run it and it will install poor audio I already have it installed but after that's done you can type in Brew install Pi audio and that should install Pi audio for you and now you will have Pi audio and poor audio installed and we can continue installing the rest of our packages the rest of these are really simple just go pip install play sound and it's going to install play sound and then we're going to do the same thing for Google Text-to-Speech and it will install and that is finished so now we can say pip install gtts and your G TTS needs to look like this or it will install the correct package and now we can go pip install and our speech recognition has a capital S and a capital r just like this and we'll let that run I probably spelled this wrong let's try again speech and I spelled Recon ignition so we can try this again and it will go ahead and install for us now we should have everything installed we have an issue here with how we imported this this is actually going to look like this from gtts import gtt s like that and now we have this typed in right sorry for that mistake so we have our API key right here I'm going to be working with the language English so I can just type in Lang equals en and I'm pretty sure this needs to be in single quotes so we can say e in and move outside the tab and we can go down and we're going to set a variable for our open API key so we can say open API dot API key equals our API key just like that so now we have our API key saved right here in this variable and this one but this is one that openai likes to work with so we're going to keep it like this now we're going to create our while loop and we'll just say while true we want it to do something and we'll start out by just creating a function so we'll say def get audio we'll make this function and then we're going to use our speech recognition so we'll just save this as R equals s r dot recognition is it recognizer and then we'll close this off because this is a method and we're now going to set up our microphone so we're going to say with Sr dot microphone and we're going to set up a device index and so for this device index you're going to have to probably play around with this um it depends on what port you have different things plugged into so sometimes I'll have this as device index one sometimes it's two sometimes I just can delete the entire thing and I don't actually need a device index at all so for you this is something you can remember you can set it to one and if your microphone doesn't work then you may have to come over here and set that to two or delete device index all together I'm not actually entirely sure how to determine which Port your microphone's plugged into so I usually just play around with a number until I get it to work so we're going to need to set up a device index and we're going to say we're going to save this as source and we're going to create another variable and we're going to call it audio and we're going to save it as r dot listen this is going to have it listen to what is coming in through the source over here so we can use the source and then we're going to have this passed in as a string so we're going to say a said equals empty string so that way our program has our audio gonna be passed in through this said right here in just a second so now we're going to make a try statement so we're going to say try we want our program to try something this is going to give it the type of function like the hey Siri and then Siri wakes up for you instead of actually having us put in like keywords in order for it to activate we're going to have it listen all the time this is a different way to have the program listen all the time rather than the way like Siri works we're going to have it just always trying to listen for a keyword for when it needs to wake so we're going to say try and we're going to have said equals r dot recognizer r dot recognize Google right here so it's going to be this Google Text-to-Speech and we're going to pass in the audio we have our recognizer working with Google so our speech recognition is going to be working with Google and we're going to be using our Google Text-to-Speech here in just a second so now our empty string should be filled with everything that was taken in one thing I like to do is print our said just to make sure that the text is taken in we can actually see it and now we're going to create our if statement so we're going to say if whatever you want to name your AI program I name mine Friday because it is something that the program is actually able to understand so if you name it something like hat it's going to probably not recognize ha bat cat fat something like that so name it like orange you're going to want it to be something unique that the program is going to be able to recognize and not get confused with other words so we're going to say if Friday and this actually needs to be in quotes so we can say if Friday is in said then we want it to do something and this is going to be really complicated so I'm actually going to type out the rest of this this is going to be the actual code in order to get open AI to work and I'm actually just copying this template straight from open AI off their website we which is over here and I'm actually going to copy this template from the open AI over here under their documentation so I'm going to paste it in here and then I'll bring you back you can either go grab this from the documentation or you can just copy it down from what I have typed in right here okay so here it is we're going to be using Friday we're going to be saving completion right here with open API we're going to be using the model GPT 3.5 because you can't get an API key for gpt4 just yet we're going to be having our roll which is going to be the user the content of what has been said right here so our roles user and the content that we're sending is going to be what we have said verbally into our microphone the text right here is going to equal the text from the content the Google speech to text we're going to save this the Google speech to text settings so the text which is going to be clearly the text which is right here which we just grabbed from the message it's going to be speaking slow false you want to change this or when your AI speaks to you it's going to be really slow the language is going to be our variable which we saved up here somewhere right here so English is what I'm going to be using and the TLD this is the accent that you want it to have and the com.au is an Australian lady which is what I have it set to and we have the speech right here which is going to save to an MP3 file right over here and then it's going to play the speech for us right over here it's going to open up that file and read it to us and then we have an exception which is just we're going to print exception it's just going to print this word you can make this say error or anything you want and then it's going to return the said and then we call our function which is going to be the get audio so this should work for us at this point but we will also want to break this if you are having a conversation and you don't want the open AI to be listening for you to tell it to do something you can add in an if statement and break the statement but for now I actually think we'll leave it like this because I think I might get a little it might get a little confusing for those of you who are really familiar with python and making Global variable making Global variables in order to get this to break so this right here should work for us okay so we're back here at our code I'm going to go ahead and run it and you're going to get to here Friday our AI that we have created talk back to us we're going to be able to ask it anything we want I think I'm going to just ask it to write a haiku about how wonderful programmers can be so we'll go ahead and do that I'm going to run it by clicking my little run button it's going to give me an output here that I'm not sure what all those errors mean but I know that it's working now we'll look at the output and then we'll also listen to what it has to say in answering our question Friday write a haiku about programmers lines and code encode right crafting genius with each byte programmers create and there is the program and how it works and we can also just scroll up here we'll actually close out of the program we can scroll up and you can see the question that we asked it and this is the statement that it is going to send back to chat GPT through the API and then it will send us back its answer and then our Google Text-to-Speech will answer the question for us so this is how you can actually carry on an audible conversation with chat GPT so if you have any questions please let me know down in the comments if there's something else you would like to make go ahead and let me know and I will try to get to that if your code is not working and you're having some kind of a problem I will leave a link to my GitHub down in the description and you can go copy and paste the code that I know for sure is working because it's working on my computer on both Linux and mac and you can paste that into your text editor and if that's still not working please let me know what error you're getting down in the comments below and I'll try to help you troubleshoot that and get this code working for you thanks for watching
Info
Channel: Ryan John
Views: 379,732
Rating: undefined out of 5
Keywords:
Id: RAKpMYOlttA
Channel Id: undefined
Length: 16min 54sec (1014 seconds)
Published: Wed May 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.