[Python] How do I stream data from Twilio to openai Whisper?

Discussão em 'Python' iniciado por Stack, Maio 18, 2025.

  1. Stack

    Stack Membro Participativo

    With the below code, the result ends up just being always the same "Thank you." Any ideas what could be going wrong. For reference, I used the load_audio function in the whisper package. And an article about twilio and Vosk.

    @sock.route('/stream')
    def stream(ws):
    while True:
    message = ws.receive()
    packet = json.loads(message)
    if packet['event'] == 'media':
    # get audio as ulaw
    audio = base64.b64decode(packet['media']['payload'])
    add_audio(audio)

    buffer = np.array([], dtype=np.float32)

    def add_audio(audio):
    global buffer
    # convert audio to numpy array
    audio = np.frombuffer(audio, np.int16).flatten().astype(np.float32) / 32768.0
    buffer = np.concatenate((buffer, audio))
    process_audio()


    def process_audio():
    global buffer
    audio = whisper.pad_or_trim(buffer)
    mel = whisper.log_mel_spectrogram(audio).to(model.device)
    mel = torch.nan_to_num(mel)
    result = whisper.decode(model, mel, options)

    Continue reading...

Compartilhe esta Página