Spaces:
Sleeping
Sleeping
Update app.py
Browse files113 unmodified lines114115116117118119113 unmodified linesfinally:await websocket.close()if __name__ == "__main__":# For local testing. On Spaces, you don't usually run uvicorn manually.113 unmodified lines114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156113 unmodified linesfinally:await websocket.close()@app.websocket("/ws/transcribe_pcm")async def websocket_transcription_pcm(websocket: WebSocket):await websocket.accept()buffer = b""SAMPLE_RATE = 16000MIN_PCM_SIZE = SAMPLE_RATE * 2 * 3 # 3 seconds buffertry:while True:chunk = await websocket.receive_bytes()# If end control message (optional)if chunk == b"__END__":breakbuffer += chunkif len(buffer) >= MIN_PCM_SIZE:with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp:temp.write(buffer)temp.flush()audio_path = temp.namesegments = model.transcribe(audio_path)text = " ".join(seg.text for seg in segments).strip()if text:await websocket.send_text(text)buffer = b""os.remove(audio_path)await websocket.send_text("[stream ended]")except:passfinally:await websocket.close()if __name__ == "__main__":# For local testing. On Spaces, you don't usually run uvicorn manually.
app.py+37