Skip to content

Commit 783b91a

Browse files
committed
move bargein detector to inference
1 parent d28ec23 commit 783b91a

File tree

9 files changed

+20
-369
lines changed

9 files changed

+20
-369
lines changed

β€Žexamples/bargein_server.pyβ€Ž

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ class BargeinRequest(BaseModel):
5353
agentId: str | None = Field(None, description="Optional agent ID")
5454
threshold: float = Field(0.95, description="Threshold for bargein detection")
5555
min_frames: int = Field(2, description="Minimum number of frames for bargein detection")
56+
created_at: float = Field(..., description="Timestamp of the audio waveform")
5657

5758

5859
class BargeinResponse(BaseModel):
5960
"""Response model for bargein detection."""
6061

6162
is_bargein: bool = Field(..., description="Whether bargein is detected")
6263
confidence: float | None = Field(None, description="Confidence score (optional)")
64+
created_at: float = Field(..., description="Timestamp of the data creation")
6365

6466

6567
def load_onnx_model(model_path: str) -> ort.InferenceSession:
@@ -165,7 +167,9 @@ async def detect_bargein(request: BargeinRequest) -> BargeinResponse:
165167

166168
logger.info(f"Bargein detection result: is_bargein={is_bargein}")
167169

168-
return BargeinResponse(is_bargein=is_bargein, confidence=None)
170+
return BargeinResponse(
171+
is_bargein=is_bargein, confidence=None, created_at=request.created_at
172+
)
169173

170174
except Exception as e:
171175
logger.error(f"Error during bargein detection: {e}", exc_info=True)
@@ -246,7 +250,12 @@ async def websocket_bargein(websocket: WebSocket) -> None:
246250
delta = time.time() - created_at
247251

248252
await websocket.send_json(
249-
{"type": "inference_done", "delta": delta, "is_bargein": is_bargein}
253+
{
254+
"type": "inference_done",
255+
"delta": delta,
256+
"is_bargein": is_bargein,
257+
"created_at": created_at,
258+
}
250259
)
251260

252261
if is_bargein:

β€Žlivekit-agents/livekit/agents/inference/bargein.pyβ€Ž

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
THRESHOLD = 0.95
4242
MIN_BARGEIN_DURATION = 0.05 # 25ms per frame
4343
MAX_WINDOW_SIZE = 3 * 16000 # 3 seconds at 16000 Hz
44-
STEP_SIZE = int(0.2 * 16000) # 0.2 second at 16000 Hz
44+
STEP_SIZE = int(0.1 * 16000) # 0.1 second at 16000 Hz
4545
REMOTE_INFERENCE_TIMEOUT = 1
4646
DEFAULT_BASE_URL = "https://agent-gateway.livekit.cloud/v1"
4747

@@ -440,7 +440,13 @@ async def recv_task(ws: aiohttp.ClientWebSocketResponse) -> None:
440440
pass
441441
elif msg_type == "inference_done":
442442
is_bargein_result = data.get("is_bargein", False)
443-
logger.debug("inference done", extra={"is_bargein": is_bargein_result})
443+
logger.debug(
444+
"inference done",
445+
extra={
446+
"is_bargein": is_bargein_result,
447+
"inference_duration": time.time() - data.get("created_at", 0.0),
448+
},
449+
)
444450
self._event_ch.send_nowait(
445451
BargeinEvent(
446452
type=BargeinEventType.INFERENCE_DONE,
@@ -489,7 +495,7 @@ async def _connect_ws(self) -> aiohttp.ClientWebSocketResponse:
489495
"""Connect to the LiveKit STT WebSocket."""
490496
params: dict[str, Any] = {
491497
"settings": {
492-
"sample_rate": str(self._opts.sample_rate),
498+
"sample_rate": self._opts.sample_rate,
493499
},
494500
}
495501

β€Žlivekit-plugins/livekit-plugins-bargein-detector/README.mdβ€Ž

Lines changed: 0 additions & 7 deletions
This file was deleted.

β€Žlivekit-plugins/livekit-plugins-bargein-detector/livekit/plugins/bargein_detector/__init__.pyβ€Ž

Lines changed: 0 additions & 44 deletions
This file was deleted.

β€Žlivekit-plugins/livekit-plugins-bargein-detector/livekit/plugins/bargein_detector/bargein.pyβ€Ž

Lines changed: 0 additions & 256 deletions
This file was deleted.

β€Žlivekit-plugins/livekit-plugins-bargein-detector/livekit/plugins/bargein_detector/log.pyβ€Ž

Lines changed: 0 additions & 3 deletions
This file was deleted.

β€Žlivekit-plugins/livekit-plugins-bargein-detector/livekit/plugins/bargein_detector/py.typedβ€Ž

Whitespace-only changes.

0 commit comments

Comments
Β (0)