11import os
22import sys
33import pathlib
4+ from types import SimpleNamespace
45from typing import List , Dict , Optional
56from label_studio_ml .model import LabelStudioMLBase
67from label_studio_ml .response import ModelResponse
@@ -20,6 +21,11 @@ class DeepgramModel(LabelStudioMLBase):
2021
2122 def setup (self ):
2223 """Initialize the Deepgram client with API key from environment"""
24+ self .test_mode = self ._is_test_mode_enabled ()
25+ if self .test_mode :
26+ self ._setup_test_clients ()
27+ return
28+
2329 api_key = os .getenv ('DEEPGRAM_API_KEY' )
2430 if not api_key :
2531 raise ValueError ("DEEPGRAM_API_KEY environment variable is not set" )
@@ -78,13 +84,46 @@ def predict(self, tasks: List[Dict], context: Optional[Dict] = None, **kwargs) -
7884 print (f"Uploaded audio to S3: { s3_url } " )
7985
8086 # Update task with S3 URL
81- ls .tasks .update (id = task_id , data = {"text" : text , "audio" : s3_url })
87+ if self .test_mode :
88+ print (f"[TEST MODE] Would update task { task_id } with audio { s3_url } " )
89+ else :
90+ ls .tasks .update (id = task_id , data = {"text" : text , "audio" : s3_url })
8291 except Exception as e :
8392 print (f"Error uploading to S3: { e } " )
8493 raise
8594 finally :
8695 # Clean up local file
8796 if os .path .exists (local_audio_path ):
8897 os .remove (local_audio_path )
89-
98+
99+ def _is_test_mode_enabled (self ) -> bool :
100+ """Check environment variables to decide if the model should use local stubs."""
101+ truthy = {'1' , 'true' , 'TRUE' , 'True' , 'yes' , 'on' }
102+ explicit_flag = os .getenv ('DEEPGRAM_TEST_MODE' )
103+ test_env_flag = os .getenv ('TEST_ENV' )
104+ return (explicit_flag in truthy ) or (test_env_flag in truthy )
105+
106+ def _setup_test_clients (self ):
107+ """Configure lightweight stub clients so docker/CI runs do not need real secrets."""
108+ print ("[TEST MODE] DeepgramModel using stubbed Deepgram/S3 clients." )
109+
110+ def fake_generate (text : str ):
111+ # Produce deterministic fake audio bytes for predictable tests.
112+ preview = text [:10 ] if text else ''
113+ return [f"fake-audio-{ preview } " .encode ('utf-8' )]
114+
115+ fake_audio = SimpleNamespace (generate = fake_generate )
116+ fake_speak = SimpleNamespace (v1 = SimpleNamespace (audio = fake_audio ))
117+ self .deepgram_client = SimpleNamespace (speak = fake_speak )
118+
119+ class _StubS3Client :
120+ """Minimal S3 client replacement for test environments."""
121+ def upload_file (self , filename , bucket , key , ExtraArgs = None ):
122+ print (f"[TEST MODE] Pretend upload of { filename } to s3://{ bucket } /{ key } " )
123+
124+ self .s3_client = _StubS3Client ()
125+ # Provide sensible defaults so downstream URL building still works.
126+ self .s3_region = os .getenv ('AWS_DEFAULT_REGION' , 'us-east-1' )
127+ self .s3_bucket = os .getenv ('S3_BUCKET' , 'test-bucket' )
128+ self .s3_folder = os .getenv ('S3_FOLDER' , 'tts' )
90129
0 commit comments