PYTHON MONEY
رفتن به کانال در Telegram
1 903
مشترکین
اطلاعاتی وجود ندارد24 ساعت
+17 روز
+630 روز
آرشیو پست ها
1 903
So, in the
modules package, we will create another file called writer.py.
In this file, we will declare a class called Writer.
This class will not act as a full actor in our bot. Instead, it will serve as an interface through which other classes (ScriptWriter and PromptsWriter) will interact with the methods for text generation.
It doesn't depend on project folders or the base class methods, and simply provides standalone functionality. Hence, it won’t inherit from the BaseGenerator class and will be independent.
Let’s create the constructor for this class. We’ll take the constructor from the ScriptWriter class and move it here, but we’ll remove the project_folder parameter and the super() call.1 903
PromptsWriter
Previously, in the
ScriptWriter class, we already implemented the functionality for generating texts. So, when we start writing the PromptsWriter class, we face the question: how can we reuse this functionality to avoid code duplication?
We'll solve this by doing the following:
We will extract the common functionality for text generation into a separate class, and then include it in both the ScriptWriter and PromptsWriter classes using composition.1 903
Result of the
execute method of the ScriptDivider class:
1. Each scene is extracted from the dictionary and written to a separate row in the CSV file scenario_scenes.csv.
2. Each script is either in a separate column of the same file or in a separate file.
3. These scenes will be needed later for prompt and voiceover generation.1 903
Next - the development of the PromptsWriter class.
The main functionality for this class is already written in the ScriptWriter class. We will discuss how to avoid code duplication and leverage this functionality through composition.
1 903
Hint 1: To avoid confusion and problems when parsing dictionaries from text due to quotation marks (double "" or single ''), you can request the necessary quotation marks for dictionary keys and values directly in the prompt (e.g., double quotes).
Hint 2: Think through the logic for writing scenes for each script into a CSV file to make future processing easier. This could be writing in one CSV file with different scripts separated by columns, or writing each script into a separate file.
1 903
The task breakdown for the
execute method of the ScriptDivider class can look like this:
1. Accept the script text.
2. Extract the list of dictionaries from the text and return it as a Python object.
3. Write the data into a separate CSV file, such as script_scenes.csv. Each dictionary key's value should be written in a separate row.
You can prepare the initial data like this:
1. Open the script.csv file.
2. Read its data and return a list of rows.
3. In a for loop, call the main class method (execute) for each script and pass the script as an argument.1 903
ScriptDivider
To standardize the data and simplify splitting the script into scenes, we requested the desired format directly in the prompt — a list of dictionaries.
As a result, the rows in the
script.csv file will contain text that includes this list of dictionaries. It may look something like this (image).
Thus, in the ScriptDivider class, we need to write a method that extracts this list of dictionaries from the text and converts it into a Python object.1 903
Let’s proceed with analyzing the development of the remaining classes. You can consider this section of the course as homework assignment.
1 903
The main part of the course is now complete.
We developed the foundation of the bot from scratch and fully developed one class and a bot mode.
You will need to write the remaining classes on your own.
Next, I will provide recommendations and suggest technologies you can use to accomplish this.
1 903
How can we standardize the responses to make it easier to split them into scenes?
We can specify the desired format directly in the prompt. For example, a list of dictionaries.
1 903
After running the bot, a file named
script.csv should appear in your project folder with content similar to this.
We can see that the responses may vary in format.
However, the next step will be to divide each script into scenes.
So, how can we standardize the responses to make it easier to split them into scenes?1 903
Tips for using g4f:
1. If you encounter the error
'No module named 'curl_cffi'' when importing the g4f module, fix this by installing the module:
pip install curl-cffi
2. You can enable debug mode to better control the process:
import g4f.debug
g4f.debug.logging = True1 903
List of main GPT models:
gpt-4o, gpt-4o-mini, gpt-4-turbo, gpt-4, gpt-3.5-turbo
You can learn about the rest in the OpenAI API documentation.1 903
For now, we’ll simply declare a list of prompts as an example.
Then, in the
run_script_writer function, we will create an instance of the ScriptWriter class, after which we will iterate over the prompts from the list using a for loop, and for each one, call the execute method.
1. Note that the Config.OPENAI_API_KEY argument contains the OpenAI API key, which was retrieved through environment variables from the .env file.
2. Through the named argument model, we specify the model, and through the named argument use_g4f, we indicate the use of the g4f library.1 903
After writing the ScriptWriter class, we need to prepare the input data (prompts).
One way to structure this data is to create a prompt builder based on Google Sheets.
1. Create a sheet for the prompt builder.
2. Create sheets with different types of prompts, where ready-made prompts from the prompt builder will be parsed.
3. Write a method to read data from Google Sheets.
4. Read the data depending on the sheet. Pass the required sheet as an argument to the method, for example, as shown here.
5. Return a dictionary or list of prompts from the method.
You will need to implement this on your own; we covered it in more detail and wrote the code in Lesson 4 of the Pinterest Money course.
For now, we'll choose another simple method as an example.
1 903
Additional recommendations for the class code:
1. File paths will need to be used repeatedly and across different classes, so they can be made into object attributes and moved to the base class.
2. Where necessary, add exception handling using try-except.
3. Add logging (messages about the start and end of tasks or errors).
1 903
The work on the ScriptWriter class is complete.
We just need to make some changes to the
main.py file to run and test this mode of the bot.
Our course is coming to an end.