Adding support for RemoteSmartTurn

This commit is contained in:
Filipi Fuchter
2025-04-16 06:33:42 -03:00
parent 3e2d21779f
commit 3ebef9346f
5 changed files with 196 additions and 14 deletions

View File

@@ -154,4 +154,15 @@ class BaseEndOfTurnAnalyzer(ABC):
@abstractmethod
def _predict_endpoint(self, buffer: np.ndarray) -> Dict[str, any]:
"""
Predict whether an audio segment is complete (turn ended) or incomplete.
Args:
audio_array: Numpy array containing audio samples at 16kHz
Returns:
Dictionary containing prediction results:
- prediction: 1 for complete, 0 for incomplete
- probability: Probability of completion class
"""
pass

View File

@@ -60,18 +60,6 @@ class LocalSmartTurnAnalyzer(BaseEndOfTurnAnalyzer):
logger.debug("Loaded Local Smart Turn")
def _predict_endpoint(self, audio_array: np.ndarray) -> Dict[str, any]:
"""
Predict whether an audio segment is complete (turn ended) or incomplete.
Args:
audio_array: Numpy array containing audio samples at 16kHz
Returns:
Dictionary containing prediction results:
- prediction: 1 for complete, 0 for incomplete
- probability: Probability of completion class
"""
inputs = self._turn_processor(
audio_array,
sampling_rate=16000,

View File

@@ -0,0 +1,73 @@
#
# Copyright (c) 20242025, Daily
#
# SPDX-License-Identifier: BSD 2-Clause License
#
import io
import os
from typing import Dict
import numpy as np
import requests
from loguru import logger
from pipecat.audio.turn.base_turn_analyzer import (
BaseEndOfTurnAnalyzer,
)
class RemoteSmartTurnAnalyzer(BaseEndOfTurnAnalyzer):
def __init__(self):
super().__init__()
self.remote_smart_turn_url = os.getenv("REMOTE_SMART_TURN_URL")
if not self.remote_smart_turn_url:
logger.error("REMOTE_SMART_TURN_URL is not set.")
raise Exception("REMOTE_SMART_TURN_URL environment variable must be provided.")
def _serialize_array(self, audio_array: np.ndarray) -> bytes:
"""Serializes a NumPy array into bytes using np.save."""
logger.debug("Serializing NumPy array to bytes...")
buffer = io.BytesIO()
np.save(buffer, audio_array) # Saves in npy format
serialized_bytes = buffer.getvalue()
logger.debug(f"Serialized size: {len(serialized_bytes)} bytes")
return serialized_bytes
def _send_raw_request(self, data_bytes: bytes):
"""Sends the bytes as the raw request body."""
headers = {"Content-Type": "application/octet-stream"}
logger.debug(
f"Sending {len(data_bytes)} bytes as raw body to {self.remote_smart_turn_url}..."
)
try:
response = requests.post(
self.remote_smart_turn_url, data=data_bytes, headers=headers, timeout=60
) # Added timeout
logger.debug("\n--- Response ---")
logger.debug(f"Status Code: {response.status_code}")
# Try to logger.debug JSON if successful, otherwise logger.debug text
if response.ok:
try:
logger.debug("Response JSON:")
logger.debug(response.json())
return response.json()
except requests.exceptions.JSONDecodeError:
logger.debug("Response Content (non-JSON):")
logger.debug(response.text)
else:
logger.debug("Response Content (Error):")
logger.debug(response.text)
response.raise_for_status() # Raise an exception for bad status codes
except requests.exceptions.RequestException as e:
logger.debug(f"Failed to send raw request to Daily Smart Turn: {e}")
raise Exception("Failed to send raw request to Daily Smart Turn.")
def _predict_endpoint(self, audio_array: np.ndarray) -> Dict[str, any]:
serialized_array = self._serialize_array(audio_array)
return self._send_raw_request(serialized_array)