Spaces:
Sleeping
Sleeping
Sasha commited on
Commit ·
613007f
1
Parent(s): e0e1da7
Integrate stream-start and stream-end signaling in worker.py and add /api/log/stream-start endpoint.
Browse files- local_worker/worker.py +19 -0
- server/server.js +12 -0
local_worker/worker.py
CHANGED
|
@@ -284,6 +284,19 @@ def send_voice_words(words):
|
|
| 284 |
except Exception as e:
|
| 285 |
print(f"[Sync] Error sending voice words to cloud: {e}")
|
| 286 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
def notify_stream_end():
|
| 288 |
"""Notify backend that the stream has ended"""
|
| 289 |
headers = {"x-api-key": API_KEY}
|
|
@@ -570,6 +583,9 @@ if __name__ == "__main__":
|
|
| 570 |
t_chat.start()
|
| 571 |
t_sender.start()
|
| 572 |
|
|
|
|
|
|
|
|
|
|
| 573 |
# 3. Start Audio Capture (Blocks main thread)
|
| 574 |
try:
|
| 575 |
if CAPTURE_METHOD == "stream":
|
|
@@ -580,6 +596,9 @@ if __name__ == "__main__":
|
|
| 580 |
print(f"[Error] Unknown CAPTURE_METHOD '{CAPTURE_METHOD}'. Choose 'stream' or 'microphone'.")
|
| 581 |
except KeyboardInterrupt:
|
| 582 |
print("\n[Shutting Down] Gracefully stopping threads...")
|
|
|
|
|
|
|
|
|
|
| 583 |
stop_flag.set()
|
| 584 |
time.sleep(1)
|
| 585 |
print("[Shutting Down] Done.")
|
|
|
|
| 284 |
except Exception as e:
|
| 285 |
print(f"[Sync] Error sending voice words to cloud: {e}")
|
| 286 |
|
| 287 |
+
def notify_stream_start():
|
| 288 |
+
"""Notify backend that the stream has started"""
|
| 289 |
+
headers = {"x-api-key": API_KEY}
|
| 290 |
+
url = f"{API_URL}/api/log/stream-start"
|
| 291 |
+
try:
|
| 292 |
+
response = requests.post(url, headers=headers, timeout=5)
|
| 293 |
+
if response.status_code == 200:
|
| 294 |
+
print("[Sync] Sent stream-start signal.")
|
| 295 |
+
else:
|
| 296 |
+
print(f"[Sync] Failed to send stream-start. Status: {response.status_code}")
|
| 297 |
+
except Exception as e:
|
| 298 |
+
print(f"[Sync] Error sending stream-start: {e}")
|
| 299 |
+
|
| 300 |
def notify_stream_end():
|
| 301 |
"""Notify backend that the stream has ended"""
|
| 302 |
headers = {"x-api-key": API_KEY}
|
|
|
|
| 583 |
t_chat.start()
|
| 584 |
t_sender.start()
|
| 585 |
|
| 586 |
+
# Notify backend that stream has started
|
| 587 |
+
notify_stream_start()
|
| 588 |
+
|
| 589 |
# 3. Start Audio Capture (Blocks main thread)
|
| 590 |
try:
|
| 591 |
if CAPTURE_METHOD == "stream":
|
|
|
|
| 596 |
print(f"[Error] Unknown CAPTURE_METHOD '{CAPTURE_METHOD}'. Choose 'stream' or 'microphone'.")
|
| 597 |
except KeyboardInterrupt:
|
| 598 |
print("\n[Shutting Down] Gracefully stopping threads...")
|
| 599 |
+
finally:
|
| 600 |
+
# Notify backend that stream has ended
|
| 601 |
+
notify_stream_end()
|
| 602 |
stop_flag.set()
|
| 603 |
time.sleep(1)
|
| 604 |
print("[Shutting Down] Done.")
|
server/server.js
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
| 21 |
getStreamMessageTimestamps,
|
| 22 |
getModActionsSummaryRaw,
|
| 23 |
syncActiveStream,
|
|
|
|
| 24 |
getModeratorProfilesData,
|
| 25 |
getActiveStream,
|
| 26 |
getPendingBackfillStreams,
|
|
@@ -224,6 +225,17 @@ app.post('/api/log/mod-action', authenticateWorker, async (req, res) => {
|
|
| 224 |
}
|
| 225 |
});
|
| 226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
// Explicit end stream command
|
| 228 |
app.post('/api/log/stream-end', authenticateWorker, async (req, res) => {
|
| 229 |
try {
|
|
|
|
| 21 |
getStreamMessageTimestamps,
|
| 22 |
getModActionsSummaryRaw,
|
| 23 |
syncActiveStream,
|
| 24 |
+
getOrStartActiveStream,
|
| 25 |
getModeratorProfilesData,
|
| 26 |
getActiveStream,
|
| 27 |
getPendingBackfillStreams,
|
|
|
|
| 225 |
}
|
| 226 |
});
|
| 227 |
|
| 228 |
+
// Explicit start stream command
|
| 229 |
+
app.post('/api/log/stream-start', authenticateWorker, async (req, res) => {
|
| 230 |
+
try {
|
| 231 |
+
const stream = await getOrStartActiveStream();
|
| 232 |
+
res.json({ success: true, streamId: stream ? stream.id : null });
|
| 233 |
+
} catch (err) {
|
| 234 |
+
console.error('[API Error] /api/log/stream-start:', err);
|
| 235 |
+
res.status(500).json({ error: 'Internal Server Error' });
|
| 236 |
+
}
|
| 237 |
+
});
|
| 238 |
+
|
| 239 |
// Explicit end stream command
|
| 240 |
app.post('/api/log/stream-end', authenticateWorker, async (req, res) => {
|
| 241 |
try {
|