Datasets:

Modalities:
Text
Tags:
Not-For-All-Audiences
License:
Delta-Vector commited on
Commit
fe2a73e
1 Parent(s): b209c86

Delete thing.py

Browse files
Files changed (1) hide show
  1. thing.py +0 -64
thing.py DELETED
@@ -1,64 +0,0 @@
1
- import os
2
- import re
3
- import json
4
- import uuid
5
- def process_conversation(file_path):
6
- # Read the TXT file
7
- with open(file_path, "r", encoding="utf-8") as file:
8
- lines = file.readlines()
9
- # Store the conversation
10
- conversation = []
11
- is_user = True # To alternate between user ('human') and gpt
12
- # Regex pattern to match speaker before the colon
13
- pattern = re.compile(r"^(.*?):")
14
- for line in lines:
15
- line = line.strip() # Remove leading/trailing whitespace
16
- if not line:
17
- continue # Skip empty lines
18
- # Match the speaker before the colon
19
- match = pattern.match(line)
20
- if match:
21
- message_dict = {
22
- "from": "human" if is_user else "gpt", # Use 'human' instead of 'user'
23
- "value": line
24
- }
25
- conversation.append(message_dict)
26
- # Alternate turns between 'human' and 'gpt'
27
- is_user = not is_user
28
- # Ensure the conversation ends with gpt's turn
29
- if conversation and conversation[-1]["from"] == "human":
30
- # If it ends on 'human', remove last entry
31
- conversation.pop()
32
- # Create structure with id and conversations
33
- conversation_data = {
34
- "id": str(uuid.uuid4()), # Generate a unique ID for the conversation
35
- "conversations": conversation
36
- }
37
- return conversation_data
38
- def write_json(conversation_data, output_path):
39
- # Prepare the output list that wraps the conversation
40
- output_json = [conversation_data]
41
- # Write to JSON file, compact, all in single line
42
- with open(output_path, "w", encoding="utf-8") as outfile:
43
- json.dump(output_json, outfile, ensure_ascii=False, separators=(',', ':'))
44
- def process_folder(input_folder, output_folder):
45
- # Make sure the output folder exists
46
- if not os.path.exists(output_folder):
47
- os.makedirs(output_folder) # Create output folder if it doesn't exist
48
- # Loop through all files in the input folder
49
- for filename in os.listdir(input_folder):
50
- if filename.endswith(".txt"): # Process only TXT files
51
- input_file_path = os.path.join(input_folder, filename)
52
- # Process the conversation
53
- conversation_data = process_conversation(input_file_path)
54
- # Construct a unique output JSON file name
55
- output_file_name = f"{os.path.splitext(filename)[0]}.json" # Use same name, but with .json extension
56
- output_file_path = os.path.join(output_folder, output_file_name)
57
- # Write the JSON file
58
- write_json(conversation_data, output_file_path)
59
- print(f"Converted: {filename} --> {output_file_name}")
60
- # Call the folder processing function
61
- input_folder = r"C:\Users\User\Documents\datasets\Special- Chat (RP logs, convert to sharegpt)\todo" # Path to folder containing .txt files
62
- output_folder = r"C:\Users\User\Documents\datasets\Special- Chat (RP logs, convert to sharegpt)\room\done" # Path to folder where JSONs will be saved
63
- process_folder(input_folder, output_folder)
64
- print(f"Conversion complete! JSONs saved to {output_folder}")