Spaces:
Sleeping
Sleeping
Update pipe_method3.py
Browse files- pipe_method3.py +29 -2
pipe_method3.py
CHANGED
|
@@ -80,10 +80,11 @@ PARTIAL_EMIT_EVERY_MS = 250
|
|
| 80 |
|
| 81 |
|
| 82 |
LAST_STATE = {
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
"last_stt": "",
|
| 85 |
"last_llm": "",
|
| 86 |
-
"last_tts": "",
|
| 87 |
"updated_ms": 0,
|
| 88 |
}
|
| 89 |
|
|
@@ -294,6 +295,9 @@ async def health():
|
|
| 294 |
@app.websocket("/stream")
|
| 295 |
async def stream(ws: WebSocket):
|
| 296 |
await ws.accept()
|
|
|
|
|
|
|
|
|
|
| 297 |
st = CallState(call_id=str(id(ws)))
|
| 298 |
st.history = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 299 |
P("SYS>", f"ws_open call_id={st.call_id}")
|
|
@@ -351,6 +355,10 @@ async def stream(ws: WebSocket):
|
|
| 351 |
st.keepalive_task.cancel()
|
| 352 |
if st.outbound_task:
|
| 353 |
st.outbound_task.cancel()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
P("SYS>", "ws_closed")
|
| 355 |
|
| 356 |
|
|
@@ -389,6 +397,10 @@ async def vad_and_stt(ws: WebSocket, st: CallState, pcm16_16k: bytes, is_speech:
|
|
| 389 |
if partial and partial != st.last_partial:
|
| 390 |
st.last_partial = partial
|
| 391 |
P("STT_PART>", partial)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
|
| 393 |
if (t - st.utter_start_ms) > MAX_UTTERANCE_MS:
|
| 394 |
await finalize_utterance(ws, st, "max_utterance")
|
|
@@ -422,6 +434,11 @@ async def finalize_utterance(ws: WebSocket, st: CallState, reason: str):
|
|
| 422 |
return
|
| 423 |
|
| 424 |
P("STT_FINAL>", f"{user_text} ({reason})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
|
| 426 |
async def bot_job():
|
| 427 |
async with st.bot_lock:
|
|
@@ -443,6 +460,10 @@ async def answer_and_speak(ws: WebSocket, st: CallState, user_text: str):
|
|
| 443 |
ans = (ans or "").strip() or "Sorry, I didn’t catch that."
|
| 444 |
|
| 445 |
P("LLM_ANS>", ans)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
|
| 447 |
st.history.append({"role": "assistant", "content": ans})
|
| 448 |
st.history = st.history[:1] + st.history[-8:]
|
|
@@ -510,6 +531,8 @@ async def outbound_sender(ws: WebSocket, st: CallState):
|
|
| 510 |
await asyncio.sleep(0.02)
|
| 511 |
if st.outbound_q.empty():
|
| 512 |
st.bot_speaking = False
|
|
|
|
|
|
|
| 513 |
st.outbound_q.task_done()
|
| 514 |
continue
|
| 515 |
|
|
@@ -531,3 +554,7 @@ async def outbound_sender(ws: WebSocket, st: CallState):
|
|
| 531 |
except Exception as e:
|
| 532 |
P("SYS>", f"outbound_sender_error={e}")
|
| 533 |
log.exception("outbound_sender_error")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
LAST_STATE = {
|
| 83 |
+
"connected": False,
|
| 84 |
+
"status": "idle", # idle | connected | listening | thinking | speaking
|
| 85 |
+
"last_partial": "",
|
| 86 |
"last_stt": "",
|
| 87 |
"last_llm": "",
|
|
|
|
| 88 |
"updated_ms": 0,
|
| 89 |
}
|
| 90 |
|
|
|
|
| 295 |
@app.websocket("/stream")
|
| 296 |
async def stream(ws: WebSocket):
|
| 297 |
await ws.accept()
|
| 298 |
+
LAST_STATE["connected"] = True
|
| 299 |
+
LAST_STATE["status"] = "connected"
|
| 300 |
+
LAST_STATE["updated_ms"] = now_ms()
|
| 301 |
st = CallState(call_id=str(id(ws)))
|
| 302 |
st.history = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 303 |
P("SYS>", f"ws_open call_id={st.call_id}")
|
|
|
|
| 355 |
st.keepalive_task.cancel()
|
| 356 |
if st.outbound_task:
|
| 357 |
st.outbound_task.cancel()
|
| 358 |
+
LAST_STATE["connected"] = False
|
| 359 |
+
LAST_STATE["status"] = "idle"
|
| 360 |
+
LAST_STATE["last_partial"] = ""
|
| 361 |
+
LAST_STATE["updated_ms"] = now_ms()
|
| 362 |
P("SYS>", "ws_closed")
|
| 363 |
|
| 364 |
|
|
|
|
| 397 |
if partial and partial != st.last_partial:
|
| 398 |
st.last_partial = partial
|
| 399 |
P("STT_PART>", partial)
|
| 400 |
+
LAST_STATE["status"] = "listening"
|
| 401 |
+
LAST_STATE["last_partial"] = partial
|
| 402 |
+
LAST_STATE["updated_ms"] = now_ms()
|
| 403 |
+
|
| 404 |
|
| 405 |
if (t - st.utter_start_ms) > MAX_UTTERANCE_MS:
|
| 406 |
await finalize_utterance(ws, st, "max_utterance")
|
|
|
|
| 434 |
return
|
| 435 |
|
| 436 |
P("STT_FINAL>", f"{user_text} ({reason})")
|
| 437 |
+
LAST_STATE["status"] = "thinking"
|
| 438 |
+
LAST_STATE["last_stt"] = user_text
|
| 439 |
+
LAST_STATE["last_partial"] = ""
|
| 440 |
+
LAST_STATE["updated_ms"] = now_ms()
|
| 441 |
+
|
| 442 |
|
| 443 |
async def bot_job():
|
| 444 |
async with st.bot_lock:
|
|
|
|
| 460 |
ans = (ans or "").strip() or "Sorry, I didn’t catch that."
|
| 461 |
|
| 462 |
P("LLM_ANS>", ans)
|
| 463 |
+
LAST_STATE["last_llm"] = ans
|
| 464 |
+
LAST_STATE["status"] = "speaking"
|
| 465 |
+
LAST_STATE["updated_ms"] = now_ms()
|
| 466 |
+
|
| 467 |
|
| 468 |
st.history.append({"role": "assistant", "content": ans})
|
| 469 |
st.history = st.history[:1] + st.history[-8:]
|
|
|
|
| 531 |
await asyncio.sleep(0.02)
|
| 532 |
if st.outbound_q.empty():
|
| 533 |
st.bot_speaking = False
|
| 534 |
+
LAST_STATE["status"] = "connected"
|
| 535 |
+
LAST_STATE["updated_ms"] = now_ms()
|
| 536 |
st.outbound_q.task_done()
|
| 537 |
continue
|
| 538 |
|
|
|
|
| 554 |
except Exception as e:
|
| 555 |
P("SYS>", f"outbound_sender_error={e}")
|
| 556 |
log.exception("outbound_sender_error")
|
| 557 |
+
|
| 558 |
+
@app.get("/debug/last")
|
| 559 |
+
async def debug_last():
|
| 560 |
+
return LAST_STATE
|