Python Automation with ChatGPT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to learn how to use Python to automate things for us so we don't have to do them manually we're going to do something super cool and interesting we're going to automate two use cases first we will write a Python program that takes any blog article or any web page and it extracts all the headers from that page or article and then it translates all those headers to Spanish and saves it into its own HTML file so you can open it in the browser and basically see all the headers of that specific blog article translated in Spanish in the Second Use case will be that we're going to write a Python program that goes through all the files in our downloads folder it then checks if we have any files that are older than 30 days or were last used more than 30 days ago and it takes all these files and puts that into a new folder called 2D delete which we can then review and basically delete if needed so basically it helps us clean up our messy downloads folder with a bunch of old downloaded files in there but here's even more interesting thing we will do we will use an artificial intelligence tool that you probably have already heard of a million times called chat GPT and we will tell python to use chat gpt's API or the underlying API that chatgpt is also using to generate the Python scripts for those two use cases that I mentioned now if you haven't heard about trade GPT by now also not a problem because I have a full video on that which I made recently which I will link in this video so basically we are going to write a Python program that will accept an input from us about the automation use case and then go to chatgpt and say hey give me a python script for this automation use case and then we'll save it into a separate python file I think it's a really cool thing to try out and I hope you find it just as cool and exciting as I do so let's get into it but before we start if you have watched our videos and they were helpful for you please subscribe to support us as we keep on creating more free valuable content for you so if you're getting value from this channel just take this second and click the Subscribe button if you don't want to miss any new videos that we post then you can also activate the notification Bell before we start many of you may be asking why does everyone use Python for automation there are so many other programming languages out there and they all seem to be doing pretty much the same thing so what is it with python that gives it a monopoly on automation well the magic is in the ecosystem of python libraries for automation so python is a very simple language and it just happened that most low-level automation tools were written in Python so now you have this huge ecosystem of python libraries that can talk to the server operating system to Cloud platforms allow configuring infrastructure networking accessing various apis and this may be very small Standalone libraries or a complex automation tool like ansible which also happens to be written with python so basically a lot of work and effort got concentrated into that which means if you want to automate something you have a specific use case in mind you can be sure that there will be a great python library for that specific automation use case and if it doesn't exist yet probably someone will create it very soon whereas for other programming languages you won't be able to find library for every automation task or the library won't be as powerful or user-friendly as the python Library so because of that python just ended up being the language when it comes to Automation and when you think about this and compare the different programming languages every language is kind of good at one specific thing right so even though they're all programming languages and at its core they're pretty similar every programming language has its strengths in one particular area that differentiates it that makes it the best language for that area so for example Java is robust mature and a good choice for enterprise software JavaScript is used to write web applications and python is the language for automation so let's get into the demo part let's first write a Python program that connects to the API I said check GPT API but technically it's an open AI API which is what chatgpt uses under the hood so open AIS API is the application interface that lets us connect to it and interact with the artificial intelligence that drives and Powers chat GPT for example and many startups and developers actually use this API to build some cool new tools so how do we connect to the open AIS API in general regardless of what programming language we're using well first of all you need to quickly sign up to create an account on open AI platform this way you are kind of creating your own space on open AI for your user and one of the reasons that you have to sign up is that the compute resources needed and the processing that takes place by the artificial intelligence in the background needs some compute resources and processing power so obviously they can provide these Limitless to everyone so they have some limits per user so that everyone can equally access and use this for free to a certain point so the first step is to sign up and create a user account on open ai's platform and once you have your user account the Second Step will be to create what's called an API key which is random generated stream which is a unique key that is associated with your user account which means when we use that API key in our Python program to make a call and connect to the API the API will know that it's associated with our user account and by the way this concept of creating an account on platform and using an API key to identify your user when making a request to it is a very general concept of using an API nothing specific or unique for open EI it's that simple so we need those two simple steps to prepare everything and get started using this API so let's go ahead and do that either open this link directly or you can even Google open AI API and it shows you exactly the same page right so right here on the open AIS page you basically do sign up create your account just providing your email address and some information actually already created one and just log into your account and that is your personal space and now let's create the API key which we will use in our Python program to connect to the API so clicking on your user account icon here view API keys so it's pretty simple actually and you will probably have zero keys at the beginning for a fresh new account so go ahead and create a new secret key click on it obviously I'm going to remove this later but before you click OK make sure to copy this secret key somewhere safe and accessible because we're gonna need that in our Python program so I'm going to copy that okay and after that you won't be actually able to see that or access it so make sure to paste it somewhere and that's how easy it is so we have the account and we have the API key to connect to the API the two things I want to mention first of all if python syntax and python generally is completely new to you then you can check out my Python tutorial where I go through all the basics including the variables if else statements functions syntax and all these basic concepts so you can learn how to work with python and the second thing is we need to have python actually installed locally so that we can run Python program if you don't have python installed yet or for example if you're on Mac you will probably already have python if not you would have to install it so you're going to have to have Python 3 so that's the new version of python and you execute that with Python 3 command actually like this and you also need to have python package manager installed which is called keep also with three so if you go through my python course you will have all these tools installed and you will know basics of python syntax so you should be able to easily follow this specific demo so let's start writing this python script and we will see what we need and how to put together this code step by step so first of all I'm going to create a separate folder for all the Python scripts that we're going to produce in this demo so in YouTube tutorial series folder and I'm going to create a new folder and let's call this python GPT and I'm going to change into that folder and now I'm going to open this inside Visual Studio code [Music] and there you go you can also just open Visual Studio code and then open the folder from this UI I just used a shortcut so this is a folder where I'm going to put the python script so let's create it and I'm actually gonna also call this python script python chat GPT dot p y and we can just start writing our script so first of all whenever we connect to an external API we're making a request so we're going to need a library a python library in this case that will allow us to send requests to external endpoints like API endpoints now how do we make a request to open AI well we actually have two options we have a generic requests Library which is great because you can use it for whatever API or whatever tool you want not just open AI so it's generic and when you learn how to use it you will be able to make a request to any API but usually the tools that provide the apis will also provide a library that hides the underlying complexity of making a request sending all the parameters Etc or setting the API key and lets you interact with the API with a more high level code so in that library that will give you objects and methods and so on that make the whole process easier and generally that's the purpose of a library right to make writing the code easier for us so we don't have to write everything from scratch and openai has a library for python and node.js for example and you can see the usage of the library for each language and you see that it's called open AI so that's the library name so you can install openai library and import it in your code and use it like the example you see right here you can see the syntax and how to use the library in the official documentation of that specific tool like what methods or objects you have available and so on and this will be very useful if you're doing more complex things and making more complex queries to the API and you want to use the library to do those things without over complicating your code but open AI is pretty recent and its Library may change they may change methods and syntax and so on because they're still adding and developing a lot of stuff on top of that so things may change so if you're gonna watch this video maybe a year later it may look very different and because of that I'm gonna stick to the generic requests library because we just want to make a request which is a pretty simple use case so the code between using the request library in openai library will not be very different and also I think it's better to learn the things that you can use for many other things as well maybe it's just my personal preference but in any case for this example I'm going to use the request Library as I said it shouldn't make that much difference if you want to you can actually go with the Open Air library and use it instead not a problem at all so you see here it's a module for making HTTP requests and in Visual Studio code you can see a cheat sheet of how to actually use the library which is pretty cool so now we have a library that helps us make requests two external endpoints now we have to find out what is the endpoint of open AI API that we want to connect to and remember we're writing a python script that takes our input which is going to be any automation use case it will then go to the API and say for this specific use case that user entered here I want to have a python script just like you would type into chatgpt please write a python script for this specific use case so we're doing that but programmatically so now we have to find out the endpoint that allows us to do that where do we find endpoints of an API well in the API documentation so right here in open AI platform you're going to click on documentation and this is basically a description of how to use this documentation and what does this API provide and again each whatever API you are using it will always come with a documentation because obviously you need to know how to use that API and on which endpoints you should send what request right so if we scroll a little bit down here you see the section prompts and completions which is the core of this API as they are stating here and this is exactly what chatgpt uses you input some text as a prompt and the model will generate a text completion that attempts to match whatever you asked from it and there are a bunch of other stuff but the completions is exactly what we need so either you click here and it takes you directly to that completion section or if you scroll down in the API reference you also have these completions section which is basically the same right so we have our endpoint I'm actually going to copy that and add that into my code and save it into a variable and I'm going to call this API endpoint we also have our API key let's actually also add this right here and you should have copied that somewhere and now you can just paste it in and by the way your API key is a secret key so it's actually sensitive piece of information you shouldn't be sharing that with anyone and you shouldn't have that directly in your code so later when you have the script ready we're going to remove this API key from here and we're gonna read that from an environment variable so it's not hard coded inside the script but we're going to do that later so we have those two pieces of information we have the endpoint and we have the key that allows us to connect to and authenticate with the open AI API in general so that it can actually take our requests and answer them so we're getting there slowly now that we have the endpoint and the key for authentication and the library to make a request it's now time to actually make the request and again we're gonna refer the document Edition to see how to generate our request I'm going to switch back first piece of information we have is that we need to do a post request so instead of get requests where we're just getting some data we are making a post request which means we are expecting to get some data back however we need to First send or post our prompt to get that request right so we have to send some information and then we'll get the reply to that and in order to make a post request with the requests Library we have a simple post method in this post method accepts several parameters the first one is API endpoint because request Library needs to know where are we posting this data which endpoint are we connecting to and now we need to Define what information are we sending in that request and there are two pieces of information or two types of information we're sending with the request first one is the metadata about the request and the second one is the actual request contents so switching back again and again this is not specific to this API this is how the requests are structured so the metadata would be things like the token or API key for authentication so it's not the actual content of our request it's not the prompt that we're saying okay please give me python script for this use case but it's more like additional information about the request itself which is here is my key to authenticate that I'm allowed to make this request again it's part of HTTP we send the metadata as headers so we're gonna set the headers on a request and by the way if you look to your right and let's actually make this larger you have the example request which we can use as a reference which you can basically just copy and it will make the request with those parameters and as you see this is a current request which you can execute on your terminal using curl command line tool and again very conveniently they also provide this same request example in Python and node.js so python is what we need and remember I told you that we have two ways of making a request to open AI One is using the request Library basically the library that provides us a generic way to make a request to any endpoint and the second one is the openai library itself and this example right here is using the open AI Library so this is an example code of how to make a request using that Library however I said that we're going to use request library because our use case is pretty simple and you can then go back and compare the openai library example with what we write so because I'm going to be using request Library I'm actually going to switch back to Kernel and I'm gonna take the information from here so we have to map that now to python request and these are HTTP headers where it's setting the API key and saying that it will send requests in a Json format so let's set those two headers which is going to be the second parameter of the post method so we need our headers equals two and we can either directly write those headers inside as an object or we could also save that into a separate variable let's call it headers or request headers and set those two headers that we need right here so we have the content type I'm actually going to copy those two which basically says that we are sending content type of Json format so that's the format of the request that we are sending to the API there you go and we have the header to set the API key now obviously we have to reference or substitute the API key so either you can type it directly here like this a string however because we want to have a clean code and we are storing things into variables we're going to reference it here simply like this this is how string concatenation syntax looks like in Python so we have the string and this is just a reference to a variable so we're setting the headers and we can set that value as a second parameter so now the post request is made to that endpoint with those headers and now we need the actual request What's called the request body so now the contents of the actual request come and that's going to be the third parameter of post method and again we can have an object here like this or we're gonna create another variable we're going to call it data or request data let's call it request data we can also call this request header like this so we have a little consistency there going back we have the example here and these are basically the request data right things that we include inside the request and this is an example request and this is the full list of parameters so these are things that we can include in our request or request data to basically communicate with the API endpoint and tell it exactly what we need right so let's go through these parameters and see what options we have here the first important and required attribute is what's called a model and this actually refers to the machine learning model of open AI that we want to use and as you see as an example value it has text DaVinci zero zero three so what is that what is a model well if you watched my chat gbt video I explain what models are a little bit however we can go through this here as well and if I open this link that says model overview we're gonna see a couple of items here these are basically group of models and if I scroll a little bit down right here gpt3 that powers GPT that's where the name comes from has a list of various models so basically open AI is an organization that works on various different types of AI tools it doesn't just work on AI that produces text or visuals it works on different types of AIS and because of that they have various models that specialize and do different things so gpt3 is actually one of these model groups for processing and generating natural language and this is a list of machine learning models that open AI has developed and uses in its projects like chat gbt or delete or whatever and also the machine learning models that that you can connect to and talk to through the API and you see the descriptions here so text DaVinci 003 is basically the name or ID of that model that's how they called it and as it says it is the most capable model it has been trained with massive amounts of data and can do very complex things and you also see it has been trained with data up to 2021. of course this may change if you watch this video one year after I release it obviously you may see different stuff in this documentation but currently that's how it looks like so that's what a model is and when we connect to the API we can basically select which machine learning model we want to talk to well in our case we want to generate a python script which is a little bit maybe a little bit more complex task so we're gonna go with the best option that we have text DaVinci 003 and that's also the example that we have here now before move on and use the text DaVinci model in our request I want to mention another model group that is listed in the overview called codex so just from the name you know that it's about code and this is basically a set of models for coding examples specifically now this was actually created on top of gpt3 models so it builds on top of the capabilities of gpt3 models but it has been trained with extensive amount from open source code on GitHub and it produces code so if I click inside you can see a detailed description how it's been trained and that it's most capable in Python plus proficient in other languages and it has these two models under its group now here's the thing I actually tested it out to compare it with with the gp3 model and it didn't really perform consistently on some occasions it didn't actually produce proper results however when it actually gave the proper response it was a nicely written well-structured code so I think there is a lot of potential there but it didn't work for all the use cases you can also see that it's in better version maybe after a couple of months or a year when you're seeing this video it will not be in beta anymore just like the other model groups so because of these reasons we're actually going to choose the gpt3 model called text DaVinci 003 which works perfectly for our use case and it actually generates really good code as well with consistent results so that's what we're going to choose however the great thing about using this API and our example is that you can literally just replace the model name with any other model names that you see here including the two models under codex group and basically just test it out and see for yourself you will not have to adjust or change anything in the code you will only replace the model name and basically compare them if you want to so even though we're going with this option for the demo you can safely use code DaVinci instead and basically just compare the results yourself so choosing the right model here is not a big deal because you can replace it later very easily just by changing the name of the model so that's the model I'm going to choose to make our requests so again when you use an API you can choose between those models and that's the model we're going to choose so that's going to be our first parameter there you go the second most important parameter in our request is obviously the prompt because prompt is the main part of the request that tells what we are asking for exactly and again for a comparison prompt is what you type in when you're chatting with chat DBT for example in this case we're sending that prompt programmatically inside the request and let's change this prompt and we're going to tell the text DaVinci model to write python script for hello world super simple right because we just want to test that we make a request successfully and we get a response so prompt is done and we have a couple of other options here but from those options these two are probably the most important so what are these very simple mix tokens basically limits the response that you get in tokens so when these parameters generally we are controlling the behavior of the API model when generating the text so this specifies the maximum number of tokens such as words or punctuation marks that the API model should generate in its response and I'm going to set these Max tokens to let's do 100 for Star the hello world script obviously is going to be very small so it's not going to need 100 tokens but we can set it to maximum 100 tokens so it's not gonna exceed that amount and we have one more parameter which is called temperature it's also very simple it basically defines or tells machine learning model hey when you're generating this response make sure to be super creative like go crazy with your creativity and that will be a temperature of one or we can say pay machine learning model please be as precise and predictable as possible and that will be temperature of zero so it's zero and one you can control that but obviously you have everything in the middle so with 0.5 temperature we can ask the API engine to generate response that is half creative half predictable so I'm going to copy that and we're gonna set it to 0.5 and we're actually going to ignore the rest of the parameters which are the most important ones and once we have our request data we're gonna set that as value for this Json parameter here so we have pieced together our request so this is actually ready to connect to the API and get a response so again to walk through this what we're doing is using the requests Library we are connecting to the API endpoint of open Ai and completions we are including the API key that is associated with our user account so that open AI can say okay I know this API key and it's associated with this account so you are authorized to make requests and once we have that authorization it will then take our request data and see what we are actually requesting and it will say we want to talk to text DaVinci 003 model and we want to ask it to create python script for hello world please do not exceed 100 tokens in your response and be somewhat creative but also more predictable and then we will get a response and we can then save this response into a variable called response and of course we want to see the result of our hard work which means we want to print out the response and see what we actually got so I'm going to do print response dot Json so here we're getting a response and the response is also in Json format just like the request that we sent so with this header content type we Define that we're sending Json and we're also receiving Json response and this is how we can grab that Json response using dot Json method now of course when we make requests when we run programs programs sometimes have errors so some issue may happen so what we can do is we can first check what's our response actually successful or maybe add an error if it had an error then we won't have a Json response we're not going to have any contents or any normal response contents is Json so we can print out the error message in that case so what we can do is we can say if response status code so we have status code for Success which is 200 which means everything was fine the model got our request our request was valid so we didn't make any mistakes when creating it and it was able to come up with a response which means status code 200 and in that case we print that response if not which is else we're gonna print request failed maybe we sent a wrong request or maybe the API server is down or whatever reason request build and we want to also know the reason why it failed so we're gonna print out the status code and this will be a reference to spawns dot status code and in Python this is actually python syntax if you want to use a reference to a variable inside a string we have this really nice shortcut or syntax where we add F before that string starts you see the syntax highlighting also changed like this so now python knows this is a string and whatever is Within These curly braces is actually not a screen but variable reference and this is going to be a number and in Python we can turn the numbers into Strings using this very simple scr or string built-in function and this is our super simple python script for connecting to open AI API so now it's time to execute this python script so we have Python 3 which we need to execute the python script and we have peep3 python package manager to install any libraries that we are using in our python script so you need to install requests because we're using a library called request which is actually external library that isn't included in the python package which means you will have to install that using peep3 package manager so it's a python package that needs to be installed I have already installed that and because of that first of all my visual studio code shows this quick prompt with the examples Etc however and let me demonstrate this if I go back and if I uninstall so pip3 uninstall requests and let's remove that and if I go back you will see what happens and that's going to be your case or how you see the code in your Visual Studio code editor you will have this yellow line underneath and if I Mouse over it will say import requests could not be resolved because we don't have that Library installed locally so even before running the script python says I don't know where to find this Library I can import anything called request and that will be your state if you don't have that installed which means either globally on the terminal or right here in the project folder you're going to do keep three install requests there you go and now we have installed requests and it shows that documentation reference again no squiggly lines here anymore and it will work and now that we have Library installed we have Python 3 installed let's execute our script using Python 3 and the name of our script which is python chatgpt dot pi let's execute and I have actually made a small syntax error here which is this part just like here it has to be a valid Json object like this or for consistency we can actually use double quotes and there you go so that should fix our attribute error since we now set the attributes with valid syntax so let's execute our script again and there you go we have a response output so right here where we're printing the Json response that's what we have right here and as you see the response is a Json object and we have a couple of things that we received as a response in addition to the actual python script that we requested right here so just like we have metadata on the request we have metadata on response as well says that it's a text completion object which model was used and so on and inside these choices attribute we have text attribute that actually contains the python script that we asked for so right here this statement is the python print function to print hello world which is the response to our prompt that we set right here and let's say we don't care about all these metadata about the response we only care about the text that we're getting so we can ignore all these other stuff only print out the response text so on the Json object we're going to grab the value of choices and this is python syntax we're going to say give me the value of choices which is an array as you see and we're gonna grab the first element of that array again I teach the syntax basics in my python course so if you want to know what arrays are what objects are how to use them you can learn all of that there and then from the first element of that array we get this Json object and we want to get the value of the text attribute so again give me the value of text and now this should print the value right here and let's see that actually let's execute again and there we have it by the way when we're executing this python script we're making a new request just like when you enter a new prompt into chat GPT so this time it gave us a new response with very slight differences like edit a comment here on the top and this line here for you know python executable so we have actually created a program that connects to DaVinci text model of open Ai and now we can send whatever prompt we want programmatically and we're going to get a response back from that API that we're connecting to which is pretty cool so let's try one more example and let's ask the API to write python script for let's say printing out two days date and let's see what we get and looks at the code it Imports daytime library and prints out today's date now if you have used chatgpt already you know that it usually gives you additional explanations in the text form on top of the code and you might get that here as well again it's the same API so you might get the same results when you access it programmatically and the text can be helpful but usually if you want to quickly generate python script you don't want to have those explanations and text you you are only interested in code so in that case just to make sure the API returns only the script and no additional text we can actually adjust our prompt and tell API to only send us the code so right here I'm going to say provide only code no text so we're just telling API what we want in my case it shouldn't be much different because it already gave me just code but it may be different in your case let's execute that again and in my case it just remove the comments which are kind of unnecessary because the code itself is pretty self-explanatory so we can leave it at that going back to our main goal is we want these python script to take our input whenever we start the script of what use case we want to automate with python right so basically this prompt here it shouldn't be hard coded here but rather it should just take input from us and then execute that as a prompt so basically write python script for blank so this is going to be our input for whatever use case we provide as an input and then the rest of them will stay the same the things that we'll always apply such as we always want the python script and we always want the code and not the text so this one needs to be parameterized and taken from user input how do we get user input from python it's actually pretty easy we have a library called ARG parse or argument parser import Arc parse there you go and different from the request Library which we had to install using pip3 uh we don't need to install Arc Parts because it is actually a built-in module so it is already part of the Python installation and again you see the description here it basically lets us parse the command line arguments that we provide to the python application when we run it so it will look like this that's the python script execution right here after space we're gonna provide our use case as an argument and the argument will be provided like this so write python script let's change this to to print today's date so that's going to be the argument that we're going to parse inside the python script and then use it here and this is a code to read the user input we're gonna use the art parts argument parser object let's save it into parser variable so this is an object that has a method that allows us to read the parameters or arguments that we provide here so this basically this part here is an argument that we pass to the application so in argument method takes two parameters the first one is the name of the argument so it's kind of like assigning the whole value here to a variable so let's call this prompt because that's what we're providing and then optionally we can add description of what that prompt is and that's a help attribute and when you say this is a prompt to send to the open AI API and finally using this parser object we're going to call a method called parse arcs which is going to go through whatever arguments we have added here and it's gonna read the first argument and say this is a prompt if we had second argument and we edit here it will assign it there so basically parse any input we provide here and we can save it into the variable called arguments argument the word in this case means the input that we provide to our script and now we can access this value using arcs.prompt because we called this value prompt kind of like assigning to a variable and we're going to use that right here again the python syntax for using a variable within a string so we have arcs dot prompt and of course we have to use f to tell python this is a variable reference and not a string so again now when we execute our script we have to provide an argument because we are expecting it now in the application so we can't just leave it otherwise we'll get an error and when we provide that with these three lines of code the argument parser will basically register that input or argument with the name of prompt and then when we parse the arguments we can access it using arguments variable dot prompt so now let's actually execute this and see how it works so executing the python script with this input let's see what comes out and there you go as I said every time we execute this script it actually makes a new request to the API and we're getting a different response that's why it looks different than the previous ones let's try another example like print hello world and there you go so now without having to modify the prompt here in the code we can just easily pass on whatever use case we want to have here right let's do some other simple example let's say write python script to calculate number of days from I don't know let's say 1 million minutes and let's see what we get and there you go so these are obviously super simple examples when we have the script ready we're gonna actually execute our two use cases of extracting the headers from blog article and translating to Spanish and then cleaning up your downloads folder with all the files that are older than 30 days so we have parameterized our script so we're passing in the prompt as an input we have two more things to do before we're done with this specific script the first one is we want to save the script whatever we get here right here as a result we want to save it into a separate python script file right so instead of having that in a console and having to copy that and create a new python file we want that to happen automatically and as I said at the beginning having an API key directly here since it's a sensitive data is actually not a good thing to do so we're gonna extract that into an environment variable both of these things are very easy to do so let's quickly add them here so first of all let's write a code that instead of printing it out to the console it just saves it into a file so let's remove the print here and instead let's save this into a variable and let's call it response text so this is the attribute that contains our code and then we're gonna create a file and we're gonna write whatever the response text is into that file very easy to do with python so we have keyboard with open so this will basically open or create a new file and we need to provide a file name for now let's call this output Dot p y so it's a python file and the open method takes a second attribute which is either we can write the contents to this file or if the file already exists and there is some content we can instead append to the previous content in our case we always want to write it from scratch so we're going to use W so that's basically the mode in which we want to open this file in right mode append mode and so on and we're going to say open is file so this line basically goes and tries to find a file called output.py if it doesn't find one it creates one and it opens it in the mode that we specified right here so in this case it will create a new file and open it in right mode now that we are in a write mode we're gonna write to that file using file dot right super easy as you see and just providing the contents we want to write to that file that's it these two lines basically allow us to save text python file so let's execute and see that it works and there you go we have an output python file if I open this we have the script API returned inside and as I said when we have the right mode open if we change the use case let's say print hello world it will overwrite whatever is inside output script let's see there you go instead of appending it will just overwrite or replace whatever was in there there you go now if we want to use this python script to generate files for multiple use cases we want to save those use cases in separate files right we don't want to keep overriding in this single output file right which means we want to create a new file every time we generate a new python script so instead of hard coding this value or file name here we're gonna also pass that as an input so we're going to tell python please send a request to generate python script for this use case and then please save it into a file with this name so we can provide that as an input as well which means this will be a user input also and we'll look like this so this is the use case or prompt and the second parameter will be the file name like Hello World dot Pi for example and the same way as we did before we need to First register this argument and then give it a name so we can use it right here so going up I'm just going to copy this line let's call this file name which it is file name of the file to save python script there you go so we have the second argument and we can access it using arcs.file name very easy arcs Dot file name and now if I execute this we should get a new file with this file name and the response text inside let's do that and we have this hello world dot Pi with print hello world inside awesome so that's one thing we wanted to optimize the second one is to extract that into environment variable which is even easier I'm just gonna copy that and instead of hard coding it directly here we're going to export an environment variable let's call it open AI API key you can call it whatever you want let's remove those quotes so this will create register environment variable in this terminal session so you have to do this in the same terminal session where you execute the script so let's execute this one but now we have to read that environment variable open AI key inside python script and we do that using a module called OS and then we execute OS get environment and the name of the environment variable as a parameter that's it so this line will read value of this environment variable and assign it to a python variable here and again you have to export this environment variable before you execute the script in the same terminal session so that was our final optimization let's actually delete those and execute our script again and there you go we have our result and we're not exposing our API key in the code and with this our python GPT script is done and ready to produce other python automation Scripts now let's go ahead and use our script to generate python code for our first use case which is going to be going through a blog post and I have an example blog post that we're going to use which is this one right here and it has different levels of headers you probably know headers from HTML so you have larger ones and then we have some subheaders like this and so on so basically the script will go through the whole blog post it will extract all those headers it will translate those headers into Spanish and then save that into an HTML file so when we open it we basically will see this blog post but basically compressed in just the list of headers translated into Spanish so let's go back to our script and let's ask it to generate python code for this use case and usually when into interacting with API it's important to formulate our prompts properly so it understands exactly what we need so let's write we want a python script to extract all headers or let's say HTML headers from a web page [Music] Translate to Spanish and save the result into an HTML file and whatever python script this provides we want to save it into a python file let's call it extract and Translate headers dot p y so let's execute this and let's see what we get as a result and also to note that your result may be very different to what I'm getting because again it will generate a different response every time we make a request so the file was created let's look inside first of all we have this dot here let's remove that and let's look at our code now this is an interesting part is you see it basically gave us a code which is not finished right so we have a syntax error here because it hasn't finished with the result and the reason for that is if we go back to our church gbt Python program you remember when we were creating a request we set this parameter called Max tokens which basically limits the number of tokens or characters that you get back as a response and because we have set a limit of 100 we basically got those hundred tokens here so we didn't get the complete result which means we want to increase that so we have enough tokens to get the full response so you know what I'm gonna set it to 500 just to give us enough amount and let's try again the file has changed let's go back and let's look through our code just one glance it actually looks pretty good we can definitely work with this so let's go through the code that we got as a response from the API let's see if there are any adjustments needed and then we're gonna test this program with our example blog article so first of all we're using the same request a library that we used here because this is a generic library that you can use basically to make requests to any URL and this will be the URL of our blog post so let's change that right away I'm just going to copy that and paste it in here instead of example and then using the requests Library we're just requesting that page and on the page we can access the contents or the text of that page and this is a library called Beautiful soup which is a very popular python library for extracting crawling and basically working with websites so anytime you want to extract some information from web pages this is a library that you're gonna use it is very powerful and it is very popular and it was also suggested for this specific use Case by the API and that's the library name PS4 beautiful soup 4. if I hover over it you see the description that basically parses the whole HTML into a tree representation so you can then access individual elements of that HTML page so that's what we're doing here parsing the page contents the whole HTML using an HTML parser it also works with XML we're specifying that this is an HTML page and we're saving that result here and this is a line where it goes through that tree representation of the HTML page and it basically finds and grabs all the headers on that page so these are HTML header attributes and we're saving that array of headers and their values of course into headers variable so this is a part where the headers are being extracted and this is a part where they are being translated as you see also very easy code it's using Google translate library and again we have a description so Google provides this translate API for free which is amazing and you can use that in your Python scripts to translate whatever you want so this is initiating the translator and this is the code section that basically takes the header text and translates it into Spanish so that's a destination language to be translated into and it then collects back those translated headers into the Spanish headers array and this is a basic for Loop you learn about for Loop in my python course but it's super simple actually it just goes through the headers or the header elements and it says for every header give me the text it will translate it into Spanish and then add that into Spanish headers array so now this array will have a list of all the header texts in Spanish and the final step is where it's creating an HTML file with HTML opening and closing tags you already know this because we used it right here basically opens the file once it creates these Spanish headers.html file which is amazing also came up with a pretty good name and then between those html text it basically goes through the Spanish headers array and for each header it adds these H1 with a header text now overall the script looks pretty good actually it's surprisingly good for the very first request and we don't need almost any adjustment except for this last part because since we're extracting headers of all levels we don't want to then save them as H1 right we want to keep that hierarchy of headers and titles and subtitles so we want to also reuse them right here so that's the only part we're gonna adjust and it's also pretty easy so what we're going to do is instead of hard coding these H1s here we're going to take that information from the header element itself so we're we're gonna need to do a little bit of adjustment so first of all going back here this only saves the header text right the actual values of those headers like this one for example and it discards um the rest of the element information including what element that is right whether it's H1 or H2 or whatever so we want to keep all that information including the text so we're gonna collect not only the text but the whole elements so now this individual header items will not be just the text right not just this text value but the whole HTML element which has the type which is H1 H2 each whatever the text and so on this means the actual value would be header dot text we have used it here as well and we will have a value called header dot name which represents exactly what type of header that is so I'm gonna copy that here as well and that should preserve the hierarchy of headers in the final HTML file as well now we can actually go ahead and test this and see if we got everything right or if something else needs adjustment so let's go ahead and do that however before we execute the script we need to install all those libraries and if you haven't installed them yet you will also see these yellow lines underneath and I can show you actually so if I uninstall them because I have them already locally let's confirm yes give it a little bit of time this will be your state so before you execute the script you will simply need to do peep3 install bs4 so let's do that and you also have to install Google translate library but in my case it didn't work with the latest installation so I had to use a specific version so we're going to provide a version here as well so it's going to be 3.1.0 a0 let's install that as well and that should fix that underlining again there you go and now with all the libraries all the dependencies installed we can go ahead and execute our script extract translate headers let's see so we got an error that says no attribute name so it seems like whatever this one or this translate result is giving back does not have a name attribute it only has text so to debug that let's actually see what is inside let's do translated translate result like this and let's print this out and if we look at any one of those items like this one for example we see that it has those attributes like Source destination text which is translated into Spanish which is great and it doesn't have the header name attribute which is fine we can fix that also very easily by grabbing that information from the header so in the Spanish headers array we can basically save an object that has the header text the translated header text and the header name so we're gonna prepare that object let's call it translate result so this is going to be an object and it is going to have a text and the value of the text will be the translation result dot text attribute right because we saw that this part here gives us the whole translate object back this one and then we have to basically grab the text from it so that's going to be our translated text and then we have the header name right and we're going to grab that directly from here enter name and that's it and finally we're gonna add the translate result we can also call this translated header and add that to the Spanish headers array so now in that array we don't only have just a list of text so this will create a python dictionary with text and name attributes with the translated text and header name attributes so the header name will be a value of H1 tlh6 and we're appending or adding that dictionary into Spanish headers array and here is we're iterating through that list of dictionary we're gonna grab the name attribute of the dictionary and text attribute and we learned the syntax of accessing the dictionary attributes is square brackets and the name of the attribute which in this case is also the name also notice that because we're using single quotes here for the whole string we have to use double quotes inside because otherwise we're gonna get an error because it's gonna think that this basically ends the string expression right so just watch out for that and again let's grab the text attribute of the dictionary and the name attribute again and let's execute the script and see if it works so the script completed we didn't get any errors which is a good sign and we have our Spanish headers.html file which should now contain all headers translated in Spanish and looks pretty good actually now the last thing we want to do is go ahead and open this in a browser and I can actually just drag and drop it into the browser window like this and there you go looks pretty Spanish I personally don't know Spanish so I can't tell how precise and correct the translations are but that's up to the Google translate Library the rest of the stuff looks pretty good actually we have our header here key so we have the larger headers and the subheaders we can even inspect that in the browser and let's see there you go so we have H2 H4 all levels of headers in our HTML file awesome so that gave us the first script for our first use case and I think the code that we got from the API was actually pretty good as a starting point because we only needed to do a couple of changes and it worked pretty well now let's go ahead and generate python automation code for our Second Use case so I'm gonna close this one and we're gonna ask our python script to give us python code to go through files in downloads folder check their dates and if they are older then 30 days move them to folder called to delete so that's our use case let's say I have a very very messy downloads folder where I never delete stuff from I just leave it in there so it kind of gets pretty messy and crowded and I'm going to ask a python script to basically just go through all the files and make sure that it leaves all the recent downloads in the downloads folder but everything older than 30 days should be moved to a separate file called to delete which I can then manually just go through and move to trash if I want to and we're going to save it into a file called clean downloads dot python let's go and there you go we have our clean downloads Python and again it added some weird line at the beginning I'm assuming this is a kind of leftover of text explanation that API is returning let's remove that and let's see what we got here so we have three libraries that we're using the OS module that we already saw then we have another module that is basically used to copy archive files and folders on our file system and finally we have date time that we're gonna need to calculate whether files are older than 30 days so let's say what this auto-generated script does first it sets path to Downloads folder in user's home directory which is good if I don't want to basically provide the whole path like users my username slash downloads basically just expands or uses the the user's home directory shortcut then it provides another path for the new directory called to delete and this is really good because it checks whether the to delete folder already exists if not it creates one using this OS module so OS path exists basically just checks whether file or folder exists and make tiers creates a directory pretty straightforward then it's going through the list of files inside the downloads folder and saves them in the files variable we're getting the current date and then for every file in this list of files in downloads folder it gets the files modification date then it checks the time difference from now and the last modified so if it's a recent file like it was modified yesterday the time difference will be one day if it's an old file it hasn't been used longer than the time difference will be higher so here it checks if the time difference is higher than 30 days then using the sh util module it moves the file from downloads folder the file name to the to delete folder this actually looks pretty good you use the right libraries the code is pretty simple you can even edit those comments on top the code is also nicely structured I actually thought that I would have to adjust way more in the final result but it seems pretty good so let's actually execute this I do have lots of files in my downloads folder which a lot of them are probably way older than 30 days so let's execute Python 3 clean downloads dot py I'm really interested to see if this works first time and it seems like my downloads folder was cleaned up so right now I have those five files left and if I click inside those you see that they have been all modified recently so these ones were today this is modified like 17 days ago and in my home folder it created to delete directory and as you see here in date modified all the files and folders inside have modified date which is more than 30 days ago so the logic worked moving the files worked and we got exactly the result that we needed I am genuinely impressed that we didn't have to modify anything at all to make this code work amazing and you even have the comments on top of those sections that say exactly what it's doing really really good so yeah we have generated a couple of Python scripts that automate some things and we also have the Python program that we can feed any automation use case that we want and it will generate a python script for us I'm really happy with the results I hope I was able to teach you some valuable skills and things in this video please comment in the sections how this whole demo worked for you or whether you had any issues and what results you get or how quality and even how good the results were that you got from the API and finally just share some other valuable and really practical use cases where you think you can use this python Automation and maybe some more examples that I should cover and with that again thank you for watching and see you in the next video
Info
Channel: TechWorld with Nana
Views: 143,626
Rating: undefined out of 5
Keywords: python automation, python automation projects, python automation ideas, python automation example, python automation chatgpt, python chatgpt api, python chatgpt, python openai, python chatbot tutorial, automation with python, automation with python and chatgpt, python automation tutorial, python automation for beginners, python scripting, chat gpt python code, techworld with nana, python, python for automation
Id: w-X_EQ2Xva4
Channel Id: undefined
Length: 74min 5sec (4445 seconds)
Published: Tue Mar 07 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.