import csv | |
# Replace 'input.txt' with the path to your input text file | |
input_file = 'works.txt' | |
# Replace 'output.csv' with the desired output CSV file path | |
output_file = 'train.csv' | |
# Read the text file and split it into paragraphs | |
with open(input_file, 'r', encoding='utf-8') as file: | |
paragraphs = file.read().split('\n\n') # Adjust the split pattern based on your paragraph format | |
# Create a CSV writer and write the combined paragraphs into the CSV file with 'text' as the column header | |
with open(output_file, 'w', newline='', encoding='utf-8') as csvfile: | |
fieldnames = ['text'] | |
csv_writer = csv.writer(csvfile) | |
csv_writer.writerow(fieldnames) | |
# Use a loop to combine prompt and continuation in the same column with appropriate tags | |
for i in range(len(paragraphs)): | |
prompt = '###Human:' + ' '.join(paragraphs[i:i + 2]) # Combine two paragraphs for the prompt | |
continuation = '###Assistant:' + ' '.join(paragraphs[i + 2:i + 7]) # Combine five paragraphs for the continuation | |
# Combine the prompt and continuation in the same column and write to the CSV | |
csv_writer.writerow([prompt.strip() + ' ' + continuation.strip()]) | |