-
Notifications
You must be signed in to change notification settings - Fork 0
/
g4lwebsockets.py
459 lines (388 loc) · 20.7 KB
/
g4lwebsockets.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
from flask import Flask, jsonify, request, copy_current_request_context
from flask_socketio import SocketIO, emit, join_room, leave_room
import gevent
from pymongo import MongoClient
from gridfs import GridFS
import base64
import redis
from g4laudio import process_audio, continue_music, generate_session_id
from bson.objectid import ObjectId
from pydantic import BaseModel, ValidationError
import torch
from flask_cors import CORS # Import CORS
import json
from typing import Optional
# MongoDB setup
# THIS IS THE LOCAL VERSION
# client = MongoClient('mongodb://localhost:27017/')
client = MongoClient('mongodb://mongo:27017/')
db = client['audio_generation_db']
sessions = db.sessions
fs = GridFS(db)
# Redis setup
# THIS IS THE LOCAL VERSION
redis_client = redis.StrictRedis(host='redis', port=6379, db=0)
# redis_client = redis.StrictRedis(host='redis', port=6379, db=0)
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
# THIS IS THE LOCAL VERSION
# socketio = SocketIO(app, message_queue='redis://localhost:6379', async_mode='gevent', cors_allowed_origins="*", logger=True, engineio_logger=True, pingTimeout=240000, pingInterval=120000, max_http_buffer_size=16*1024*1024)
socketio = SocketIO(app, message_queue='redis://redis:6379', async_mode='gevent', cors_allowed_origins="*", logger=True, engineio_logger=True, pingTimeout=240000, pingInterval=120000, max_http_buffer_size=64*1024*1024)
@app.route('/')
def index():
return "The WebSocket server is running."
# Pydantic models for validation
class AudioRequest(BaseModel):
audio_data: str
model_name: str
prompt_duration: int
top_k: Optional[int] = None
temperature: Optional[float] = None
cfg_coef: Optional[float] = None
description: Optional[str] = None # New optional field
class SessionRequest(BaseModel):
session_id: str
model_name: Optional[str] = None
prompt_duration: Optional[int] = None
top_k: Optional[int] = None
temperature: Optional[float] = None
cfg_coef: Optional[float] = None
description: Optional[str] = None # New optional field
class ContinueMusicRequest(BaseModel):
session_id: str
model_name: Optional[str] = None
prompt_duration: Optional[int] = None
audio_data: Optional[str] = None
top_k: Optional[int] = None
temperature: Optional[float] = None
cfg_coef: Optional[float] = None
description: Optional[str] = None # New optional field
def store_audio_in_gridfs(data, filename):
"""Store audio data in GridFS."""
audio_data = base64.b64decode(data)
file_id = fs.put(audio_data, filename=filename)
return str(file_id)
def retrieve_audio_from_gridfs(file_id):
"""Retrieve audio data from GridFS."""
try:
file = fs.get(ObjectId(file_id))
return base64.b64encode(file.read()).decode('utf-8')
except Exception as e:
print(f"Error retrieving audio from GridFS: {e}")
return None
def store_audio_data(session_id, audio_data, key):
"""Store session data in MongoDB with GridFS."""
file_id = store_audio_in_gridfs(audio_data, f"{session_id}_{key}.wav")
sessions.update_one({'_id': session_id}, {'$set': {key: file_id}}, upsert=True)
def retrieve_audio_data(session_id, key):
"""Retrieve specific audio data from MongoDB."""
session_data = sessions.find_one({'_id': session_id})
file_id = session_data.get(key) if session_data else None
return retrieve_audio_from_gridfs(file_id) if file_id else None
def set_generation_in_progress(session_id, in_progress):
"""Set or unset the generation_in_progress flag in Redis."""
redis_client.set(f"{session_id}_generation_in_progress", str(in_progress))
def is_generation_in_progress(session_id):
"""Check if generation is in progress using Redis."""
return redis_client.get(f"{session_id}_generation_in_progress") == b'True'
@socketio.on('cleanup_session_request')
def handle_cleanup_request(data):
try:
request_data = SessionRequest(**data)
session_id = request_data.session_id
if session_id:
sessions.delete_one({'_id': session_id})
leave_room(session_id)
redis_client.delete(f"{session_id}_generation_in_progress")
emit('cleanup_complete', {'message': 'Session cleaned up', 'session_id': session_id}, room=session_id)
except ValidationError as e:
emit('error', {'message': str(e), 'session_id': data.get('session_id')})
@socketio.on('process_audio_request')
def handle_audio_processing(data):
try:
# Check if the received data is a string (raw JSON string from Swift)
if isinstance(data, str):
# Remove both single and double backslashes
clean_data = data.replace("\\\\", "\\").replace("\\", "")
# Parse the cleaned raw JSON string into a dictionary
try:
data = json.loads(clean_data)
except json.JSONDecodeError as e:
emit('error', {'message': 'Invalid JSON format: ' + str(e)})
return
# Clean model_name and strip leading/trailing spaces
if "model_name" in data:
data["model_name"] = data["model_name"].replace("\\", "").strip()
# Clean optional parameters and strip leading/trailing spaces
for param in ['top_k', 'temperature', 'cfg_coef', 'description']:
if param in data and data[param] is not None:
data[param] = str(data[param]).replace("\\", "").strip()
# Proceed with the usual flow
request_data = AudioRequest(**data)
session_id = generate_session_id()
if is_generation_in_progress(session_id):
emit('error', {'message': 'Generation already in progress', 'session_id': session_id}, room=session_id)
return
join_room(session_id)
input_data_base64 = request_data.audio_data
model_name = request_data.model_name
prompt_duration = request_data.prompt_duration
# Extract optional parameters with default values if not provided
top_k = int(request_data.top_k) if request_data.top_k is not None else 250
temperature = float(request_data.temperature) if request_data.temperature is not None else 1.0
cfg_coef = float(request_data.cfg_coef) if request_data.cfg_coef is not None else 3.0
description = request_data.description if request_data.description else None
# Log relevant information without base64 data
print(f"Received process_audio_request for session {session_id} with model_name: {model_name}, prompt_duration: {prompt_duration}")
store_audio_data(session_id, input_data_base64, 'initial_audio')
set_generation_in_progress(session_id, True)
@copy_current_request_context
def audio_processing_thread():
try:
def progress_callback(current, total):
progress_percent = (current / total) * 100
emit('progress_update', {'progress': int(progress_percent), 'session_id': session_id}, room=session_id)
# Call process_audio with new parameters
result_base64 = process_audio(
input_data_base64,
model_name,
progress_callback,
prompt_duration=prompt_duration,
top_k=top_k,
temperature=temperature,
cfg_coef=cfg_coef,
description=description
)
print(f"Audio processed successfully for session {session_id}")
store_audio_data(session_id, result_base64, 'last_processed_audio')
emit('audio_processed', {'audio_data': result_base64, 'session_id': session_id}, room=session_id)
except Exception as e:
print(f"Error during audio processing thread for session {session_id}: {e}")
emit('error', {'message': str(e), 'session_id': session_id})
finally:
set_generation_in_progress(session_id, False)
gevent.spawn(audio_processing_thread)
except ValidationError as e:
emit('error', {'message': str(e), 'session_id': generate_session_id()})
@socketio.on('continue_music_request')
def handle_continue_music(data):
try:
# Check if the received data is a string (raw JSON string from Swift)
if isinstance(data, str):
# Remove both single and double backslashes
clean_data = data.replace("\\\\", "\\").replace("\\", "")
# Parse the cleaned raw JSON string into a dictionary
try:
data = json.loads(clean_data)
except json.JSONDecodeError as e:
emit('error', {'message': 'Invalid JSON format: ' + str(e)})
return
# Clean model_name and strip leading/trailing spaces
if "model_name" in data:
data["model_name"] = data["model_name"].replace("\\", "").strip()
# Clean session_id
if "session_id" in data:
data["session_id"] = data["session_id"].replace("\\", "").strip()
# Clean optional parameters and strip leading/trailing spaces
for param in ['top_k', 'temperature', 'cfg_coef', 'description']:
if param in data and data[param] is not None:
data[param] = str(data[param]).replace("\\", "").strip()
# Proceed with the usual flow using the updated Pydantic model
request_data = ContinueMusicRequest(**data)
session_id = request_data.session_id
if is_generation_in_progress(session_id):
emit('error', {'message': 'Generation already in progress', 'session_id': session_id}, room=session_id)
return
# Use 'audio_data' from data if available, else retrieve from session
if request_data.audio_data:
input_data_base64 = request_data.audio_data
print(f"Using 'audio_data' from request for session {session_id}")
else:
input_data_base64 = retrieve_audio_data(session_id, 'last_processed_audio')
print(f"Retrieved 'last_processed_audio' from session {session_id}")
if input_data_base64 is None:
emit('error', {'message': 'No audio data available for continuation', 'session_id': session_id}, room=session_id)
return
# Extract optional parameters with default values if not provided
top_k = int(request_data.top_k) if request_data.top_k is not None else 250
temperature = float(request_data.temperature) if request_data.temperature is not None else 1.0
cfg_coef = float(request_data.cfg_coef) if request_data.cfg_coef is not None else 3.0
description = request_data.description if request_data.description else None
model_name = request_data.model_name or sessions.find_one({'_id': session_id}).get('model_name')
prompt_duration = request_data.prompt_duration or sessions.find_one({'_id': session_id}).get('prompt_duration')
print(f"Continuing music for session {session_id} with model_name: {model_name}, prompt_duration: {prompt_duration}")
set_generation_in_progress(session_id, True)
@copy_current_request_context
def continue_music_thread():
try:
def progress_callback(current, total):
progress_percent = (current / total) * 100
emit('progress_update', {'progress': int(progress_percent), 'session_id': session_id}, room=session_id)
result_base64 = continue_music(
input_data_base64,
model_name,
progress_callback,
prompt_duration=prompt_duration,
top_k=top_k,
temperature=temperature,
cfg_coef=cfg_coef,
description=description
)
store_audio_data(session_id, input_data_base64, 'last_input_audio')
store_audio_data(session_id, result_base64, 'last_processed_audio')
# Calculate the size of the base64 string in bytes
result_size_bytes = len(result_base64.encode('utf-8'))
# Get the max_http_buffer_size (ensure it's consistent with your SocketIO configuration)
max_size_bytes = 64 * 1024 * 1024 # 64 MB
if result_size_bytes > max_size_bytes:
emit('error', {
'message': 'Generated audio data is too large to send.',
'session_id': session_id,
'code': 'DATA_TOO_LARGE'
}, room=session_id)
print(f"Generated audio data is too large for session {session_id}: {result_size_bytes} bytes.")
else:
try:
emit('music_continued', {'audio_data': result_base64, 'session_id': session_id}, room=session_id)
except Exception as e:
print(f"Error emitting music_continued for session {session_id}: {e}")
emit('error', {
'message': 'Error sending generated audio data.',
'session_id': session_id,
'code': 'EMIT_ERROR'
}, room=session_id)
except Exception as e:
print(f"Error during continue_music_thread for session {session_id}: {e}")
emit('error', {'message': str(e), 'session_id': session_id}, room=session_id)
finally:
set_generation_in_progress(session_id, False)
gevent.spawn(continue_music_thread)
except ValidationError as e:
emit('error', {'message': str(e), 'session_id': data.get('session_id') if isinstance(data, dict) else None})
@socketio.on('retry_music_request')
def handle_retry_music(data):
try:
# Check if the received data is a string (raw JSON string from Swift)
if isinstance(data, str):
# Remove both single and double backslashes
clean_data = data.replace("\\\\", "\\").replace("\\", "")
# Parse the cleaned raw JSON string into a dictionary
try:
data = json.loads(clean_data)
except json.JSONDecodeError as e:
emit('error', {'message': 'Invalid JSON format: ' + str(e)})
return
# Clean model_name and strip leading/trailing spaces
if "model_name" in data:
data["model_name"] = data["model_name"].replace("\\", "").strip()
# Clean session_id
if "session_id" in data:
data["session_id"] = data["session_id"].replace("\\", "").strip()
# Clean optional parameters and strip leading/trailing spaces
for param in ['top_k', 'temperature', 'cfg_coef', 'description']:
if param in data and data[param] is not None:
data[param] = str(data[param]).replace("\\", "").strip()
# Proceed with the usual flow using the updated data
request_data = SessionRequest(**data)
session_id = request_data.session_id
if is_generation_in_progress(session_id):
emit('error', {'message': 'Generation already in progress', 'session_id': session_id}, room=session_id)
return
last_input_base64 = retrieve_audio_data(session_id, 'last_input_audio')
if last_input_base64 is None:
emit('error', {'message': 'No last input audio available for retry', 'session_id': session_id}, room=session_id)
return
# Extract optional parameters with default values if not provided
top_k = int(request_data.top_k) if request_data.top_k is not None else 250
temperature = float(request_data.temperature) if request_data.temperature is not None else 1.0
cfg_coef = float(request_data.cfg_coef) if request_data.cfg_coef is not None else 3.0
description = request_data.description if request_data.description else None
model_name = request_data.model_name or sessions.find_one({'_id': session_id}).get('model_name')
prompt_duration = request_data.prompt_duration or sessions.find_one({'_id': session_id}).get('prompt_duration')
print(f"Retrying music for session {session_id} with model_name: {model_name}, prompt_duration: {prompt_duration}")
set_generation_in_progress(session_id, True)
@copy_current_request_context
def retry_music_thread():
try:
def progress_callback(current, total):
progress_percent = (current / total) * 100
emit('progress_update', {'progress': int(progress_percent), 'session_id': session_id}, room=session_id)
result_base64 = continue_music(
last_input_base64,
model_name,
progress_callback,
prompt_duration=prompt_duration,
top_k=top_k,
temperature=temperature,
cfg_coef=cfg_coef,
description=description
)
store_audio_data(session_id, last_input_base64, 'last_input_audio')
store_audio_data(session_id, result_base64, 'last_processed_audio')
emit('music_retried', {'audio_data': result_base64, 'session_id': session_id}, room=session_id)
except Exception as e:
print(f"Error during retry_music_thread for session {session_id}: {e}")
emit('error', {'message': str(e), 'session_id': session_id}, room=session_id)
finally:
set_generation_in_progress(session_id, False)
gevent.spawn(retry_music_thread)
except ValidationError as e:
session_id = data.get('session_id') if isinstance(data, dict) else None
emit('error', {'message': str(e), 'session_id': session_id})
@socketio.on('update_cropped_audio')
def handle_update_cropped_audio(data):
try:
# Check if the received data is a string (raw JSON string from Swift)
if isinstance(data, str):
# Remove both single and double backslashes
clean_data = data.replace("\\\\", "\\").replace("\\", "")
# Parse the cleaned raw JSON string into a dictionary
try:
data = json.loads(clean_data)
except json.JSONDecodeError as e:
emit('error', {'message': 'Invalid JSON format: ' + str(e)})
return
# Proceed with the usual flow
request_data = SessionRequest(**data)
session_id = request_data.session_id
audio_data_base64 = data.get('audio_data') # Use get method to safely retrieve audio_data
if session_id and audio_data_base64:
store_audio_data(session_id, audio_data_base64, 'last_processed_audio')
emit('update_cropped_audio_complete', {'message': 'Cropped audio updated', 'session_id': session_id}, room=session_id)
print(f"Cropped audio updated for session {session_id}")
else:
raise ValueError("Missing session_id or audio_data")
except Exception as e:
session_id = data.get('session_id') if isinstance(data, dict) else 'unknown'
print(f"Error in update_cropped_audio for session {session_id}: {e}")
emit('error', {'message': str(e), 'session_id': session_id})
# Robust Health Check Route
@app.route('/health', methods=['GET'])
def health_check():
health_status = {"status": "live"}
# Check MongoDB
try:
client.admin.command('ping')
health_status['mongodb'] = 'live'
except Exception as e:
health_status['mongodb'] = f'down: {str(e)}'
health_status['status'] = 'degraded'
# Check Redis
try:
redis_client.ping()
health_status['redis'] = 'live'
except Exception as e:
health_status['redis'] = f'down: {str(e)}'
health_status['status'] = 'degraded'
# Check PyTorch (Optional, if it's critical)
try:
if torch.cuda.is_available():
health_status['pytorch'] = 'live'
else:
health_status['pytorch'] = 'no GPU detected'
except Exception as e:
health_status['pytorch'] = f'down: {str(e)}'
health_status['status'] = 'degraded'
return jsonify(health_status), 200 if health_status['status'] == 'live' else 500
if __name__ == '__main__':
socketio.run(app, debug=False)