From a80f82cdb6a2e476a1771e3aa27d67ba6a3be85a Mon Sep 17 00:00:00 2001 From: Filipi Fuchter Date: Thu, 17 Apr 2025 16:28:50 -0300 Subject: [PATCH] Moving the environment variables to inside the demo. --- examples/foundational/38-smart-turn.py | 4 +++- examples/foundational/38a-local-smart-turn.py | 21 +++++++++++++++++- src/pipecat/audio/turn/local_smart_turn.py | 22 +++---------------- src/pipecat/audio/turn/smart_turn.py | 8 +++---- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/examples/foundational/38-smart-turn.py b/examples/foundational/38-smart-turn.py index fb910b980..f342f93c6 100644 --- a/examples/foundational/38-smart-turn.py +++ b/examples/foundational/38-smart-turn.py @@ -29,6 +29,8 @@ load_dotenv(override=True) async def run_bot(webrtc_connection: SmallWebRTCConnection): logger.info(f"Starting bot") + remote_smart_turn_url = os.getenv("REMOTE_SMART_TURN_URL") + transport = SmallWebRTCTransport( webrtc_connection=webrtc_connection, params=TransportParams( @@ -37,7 +39,7 @@ async def run_bot(webrtc_connection: SmallWebRTCConnection): vad_enabled=True, vad_analyzer=SileroVADAnalyzer(params=VADParams(stop_secs=0.2)), vad_audio_passthrough=True, - end_of_turn_analyzer=SmartTurnAnalyzer(), + end_of_turn_analyzer=SmartTurnAnalyzer(url=remote_smart_turn_url), ), ) diff --git a/examples/foundational/38a-local-smart-turn.py b/examples/foundational/38a-local-smart-turn.py index 8cf3beb48..dffcec573 100644 --- a/examples/foundational/38a-local-smart-turn.py +++ b/examples/foundational/38a-local-smart-turn.py @@ -30,6 +30,23 @@ load_dotenv(override=True) async def run_bot(webrtc_connection: SmallWebRTCConnection): logger.info(f"Starting bot") + # To use this locally, set the environment variable LOCAL_SMART_TURN_MODEL_PATH + # to the path where the smart-turn repo is cloned. + # + # Example setup: + # + # # Git LFS (Large File Storage) + # brew install git-lfs + # # Hugging Face uses LFS to store large model files, including .mlpackage + # git lfs install + # # Clone the repo with the smart_turn_classifier.mlpackage + # git clone https://huggingface.co/pipecat-ai/smart-turn + # + # Then set the env variable: + # export LOCAL_SMART_TURN_MODEL_PATH=./smart-turn + # or add it to your .env file + smart_turn_model_path = os.getenv("LOCAL_SMART_TURN_MODEL_PATH") + transport = SmallWebRTCTransport( webrtc_connection=webrtc_connection, params=TransportParams( @@ -38,7 +55,9 @@ async def run_bot(webrtc_connection: SmallWebRTCConnection): vad_enabled=True, vad_analyzer=SileroVADAnalyzer(params=VADParams(stop_secs=0.2)), vad_audio_passthrough=True, - end_of_turn_analyzer=LocalCoreMLSmartTurnAnalyzer(params=SmartTurnParams(stop_secs=5)), + end_of_turn_analyzer=LocalCoreMLSmartTurnAnalyzer( + smart_turn_model_path=smart_turn_model_path, params=SmartTurnParams(stop_secs=5) + ), ), ) diff --git a/src/pipecat/audio/turn/local_smart_turn.py b/src/pipecat/audio/turn/local_smart_turn.py index 669cabf61..665e4b64d 100644 --- a/src/pipecat/audio/turn/local_smart_turn.py +++ b/src/pipecat/audio/turn/local_smart_turn.py @@ -26,28 +26,12 @@ except ModuleNotFoundError as e: class LocalCoreMLSmartTurnAnalyzer(BaseSmartTurn): - def __init__(self, **kwargs): + def __init__(self, smart_turn_model_path: str, **kwargs): super().__init__(**kwargs) - # To use this locally, set the environment variable LOCAL_SMART_TURN_MODEL_PATH - # to the path where the smart-turn repo is cloned. - # - # Example setup: - # - # # Git LFS (Large File Storage) - # brew install git-lfs - # # Hugging Face uses LFS to store large model files, including .mlpackage - # git lfs install - # # Clone the repo with the smart_turn_classifier.mlpackage - # git clone https://huggingface.co/pipecat-ai/smart-turn - # - # Then set the env variable: - # export LOCAL_SMART_TURN_MODEL_PATH=./smart-turn - # or add it to your .env file - smart_turn_model_path = os.getenv("LOCAL_SMART_TURN_MODEL_PATH") if not smart_turn_model_path: - logger.error("LOCAL_SMART_TURN_MODEL_PATH is not set.") - raise Exception("LOCAL_SMART_TURN_MODEL_PATH environment variable must be provided.") + logger.error("smart_turn_model_path is not set.") + raise Exception("smart_turn_model_path must be provided.") core_ml_model_path = f"{smart_turn_model_path}/coreml/smart_turn_classifier.mlpackage" diff --git a/src/pipecat/audio/turn/smart_turn.py b/src/pipecat/audio/turn/smart_turn.py index c6e2f2af1..5378e4b5b 100644 --- a/src/pipecat/audio/turn/smart_turn.py +++ b/src/pipecat/audio/turn/smart_turn.py @@ -17,13 +17,13 @@ from pipecat.audio.turn.base_smart_turn import BaseSmartTurn class SmartTurnAnalyzer(BaseSmartTurn): - def __init__(self, **kwargs): + def __init__(self, url: str, **kwargs): super().__init__(**kwargs) - self.remote_smart_turn_url = os.getenv("REMOTE_SMART_TURN_URL") + self.remote_smart_turn_url = 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.") + logger.error("remote_smart_turn_url is not set.") + raise Exception("remote_smart_turn_url must be provided.") # Use a session to reuse connections (keep-alive) self.session = requests.Session()