In this video, we're 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'll write a Python program that takes any blog article or a
webpage 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.
And 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
Thursdays or were last used more than 30 days ago. And it takes all these files and puts that
into a new folder called to 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
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 chat GPT is also using to generate the Python
scripts for those two use cases that I mentioned. Now, if you haven't heard about chat 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 chat GPT 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 a school
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 the 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 chat GPT API, but technically it's an open AI API, which is what chat GPT uses under the hood.
So open AI's 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 AI's
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 these for free to 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
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 by making a request to it is a very general concept of using an API, nothing specific or
unique for open AI. 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
AI 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, okay, make sure to copy this secret key somewhere safe and accessible
because we're going to need that, you know, 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.
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, 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 three. So that's the new version of Python. And you execute
with Python three command actually like this. And you also need to have Python package manager
installed, which is called PIP 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, Chet's GPT. And I'm going to change into that folder. And now I'm going to open these
inside Visual Studio code. 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 going to also call this Python script Python
Chet's GPT dot py. 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 request 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
making a request, sending all the parameters, et cetera, 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 open AI 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 open AI 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 overcomplicating your code.
But open AI is pretty recent and its library may change, like 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 going to watch this video maybe a year later it may look
very different. And because of that I'm going to stick to the generic request library because
we just want to make a request which is a pretty simple use case. So the code between using the
request library and opening a 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 should make
that much difference if you want to. You can actually go with the opening a 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 to 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 chat chip T 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. Whether 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're using
it will always come with that 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 chat chip it 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
bunch of other stuff 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 that'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 school
ready we're going to remove this API key from here and we're going to 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 an 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 going to refer the documentation 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 request's library we have a simple post method and 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 hey 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 going to 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 curl 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
library that provides us a generic way to make a request to any endpoint and the second one is the
open AI 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
open AI library example with what we write so because I'm going to be using request library I'm
actually going to switch back to curve and I'm going to 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 request 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 post method and again we can have
an object here like this or we're gonna create another variable we're gonna call it data or request
let's call it request data we can also call this request header
like this so we have a little consistency there you go and now let's see what we need inside that
request data now 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 this
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 the vinci 003 so what is that what is
a model well if you watched my chat your 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
a couple of items here these are basically group of models and if I scroll a little bit down
right here gpt3 that powers chat 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 AI and because of that
they have various models that specialize and do different things so gpt3 is actually one of
this 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 gpt or deli or whatever and
also the machine learning models that you can connect to and talk to through the API and you see
the descriptions here so text the vinci 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
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 the vinci 003 and that's
also the example that we have here now before we move on and use the text the vinci 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
example 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
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 the gpt3 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 year when you're seeing this video it will not be in better anymore just like the other
model groups so because of these reasons we're actually going to choose the gpt3 model called
text the benchy 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 codecs 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 da Vinci 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 comparison prompt
is what you type in when you're chatting with chat tpt 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 da Vinci 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 max tokens basically limits the response that you get in tokens
so with 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 this max tokens to let's do 100 for start
the hello word 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 going to exceed that amount and we have a
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
hey machine learning model please be as precise and predictable as possible and that will be
temperature of zero so in zero and one you can control that but obviously you have everything in the
middle so with zero point five 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 going to set it to
zero point five 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 going to 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 see we want to talk to text the binchy 0003 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
was our response actually successful or maybe head and 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 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 going to print request failed maybe we sent a wrong request
or maybe the API server is down or whatever reason request failed and we want to also know the
reason why it failed so we're going to print out the status code and this will be a reference to
response 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 this curly braces is actually not a string 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 p3 python package
manager to install any libraries that we're 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 p3
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 p3
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 you will say import requests could not be
result 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 are going to do p3 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 then we have library installed have python3 installed
let's execute our script you see python3 and the name of our script which is python
chat gpt.py 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 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 work which is the response to our prompt that we said 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 gonna grab the value of choices and this is python syntax we're gonna 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
Jupyte 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 the vinci text model of open AI and now we can send whatever prompt we want
programmatically and we're gonna 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 date time
library and prints out today's date now if you have used chat Jupyte 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're 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 gonna 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 it's executed that again and in my case it just
removed 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 this 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 coding 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 will 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 arg parse there you go and different from the request
library which we had to install using pip3 we don't need to install arg parse 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 gonna 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 parse 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 an 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
and that's a help attribute and let me say this is a prompt to send to the open AI API and finally
using this parser object we're gonna call a method called parsearts which is gonna go through
whatever arguments we have edit 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 it 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 arts dot prompt
because we call this value prompt kind of like assigning to a variable and we're gonna use that
right here again the Python syntax for using a variable within a string so we have arts 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 this 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 it comes out and there we 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 one million
minutes and let's say what we get and there we go so these are obviously super simple examples
when we have the script ready we're gonna actually execute or two use cases of extracting the
headers from block 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 have them 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 have a keyword 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 py 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 gonna 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 gonna say open as file so this line
basically goes and tries to find a file called output dot 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 right mode we're gonna write to that file using
file dot write 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 this response text into a separate 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 that 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 you 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 overwriting 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 filing here we're gonna
also pass that as an input so we're gonna 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 it 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 gonna copy
this line let's call this file name which it is file name name of the file to save Python script
there you go so we have the second argument and we can access it using rx.file name very easy
so rx.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 it's gonna copy that
and instead of hard coding it directly here we're gonna 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 is 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
so that was our final optimization let's actually delete those and execute our script again
and there we go we have our result and we're not exposing our API key in the code and with this
our Python chat 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 gonna use which is
this one right here and it has different levels of headers you probably know headers from each
email so we have larger ones and then we have some sub headers 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 we open it we basically will
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 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 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
and also to know 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 and 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 chat GPT 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 100 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 any adjustments needed and then we're gonna test this program with our example log 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 this will be the URL of our
blog post so let's change that right way i'm just gonna copy that and paste it in here instead of
example and then using the request library we're just requesting that page and on the page we
kind of access the content 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 a library name bs4 beautiful soup for 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 translated 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 these 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 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
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 text you already know this because we used it right here
basically opens the file once it creates this 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
these Spanish headers array and for each header it adds this 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 here a key of headers
and titles and subtitles so we want to also reuse them right here so that's the only part we're
going to 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 going to
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
the rest of the element information including what element that is right whether it's h1 or h2
whatever so we want to keep all that information including the text so we're going to 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, h 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 we're going to 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
ahead and pass 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 pip3 install ps4 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.080 that's installed 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 of 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 or translate result
like this and let's print this out and if we look at anyone 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 gonna be an object
and it is going to have a text and the value of the text will be the translation result
the 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 gonna be our translated
text and then we have the header name right and we're gonna grab that directly from here
header name and that's it and finally we're gonna add that translate result we can also call these
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 till
h6 and we're appending or adding that dictionary into Spanish headers array and here as 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 dot
HTML file which should now contain all headers translated in Spanish and it 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 hierarchy so we have the larger headers and the sub headers we can even
expect 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 going to close this one and we are going to ask our
python script to give us python code to go through files in downloads folder check their dates
and if they are older than 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 going to 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 users 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 users 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 two 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
yours 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 shutil module it moves the file from downloads folder the file name
to the to delete folder this actually looks pretty good it used the right libraries the code is
pretty simple it even added those comments on top the code is also nicely structured 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 python3 clean downloads dot py i'm really interested to see
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 this one is where today this is modified like 17 days ago and in my whole folder it created
two 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 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 automates some things and we also have the python program that we can feed any automation
use case that we want and it will generate 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
how this will them award for you or whether you had any issues and what results you get or how
quality that even how good the results where 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. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video. And see you in the next video
Einführung in die Python-Automatisierung
Anwendungsfall 1: Extrahieren von Überschriften
Anwendungsfall 2: Downloads bereinigen
Warum Python für Automatisierung?
Verbindung zur OpenAI API herstellen
Python-Skripte mit ChatGPT generieren
Übersetzung von Überschriften ins Spanische
Verstehen von API-Endpunkten
HTML-Datei-Erstellung
POST-Anfragen in Python erstellen
Best Practices für das Dateimanagement
Einrichtung der Download-Automatisierung
Fehlerbehandlung in der Python-Automatisierung
Die richtige KI-Modell auswählen
Python-Skripte testen
Best Practices für die Arbeit mit APIs
Integration von ChatGPT in lokale Projekte
Automatisierung der Dateiorganisation
Eigene Skripte erstellen
Skripte aus Benutzereingaben erstellen
Überprüfung von Dateien vor der Löschung
ChatGPT für Automatisierung nutzen
Was sind die zwei coolen Anwendungsfälle, die wir mit Python automatisieren werden?
Warum ist Python die erste Wahl für Automatisierungsaufgaben?
Wie meldet man sich für den Zugriff auf die OpenAI-API an?
Warum sind Bibliotheken wie OpenAI in Python so wichtig?
Wie kann dir ChatGPT helfen, ganz easy eigene Python-Skripte zu erstellen?
Welche Schritte machst du, um Überschriften in eine andere Sprache zu übersetzen?
Warum ist es wichtig, deine Downloads zu organisieren, um produktiv zu sein?
Wie kannst du Fehler in Python-Automatisierungsaufgaben richtig angehen?
Was sind die wichtigsten Schritte, um deine Python-Skripte zu testen?
Wie verbessern Best Practices deine API-Interaktionen?
Wie kann Automatisierung dir Stunden bei der Dateiorganisation sparen?
Was sind die wichtigsten Schritte, um effektive Skripte zu erstellen?
Warum solltest du die Dateien überprüfen, bevor du sie löschst?
Wie findest du die Dokumentation einer API für die Nutzungsrichtlinien?
Welche Parameter brauchst du für eine POST-Anfrage in Python?
Warum ist es wichtig, das richtige KI-Modell für die Code-Generierung auszuwählen?
Wie kann man die Eingaben von Nutzern nutzen, um die Skripterstellung anzupassen?
In diesem Video werden wir erkunden, wie man Python für Automatisierung nutzt, indem wir die API von ChatGPT verwenden. Wir fangen damit an, ein Python-Programm zu schreiben, das eine beliebige Webseite oder einen Artikel nimmt und alle Überschriften daraus extrahiert. Dann übersetzen wir diese Überschriften ins Spanische und speichern sie in einer eigenen HTML-Datei. Danach kümmern wir uns darum, unseren Downloads-Ordner aufzuräumen, indem wir Dateien identifizieren, die älter als Donnerstag sind oder seit mehr als 30 Tagen nicht genutzt wurden, und sie in einen 'Löschen'-Ordner für die Überprüfung verschieben. Das Beste daran ist, dass wir die API von ChatGPT nutzen werden, um Python-Skripte für diese beiden Anwendungsfälle zu generieren. Wir zeigen dir, wie du Eingaben akzeptierst, ChatGPT nach einem Skript fragst und es in einer separaten Datei speicherst. Am Ende dieses Videos wirst du ein besseres Verständnis dafür haben, wie man Python für Automatisierung nutzt und die API von ChatGPT in deinen Workflow integriert.