Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix interrupts #127

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions openduck-py/openduck_py/response_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def __init__(
self.response_task: Optional[asyncio.Task] = None
self.interrupt_event = asyncio.Event()
self.system_prompt = system_prompt
self.speech_has_started = False

if context is None:
context = {}
Expand Down Expand Up @@ -325,10 +326,18 @@ async def receive_audio(self, message: bytes):
vad_result = self.vad(audio_16k_chunk)
if vad_result:
async with SessionAsync() as db:
if "end" in vad_result:
print("end of speech detected.")
transcription = ""
if "start" in vad_result or "end" in vad_result:
self.time_of_last_activity = time()
await log_event(db, self.session_id, "detected_end_of_speech")
if "start" in vad_result:
self.speech_has_started = True
print("Detected start of speech", flush=True)
else:
self.speech_has_started = False
print("Detected end of speech", flush=True)

if self.speech_has_started:
self.audio_data.append(audio_16k_np)

audio_data = np.concatenate(self.audio_data)
transcription = await _transcribe(audio_data)
Expand All @@ -344,17 +353,16 @@ async def receive_audio(self, message: bytes):
await log_event(db, self.session_id, "interrupted_response")
await self.interrupt(self.response_task)

await log_event(
db, self.session_id, "started_response", audio=audio_data
)
self.response_task = asyncio.create_task(
self.start_response(transcription)
)

if "start" in vad_result:
print("start of speech detected.")
self.time_of_last_activity = time()
await log_event(db, self.session_id, "detected_start_of_speech")
if "end" in vad_result:
await log_event(
db,
self.session_id,
"started_response",
audio=audio_data,
)
self.response_task = asyncio.create_task(
self.start_response(transcription)
)
i = upper

async def _generate_and_speak(
Expand Down
Loading