WebSocket disconnected with code: 1011 in django

consumers.py

import asyncio
import websockets
import sounddevice as sd
import vosk
import queue
from channels.generic.websocket import AsyncWebsocketConsumer

model_path = "vosk_model/vosk-model-small-en-us-0.15"
model = vosk.Model(model_path)
sample_rate = 16000
audio_queue = queue.Queue()

class SpeechRecognitionConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        await self.accept()
        self.rec = vosk.KaldiRecognizer(model, sample_rate)
        print("WebSocket connection accepted")

    async def receive(self, bytes_data):
        audio_queue.put(bytes_data)
        await self.process_audio()

    async def process_audio(self):
        while not audio_queue.empty():
            data = audio_queue.get()
            if self.rec.AcceptWaveform(data):
                result = self.rec.Result()
                result_dict = json.loads(result)
                recognized_text = result_dict.get('text', '')
                print("recognized_text: ", recognized_text)
                print("Final Result: ", recognized_text)
                await self.send(text_data=json.dumps({
                    'type': 'result',
                    'text': recognized_text
                }))
            else:
                partial_result = self.rec.PartialResult()
                partial_dict = json.loads(partial_result)
                partial_text = partial_dict.get('partial', '')
                print("Partial Result: ", partial_text)
                await self.send(text_data=json.dumps({
                    'type': 'partial',
                    'text': partial_text
                }))

    async def disconnect(self, close_code):
        print(f"WebSocket disconnected with code: {close_code}")

audio.py

import asyncio
import websockets
import sounddevice as sd

async def send_audio_data(websocket):
    loop = asyncio.get_event_loop()

    def audio_callback(indata, frames, time, status):
        if status:
            print(f"Audio Status: {status}")
        audio_data = bytes(indata)
        print(f"Received audio data of length: {len(audio_data)}")
        asyncio.run_coroutine_threadsafe(websocket.send(audio_data), loop)

    with sd.RawInputStream(samplerate=16000, channels=1, dtype='int16', blocksize=1024, callback=audio_callback):
        print("Recording and sending audio data...")
        while True:
            await asyncio.sleep(0.05)


async def main():
    uri = "ws://localhost:8000/ws/recognize/"
    async with websockets.connect(uri) as websocket:
        print("Connected to WebSocket server")
        await send_audio_data(websocket)

# Create an event loop
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

I'm developing a speech recognition feature in my Django application using Django Channels and the Vosk library. However, I'm encountering an issue where the WebSocket connection closes with status code 1011.

When I run the client, the WebSocket connection gets established, but it frequently closes with the status code 1011. I'm unsure what might be causing this issue. I've checked the following:

  • Server Logs: No clear errors are reported.
  • Audio Data: The audio data being sent seems to be valid, and I can confirm it's being received in the consumer.
Вернуться на верх