-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
343 lines (284 loc) · 13.1 KB
/
server.py
File metadata and controls
343 lines (284 loc) · 13.1 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
from flask import Flask, jsonify, send_from_directory, request, Response, send_file
from flask_cors import CORS
import os
import json
import logging
from typing import Dict, List, Optional
from pathlib import Path
from blackbird.dataset import Dataset
from tqdm import tqdm
import math
import socket
import mimetypes
from text_compare import get_diff
# Configure logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
app = Flask(__name__)
CORS(app)
# Global variables
dataset = None
compositions = []
HOST = '0.0.0.0' # This will allow external connections
PORT = 7779
@app.route('/')
def index():
return "Server is running! 🚀"
def get_server_url():
# Get the actual network IP address
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# Doesn't need to be reachable
s.connect(('10.255.255.255', 1))
actual_ip = s.getsockname()[0]
except Exception:
actual_ip = '127.0.0.1'
finally:
s.close()
return f"http://{actual_ip}:{PORT}"
"""Process text differences and add HTML spans for highlighting."""
def highlight_diffs(text1, text2, text3):
# Use get_diff which takes a list of texts
diff_result = get_diff([text1, text2, text3])
# Process each text's diff results
html_texts = []
for text_result in diff_result:
processed_text = []
for char, presence in text_result:
is_unique = len(presence) == 1 # Unique to this text if only present in one text
if not is_unique:
if char == '\n':
processed_text.append(char + '<br>')
elif char == ' ':
processed_text.append(' ')
else:
processed_text.append(char)
else:
style_attr = ' style="background: #ffebee;"'
if char == '\n':
processed_text.append(f'<span{style_attr}>{char}</span><br>')
elif char == ' ':
processed_text.append(f'<span{style_attr}> </span>')
else:
processed_text.append(f'<span{style_attr}>{char}</span>')
html_texts.append(''.join(processed_text))
return html_texts
def initialize_dataset() -> None:
"""Initialize the blackbird dataset and scan for compositions."""
global dataset, compositions
try:
dataset_path = Path("/media/k4_nas/disk1/Datasets/Music_Part1")
logger.info(f"Initializing dataset at {dataset_path}")
dataset = Dataset(dataset_path)
# Get statistics after loading
stats = dataset.analyze()
logger.info("\nDataset Statistics:")
logger.info(f"Total tracks: {stats['tracks']['total']}")
logger.info(f"Complete tracks: {stats['tracks']['complete']}")
# Get all tracks and build compositions
tracks = dataset.find_tracks()
logger.info(f"\nFound {len(tracks)} total tracks")
# Group tracks by album with progress bar
albums = {}
for track_name, track_paths in tqdm(tracks.items(), desc="Grouping tracks by album"):
for track_path in track_paths:
track_path = os.path.relpath(track_path, dataset_path)
album_path = os.path.dirname(track_path)
if album_path not in albums:
albums[album_path] = []
albums[album_path].append(str(track_path))
# Convert albums to compositions format
compositions_list = []
for album_path, album_tracks in tqdm(albums.items(), desc="Processing albums"):
if not album_tracks:
continue
# Process first track from album
track_path = album_tracks[0]
track_dir = os.path.dirname(track_path)
track_base = os.path.splitext(os.path.basename(track_path))[0]
# Extract group name and track name
group_name = os.path.basename(os.path.dirname(album_path))
track_name = track_base
composition_title = f"{group_name} - {track_name}"
composition = {
"id": len(compositions_list) + 1,
"title": composition_title,
"tracks": []
}
main_track_path = os.path.join(dataset_path, track_dir, track_base + ".opus")
vocal1_path = os.path.join(dataset_path, track_dir, track_base + "_vocal1.mp3")
vocal1_dereverb_path = os.path.join(dataset_path, track_dir, track_base + "_vocal1_dereverb.mp3")
vocal2_path = os.path.join(dataset_path, track_dir, track_base + "_vocal2.mp3")
vocal2_dereverb_path = os.path.join(dataset_path, track_dir, track_base + "_vocal2_dereverb.mp3")
vocal2_dereverb_json_path = os.path.join(dataset_path, track_dir, track_base + "_vocal2_dereverb.json")
if os.path.exists(main_track_path) and os.path.exists(vocal1_path) and os.path.exists(vocal1_dereverb_path) and os.path.exists(vocal2_path) and os.path.exists(vocal2_dereverb_path) and os.path.exists(vocal2_dereverb_json_path):
composition["tracks"] = [
{
"title": "Main Track",
"type": "main",
"url": f"{get_server_url()}/audio/{os.path.relpath(main_track_path, dataset_path)}",
"markers": []
},
{
"title": "Vocal 1",
"type": "vocal1",
"url": f"{get_server_url()}/audio/{os.path.relpath(vocal1_path, dataset_path)}",
"markers": []
},
{
"title": "Vocal 2",
"type": "vocal2",
"url": f"{get_server_url()}/audio/{os.path.relpath(vocal2_path, dataset_path)}",
"markers": []
},
{
"title": "Vocal 1 (Dereverb)",
"type": "vocal1_dereverb",
"url": f"{get_server_url()}/audio/{os.path.relpath(vocal1_dereverb_path, dataset_path)}",
"markers": []
},
{
"title": "Vocal 2 (Dereverb)",
"type": "vocal2_dereverb",
"url": f"{get_server_url()}/audio/{os.path.relpath(vocal2_dereverb_path, dataset_path)}",
"markers": []
}
]
# add markers from json
with open(vocal2_dereverb_json_path, 'r') as f:
json_data = json.load(f)
# Convert markers format
converted_markers = []
segments = []
if "segments" in json_data:
for idx, segment in enumerate(json_data["segments"], 1):
# Convert milliseconds to seconds
start_sec = segment["start_ms"] / 1000
end_sec = segment["end_ms"] / 1000
phi4_text = segment.get("phi4_text", "")
whisper3_text = segment.get("whisper3_text", "")
nemo_text = segment.get("nemo_text", "")
# process texts with highlighting
phi4_text, whisper3_text, nemo_text = highlight_diffs(phi4_text, whisper3_text, nemo_text)
marker = {
"start": start_sec,
"end": end_sec,
"label": f"label_{idx}",
"phi4_text": phi4_text,
"whisper3_text": whisper3_text,
"nemo_text": nemo_text,
}
converted_markers.append(marker)
segment = {
"label": f"label_{idx}",
"phi4_text": phi4_text,
"whisper3_text": whisper3_text,
"nemo_text": nemo_text
}
segments.append(segment)
composition["tracks"][4]["markers"] = converted_markers
composition["tracks"][4]["segments"] = segments
compositions_list.append(composition)
compositions = compositions_list
logger.info(f"Successfully loaded {len(compositions)} compositions")
logger.info(f"Server running at {get_server_url()}")
except Exception as error:
logger.error(f'Error initializing dataset: {str(error)}')
compositions = []
@app.route('/api/compositions')
def get_compositions():
# Get pagination parameters from query string
page = request.args.get('page', default=1, type=int)
limit = request.args.get('limit', default=5, type=int)
# Calculate start and end indices
start_index = (page - 1) * limit
end_index = page * limit
# Get paginated compositions
paginated_compositions = compositions[start_index:end_index]
# Prepare response with pagination metadata
response = {
"compositions": paginated_compositions,
"pagination": {
"total": len(compositions),
"currentPage": page,
"totalPages": math.ceil(len(compositions) / limit),
"hasNextPage": end_index < len(compositions),
"hasPrevPage": page > 1
}
}
return jsonify(response)
@app.route('/api/compositions/<int:composition_id>')
def get_composition(composition_id):
composition = next((c for c in compositions if c["id"] == composition_id), None)
if not composition:
return jsonify({"error": "Composition not found"}), 404
return jsonify(composition)
@app.route('/audio/<path:filename>')
def serve_audio(filename):
dataset_path = Path("/media/k4_nas/disk1/Datasets/Music_Part1")
file_path = os.path.join(dataset_path, filename)
if not os.path.exists(file_path):
logger.error(f"Audio file not found: {file_path}")
return jsonify({"error": "File not found"}), 404
# Get file size and mime type
file_size = os.path.getsize(file_path)
mime_type = mimetypes.guess_type(file_path)[0] or 'application/octet-stream'
# Handle range request
range_header = request.headers.get('Range')
if range_header:
try:
ranges = range_header.replace('bytes=', '').split('-')
start = int(ranges[0]) if ranges[0] else 0
end = int(ranges[1]) if ranges[1] else file_size - 1
# Ensure valid range
if start >= file_size:
return jsonify({"error": "Invalid range"}), 416
chunk_size = end - start + 1
# Create response with partial content
response = send_file(
file_path,
mimetype=mime_type,
as_attachment=False,
conditional=True
)
response.headers['Content-Range'] = f'bytes {start}-{end}/{file_size}'
response.headers['Accept-Ranges'] = 'bytes'
response.headers['Content-Length'] = str(chunk_size)
response.status_code = 206
return response
except (ValueError, IndexError) as e:
logger.error(f"Invalid range request: {str(e)}")
return jsonify({"error": "Invalid range"}), 416
# No range request, serve entire file
response = send_file(
file_path,
mimetype=mime_type,
as_attachment=False,
conditional=True
)
response.headers['Accept-Ranges'] = 'bytes'
response.headers['Content-Length'] = str(file_size)
return response
@app.route('/api/rescan', methods=['POST'])
def rescan():
try:
initialize_dataset()
return jsonify({
"message": "Dataset rescanned successfully",
"count": len(compositions)
})
except Exception as error:
return jsonify({
"error": f"Failed to rescan dataset: {str(error)}"
}), 500
if __name__ == '__main__':
# Only initialize the dataset in the main process when in debug mode
# When debug=True, Flask spawns a reloader process that would cause the initialization to run twice
if not os.environ.get('WERKZEUG_RUN_MAIN'):
print(f"Starting server on http://{HOST}:{PORT}")
print(f"You can access the server at http://localhost:{PORT} or http://<your-ip-address>:{PORT}")
else:
# Initialize only in the main process
initialize_dataset()
# Start the server with threaded=True for better performance
app.run(host=HOST, port=PORT, debug=True, threaded=True)