|
|
"""Module to generate a JSON Lines file from intro.txt and qa.txt files in a given directory.""" |
|
|
|
|
|
import logging |
|
|
import os |
|
|
import json |
|
|
|
|
|
|
|
|
OUTPUT_DIR = os.path.join('.', 'output') |
|
|
|
|
|
def generate_jsonlines(directory, output_filename): |
|
|
""" |
|
|
Generate a JSON Lines file from intro.txt and qa.txt files in the given directory. |
|
|
|
|
|
Args: |
|
|
directory (str): The directory path containing the intro.txt and qa.txt files. |
|
|
output_filename (str): The name of the output JSON Lines file to be generated. |
|
|
|
|
|
Raises: |
|
|
FileNotFoundError: If intro.txt or qa.txt is not found in the given directory. |
|
|
ValueError: If qa.txt is not in the correct format. |
|
|
|
|
|
""" |
|
|
|
|
|
if not os.path.exists(os.path.join(directory, 'intro.txt')) or not os.path.exists(os.path.join(directory, 'qa.txt')): |
|
|
logging.info("Ignoring %s", directory) |
|
|
return |
|
|
|
|
|
if not verify_file(os.path.join(directory, 'qa.txt')): |
|
|
raise ValueError('qa.txt is not in the correct format') |
|
|
|
|
|
logging.info("Processing %s", directory) |
|
|
|
|
|
with open(os.path.join(directory, 'intro.txt'), 'r', encoding='utf-8') as intro_file: |
|
|
intro_content = intro_file.read().strip() |
|
|
|
|
|
messages = [] |
|
|
|
|
|
with open(os.path.join(directory, 'qa.txt'), 'r', encoding='utf-8') as qa_file: |
|
|
lines = qa_file.readlines() |
|
|
|
|
|
for line in lines: |
|
|
line = line.strip() |
|
|
if line.startswith('Q:'): |
|
|
question = line[2:].strip() |
|
|
elif line.startswith('A:'): |
|
|
messages.append([ |
|
|
{'role': 'system', 'content': intro_content}, |
|
|
{'role': 'user', 'content': question}, |
|
|
{'role': 'assistant', 'content': line[2:].strip()} |
|
|
]) |
|
|
|
|
|
with open(output_filename, 'w', encoding='utf-8') as jsonl_file: |
|
|
for message in messages: |
|
|
jsonl_file.write(json.dumps({'messages': message}) + '\n') |
|
|
|
|
|
def verify_file(file_path): |
|
|
""" |
|
|
Verify if the lines in a file have the correct prefix. |
|
|
|
|
|
Args: |
|
|
file_path (str): The path to the file to be verified. |
|
|
|
|
|
Returns: |
|
|
bool: True if all lines have the correct prefix, False otherwise. |
|
|
""" |
|
|
|
|
|
with open(file_path, 'r', encoding='utf-8') as file: |
|
|
lines = file.readlines() |
|
|
|
|
|
for i, line in enumerate(lines): |
|
|
if i % 2 == 0: |
|
|
if not line.startswith('Q:'): |
|
|
return False |
|
|
else: |
|
|
if not line.startswith('A:'): |
|
|
return False |
|
|
|
|
|
return True |
|
|
|
|
|
def main(): |
|
|
""" |
|
|
Main function to generate JSON Lines files from intro.txt and qa.txt files in the current directory. |
|
|
|
|
|
It creates an output directory if it doesn't exist and processes each subdirectory in the current directory. |
|
|
For each subdirectory, it generates a JSON Lines file with the same name as the subdirectory. |
|
|
|
|
|
""" |
|
|
|
|
|
os.makedirs(OUTPUT_DIR, exist_ok=True) |
|
|
|
|
|
for directory_name in os.listdir('.'): |
|
|
directory_path = os.path.join('.', directory_name) |
|
|
if os.path.isdir(directory_path) and directory_path != OUTPUT_DIR: |
|
|
if os.path.isdir(directory_path): |
|
|
output_filename = f"{directory_name}.txt" |
|
|
generate_jsonlines(directory_path, os.path.join(OUTPUT_DIR, output_filename)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') |
|
|
main() |