Guest User

ChatGPT Export to BetterChatGPT Converter

a guest
Aug 18th, 2023
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | Source Code | 0 0
  1. import json
  2. import os
  3. import sys
  4. from collections import OrderedDict
  5.  
  6. # Request an input file path
  7. input_file_path = input("Enter the path of the input JSON file: ")
  8.  
  9. # Check if the file path exists and if the file is JSON
  10. if not os.path.exists(input_file_path):
  11.     sys.exit("ERROR: The file does not exist.")
  12.  
  13. if not input_file_path.lower().endswith('.json'):
  14.     sys.exit("ERROR: The file is not a JSON file.")
  15.  
  16. # Read the JSON file
  17. try:
  18.     with open(input_file_path, "r") as input_file:
  19.         input_data = json.load(input_file, object_pairs_hook=OrderedDict)
  20. except json.JSONDecodeError:
  21.     sys.exit("ERROR: The file is not a valid JSON file.")
  22.  
  23. # Prepare the output dictionary
  24. output_data = OrderedDict()
  25.  
  26. # Copying `id` and `title`
  27. output_data["id"] = input_data.get("id")
  28. output_data["title"] = input_data.get("title")
  29.  
  30. # Copying `mapping`
  31. messages = []
  32. for key in input_data.get("mapping", {}):
  33.     try:
  34.         single_message = input_data["mapping"][key]["message"]
  35.         messages.append({"role": single_message["author"]["role"], "content": single_message["content"]["parts"][0]})
  36.     except KeyError:
  37.         print(f"WARNING: Skipping message {key} due to missing key(s).")
  38.         continue
  39. output_data["messages"] = messages
  40.  
  41. # Copying `config` and `titleSet` data
  42. output_data["config"] = {
  43.     "model": "gpt-3.5-turbo",
  44.     "max_tokens": 4000,
  45.     "temperature": 1,
  46.     "presence_penalty": 0,
  47.     "top_p": 1,
  48.     "frequency_penalty": 0
  49. }
  50.  
  51. output_data["titleSet"] = False
  52.  
  53. # Wrap output data into JSON Array
  54. output_str = json.dumps([output_data], indent=4)
  55.  
  56. print()
  57. print(output_str)
  58. print()
  59.  
  60. # Ask if user wants to overwrite the original file with the transformed data
  61. overwrite = input("Do you want to overwrite the original file with the new data? (yes/no): ")
  62. if overwrite.lower()[0] == 'y':
  63.     with open(input_file_path, 'w') as f:
  64.         f.write(output_str)
  65.         print("The original file has been overwritten with the new data.")
  66.  
Advertisement
Add Comment
Please, Sign In to add comment