-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_tool.py
More file actions
195 lines (156 loc) · 7.03 KB
/
data_tool.py
File metadata and controls
195 lines (156 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import sys
import os
import json
from PyQt5.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QHBoxLayout, QTextEdit,
QPushButton, QListWidget, QLineEdit, QListWidgetItem, QMessageBox
)
from PyQt5.QtGui import QColor, QTextCursor
from PyQt5.QtCore import Qt
class ChatDatasetTool(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Chat Dataset Creator")
self.resize(800, 600)
# Role toggle: starts with "user"
self.current_role = "user"
self.chat_data = []
# Main layout
main_layout = QHBoxLayout()
# Left: Message list
self.message_list = QListWidget()
self.message_list.setFixedWidth(200)
self.message_list.setFocusPolicy(Qt.StrongFocus)
self.message_list.keyPressEvent = self.handle_keypress # Override key press
main_layout.addWidget(self.message_list)
# Right: Chat and Input
right_layout = QVBoxLayout()
# Chat history display
self.chat_history = QTextEdit()
self.chat_history.setReadOnly(True)
right_layout.addWidget(self.chat_history)
# Input layout
input_layout = QHBoxLayout()
self.input_field = QLineEdit()
self.input_field.returnPressed.connect(self.handle_send) # Enter key triggers send
input_layout.addWidget(self.input_field)
self.send_button = QPushButton("Send")
self.send_button.clicked.connect(self.handle_send) # Send button triggers send
input_layout.addWidget(self.send_button)
right_layout.addLayout(input_layout)
main_layout.addLayout(right_layout)
self.setLayout(main_layout)
# Load existing data and update the UI
self.data_dir = "train/messages/"
self.finetuning_file = "train/finetuning.jsonl"
os.makedirs(self.data_dir, exist_ok=True)
os.makedirs(os.path.dirname(self.finetuning_file), exist_ok=True)
self.load_existing_data()
self.update_message_list()
def closeEvent(self, event):
"""Merge all conversation files when the application closes."""
self.merge_conversations()
event.accept() # Proceed with the close event
def load_existing_data(self):
self.chat_data = []
for file in sorted(os.listdir(self.data_dir)):
if file.endswith(".jsonl"):
with open(os.path.join(self.data_dir, file), 'r', encoding='utf-8') as f:
self.chat_data.extend(json.loads(line) for line in f)
self.update_message_list()
def update_message_list(self):
self.message_list.clear()
for idx, entry in enumerate(self.chat_data):
item = QListWidgetItem(f"Conversation {idx + 1}")
item.setData(Qt.UserRole, idx) # Store the index
self.message_list.addItem(item)
self.message_list.currentRowChanged.connect(self.display_messages)
def display_messages(self, index):
if index < 0 or index >= len(self.chat_data):
return
messages = self.chat_data[index]["messages"]
self.chat_history.clear()
for msg in messages:
self.append_message(msg["role"], msg["content"])
def append_message(self, role, content):
color = QColor(255, 228, 228) if role == "user" else QColor(228, 241, 255)
cursor = self.chat_history.textCursor()
cursor.movePosition(QTextCursor.End)
self.chat_history.setTextCursor(cursor)
self.chat_history.setTextBackgroundColor(color)
self.chat_history.insertPlainText(f"{role.capitalize()}: {content}\n")
self.chat_history.setTextBackgroundColor(QColor(255, 255, 255))
def handle_send(self):
text = self.input_field.text().strip()
if not text or text.lower() == "stop":
self.start_new_conversation()
return
self.add_message(self.current_role, text)
# Toggle role for next input
self.current_role = "assistant" if self.current_role == "user" else "user"
self.input_field.clear()
def start_new_conversation(self):
self.current_role = "user"
self.chat_data.append({"messages": []})
self.save_current_conversation()
self.input_field.clear()
self.chat_history.clear()
self.update_message_list()
def add_message(self, role, content):
if not self.chat_data or "messages" not in self.chat_data[-1]:
self.chat_data.append({"messages": []})
self.chat_data[-1]["messages"].append({"role": role, "content": content})
self.save_current_conversation()
self.update_message_list()
self.display_messages(len(self.chat_data) - 1)
def save_current_conversation(self):
if not self.chat_data:
return
filename = os.path.join(self.data_dir, f"conversation_{len(self.chat_data)}.jsonl")
with open(filename, 'w', encoding='utf-8') as file:
json.dump(self.chat_data[-1], file, ensure_ascii=False)
file.write('\n')
def merge_conversations(self):
with open(self.finetuning_file, 'w', encoding='utf-8') as outfile:
for file in sorted(os.listdir(self.data_dir)):
if file.endswith(".jsonl"):
with open(os.path.join(self.data_dir, file), 'r', encoding='utf-8') as infile:
for line in infile:
outfile.write(line)
def handle_keypress(self, event):
if event.key() == Qt.Key_Delete:
self.delete_selected_conversation()
else:
super(QListWidget, self.message_list).keyPressEvent(event)
def delete_selected_conversation(self):
current_item = self.message_list.currentItem()
if not current_item:
return
conversation_index = current_item.data(Qt.UserRole)
confirmation = QMessageBox.question(
self, "Delete Conversation", "Are you sure you want to delete this conversation?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No
)
if confirmation == QMessageBox.Yes:
# Remove conversation file
filename = os.path.join(self.data_dir, f"conversation_{conversation_index + 1}.jsonl")
if os.path.exists(filename):
os.remove(filename)
# Remove from data and UI
del self.chat_data[conversation_index]
self.update_message_list()
self.chat_history.clear()
# Renumber the remaining conversation files
self.renumber_files()
def renumber_files(self):
"""Renumber conversation files to maintain consistent numbering."""
for idx, entry in enumerate(self.chat_data):
filename = os.path.join(self.data_dir, f"conversation_{idx + 1}.jsonl")
with open(filename, 'w', encoding='utf-8') as file:
json.dump(entry, file, ensure_ascii=False)
file.write('\n')
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ChatDatasetTool()
window.show()
sys.exit(app.exec_())