- Added DashScope ASR service implementation for real-time streaming. - Updated ASR provider logic to support DashScope alongside existing providers. - Enhanced runtime metadata resolution to include DashScope as a valid ASR provider. - Modified configuration files and documentation to reflect the addition of DashScope. - Introduced tests to validate DashScope integration and ASR service behavior. - Refactored ASR service factory to accommodate new provider options and modes.
68 lines
2.1 KiB
Python
68 lines
2.1 KiB
Python
import asyncio
|
|
|
|
import pytest
|
|
|
|
from providers.asr.dashscope import DashScopeRealtimeASRService
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_dashscope_asr_interim_event_emits_interim_transcript():
|
|
received = []
|
|
|
|
async def _on_transcript(text: str, is_final: bool) -> None:
|
|
received.append((text, is_final))
|
|
|
|
service = DashScopeRealtimeASRService(api_key="test-key", on_transcript=_on_transcript)
|
|
service._loop = asyncio.get_running_loop()
|
|
service._running = True
|
|
|
|
service._on_ws_event(
|
|
{
|
|
"type": "conversation.item.input_audio_transcription.text",
|
|
"stash": "你好世界",
|
|
}
|
|
)
|
|
await asyncio.sleep(0.05)
|
|
|
|
result = service._transcript_queue.get_nowait()
|
|
assert result.text == "你好世界"
|
|
assert result.is_final is False
|
|
assert received == [("你好世界", False)]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_dashscope_asr_final_event_emits_final_transcript_and_final_queue():
|
|
received = []
|
|
|
|
async def _on_transcript(text: str, is_final: bool) -> None:
|
|
received.append((text, is_final))
|
|
|
|
service = DashScopeRealtimeASRService(api_key="test-key", on_transcript=_on_transcript)
|
|
service._loop = asyncio.get_running_loop()
|
|
service._running = True
|
|
service._audio_sent_in_utterance = True
|
|
|
|
service._on_ws_event(
|
|
{
|
|
"type": "conversation.item.input_audio_transcription.completed",
|
|
"transcript": "最终识别结果",
|
|
}
|
|
)
|
|
await asyncio.sleep(0.05)
|
|
|
|
result = service._transcript_queue.get_nowait()
|
|
assert result.text == "最终识别结果"
|
|
assert result.is_final is True
|
|
assert service._final_queue.get_nowait() == "最终识别结果"
|
|
assert received == [("最终识别结果", True)]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_dashscope_wait_for_final_falls_back_to_latest_interim_on_timeout():
|
|
service = DashScopeRealtimeASRService(api_key="test-key")
|
|
service._audio_sent_in_utterance = True
|
|
service._last_interim_text = "部分结果"
|
|
|
|
text = await service.wait_for_final_transcription(timeout_ms=10)
|
|
assert text == "部分结果"
|