dlxj commited on
Commit ·
97cb019
1
Parent(s): 0af95c0
add 测试文件
Browse files- harvard_16k.wav +3 -0
- requirements.txt +0 -0
- test_websocket_client.py +182 -0
harvard_16k.wav
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:aa5b415ee7caf6606a04f739a9fe8490fae3d726157cc080060426ead07dfb8b
|
| 3 |
+
size 587496
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|
test_websocket_client.py
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Test WebSocket client for streaming ASR server."""
|
| 3 |
+
|
| 4 |
+
import asyncio
|
| 5 |
+
import json
|
| 6 |
+
import sys
|
| 7 |
+
import time
|
| 8 |
+
import wave
|
| 9 |
+
|
| 10 |
+
import websockets
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
async def test_asr_streaming(
|
| 14 |
+
audio_path: str,
|
| 15 |
+
server_url: str = "ws://localhost:8080",
|
| 16 |
+
chunk_ms: int = 500,
|
| 17 |
+
):
|
| 18 |
+
"""Send audio file to streaming ASR server and show interim results."""
|
| 19 |
+
|
| 20 |
+
print(f"Reading audio file: {audio_path}")
|
| 21 |
+
|
| 22 |
+
# Read WAV file
|
| 23 |
+
with wave.open(audio_path, 'rb') as wf:
|
| 24 |
+
sample_rate = wf.getframerate()
|
| 25 |
+
n_channels = wf.getnchannels()
|
| 26 |
+
sample_width = wf.getsampwidth()
|
| 27 |
+
n_frames = wf.getnframes()
|
| 28 |
+
audio_data = wf.readframes(n_frames)
|
| 29 |
+
|
| 30 |
+
duration = n_frames / sample_rate
|
| 31 |
+
print(f" Sample rate: {sample_rate} Hz")
|
| 32 |
+
print(f" Channels: {n_channels}")
|
| 33 |
+
print(f" Duration: {duration:.2f}s")
|
| 34 |
+
print(f" Size: {len(audio_data)} bytes")
|
| 35 |
+
|
| 36 |
+
# Calculate chunk size in bytes (16kHz, 16-bit = 2 bytes/sample)
|
| 37 |
+
chunk_samples = int(sample_rate * chunk_ms / 1000)
|
| 38 |
+
chunk_bytes = chunk_samples * sample_width # sample_width = 2 for 16-bit
|
| 39 |
+
|
| 40 |
+
print(f"\nChunk size: {chunk_ms}ms = {chunk_samples} samples = {chunk_bytes} bytes")
|
| 41 |
+
print(f"Connecting to {server_url}...")
|
| 42 |
+
|
| 43 |
+
start_time = time.time()
|
| 44 |
+
|
| 45 |
+
async with websockets.connect(server_url) as ws:
|
| 46 |
+
connect_time = time.time()
|
| 47 |
+
print(f" Connected in {(connect_time - start_time)*1000:.0f}ms")
|
| 48 |
+
|
| 49 |
+
# Wait for ready message
|
| 50 |
+
ready_msg = await ws.recv()
|
| 51 |
+
ready_data = json.loads(ready_msg)
|
| 52 |
+
if ready_data.get("type") != "ready":
|
| 53 |
+
print(f" WARNING: Expected 'ready', got: {ready_data}")
|
| 54 |
+
else:
|
| 55 |
+
print(f" Server ready")
|
| 56 |
+
|
| 57 |
+
ready_time = time.time()
|
| 58 |
+
|
| 59 |
+
# Track interim results
|
| 60 |
+
interim_count = 0
|
| 61 |
+
last_interim = ""
|
| 62 |
+
|
| 63 |
+
# Create a task to receive messages
|
| 64 |
+
async def receive_messages():
|
| 65 |
+
nonlocal interim_count, last_interim
|
| 66 |
+
try:
|
| 67 |
+
async for message in ws:
|
| 68 |
+
data = json.loads(message)
|
| 69 |
+
if data.get("type") == "transcript":
|
| 70 |
+
text = data.get("text", "")
|
| 71 |
+
is_final = data.get("is_final", False)
|
| 72 |
+
|
| 73 |
+
if is_final:
|
| 74 |
+
return text
|
| 75 |
+
else:
|
| 76 |
+
interim_count += 1
|
| 77 |
+
last_interim = text
|
| 78 |
+
# Show interim result (truncated)
|
| 79 |
+
display = text[:60] + "..." if len(text) > 60 else text
|
| 80 |
+
print(f" [interim {interim_count}] {display}")
|
| 81 |
+
elif data.get("type") == "error":
|
| 82 |
+
print(f" ERROR: {data.get('message')}")
|
| 83 |
+
return None
|
| 84 |
+
except websockets.exceptions.ConnectionClosed:
|
| 85 |
+
return last_interim
|
| 86 |
+
|
| 87 |
+
# Start receiving in background
|
| 88 |
+
receive_task = asyncio.create_task(receive_messages())
|
| 89 |
+
|
| 90 |
+
# Send audio data in chunks
|
| 91 |
+
total_sent = 0
|
| 92 |
+
chunks_sent = 0
|
| 93 |
+
|
| 94 |
+
print(f"\nSending audio in {chunk_ms}ms chunks...")
|
| 95 |
+
send_start = time.time()
|
| 96 |
+
|
| 97 |
+
for i in range(0, len(audio_data), chunk_bytes):
|
| 98 |
+
chunk = audio_data[i:i+chunk_bytes]
|
| 99 |
+
await ws.send(chunk)
|
| 100 |
+
total_sent += len(chunk)
|
| 101 |
+
chunks_sent += 1
|
| 102 |
+
|
| 103 |
+
# Simulate real-time streaming
|
| 104 |
+
await asyncio.sleep(chunk_ms / 1000)
|
| 105 |
+
|
| 106 |
+
send_time = time.time()
|
| 107 |
+
print(f" Sent {chunks_sent} chunks ({total_sent} bytes) in {(send_time - send_start)*1000:.0f}ms")
|
| 108 |
+
|
| 109 |
+
# Record time of last audio chunk sent
|
| 110 |
+
last_audio_time = send_time
|
| 111 |
+
|
| 112 |
+
# Signal end of audio
|
| 113 |
+
end_signal_time = time.time()
|
| 114 |
+
await ws.send(json.dumps({"type": "reset"}))
|
| 115 |
+
|
| 116 |
+
# Wait for final transcript
|
| 117 |
+
print("\nWaiting for final transcript...")
|
| 118 |
+
transcript = await receive_task
|
| 119 |
+
final_recv_time = time.time()
|
| 120 |
+
|
| 121 |
+
# Calculate time-to-final-transcription
|
| 122 |
+
time_to_final = (final_recv_time - last_audio_time) * 1000
|
| 123 |
+
end_signal_to_final = (final_recv_time - end_signal_time) * 1000
|
| 124 |
+
|
| 125 |
+
print(f"\n{'='*60}")
|
| 126 |
+
print("FINAL TRANSCRIPT:")
|
| 127 |
+
print(f"{'='*60}")
|
| 128 |
+
print(transcript if transcript else "(empty)")
|
| 129 |
+
print(f"{'='*60}")
|
| 130 |
+
|
| 131 |
+
total_time = final_recv_time - start_time
|
| 132 |
+
print(f"\nStatistics:")
|
| 133 |
+
print(f" Interim results: {interim_count}")
|
| 134 |
+
print(f" Total time: {total_time*1000:.0f}ms")
|
| 135 |
+
print(f" Audio duration: {duration:.2f}s")
|
| 136 |
+
print(f" Real-time factor: {total_time/duration:.2f}x")
|
| 137 |
+
print(f"\nFinalization latency:")
|
| 138 |
+
print(f" Last audio chunk -> final transcript: {time_to_final:.0f}ms")
|
| 139 |
+
print(f" End signal -> final transcript: {end_signal_to_final:.0f}ms")
|
| 140 |
+
|
| 141 |
+
return transcript
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
async def test_multiple_chunk_sizes(audio_path: str, server_url: str):
|
| 145 |
+
"""Test with different chunk sizes."""
|
| 146 |
+
print("=" * 60)
|
| 147 |
+
print("Testing Multiple Chunk Sizes")
|
| 148 |
+
print("=" * 60)
|
| 149 |
+
|
| 150 |
+
for chunk_ms in [500, 160, 80]:
|
| 151 |
+
print(f"\n{'='*60}")
|
| 152 |
+
print(f"CHUNK SIZE: {chunk_ms}ms")
|
| 153 |
+
print(f"{'='*60}")
|
| 154 |
+
|
| 155 |
+
try:
|
| 156 |
+
await test_asr_streaming(audio_path, server_url, chunk_ms)
|
| 157 |
+
except Exception as e:
|
| 158 |
+
print(f"ERROR with {chunk_ms}ms chunks: {e}")
|
| 159 |
+
|
| 160 |
+
# Small delay between tests
|
| 161 |
+
await asyncio.sleep(1)
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
if __name__ == "__main__":
|
| 165 |
+
|
| 166 |
+
import sys
|
| 167 |
+
sys.argv.append( '--all' )
|
| 168 |
+
|
| 169 |
+
audio_path = "./harvard_16k.wav"
|
| 170 |
+
server_url = "ws://127.0.0.1:8080"
|
| 171 |
+
|
| 172 |
+
# Check for --all flag to test all chunk sizes
|
| 173 |
+
if "--all" in sys.argv:
|
| 174 |
+
asyncio.run(test_multiple_chunk_sizes(audio_path, server_url))
|
| 175 |
+
else:
|
| 176 |
+
chunk_ms = 500
|
| 177 |
+
if "--chunk" in sys.argv:
|
| 178 |
+
idx = sys.argv.index("--chunk")
|
| 179 |
+
if idx + 1 < len(sys.argv):
|
| 180 |
+
chunk_ms = int(sys.argv[idx + 1])
|
| 181 |
+
|
| 182 |
+
asyncio.run(test_asr_streaming(audio_path, server_url, chunk_ms))
|