9. OpenAI ChatGPT API (NEW GPT 3.5) and Whisper API - Python and Gradio Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what brings me in here well I lost all my money in the crypto Market I bought some stupid token I heard about on YouTube and basically lost my whole life savings and my wife left me I'm sorry to hear that losing money and a loved one can be extremely taxing let's explore ways to cope and start rebuilding your financial and personal life okay how do I rebuild my life I feel like I just lost everything I've worked my whole life for so what do I even do now I feel like money was everything for me I've been focused on it for so long and then I lost it all in an instant it's understand able to feel better let's work on finding new passions and girls and explore ways to manage emotions and rebuild Financial stability well I do have some other things I do like teaching online I like to make these YouTube videos that people seem to like how does that help me that's great focusing on those passions can bring purpose and fulfillment back into your life we can explore ways to monetize your teaching and YouTube videos to help rebuild your finances hey everyone welcome back today March 1st open AI released two new apis the chat GPT and Whisper apis and this evening I had a little bit of time on my hands and whipped up a project that I thought I would share with you and since I got it out the same day that means it's probably pretty easy to use so this little application I built the whole front end recording my voice getting a response that it says back to me using whisper and the chat GPT API this whole application only took me 30 lines of code and probably about 30 minutes for me to even think of the project and write it so uh that's the power of some of these tools this wasn't possible when I first started this series and I got excited when this came out to win this drop today so I thought I'd get something out pretty quick quick to show people how to use it and give you some ideas on ways you might use this thing so the application I just demoed is pretty powerful like this the kind of stuff you saw in the old movies where the guy's talking to his phone and it's actually delivering pretty uh intelligent answers and I've created this using a few pieces just a few simple pieces the first part that you see on your screen here is this user interface and I've created that using a library or package called radio and radio makes it really easy to create interactive applications so this is people a lot of machine learning engineers and data scientists they're not that great at creating user interfaces or they might not be trained on making full stack web applications you know I'm more of a web app developer typically so that stuff I know how to do but a lot of people just want a very simple way to hook up a couple widgets so you can create a little text widget just like that and say oh createman interface give me an input of type text and an output of type text and call this function to transform that input into this output and it provides all these different ways to create input so there's audio input video input you can upload a file you can record from your microphone and then it creates this little widget for you right and so all I did is create a microphone input and then I could just record from the microphone hey this is me recording from the microphone right and then I submit it and what I'm doing when I submit it right how do feel about recording your content and exploring ways to monetize it let's work towards your goals and help you find success in your passions that's funny you see I'm actually transcribing what I recorded in my microphone using the whisper API so this new API that came out today what I did is record it it saves it as a local file and then I take that file and I post it over to open AI servers and it transcribes that and gives me the text right back to me and so what I do with radio is I call the open AI whisper API I get the text back and then I send that text over to the new chat GPT API and so you see there this conversation here is actually a continuation of what I was discussing at the very beginning in the intro of this video so it's kind of funny I just said that sentence there to test it out and it actually replied and I have all this context saved here so it's more instead of like gpt3 API that I used earlier in this series there's a whole dialogue happening and it knows is what I've said in the past which is really cool and so I'm saving all those messages that were saved in the past and I'm having this conversation using my voice transcribing it to a text using whisper sending that text to chat GPT API it's responding and guess what with the text that's responded from the chat GPT API I'm piping that to my operating system and having Mac OS X say that text out loud and so that's how I get the little uh the synthesized voice and there's a lot of different tools you can use for creating a synthesized voice you can even clone voices there's a lot of cool stuff you can do now so uh yeah let's go ahead and get started with discussing how to create this thing so to build this application we'll first start with the user interface so the most basic user interface we can just take it from the radio home page there's a little quick start guide right here and this shows how to use it and so you just need to pip install radio so you're going to install the radio python package so you can do a pip install radio just like that and if you look I've included a requirements.txt so you can do pip install Dash R requirements.txt if you just want to install the packages here but there's only two of them open Ai and radio so now that I have radio installed let me just show you a quick Bare Bones Radio application so I'm going to make one called a web.pi and I'm just going to paste in the most basic radio app right here from the home page so you can see you just import the package you create a new radio interface that you launch and what you need to do is say what are the inputs what are the outputs and what function do you call to transform the input into the output and so let's run this and you'll see what it does and so I'm just going to do I'm just going to run it so I'm going to run that file it's going to tell me it's running on localhost at support 7860 so I'm going to take this URL I'm going to go to my browser and you see I have an input called name right so there's my input and then my output is right here and those are both text fields and so whenever I put some input into text there so I'm going to say Larry here is my name and I submit it you see it's going to say Hello Larry just like that so my input I put hello that calls a function called greet and that input comes in as an input to this function called name and it just returns whatever returns goes into the output text so it says hello and then whatever name I put in right just like that and so what I could do here is just take the text I put in and send that over the chat GPT API and have it respond with text and have a little my own private chat interface over here that's not running on chat GPT on openai.com right we can make your own custom conversational bot for your own purposes or for your business there's tons of value people are going to create these everywhere I heard Snapchat has already integrated this a bunch of companies you're gonna there's gonna be chat Bots all over the place so it's good to know how to make your own there's gonna be a lot of jobs based around this concept Etc et cetera so um there you go that's how you use radio so now instead of a text input I want an audio input so if you go through the documentation of Grado you'll see there's all these different widgets and plots visualizations etc etc but you see there's a component for audio so all you need is this gr dot audio and you give it a source either you can upload a file audio from a file or you can do it from Source microphone and so you can see in the therapist app here what I did is at the bottom you see instead on my interface here for the web instead of interface input type text and output text I said input type it's audio I said source is microphone and then output is still text right and so instead of calling greet what we're doing is making a function called transcribe and so since this is an audio input what it'll actually do is pass in some audio input into the transcribe function so let's see what happens here so I'm just going to return hello for now and then actually I'll just return the transcription goes here and then I'm going to say a print audio just so you see what goes in here so I'm going to stop this right there I'm going to relaunch this um I called it demo um I changed it to UI so UI gradient and phrase audio input and I do UI launch right there and so I'm going to run this again and let's see now that I change type to audio look at that magically it went from input type text to record for microphone hi I'm recording from the microphone I stop the recording hi I'm recording from the microphone it knows how to record audio I didn't have to write a lot of code to figure out how to do this now I'm going to click submit and it says transcription goes here so we need to fill in how to process this and you notice I printed the audio input that came in here and you notice look at that it gives me a file path to a WAV file perfect I have an audio file which is exactly what the whisper API wants so I'm going to post this WAV file over to the open AI speech to text API look at this so if I go to the open API and their documentation you see there's this new one called Speech to Text I go to the quick start I need to import open AI I'm going to import that and if you follow this tutorial you know that we need an API key we've done this on every single video almost so you need an API key so you just need to go to your account settings on open AI generate an API key watch my other videos and everyone should know how to do this by now and put an API key there you can also put this in an environment variable or you can hard code it here if you're just playing around with this no big deal and delete it later all right so I put mine in fig file so I'm importing config and in there I have a variable called open AI API key so I'm just doing config.openai API key and just like that the open AI API is ready to use and so let's just take the first simple code example we have so it just wants a path to the file we have a path to the file because it's called audio so I'm just going to say open audio right that comes in from gradio and I'm going to say transcript equals openai.audio.transcribe this is the name of a model so I'll just leave that there whisper one and let's just see if it will transcribe my audio file so I'm going to say print transcript just like that and I don't think radio Auto reload so I'm just going to stop it and rerun it and so I'm going to refresh there uh try to transcribe this see if you can do it and then let's see transcription goes here ah so I need to return so whatever I return from this function here is what goes into the output so I return transcription goes here but what I really want to do is you see I printed transcript it gives me this response here in Json format and so the response has an attribute called text try to transcribe that see if you can do this look at that open AI whisper did it just like that and so I'm going to say return transcript and the key here is text and return that and I'll run this and let's try another transcription submit that let's try another transcription so look at that I already have a web application that transcribes audio that I speak into the microphone and that's only like 18 lines of code now what's there left to do well let's figure out how to use the chat GPT API so if I go here there's a new so we use text completion already look there's one called chat completion now and if you look at how this looks let's study this real quick so I'm going to copy this chat completion.create right and so I'm going to put this here all right I'm going to paste this right into my transcribe function right and so looks like all we need to do is call a function called create on chat completion here it has a new model note we've been using text DaVinci 3 right we've been using that in previous videos now there's a new one GPT 3.5 turbo so it must be better like Street Fighter 2 turbo so much better than the first edition in the previous Edition so this thing is getting better and better and not only that it's like 10 times cheaper if you read the documentation so a lot of things I've had I've had some comments events or in my earlier in the series saying oh this is much too expensive to be feasible and you can't have a financial advisor you can't do this you can't do that Focus Less on what's possible now and think about how fast these things are changing every single day getting better every day smart people working on this and so it's going to blow your mind what's possible by the next few months the end of the year and it's crazy so um look at this I'm I have I didn't know I was going to be able to create a 30 line bot here where I can talk to and it gives me an intelligent answer back so who knows where this thing is going so what I'm going to do here is say response equals open ad completion create and I'm just going to send it this list of messages now note instead of just a single message like completing one sentence note this is a list of messages here and each message here is a dictionary and one key is called roll and one's called user and one's called assistant you'll notice the very first message is role system and so here says content you are a helpful assistant so you're telling a TPT or chat gbt what it is so you're a helpful assistant you can say you are a command line prompt generator or you can say you are an ex sports announcer you are a rapper Jay-Z and that'll kind of Define kind of how it behaves a bit so I'm going to say you are a therapist right and that's how I came up with this one and so I just I'm just saying you're a therapist and so that's the first message and then the other roles here it's a list this is like the list of chat messages and so anything that comes from me I'm the user and so what did I say so let's say I'm saying uh who won the World Series in 2020 and I'm going to change this I'm going to say instead of that instead of who won the World Series I just wanted to say um whatever came in my transcript so I'm going to say transcript text and plug that right in and I'm going to take out this part for now I'm just going to comment that out and see the response so I'm just going to go directly from what I say in my audio it's send that in and I'm going to print out the response here okay so I'm going to print the response and yeah we might already have this working a simple version so I'm going to do that and so now I'm going to reload this guy one more time all right I'm going to say my first message to my therapist now I'm going to say I have a terrible gambling addiction and I'm going to say that and I'm going to submit it and that's what I said but let's see what the response is that I printed here so the content of the response is I'm here to listen to you and I want to let you know you're not alone in this gambling addiction can be difficult to overcome but it's important to remember the help is available can you tell me more about what's happening with your addiction and how it's affecting your life so great I'm already able to talk to the chat GPT API using my voice and I get a text response back right and so that's great and so you see the next role in here is called assistant so this keeps track of what the assistant said back and then I can say something else and continue this conversation so what I want to do is keep track of all the messages and so what I'm going to do what I decided to do and maybe there's another way you want to do this I just kept a list of messages at the top globally here to demonstrate this and eventually you know it's probably not a good idea to have a giant list but for the purposes this is here it's pretty good maybe you might want to store this in a database red is some other place you want to Cache this so I'm going to start out with the messages at the top here and I'm going to pass in the list of messages right and so here at the top in messages I'm going to initialize this uh with just the system here so so at the very top here I have my list of messages and I'm going to keep appending messages to this list and so all I'm all I have at the beginning is sending it you are a therapist right and then inside of here I'm just going to do Global messages since I want this messages to be in scope of transcribe and I'm going to be changing the messages inside of this transcribed function so what I'm going to do here is I am going to pass it the messages and so what I want is to append to that messages list so I'm going to do messages dot append so when I talk I want to append a a new message and so let me look at what I did here let me remind myself uh yeah I appended a new message of role user and so I'm going to append a message with role user because I'm going to say something and then real User it's going to be me I'm going to send it my transcript text right and then I'm going to call chat gbt with my list of messages right with my appended user text and then when it responds I want to append uh what chat GPT says back to me so I'm going to do another append here so first I'm going to store the system message so uh chat TPT responds it has this dictionary it has an attribute called choices message and then content so I'm going to do a choices which is a list so I'm going to say system message equals choices it's the zeroth element the first element in this list and then I'm getting the message attribute right there and then I'm going to get the con content which is that key right there and so that is the message from the assistant just like that so it's a response choices zero message content and let me just make sure yeah response choices zero message let's respond okay so that's the system message and so then I can just append that message so I'm going to say um messages dot append right and then instead of roll user I'm going to impend the system message so I'm keeping track of the state of this conversation I just have a big list of messages right here and I'm an append roll assistant right and I'm going to append the system message right there just like that and then I'll have what I said and what uh chat gbd said back and the next time I call it I'm going to pen the other thing I said and then another thing it sends back and then I have this big list of messages of this chat dialogue that I'm having with my therapist just like that so uh pretty cool there right and then all I really need to do after that is just return I just wanted to return the complete text transcripts so what I want to put in this output now I don't want to just put what I said I want to put what I said in my name and then I want to put what the system said back to me and record the whole chat dialogue and put it in this output here and so what I just want to do is build a string of text from that messages list and so let me go back to my example here and so what I did was create this chat transcript just like that and so what I did was just Loop through the messages and then record them all and so what I did was just Loop through the messages so I start with an empty transcript I Loop through all the messages and then I read record the role so I append the role and the message content and a couple Carriage returns and then I just return this chat chat transcript so I'm just looping the messages converting it to plain text and then return that text right here chat transcript just like that and then I can remove that response and let me run this one more time let me see if this works so I'm going to reload this hey I have a terrible gambling addiction I submit that and you see I said user hey I have a terrible gambling addiction assistant hi there sorry to hear you're struggling Etc et cetera let's see if I can record uh something else so I'm going to clear this start again well I've been playing a lot of blackjack lately and I hit on 18. I can't help myself and then you see my transcript I've appended more messages so I said uh well I've been playing a lot of blackjack lately and I hit on 18 I can't help myself sounds like you're struggling with difficult situation Etc et cetera treatable condition so on and so forth and I can even send other texts saying at the beginning of system here if I don't want those verbose responses I can say limit your response to 20 Words Be concise you can give it tons of instructions on how to respond so I could say you're a therapist respond as if you were a rapper Jay-Z right and so I can run this again and do this and it said uh I do a lot of drugs and I have a gambling addiction uh what do I do and now you can see the assistant response yo yo yo I hear you loud and clear you're dealing with some problems that bring you fear but listen up my friend because I got some tips to help you out of this red you got to get your way to Griffin so far so it writes a whole rap song in my I can say you're a therapist Jay-Z style rapper and it'll give me my response that way and so that's how you use this uh roll system here and then I can chat with it there and so the final thing I want to do to give this a little bit more Pizzazz is to have it say something back to me and Mac OS X actually has a command called say and it can say hello how are you and hello how are you right it's just a command line command and in the system settings in OS X you can uh in the there's a setting in there where you can change the different voices so it has a lot of different voices and if you want to experiment with voice cloning and other things you can do that as well but to keep it simple I just did a a system call and so in Python you can just import sub process right there so that's built into python so I can just do import a sub process just like that so then just below system message so once I get what the assistant says back I can do my subprocess DOT call and I'm going to say say and then give it the system message I want it to say just like that and yes system message I don't think I need the content now with this version okay cool subpress.com say and so let me run this again all right and then I'm going to record I have a problem I'm addicted to the stock market you are addicted to the stock market huh let me tell you something about it listen up first you've got to ask yourself why you're addicted is a thrill seeking or you're trying to get rich or quick it if it's the former find some legal ways to get to the tie you don't want the feds bursting in with an FBI tie if it's a latter slow down take it easy investing aimed no joke it's better to be cheesy and invest in index funds those with less volatility less loss more gain that's the money mentality but seriously addiction ain't no joke it's hard to overcome it's like you're being choked talked to a therapist find the root of the problem and soon enough you'll be out of the stock market column and there you go now you know how to use gradio open AI whisper API and the chat GPT API hope you build something cool with it that's it for this tutorial thanks a lot see you next video thanks
Info
Channel: Part Time Larry
Views: 77,993
Rating: undefined out of 5
Keywords: chatgpt api, whisper api, openai, gradio, python, tutorial, voice assistant, her movie, financial advisor
Id: Si0vFx_dJ5Y
Channel Id: undefined
Length: 23min 9sec (1389 seconds)
Published: Wed Mar 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.