Spaces:
Sleeping
Sleeping
Update app.py
Browse files113 unmodified lines114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156113 unmodified lines finally: await websocket.close()
@app.websocket("/ws/transcribe_pcm")async def websocket_transcription_pcm(websocket: WebSocket): await websocket.accept() buffer = b"" SAMPLE_RATE = 16000 MIN_PCM_SIZE = SAMPLE_RATE * 2 * 3 # 3 seconds buffer
try: while True: chunk = await websocket.receive_bytes()
# If end control message (optional) if chunk == b"__END__": break
buffer += chunk
if len(buffer) >= MIN_PCM_SIZE: with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp: temp.write(buffer) temp.flush() audio_path = temp.name
segments = 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: pass finally: await websocket.close()
if __name__ == "__main__": # For local testing. On Spaces, you don't usually run uvicorn manually.app.py+37