Example of bot control implementation via command line argument parsing (argparse)
How it works:
1. argparse is used to get the mode argument from the command line. This allows the user to choose which mode to run.
2. Depending on the value of args.mode, the corresponding functions are called (e.g., run_script_writer, run_script_divider, etc.).
3. Run it through the console, providing the required mode (e.g., full):
How it works:
1. argparse is used to get the mode argument from the command line. This allows the user to choose which mode to run.
2. Depending on the value of args.mode, the corresponding functions are called (e.g., run_script_writer, run_script_divider, etc.).
3. Run it through the console, providing the required mode (e.g., full):
python main.py fullπ18π₯1
You can now run the bot in any mode and check if the constructor of the base class works.
After running, the project folders should be created automatically. This means that everything is working correctly.
You can create folders for another project by assigning a new value to the project_folder variable in the main function of the
* Run the
After running, the project folders should be created automatically. This means that everything is working correctly.
You can create folders for another project by assigning a new value to the project_folder variable in the main function of the
main.py file, and then run the bot again.* Run the
main.py file.π₯14β€βπ₯4
As we finish working on the botβs basis, letβs think about what we havenβt taken care of yet.
Storing API keys and other project settings.
To address this, we will follow the standard practice and create a
In this file, we will later create a settings class, where we will store these data in its attributes.
We will read the API keys from environment variables. This approach protects the data from accidentally being exposed in the code and repository.
Storing API keys and other project settings.
To address this, we will follow the standard practice and create a
config.py file to store such data.In this file, we will later create a settings class, where we will store these data in its attributes.
We will read the API keys from environment variables. This approach protects the data from accidentally being exposed in the code and repository.
π₯21
Next, in the
1. Import load_dotenv.
2. Call the load_dotenv() function to load variables from the .env file.
3. Declare a Config class, where we will store API keys and other settings in its attributes.
4. Declare class attributes corresponding to the required API keys and retrieve their values using os.getenv.
5. Additionally, you can store other settings, such as video parameters, in the attributes of this class.
* Pay attention to this class. Since this class is only needed for storing data, there's no need to create a class constructor here; we can simply store the data in class attributes, as we have done in this case.
Also, note that attributes created outside the class constructor (the init method) are called class attributes, not instance attributes. Class attributes are the same for all instances of the class (objects), unlike instance attributes, which are created in the constructor and can be unique for each object.
config.py file:1. Import load_dotenv.
2. Call the load_dotenv() function to load variables from the .env file.
3. Declare a Config class, where we will store API keys and other settings in its attributes.
4. Declare class attributes corresponding to the required API keys and retrieve their values using os.getenv.
5. Additionally, you can store other settings, such as video parameters, in the attributes of this class.
* Pay attention to this class. Since this class is only needed for storing data, there's no need to create a class constructor here; we can simply store the data in class attributes, as we have done in this case.
Also, note that attributes created outside the class constructor (the init method) are called class attributes, not instance attributes. Class attributes are the same for all instances of the class (objects), unlike instance attributes, which are created in the constructor and can be unique for each object.
π₯13
Now, to use these parameters in other parts of the code, simply import the Config class and access its attributes.
For example, we import this class in the
* Note that in this case, we are accessing the attributes of the Config class directly without creating an instance of this class:
This is possible because OPENAI_API_KEY is a class attribute in the Config class, and we can access it through the class itself rather than through an instance.
However, we can also create an object and then access the class attributes through the object:
But if the OPENAI_API_KEY attribute were declared in the constructor of the Config class, we would only be able to access it through an instance (object) of the class.
For example, we import this class in the
main.py file, retrieve the value of the Config.OPENAI_API_KEY attribute, and pass it to the ScriptWriter class constructor when creating an object.* Note that in this case, we are accessing the attributes of the Config class directly without creating an instance of this class:
Config.OPENAI_API_KEY
This is possible because OPENAI_API_KEY is a class attribute in the Config class, and we can access it through the class itself rather than through an instance.
However, we can also create an object and then access the class attributes through the object:
config = Config()
api_key = config.OPENAI_API_KEYBut if the OPENAI_API_KEY attribute were declared in the constructor of the Config class, we would only be able to access it through an instance (object) of the class.
π₯12π1
The foundation of our bot is now complete.
We've done a lot of work and learned a lot. Now let's take a break for 1-2 days and then start putting meat on the skeleton of our Shorts Monster.
We'll fully develop the ScriptWriter class. After that, I'll give you recommendations for the remaining classes and set you free to swim on your own, so you can finish the rest independently.
We've done a lot of work and learned a lot. Now let's take a break for 1-2 days and then start putting meat on the skeleton of our Shorts Monster.
We'll fully develop the ScriptWriter class. After that, I'll give you recommendations for the remaining classes and set you free to swim on your own, so you can finish the rest independently.
π13π₯8π5π€―2π€1π±1
βοΈ The basis of the bot we've written can serve as the basis for many other projects. Especially those where multi-project capability is important.
For example, for generating WordPress websites.
For example, for generating WordPress websites.
π₯22
So, let's begin developing the ScriptWriter class.
How can we implement this task?
1. By using the API of one of the AI assistants, such as ChatGPT or Claude. We'll choose ChatGPT.
2. We'll also add the option to use the g4f library, which allows access to various AI models for free.
Let's start by installing the necessary packages:
How can we implement this task?
1. By using the API of one of the AI assistants, such as ChatGPT or Claude. We'll choose ChatGPT.
2. We'll also add the option to use the g4f library, which allows access to various AI models for free.
Let's start by installing the necessary packages:
pip install openai
pip install g4fπ₯14π1
In the ScriptWriter class, we have declared the execute method. This will be the central hub method where we build a pipeline to run other helper methods that we will write now.
To begin, let's decompose the tasks of the execute method into smaller steps.
We need to:
1. Accept the initial data in the method (a prompt for the request to the AI assistant).
2. Send a request to the AI assistant's API and save the response to a variable.
3. Write the result to a CSV file named script.csv and save it in the project folder.
To begin, let's decompose the tasks of the execute method into smaller steps.
We need to:
1. Accept the initial data in the method (a prompt for the request to the AI assistant).
2. Send a request to the AI assistant's API and save the response to a variable.
3. Write the result to a CSV file named script.csv and save it in the project folder.
π11π₯6
But before we develop the method, let's refine the class constructor.
What arguments should it accept besides
First, we need to pass the OpenAI API key. To do this, we'll accept an
We'll also set a
* I will provide the code a bit later
What arguments should it accept besides
project_folder?First, we need to pass the OpenAI API key. To do this, we'll accept an
api_key argument.We'll also set a
model parameter to specify the OpenAI model, with a default value of "gpt-4".* I will provide the code a bit later
π₯15
Once again, pay attention to the parameters in the class constructor.
β’ The parameters
β’ The parameter
β’ The parameters
project_folder and api_key are defined without default values. These are called required parameters, meaning they must be provided when creating an object.β’ The parameter
model has a default value ("gpt-4"). Such parameters are called optional. This means that if no value is provided for model when creating an object, the default value ("gpt-4") will be used.π₯11π2
Now, based on the arguments passed to the constructor, we will create attributes of the ScriptWriter class object.
1. First, weβll create an OpenAI client object (
According to the documentation of the openai-python library or the OpenAI API documentation, we can do this by passing the API key to the constructor of the OpenAI class.
2. Next, we will create an attribute for setting the model (
* Note that object attributes should be initialized after calling the parent class constructor (super function). We discussed this earlier in one of the previous posts.
1. First, weβll create an OpenAI client object (
self.client).According to the documentation of the openai-python library or the OpenAI API documentation, we can do this by passing the API key to the constructor of the OpenAI class.
2. Next, we will create an attribute for setting the model (
self.model) and assign it the value of the model argument.* Note that object attributes should be initialized after calling the parent class constructor (super function). We discussed this earlier in one of the previous posts.
π₯15