-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatasetViewer.py
181 lines (148 loc) · 7.84 KB
/
DatasetViewer.py
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
import sys
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QTableView, QPushButton, QMessageBox, QApplication, QHeaderView, QSizePolicy, QHBoxLayout, QTextEdit
from PyQt5.QtGui import QStandardItemModel, QStandardItem, QIcon
import os
import csv
from Utils.ConfigHandle import read_parameters_from_json
import threading
from Utils.Translation import load_translation_model, whisper_translation
class DataWindow(QWidget):
def __init__(self, file_path):
super().__init__()
self.file_path = file_path
self.transcriber = load_translation_model()
self.setWindowTitle("Data Viewer")
self.setFixedSize(800, 600)
self.setWindowIcon(QIcon('Images/fig.png'))
self.table_view = QTableView()
self.model = QStandardItemModel(self)
self.table_view.setModel(self.model)
self.table_view.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) # Adjust column widths to fill the available space
self.table_view.verticalHeader().setVisible(False) # Hide vertical header
self.loadCSV(file_path)
self.delete_button = QPushButton("Delete Row")
self.delete_button.clicked.connect(self.deleteRow)
self.delete_button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.save_button = QPushButton("Save Changes")
self.save_button.clicked.connect(self.saveChanges)
self.save_button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.refresh_button = QPushButton("Refresh Data View")
self.refresh_button.clicked.connect(self.refreshCSV)
self.refresh_button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.transcribe_button = QPushButton("Transcribe Remaining Files")
self.transcribe_button.clicked.connect(self.transcribeRemainingFiles)
self.transcribe_button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.extra_button = QPushButton("Export to Huggingface")
self.extra_button.clicked.connect(self.openExportWindow)
self.extra_button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.extra_button.setEnabled(True) # Enable the button
self.delete_button.setFixedWidth(100)
self.save_button.setFixedWidth(150)
self.refresh_button.setFixedWidth(160)
self.transcribe_button.setFixedWidth(250)
self.extra_button.setFixedWidth(160)
layout = QVBoxLayout()
layout.addWidget(self.table_view)
button_layout = QHBoxLayout()
button_layout.addWidget(self.refresh_button)
button_layout.addStretch(1)
button_layout.addWidget(self.transcribe_button)
button_layout.addWidget(self.delete_button)
# Add the button layout to the main layout
layout.addLayout(button_layout)
self.setLayout(layout)
layout.addWidget(self.extra_button)
self.terminal = QTextEdit()
self.terminal.setReadOnly(True)
self.terminal.setFixedHeight(100)
layout.addWidget(self.terminal)
def loadCSV(self, csv_file):
try:
with open(csv_file, 'r', newline='') as file:
reader = csv.reader(file)
header = next(reader)
self.model.setHorizontalHeaderLabels(header)
for i, row in enumerate(reader):
items = []
for j, field in enumerate(row):
item = QStandardItem(field)
if j == 1:
item.setEditable(True)
else:
item.setEditable(False)
items.append(item)
self.model.appendRow(items)
except Exception as e:
QMessageBox.critical(self, "Error", f"An error occurred while opening the CSV file:\n{str(e)}")
def deleteRow(self):
selected = self.table_view.selectedIndexes()
if selected:
rows_to_delete = set(index.row() for index in selected)
for row in sorted(rows_to_delete, reverse=True):
row_data = []
for col in range(self.model.columnCount()):
item = self.model.item(row, col)
if item is not None:
row_data.append(item.text())
else:
row_data.append("")
print("Deleting row:", row_data)
self.model.removeRow(row)
if os.path.exists(row_data[0]):
os.remove(row_data[0])
if os.path.exists(row_data[0].replace('.wav','.txt')):
os.remove(row_data[0].replace('.wav','.txt').replace('/Audios','/Translations'))
self.saveCSV()
QMessageBox.information(self, "Save Changes", "Changes saved successfully!")
def saveChanges(self):
self.terminal.append("Changes Saved")
self.saveCSV()
QMessageBox.information(self, "Save Changes", "Changes saved successfully!")
def saveCSV(self):
try:
with open(self.file_path, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['file_name', 'transcription','sample_rate','duration_ms'])
for row in range(self.model.rowCount()):
data = [self.model.item(row, col).text() for col in range(self.model.columnCount())]
writer.writerow(data)
except Exception as e:
QMessageBox.critical(self, "Error", f"An error occurred while saving the CSV file:\n{str(e)}")
def refreshCSV(self):
self.terminal.append("Refreshed Data Viewer")
self.model.clear()
self.loadCSV(self.file_path)
def transcribeRemainingFiles(self):
parameters = read_parameters_from_json('config.json')
audio_path = parameters['audios_path']
transcribed_files = self.find_files_to_transcribe()
self.terminal.append("Transcribing Remaining files. Transcriptions will be saved automatically at each file iteration.")
self.terminal.append("Transcribed Files:")
for file in transcribed_files:
print(file)
text = whisper_translation(self.transcriber, 'en', audio_path +'/'+ file[6:]) #TODO
# Set 'Transcribed' in the second column of the current row #transcribe Here
for row in range(self.model.rowCount()):
if self.model.item(row, 0).text() == file:
item = QStandardItem(text)
self.model.setItem(row, 1, item) # Setting the second column to 'Transcribed'
break
self.terminal.append(file)
self.saveCSV()
QMessageBox.information(self, "Save Changes", "Changes saved successfully!")
def find_files_to_transcribe(self):
transcribed_files = []
for row in range(self.model.rowCount()):
transcription = self.model.item(row, 1).text().strip()
if transcription in ['No transcription found.',"Not translated: Coming from file drop."]: #this is the cases in which
file_path = self.model.item(row, 0).text()
transcribed_files.append(file_path)
return transcribed_files
def openExportWindow(self):
self.export_window = ExportWindow()
self.export_window.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = DataWindow("projects/Project/metadata.csv")
window.show()
sys.exit(app.exec_())