Files
rag-technical-docs-assistant/services/local_s2s/gapgpt_audio.py
T
amirhosein mazaheri 42400495af feat(backend): integrate document deletion and grounded speech providers
Enable replay-safe document deletion and public deployment model identifiers with the forward voice-model schema revision.

Add GapGPT dialogue and ElevenLabs speech routing, strict extractive grounding, ten-source retrieval and regression coverage.

Validation: 199 targeted provider-free tests passed. The full provider-free run stalled and was interrupted; PostgreSQL acceptance was not run because TEST_DATABASE_URL is unset.
2026-10-20 09:00:37 +03:30

35 lines
1.5 KiB
Python

"""GapGPT Gemini TTS audio envelope to the existing bounded PCM contract."""
import base64
import binascii
from io import BytesIO
import wave
def speech_payload(payload):
return dict(model=payload['model'], stream=False, modalities=['audio'],
audio=dict(voice=payload['voice'], format='pcm16'),
messages=[dict(role='user', content=(
'Read aloud only the following text in its original language. '
f"Speak at {payload.get('speed', 1.0)} times normal pace. Text: " + payload['input']))])
def decode_pcm(payload, limit):
try:
choice, = payload['choices']
if choice['finish_reason'] != 'stop':
raise ValueError('Incomplete speech')
encoded = choice['message']['audio']['data']
if not isinstance(encoded, str) or len(encoded) > 4 * ((limit + 44 + 2) // 3):
raise ValueError('Speech capacity')
data = base64.b64decode(encoded, validate=True)
if data.startswith(b'RIFF'):
with wave.open(BytesIO(data), 'rb') as audio:
if (audio.getnchannels(), audio.getsampwidth(), audio.getframerate()) != (1, 2, 24000):
raise ValueError('Speech format')
data = audio.readframes(limit // 2 + 1)
if not data or len(data) % 2 or len(data) > limit:
raise ValueError('Speech capacity')
return data
except (KeyError, TypeError, binascii.Error, wave.Error, EOFError) as exc:
raise ValueError('Invalid GapGPT speech response') from None