-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathroutes.py
More file actions
440 lines (372 loc) · 15.7 KB
/
Copy pathroutes.py
File metadata and controls
440 lines (372 loc) · 15.7 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
"""
Chord recognition routes for ChordMini Flask application.
This module provides all chord recognition endpoints including model testing
and information endpoints.
"""
import os
import tempfile
import traceback
import requests
from flask import Blueprint, request, jsonify, current_app
from config import get_config
from extensions import limiter
from utils.logging import log_info, log_error, log_debug
from utils.paths import AUDIO_DIR
from services.audio.tempfiles import temporary_file
from .validators import (
validate_chord_recognition_request,
validate_firebase_chord_recognition_request,
validate_file_size,
normalize_audio_url_to_path,
get_detector_display_name
)
# Create blueprint
chords_bp = Blueprint('chords', __name__)
# Get configuration
config = get_config()
def _download_remote_audio_to_temp_path(file_url: str, temp_path: str, timeout_seconds: int = 300) -> None:
"""Stream a remote audio file into a temporary path."""
with requests.get(file_url, stream=True, timeout=(30, timeout_seconds)) as response:
response.raise_for_status()
with open(temp_path, 'wb') as file_handle:
for chunk in response.iter_content(chunk_size=1024 * 1024):
if chunk:
file_handle.write(chunk)
@chords_bp.route('/api/recognize-chords', methods=['POST'])
@limiter.limit(config.get_rate_limit('heavy_processing'))
def recognize_chords():
"""
Recognize chords in an audio file using various models.
Parameters:
- file: The audio file to analyze (multipart/form-data)
- audio_path: Alternative to file, path to an existing audio file on the server
- detector: Model to use ('chord-cnn-lstm', 'btc-sl', 'btc-pl', 'auto')
- chord_dict: Optional chord dictionary to use
- force: Force use of detector even if file is large
- use_spleeter: Use Spleeter for audio separation
Returns:
- JSON with chord recognition results
"""
temp_file_path = None
try:
# Validate request
is_valid, error_msg, file, params = validate_chord_recognition_request()
if not is_valid:
return jsonify({"error": error_msg}), 400
# Get chord recognition service
chord_service = current_app.extensions['services']['chord_recognition']
# Handle different input types
if params['json_data']:
# JSON request with audioUrl
data = params['json_data']
audio_url = data.get('audioUrl')
if audio_url and audio_url.startswith('/audio/'):
file_path = normalize_audio_url_to_path(audio_url, str(AUDIO_DIR))
if not os.path.exists(file_path):
return jsonify({"error": f"Audio file not found: {audio_url}"}), 404
else:
return jsonify({"error": "Invalid audioUrl format"}), 400
elif file:
# File upload
# Validate file size
size_valid, size_error = validate_file_size(file, params['detector'], params['force'])
if not size_valid:
return jsonify({"error": size_error}), 413
# Save uploaded file temporarily
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
file.save(temp_file.name)
temp_file_path = temp_file.name
file_path = temp_file_path
elif params['audio_path']:
# Existing file path
file_path = params['audio_path']
if not os.path.exists(file_path):
return jsonify({"error": f"Audio file not found: {file_path}"}), 404
else:
return jsonify({"error": "No valid audio input provided"}), 400
log_info(f"Processing chord recognition request: detector={params['detector']}, "
f"chord_dict={params['chord_dict']}, force={params['force']}, "
f"use_spleeter={params['use_spleeter']}")
# Run chord recognition
result = chord_service.recognize_chords(
file_path=file_path,
detector=params['detector'],
chord_dict=params['chord_dict'],
force=params['force'],
use_spleeter=params['use_spleeter']
)
if result.get('success'):
log_info(f"Chord recognition successful: {result['total_chords']} chords detected "
f"using {result['model_used']} with {result['chord_dict']} dictionary")
else:
log_error(f"Chord recognition failed: {result.get('error', 'Unknown error')}")
return jsonify(result)
except Exception as e:
error_msg = f"Chord recognition error: {str(e)}"
log_error(error_msg)
log_error(traceback.format_exc())
return jsonify({
"success": False,
"error": error_msg,
"traceback": traceback.format_exc() if not config.PRODUCTION_MODE else None
}), 500
finally:
# Clean up temporary file
if temp_file_path and os.path.exists(temp_file_path):
try:
os.unlink(temp_file_path)
log_debug(f"Cleaned up temporary file: {temp_file_path}")
except Exception as cleanup_error:
log_error(f"Failed to clean up temporary file {temp_file_path}: {cleanup_error}")
@chords_bp.route('/api/recognize-chords-firebase', methods=['POST'])
@limiter.limit(config.get_rate_limit('heavy_processing'))
def recognize_chords_firebase():
"""
Recognize chords in an audio file from Firebase Storage URL.
Parameters:
- firebase_url: Firebase Storage URL of the audio file
- detector: Chord recognition model to use
- chord_dict: Optional chord dictionary to use
Returns:
- JSON with chord recognition results
"""
try:
# Validate request
is_valid, error_msg, params = validate_firebase_chord_recognition_request()
if not is_valid:
return jsonify({"error": error_msg}), 400
firebase_url = params['firebase_url']
detector = params['detector']
chord_dict = params['chord_dict']
log_info(f"Processing Firebase chord recognition: {firebase_url[:100]}... "
f"with detector={detector}")
# Download file from Firebase Storage
log_info("Downloading file from Firebase Storage...")
with temporary_file(suffix='.mp3') as temp_file_path:
_download_remote_audio_to_temp_path(firebase_url, temp_file_path)
log_info(f"Downloaded file to: {temp_file_path}")
log_info(f"File size: {os.path.getsize(temp_file_path) / (1024 * 1024):.1f}MB")
# Get chord recognition service
chord_service = current_app.extensions['services']['chord_recognition']
# Run chord recognition
result = chord_service.recognize_chords(
file_path=temp_file_path,
detector=detector,
chord_dict=chord_dict,
force=False, # Don't force for Firebase requests
use_spleeter=False # Don't use Spleeter for Firebase requests
)
if result.get('success'):
log_info(f"Firebase chord recognition successful: {result['total_chords']} chords detected")
else:
log_error(f"Firebase chord recognition failed: {result.get('error', 'Unknown error')}")
return jsonify(result)
except requests.exceptions.RequestException as e:
error_msg = f"Failed to download file from Firebase Storage: {str(e)}"
log_error(error_msg)
return jsonify({"error": error_msg}), 400
except Exception as e:
error_msg = f"Firebase chord recognition error: {str(e)}"
log_error(error_msg)
log_error(traceback.format_exc())
return jsonify({
"success": False,
"error": error_msg,
"traceback": traceback.format_exc() if not config.PRODUCTION_MODE else None
}), 500
finally:
# Clean up temporary file
if temp_file_path and os.path.exists(temp_file_path):
try:
os.unlink(temp_file_path)
log_debug(f"Cleaned up temporary file: {temp_file_path}")
except Exception as cleanup_error:
log_error(f"Failed to clean up temporary file {temp_file_path}: {cleanup_error}")
@chords_bp.route('/api/chord-model-info', methods=['GET'])
@limiter.limit(config.get_rate_limit('light_processing'))
def chord_model_info():
"""Return information about available chord recognition models"""
try:
# Get chord recognition service
chord_service = current_app.extensions['services']['chord_recognition']
# Get detector information
detector_info = chord_service.get_detector_info()
return jsonify({
"success": True,
"available_chord_models": detector_info["available_detectors"],
"chord_model_info": {
name: {
"name": info["name"],
"description": info["description"],
"available": info["available"],
"supported_chord_dicts": info["supported_chord_dicts"],
"default_chord_dict": info["default_chord_dict"],
"size_limit_mb": info["size_limit_mb"]
}
for name, info in detector_info["detectors"].items()
},
"spleeter_available": detector_info["spleeter_available"],
"default_chord_model": detector_info["available_detectors"][0] if detector_info["available_detectors"] else None
})
except Exception as e:
error_msg = f"Error getting chord model info: {str(e)}"
log_error(error_msg)
return jsonify({
"success": False,
"error": error_msg
}), 500
@chords_bp.route('/api/test-chord-cnn-lstm', methods=['GET'])
@limiter.limit(config.get_rate_limit('test'))
def test_chord_cnn_lstm():
"""Test Chord-CNN-LSTM model availability"""
try:
# Get chord recognition service
chord_service = current_app.extensions['services']['chord_recognition']
# Get detector
detector = chord_service.detectors['chord-cnn-lstm']
if detector.is_available():
model_info = detector.get_model_info()
return jsonify({
"success": True,
"model": "Chord-CNN-LSTM",
"status": "available",
"message": "Chord-CNN-LSTM model is ready for use",
"model_info": model_info
})
else:
return jsonify({
"success": False,
"model": "Chord-CNN-LSTM",
"status": "unavailable",
"error": "Chord-CNN-LSTM model is not available"
})
except Exception as e:
error_msg = f"Error testing Chord-CNN-LSTM: {str(e)}"
log_error(error_msg)
return jsonify({
"success": False,
"model": "Chord-CNN-LSTM",
"status": "error",
"error": error_msg
}), 500
@chords_bp.route('/api/test-btc-sl', methods=['GET'])
@limiter.limit(config.get_rate_limit('test'))
def test_btc_sl():
"""Test BTC-SL (Self-Label) model availability"""
try:
# Get chord recognition service
chord_service = current_app.extensions['services']['chord_recognition']
# Get detector
detector = chord_service.detectors['btc-sl']
if detector.is_available():
model_info = detector.get_model_info()
return jsonify({
"success": True,
"model": "BTC-SL",
"status": "available",
"message": "BTC-SL model is ready for use",
"model_info": model_info
})
else:
return jsonify({
"success": False,
"model": "BTC-SL",
"status": "unavailable",
"error": "BTC-SL model is not available"
})
except Exception as e:
error_msg = f"Error testing BTC-SL: {str(e)}"
log_error(error_msg)
return jsonify({
"success": False,
"model": "BTC-SL",
"status": "error",
"error": error_msg
}), 500
@chords_bp.route('/api/test-btc-pl', methods=['GET'])
@limiter.limit(config.get_rate_limit('test'))
def test_btc_pl():
"""Test BTC-PL (Pseudo-Label) model availability"""
try:
# Get chord recognition service
chord_service = current_app.extensions['services']['chord_recognition']
# Get detector
detector = chord_service.detectors['btc-pl']
if detector.is_available():
model_info = detector.get_model_info()
return jsonify({
"success": True,
"model": "BTC-PL",
"status": "available",
"message": "BTC-PL model is ready for use",
"model_info": model_info
})
else:
return jsonify({
"success": False,
"model": "BTC-PL",
"status": "unavailable",
"error": "BTC-PL model is not available"
})
except Exception as e:
error_msg = f"Error testing BTC-PL: {str(e)}"
log_error(error_msg)
return jsonify({
"success": False,
"model": "BTC-PL",
"status": "error",
"error": error_msg
}), 500
@chords_bp.route('/api/test-all-chord-models', methods=['GET'])
@limiter.limit(config.get_rate_limit('test'))
def test_all_chord_models():
"""Test all available chord recognition models"""
try:
# Get chord recognition service
chord_service = current_app.extensions['services']['chord_recognition']
results = {
"success": True,
"models_tested": [],
"available_models": [],
"unavailable_models": []
}
# Test each detector
for detector_name in ['chord-cnn-lstm', 'btc-sl', 'btc-pl']:
detector = chord_service.detectors[detector_name]
test_result = {
"name": detector_name,
"display_name": get_detector_display_name(detector_name),
"available": detector.is_available()
}
if detector.is_available():
results["available_models"].append(detector_name)
test_result["status"] = "available"
# Add model info
try:
model_info = detector.get_model_info()
test_result["model_info"] = model_info
except Exception as e:
test_result["info_error"] = str(e)
else:
results["unavailable_models"].append(detector_name)
test_result["status"] = "unavailable"
test_result["error"] = f"{detector_name} not available"
results["models_tested"].append(test_result)
# Add summary
results["summary"] = {
"total_models": len(results["models_tested"]),
"available_count": len(results["available_models"]),
"unavailable_count": len(results["unavailable_models"]),
"default_model": results["available_models"][0] if results["available_models"] else "none"
}
# Add Spleeter info
results["spleeter_available"] = chord_service.spleeter_service.is_available()
return jsonify(results)
except Exception as e:
error_msg = f"Error testing chord models: {str(e)}"
log_error(error_msg)
return jsonify({
"success": False,
"error": error_msg,
"traceback": traceback.format_exc() if not config.PRODUCTION_MODE else None
}), 500