Alina commited on
Commit
cd07e77
·
unverified ·
2 Parent(s): cced4d1 c10cc03

Merge pull request #117 from pollen-robotics/114-improve-transcript-accuracy

Browse files
src/reachy_mini_conversation_app/openai_realtime.py CHANGED
@@ -58,10 +58,29 @@ class OpenaiRealtimeHandler(AsyncStreamHandler):
58
  self.start_time = asyncio.get_event_loop().time()
59
  self.is_idle_tool_call = False
60
 
 
 
 
 
 
61
  def copy(self) -> "OpenaiRealtimeHandler":
62
  """Create a copy of the handler."""
63
  return OpenaiRealtimeHandler(self.deps)
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  async def start_up(self) -> None:
66
  """Start the handler with minimal retries on unexpected websocket closure."""
67
  self.client = AsyncOpenAI(api_key=config.OPENAI_API_KEY)
@@ -106,7 +125,7 @@ class OpenaiRealtimeHandler(AsyncStreamHandler):
106
  "rate": self.input_sample_rate,
107
  },
108
  "transcription": {
109
- "model": "whisper-1",
110
  "language": "en"
111
  },
112
  "turn_detection": {
@@ -166,13 +185,36 @@ class OpenaiRealtimeHandler(AsyncStreamHandler):
166
  # Handle partial transcription (user speaking in real-time)
167
  if event.type == "conversation.item.input_audio_transcription.partial":
168
  logger.debug(f"User partial transcript: {event.transcript}")
169
- await self.output_queue.put(
170
- AdditionalOutputs({"role": "user_partial", "content": event.transcript})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  )
172
 
173
  # Handle completed transcription (user finished speaking)
174
  if event.type == "conversation.item.input_audio_transcription.completed":
175
  logger.debug(f"User transcript: {event.transcript}")
 
 
 
 
 
 
 
 
 
176
  await self.output_queue.put(AdditionalOutputs({"role": "user", "content": event.transcript}))
177
 
178
  # Handle assistant transcription
@@ -342,6 +384,14 @@ class OpenaiRealtimeHandler(AsyncStreamHandler):
342
 
343
  async def shutdown(self) -> None:
344
  """Shutdown the handler."""
 
 
 
 
 
 
 
 
345
  if self.connection:
346
  try:
347
  await self.connection.close()
 
58
  self.start_time = asyncio.get_event_loop().time()
59
  self.is_idle_tool_call = False
60
 
61
+ # Debouncing for partial transcripts
62
+ self.partial_transcript_task: asyncio.Task[None] | None = None
63
+ self.partial_transcript_sequence: int = 0 # sequence counter to prevent stale emissions
64
+ self.partial_debounce_delay = 0.5 # seconds
65
+
66
  def copy(self) -> "OpenaiRealtimeHandler":
67
  """Create a copy of the handler."""
68
  return OpenaiRealtimeHandler(self.deps)
69
 
70
+ async def _emit_debounced_partial(self, transcript: str, sequence: int) -> None:
71
+ """Emit partial transcript after debounce delay."""
72
+ try:
73
+ await asyncio.sleep(self.partial_debounce_delay)
74
+ # Only emit if this is still the latest partial (by sequence number)
75
+ if self.partial_transcript_sequence == sequence:
76
+ await self.output_queue.put(
77
+ AdditionalOutputs({"role": "user_partial", "content": transcript})
78
+ )
79
+ logger.debug(f"Debounced partial emitted: {transcript}")
80
+ except asyncio.CancelledError:
81
+ logger.debug("Debounced partial cancelled")
82
+ raise
83
+
84
  async def start_up(self) -> None:
85
  """Start the handler with minimal retries on unexpected websocket closure."""
86
  self.client = AsyncOpenAI(api_key=config.OPENAI_API_KEY)
 
125
  "rate": self.input_sample_rate,
126
  },
127
  "transcription": {
128
+ "model": "gpt-4o-transcribe",
129
  "language": "en"
130
  },
131
  "turn_detection": {
 
185
  # Handle partial transcription (user speaking in real-time)
186
  if event.type == "conversation.item.input_audio_transcription.partial":
187
  logger.debug(f"User partial transcript: {event.transcript}")
188
+
189
+ # Increment sequence
190
+ self.partial_transcript_sequence += 1
191
+ current_sequence = self.partial_transcript_sequence
192
+
193
+ # Cancel previous debounce task if it exists
194
+ if self.partial_transcript_task and not self.partial_transcript_task.done():
195
+ self.partial_transcript_task.cancel()
196
+ try:
197
+ await self.partial_transcript_task
198
+ except asyncio.CancelledError:
199
+ pass
200
+
201
+ # Start new debounce timer with sequence number
202
+ self.partial_transcript_task = asyncio.create_task(
203
+ self._emit_debounced_partial(event.transcript, current_sequence)
204
  )
205
 
206
  # Handle completed transcription (user finished speaking)
207
  if event.type == "conversation.item.input_audio_transcription.completed":
208
  logger.debug(f"User transcript: {event.transcript}")
209
+
210
+ # Cancel any pending partial emission
211
+ if self.partial_transcript_task and not self.partial_transcript_task.done():
212
+ self.partial_transcript_task.cancel()
213
+ try:
214
+ await self.partial_transcript_task
215
+ except asyncio.CancelledError:
216
+ pass
217
+
218
  await self.output_queue.put(AdditionalOutputs({"role": "user", "content": event.transcript}))
219
 
220
  # Handle assistant transcription
 
384
 
385
  async def shutdown(self) -> None:
386
  """Shutdown the handler."""
387
+ # Cancel any pending debounce task
388
+ if self.partial_transcript_task and not self.partial_transcript_task.done():
389
+ self.partial_transcript_task.cancel()
390
+ try:
391
+ await self.partial_transcript_task
392
+ except asyncio.CancelledError:
393
+ pass
394
+
395
  if self.connection:
396
  try:
397
  await self.connection.close()