Skip to content

Commit a34a431

Browse files
committed
put wav creation at the module level to prevent being recreated every time
Signed-off-by: David Gao <[email protected]>
1 parent 4413d83 commit a34a431

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/vllm_router/utils.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
1414

1515
logger = init_logger(__name__)
1616

17+
# prepare a WAV byte to prevent repeatedly generating it
18+
# Generate a 0.1 second silent audio file
19+
_SILENT_WAV_BYTES = None
20+
with io.BytesIO() as wav_buffer:
21+
with wave.open(wav_buffer, "wb") as wf:
22+
wf.setnchannels(1) # mono audio channel, standard configuration
23+
wf.setsampwidth(2) # 16 bit audio, common bit depth for wav file
24+
wf.setframerate(16000) # 16 kHz sample rate
25+
wf.writeframes(b"\x00\x00" * 1600) # 0.1 second of silence
26+
27+
# retrieves the generated wav bytes, return
28+
_SILENT_WAV_BYTES = wav_buffer.getvalue()
29+
1730

1831
class SingletonMeta(type):
1932
_instances = {}
@@ -80,19 +93,10 @@ def get_test_payload(model_type: str):
8093
return {"encoding_format": "float", "text_1": "Test", "test_2": "Test2"}
8194
case ModelType.transcription:
8295
# Generate a 0.1 second silent audio file
83-
with io.BytesIO() as wav_buffer:
84-
with wave.open(wav_buffer, "wb") as wf:
85-
wf.setnchannels(1) # mono audio channel, standard configuration
86-
wf.setsampwidth(2) # 16 bit audio, common bit depth for wav file
87-
wf.setframerate(16000) # 16 kHz sample rate
88-
wf.writeframes(b"\x00\x00" * 1600) # 0.1 second of silence
89-
90-
# retrieves the generated wav bytes, return
91-
wav_bytes = wav_buffer.getvalue()
92-
93-
return {
94-
"file": ("empty.wav", wav_bytes, "audio/wav"),
95-
}
96+
if _SILENT_WAV_BYTES is not None:
97+
return {
98+
"file": ("empty.wav", _SILENT_WAV_BYTES, "audio/wav"),
99+
}
96100

97101

98102
@staticmethod

0 commit comments

Comments
 (0)