Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import os
- import sys
- from collections import OrderedDict
- # Request an input file path
- input_file_path = input("Enter the path of the input JSON file: ")
- # Check if the file path exists and if the file is JSON
- if not os.path.exists(input_file_path):
- sys.exit("ERROR: The file does not exist.")
- if not input_file_path.lower().endswith('.json'):
- sys.exit("ERROR: The file is not a JSON file.")
- # Read the JSON file
- try:
- with open(input_file_path, "r") as input_file:
- input_data = json.load(input_file, object_pairs_hook=OrderedDict)
- except json.JSONDecodeError:
- sys.exit("ERROR: The file is not a valid JSON file.")
- # Prepare the output dictionary
- output_data = OrderedDict()
- # Copying `id` and `title`
- output_data["id"] = input_data.get("id")
- output_data["title"] = input_data.get("title")
- # Copying `mapping`
- messages = []
- for key in input_data.get("mapping", {}):
- try:
- single_message = input_data["mapping"][key]["message"]
- messages.append({"role": single_message["author"]["role"], "content": single_message["content"]["parts"][0]})
- except KeyError:
- print(f"WARNING: Skipping message {key} due to missing key(s).")
- continue
- output_data["messages"] = messages
- # Copying `config` and `titleSet` data
- output_data["config"] = {
- "model": "gpt-3.5-turbo",
- "max_tokens": 4000,
- "temperature": 1,
- "presence_penalty": 0,
- "top_p": 1,
- "frequency_penalty": 0
- }
- output_data["titleSet"] = False
- # Wrap output data into JSON Array
- output_str = json.dumps([output_data], indent=4)
- print()
- print(output_str)
- print()
- # Ask if user wants to overwrite the original file with the transformed data
- overwrite = input("Do you want to overwrite the original file with the new data? (yes/no): ")
- if overwrite.lower()[0] == 'y':
- with open(input_file_path, 'w') as f:
- f.write(output_str)
- print("The original file has been overwritten with the new data.")
Advertisement
Add Comment
Please, Sign In to add comment