Spaces:
Running
Running
Joffrey Thomas commited on
Commit ·
5cc830b
1
Parent(s): 974898d
lower max sessions
Browse files
app.py
CHANGED
|
@@ -25,7 +25,7 @@ WPM_WINDOW = 10 # seconds for running mean calculation
|
|
| 25 |
CALIBRATION_PERIOD = 5 # seconds before showing WPM
|
| 26 |
SESSION_TIMEOUT = int(os.environ.get("SESSION_TIMEOUT", "30")) # Max 30s per session
|
| 27 |
INACTIVITY_TIMEOUT = int(os.environ.get("INACTIVITY_TIMEOUT", "10")) # Close after 10s silence
|
| 28 |
-
MAX_CONCURRENT_SESSIONS = int(os.environ.get("MAX_SESSIONS", "
|
| 29 |
|
| 30 |
# Global config (shared across users)
|
| 31 |
ws_url = ""
|
|
@@ -469,33 +469,37 @@ def auto_start_recording(session):
|
|
| 469 |
|
| 470 |
|
| 471 |
def stop_session(session_id):
|
| 472 |
-
"""Stop the websocket connection
|
| 473 |
-
session = ensure_session(session_id)
|
| 474 |
-
|
| 475 |
-
if not session.is_running:
|
| 476 |
-
# Already stopped
|
| 477 |
-
return get_transcription_html(session.transcription_text, session.status_message, session.current_wpm), session.session_id
|
| 478 |
-
|
| 479 |
-
session.is_running = False
|
| 480 |
-
session.last_audio_time = None
|
| 481 |
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
with _sessions_lock:
|
| 489 |
-
_active_sessions.pop(session.session_id, None)
|
| 490 |
-
active_count = len(_active_sessions)
|
| 491 |
|
| 492 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
|
| 494 |
-
#
|
| 495 |
-
session.
|
| 496 |
-
session.session_start_time = None
|
| 497 |
|
| 498 |
-
|
|
|
|
|
|
|
| 499 |
|
| 500 |
|
| 501 |
def clear_history(session_id):
|
|
@@ -528,6 +532,18 @@ def clear_history(session_id):
|
|
| 528 |
|
| 529 |
def process_audio(audio, session_id):
|
| 530 |
"""Process incoming audio and queue for streaming."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 531 |
# Always ensure we have a valid session first
|
| 532 |
try:
|
| 533 |
session = ensure_session(session_id)
|
|
|
|
| 25 |
CALIBRATION_PERIOD = 5 # seconds before showing WPM
|
| 26 |
SESSION_TIMEOUT = int(os.environ.get("SESSION_TIMEOUT", "30")) # Max 30s per session
|
| 27 |
INACTIVITY_TIMEOUT = int(os.environ.get("INACTIVITY_TIMEOUT", "10")) # Close after 10s silence
|
| 28 |
+
MAX_CONCURRENT_SESSIONS = int(os.environ.get("MAX_SESSIONS", "50"))
|
| 29 |
|
| 30 |
# Global config (shared across users)
|
| 31 |
ws_url = ""
|
|
|
|
| 469 |
|
| 470 |
|
| 471 |
def stop_session(session_id):
|
| 472 |
+
"""Stop the websocket connection and invalidate the session.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
|
| 474 |
+
Returns None for session_id so a fresh session is created on next recording.
|
| 475 |
+
This prevents duplicate session issues when users stop and restart quickly.
|
| 476 |
+
"""
|
| 477 |
+
session = ensure_session(session_id)
|
| 478 |
+
old_transcript = session.transcription_text
|
| 479 |
+
old_wpm = session.current_wpm
|
|
|
|
|
|
|
|
|
|
| 480 |
|
| 481 |
+
if session.is_running:
|
| 482 |
+
session.is_running = False
|
| 483 |
+
session.last_audio_time = None
|
| 484 |
+
|
| 485 |
+
# Cancel the running task if any
|
| 486 |
+
if session._task is not None:
|
| 487 |
+
session._task.cancel()
|
| 488 |
+
session._task = None
|
| 489 |
+
|
| 490 |
+
# Remove from active sessions
|
| 491 |
+
with _sessions_lock:
|
| 492 |
+
_active_sessions.pop(session.session_id, None)
|
| 493 |
+
active_count = len(_active_sessions)
|
| 494 |
+
|
| 495 |
+
print(f"Mic stopped - session {session.session_id[:8]} ended. Active sessions: {active_count}")
|
| 496 |
|
| 497 |
+
# Remove from registry - the session is done
|
| 498 |
+
cleanup_session(session.session_id)
|
|
|
|
| 499 |
|
| 500 |
+
# Return None for session_id - a fresh session will be created on next recording
|
| 501 |
+
# This ensures no duplicate sessions when users stop/start quickly
|
| 502 |
+
return get_transcription_html(old_transcript, "ready", old_wpm), None
|
| 503 |
|
| 504 |
|
| 505 |
def clear_history(session_id):
|
|
|
|
| 532 |
|
| 533 |
def process_audio(audio, session_id):
|
| 534 |
"""Process incoming audio and queue for streaming."""
|
| 535 |
+
# Check capacity early - if at max and this is a new user, reject immediately
|
| 536 |
+
with _sessions_lock:
|
| 537 |
+
at_capacity = len(_active_sessions) >= MAX_CONCURRENT_SESSIONS
|
| 538 |
+
is_active_user = session_id and any(s.session_id == session_id for s in _active_sessions.values())
|
| 539 |
+
|
| 540 |
+
if at_capacity and not is_active_user:
|
| 541 |
+
return get_transcription_html(
|
| 542 |
+
"The server is currently at capacity, try again later.",
|
| 543 |
+
"error",
|
| 544 |
+
""
|
| 545 |
+
), session_id
|
| 546 |
+
|
| 547 |
# Always ensure we have a valid session first
|
| 548 |
try:
|
| 549 |
session = ensure_session(session_id)
|