Skip to content

Commit 05021e5

Browse files
committed
feat(improve audio modality generation):
1 parent 3ae4f34 commit 05021e5

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

agentic_security/probe_data/audio_generator.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,37 @@ def generate_audio_mac_wav(prompt: str) -> bytes:
5252
return audio_bytes
5353

5454

55+
def generate_audio_cross_platform(prompt: str) -> bytes:
56+
"""
57+
Generate an audio file from the provided prompt using gTTS for cross-platform support.
58+
59+
Parameters:
60+
prompt (str): Text to convert into audio.
61+
62+
Returns:
63+
bytes: The audio data in MP3 format.
64+
"""
65+
from gtts import gTTS # Import gTTS for cross-platform support
66+
67+
tts = gTTS(text=prompt, lang="en")
68+
temp_mp3_path = f"temp_audio_{uuid.uuid4().hex}.mp3"
69+
tts.save(temp_mp3_path)
70+
71+
try:
72+
with open(temp_mp3_path, "rb") as f:
73+
audio_bytes = f.read()
74+
finally:
75+
if os.path.exists(temp_mp3_path):
76+
os.remove(temp_mp3_path)
77+
78+
return audio_bytes
79+
80+
5581
@cache_to_disk()
5682
def generate_audioform(prompt: str) -> bytes:
5783
"""
5884
Generate an audio file from the provided prompt in WAV format.
59-
Uses macOS 'say' command if the operating system is macOS.
85+
Uses macOS 'say' command if the operating system is macOS, otherwise uses gTTS.
6086
6187
Parameters:
6288
prompt (str): Text to convert into audio.
@@ -67,9 +93,11 @@ def generate_audioform(prompt: str) -> bytes:
6793
current_os = platform.system()
6894
if current_os == "Darwin": # macOS
6995
return generate_audio_mac_wav(prompt)
96+
elif current_os in ["Windows", "Linux"]:
97+
return generate_audio_cross_platform(prompt)
7098
else:
7199
raise NotImplementedError(
72-
"Audio generation is only supported on macOS for now."
100+
"Audio generation is only supported on macOS, Windows, and Linux for now."
73101
)
74102

75103

agentic_security/probe_data/test_audio_generator.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
from agentic_security.probe_data.audio_generator import (
6+
generate_audio_cross_platform,
67
generate_audio_mac_wav,
78
generate_audioform,
89
)
@@ -24,6 +25,13 @@ def test_generate_audioform_mac():
2425
audio_bytes = generate_audioform(prompt)
2526
assert isinstance(audio_bytes, bytes)
2627
assert len(audio_bytes) > 0
28+
29+
30+
def test_generate_audio_cross_platform():
31+
if platform.system() in ["Windows", "Linux"]:
32+
prompt = "This is a cross-platform test."
33+
audio_bytes = generate_audio_cross_platform(prompt)
34+
assert isinstance(audio_bytes, bytes)
35+
assert len(audio_bytes) > 0
2736
else:
28-
with pytest.raises(NotImplementedError):
29-
generate_audioform("This should raise an error on non-macOS systems.")
37+
pytest.skip("Test is only applicable on Windows and Linux.")

agentic_security/probe_data/test_image_generator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from unittest.mock import patch
2+
23
import pytest
34

45
from agentic_security.probe_data.image_generator import (

0 commit comments

Comments
 (0)