To break the text into sections of about 4 paragraphs each, you can modify the script as follows:
import csv
import glob
import os
# Initialize LLM model
llm_model = LlmModel()
# Define transform prompt
transform_prompt = "Transform the given text according to the following prompt: Adapt a story to include a man discovering a magical artifact in a strange, mystical forest."
# Define CSV file names
input_files = glob.glob('*.txt')
output_file = 'output.csv'
# Initialize CSV writer
with open(output_file, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["User Prompt", "Original Text", "Transformed Text"])
# Loop over input files
for input_file in input_files:
# Open input file
with open(input_file, 'r') as file:
# Read lines
lines = file.readlines()
# Initialize sections
sections = []
# Loop over lines
for i, line in enumerate(lines):
# Check if line is a paragraph (i.e., ends with '\n\n')
if line.endswith('\n\n'):
# If it is, add it to the last section or create a new section
sections[-1] = sections[-1] + line if sections else line
# If sections has 4 or more sections, break the loop
if len(sections) >= 4:
break
else:
# If it is not a paragraph, continue to the next line
continue
# Loop over sections
for i, section in enumerate(sections):
# Get user prompt
user_prompt = llm_model.get_user_prompt(section)
# Transform text according to transform prompt
transformed_text = llm_model.generate(transform_prompt, section)
# Write to CSV file
writer.writerow([user_prompt, section, transformed_text])
This modified script does the following:
- It reads all lines from each
.txt file, as before.
- It loops over each line and checks if it is a paragraph. If it is, it adds it to the last section or creates a new section if there are no sections yet.
- If it is not a paragraph, it continues to the next line.
- Once it has read all lines, it checks if there are 4 or more sections. If there are, it breaks the loop.
- It then loops over each section, gets a user prompt, transforms the text, and writes the user prompt, the original text, and the transformed text to the CSV file.
This should break the text into sections of about 4 paragraphs each, assuming that each paragraph is separated by two newline characters (\n\n). If your text is structured differently, you might need to modify the code accordingly.