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.
* Donβt think that itβs too difficult. You can write the code with the help of ChatGPT or another assistant by providing it with the class skeleton and task decomposition.
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.
* Donβt think that itβs too difficult. You can write the code with the help of ChatGPT or another assistant by providing it with the class skeleton and task decomposition.
β€15π€―3π€·3π2π₯1
Letβs proceed with analyzing the development of the remaining classes. You can consider this section of the course as homework assignment.
π8π₯5π€―2π¨1
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
Thus, in the
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.π₯8π3
The task breakdown for the
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
You can prepare the initial data like this:
1. Open the
2. Read its data and return a list of rows.
3. In a for loop, call the main class method (
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.π₯7π3
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.
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.
π5π₯4
Next - the development of the
The main functionality for this class is already written in 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.β€βπ₯6π₯5
Result of the
1. Each scene is extracted from the dictionary and written to a separate row in the CSV file
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.
execute method of the ScriptDivider class:1. Each scene is extracted from the dictionary and written to a separate row in the CSV file
script_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.
π₯9
PromptsWriter
Previously, in the
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
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.π₯8
So, in the
In this file, we will declare a class called
This class will not act as a full actor in our bot. Instead, it will serve as an interface through which other classes (
It doesn't depend on project folders or the base class methods, and simply provides standalone functionality. Hence, it wonβt inherit from the
Letβs create the constructor for this class. Weβll take the constructor from 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.π₯7
Composition
In our example, the classes
Composition is a principle where one class includes another class as a component and uses it to accomplish its tasks.
Composition creates a "has-a" relationship. One object contains (has) another object and utilizes its functionality.
In our example, the classes
ScriptWriter and PromptsWriter create an instance of the Writer class and use it to perform text generation tasks. This is a classic example of composition.Composition is a principle where one class includes another class as a component and uses it to accomplish its tasks.
Composition creates a "has-a" relationship. One object contains (has) another object and utilizes its functionality.
π₯8
"Is-a" or "Has-a"
These principles help determine when to use inheritance and when to use composition.
1. Inheritance (is-a). If one class is a specialized or extended version of another, use inheritance.
Example: ScriptWriter, ScriptDivider, PromptsWriter, and other classes are (is-a) types of BaseGenerator. These classes have the functionality of BaseGenerator and extend it by adding their own logic.
βοΈ Inheritance is suitable when classes have a "generalization-specialization" relationship: BaseGenerator (generalization) - ScriptWriter (specialization).
2. Composition (has-a). If one class contains or uses another class as a component, use composition.
Example: ScriptWriter and PromptsWriter include (has-a) an instance of Writer and use its functionality for text generation.
βοΈ Composition is preferred when classes have a "part-whole" relationship: Writer (part) - ScriptWriter (whole).
* Despite the name Writer, it may seem like ScriptWriter is a type of Writer, but this is not the case. ScriptWriter (whole) simply uses Writer (part) as a component.
These principles help determine when to use inheritance and when to use composition.
1. Inheritance (is-a). If one class is a specialized or extended version of another, use inheritance.
Example: ScriptWriter, ScriptDivider, PromptsWriter, and other classes are (is-a) types of BaseGenerator. These classes have the functionality of BaseGenerator and extend it by adding their own logic.
βοΈ Inheritance is suitable when classes have a "generalization-specialization" relationship: BaseGenerator (generalization) - ScriptWriter (specialization).
2. Composition (has-a). If one class contains or uses another class as a component, use composition.
Example: ScriptWriter and PromptsWriter include (has-a) an instance of Writer and use its functionality for text generation.
βοΈ Composition is preferred when classes have a "part-whole" relationship: Writer (part) - ScriptWriter (whole).
* Despite the name Writer, it may seem like ScriptWriter is a type of Writer, but this is not the case. ScriptWriter (whole) simply uses Writer (part) as a component.
π₯11β€2π1
Now we need to refine the project structure.
We see that the modules package contains three files related to text generation:
Let's improve the project structure.
In the modules package, we'll create a separate
This will create a clear separation of responsibility and make it easier to manage the logic related to text generation.
* Don't forget to update the imports in the
We see that the modules package contains three files related to text generation:
writer.py, script_writer.py, and prompts_writer.py.Let's improve the project structure.
In the modules package, we'll create a separate
writer package and move the files writer.py, script_writer.py, and prompts_writer.py into it.This will create a clear separation of responsibility and make it easier to manage the logic related to text generation.
* Don't forget to update the imports in the
main.py file accordingly:from modules.writer.script_writer import ScriptWriterfrom modules.writer.prompts_writer import PromptsWriterπ₯15
Let's start with the tasks for the PromptsWriter:
The initial data can be prepared like this:
1. Read the data from the file(s) obtained in the previous stage (
2. Pack the data into a list of lists. Each script is a list of strings or a list of dictionaries. The scenes of the script are the elements of the list (strings, or the values of the dictionary keys).
3. Iterate over each script (list) in a loop.
4. Iterate over the list elements (scenes) in a loop. For each scene, create a prompt by inserting the scene's value.
5. Pass the prepared prompt to the
* Example of a prompt to be passed to the execute method:
For scene [scene] write an image prompt. The goal is to generate visuals for this scene using this prompt.
The work of the
1. Accept the prompt in the method.
2. Send the request to generate text through the
3. Receive the response and save the result in a separate CSV file.
The initial data can be prepared like this:
1. Read the data from the file(s) obtained in the previous stage (
ScriptDivider).2. Pack the data into a list of lists. Each script is a list of strings or a list of dictionaries. The scenes of the script are the elements of the list (strings, or the values of the dictionary keys).
3. Iterate over each script (list) in a loop.
4. Iterate over the list elements (scenes) in a loop. For each scene, create a prompt by inserting the scene's value.
5. Pass the prepared prompt to the
execute method of the PromptsWriter class.* Example of a prompt to be passed to the execute method:
For scene [scene] write an image prompt. The goal is to generate visuals for this scene using this prompt.
The work of the
execute method:1. Accept the prompt in the method.
2. Send the request to generate text through the
generate_text method of the self.writer object, which was created in the constructor of the class.3. Receive the response and save the result in a separate CSV file.
π₯4
Hint 1. The result may look like this (image). There is a separate folder for the generated prompts, and each script has its own CSV file with the generated prompts. The rows in the file represent the prompts for each scene of the script.
Hint 2. To simplify extracting the result, request a dictionary format in the initial prompt. Then, write a method to extract the values of the dictionary keys from the text, similar to what we did in the ScriptDivider class.
Hint 2. To simplify extracting the result, request a dictionary format in the initial prompt. Then, write a method to extract the values of the dictionary keys from the text, similar to what we did in the ScriptDivider class.
π₯11β€1