Skip to content

Commit 2b8aeb6

Browse files
committed
update transcribe method and add example
1 parent 1aea59f commit 2b8aeb6

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

examples/rt/async/file/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import asyncio
3+
from speechmatics.rt import AsyncClient, ServerMessageType
4+
5+
6+
async def main():
7+
# Create a client using environment variable SPEECHMATICS_API_KEY
8+
async with AsyncClient() as client:
9+
# Register event handlers
10+
@client.on(ServerMessageType.ADD_TRANSCRIPT)
11+
def handle_final_transcript(msg):
12+
print(f"Final: {msg['metadata']['transcript']}")
13+
14+
# Transcribe audio file
15+
with open("../../../example.wav", "rb") as audio_file:
16+
await client.transcribe(audio_file)
17+
18+
# Run the async function
19+
asyncio.run(main())

sdk/rt/speechmatics/rt/_async_client.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ async def stop_session(self) -> None:
158158
... await client.stop_session()
159159
"""
160160
await self._send_eos(self._seq_no)
161-
await self._session_done_evt.wait() # Wait for end of transcript event to indicate we can stop listening
161+
await self._session_done_evt.wait() # Wait for end of transcript event to indicate we can stop listening
162162
await self.close()
163163

164164
async def transcribe(
@@ -253,7 +253,6 @@ async def _audio_producer(self, source: BinaryIO, chunk_size: int) -> None:
253253
chunk_size: Chunk size for audio data
254254
"""
255255
src = FileSource(source, chunk_size=chunk_size)
256-
seq_no = 0
257256

258257
try:
259258
async for frame in src:
@@ -262,13 +261,12 @@ async def _audio_producer(self, source: BinaryIO, chunk_size: int) -> None:
262261

263262
try:
264263
await self.send_audio(frame)
265-
seq_no += 1
266264
except Exception as e:
267265
self._logger.error("Failed to send audio frame: %s", e)
268266
self._session_done_evt.set()
269267
break
270268

271-
await self._send_eos(seq_no)
269+
await self.stop_session()
272270
except asyncio.CancelledError:
273271
raise
274272
except Exception as e:

0 commit comments

Comments
 (0)