Not able to connect to backend made in python

Error :-

---------------------------------------------------------------------------------------------
Server is listening on ws://localhost:7000
Client connected: 5c2d517e-80ec-4304-afc1-026f80fe38e5
connection handler failed
Traceback (most recent call last):
  File "C:\Users\user1\AppData\Roaming\Python\Python312\site-packages\websockets\legacy\protocol.py", line 953, in transfer_data
    message = await self.read_message()
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\AppData\Roaming\Python\Python312\site-packages\websockets\legacy\protocol.py", line 1023, in read_message
    frame = await self.read_data_frame(max_size=self.max_size)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\AppData\Roaming\Python\Python312\site-packages\websockets\legacy\protocol.py", line 1098, in read_data_frame
    frame = await self.read_frame(max_size)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\AppData\Roaming\Python\Python312\site-packages\websockets\legacy\protocol.py", line 1155, in read_frame
    frame = await Frame.read(
            ^^^^^^^^^^^^^^^^^
  File "C:\Users\user1\AppData\Roaming\Python\Python312\site-packages\websockets\legacy\framing.py", line 70, in read
    data = await reader(2)
           ^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\asyncio\streams.py", line 750, in readexactly
    raise exceptions.IncompleteReadError(incomplete, n)
asyncio.exceptions.IncompleteReadError: 0 bytes read on a total of 2 expected bytes

The above exception was the direct cause of the following exception:
---------------------------------------------------------------------------------------------

Code starts here :-

import asyncio
import websockets
import uuid
import os
# from interview.utils import get_transcript_for_video
# from moviepy.editor import VideoFileClip, concatenate_videoclips
# from interview.interview_main import get_interview_response
# from openai_functions import speech_to_text_file
import json
 
clients = {}
mp4_file = 'C:\\Users\\Anika.khandelwal\\Documents\\repo\\ai_interview_engine\\test_stream\\output_video.mp4'
interview_length_in_minutes = 10
chunk_size_in_ms = 500
start = 0
pause_limit = 4
sentence = ""
ended = False
count_chunk = 0
video_chunks = {}
 
# Create a directory for storing video files if it doesn't exist
if not os.path.exists('videos'):
    os.makedirs('videos')
 
async def handle_client(websocket, path):
    # Generate a unique client ID
    client_id = str(uuid.uuid4())
    clients[client_id] = websocket
    print(f'Client connected: {client_id}')
 
    # Create a file to write the video chunks
 
    video_file_path = "speech.mp4"
    thread = "thread_KCnY8nWXGJsOzhGsioDt18FQ"
    # chunk = data.get('chunk')
    # assistant = data.get('assistant')
    assistant = "asst_x7J07sjtQheyA2o2tz5tLrhw"
    if thread not in video_chunks:
        video_chunks[thread] = []
    # with open(video_file_path, 'wb') as video_file:
    async for message in websocket:
        # Write each chunk of data to the file
        # with open(video_file_path, 'wb') as video_file:
        #     video_file.write(message)
            # video_file.close()
        print(message)
        # time.sleep(10)
        # video_chunks[thread].append(VideoFileClip("speech.mp4"))
        # count_chunk += 1
        # print(speech_to_text_file(message))
        # if (chunk_size_in_ms * count_chunk) / 1000 >= 5:
        #     transcript = get_transcript_for_video(video_chunks[thread][start_chunk:])
        #     start_chunk = len(video_chunks[thread])
        #     if len(video_chunks[thread]) < (0.9 * interview_length_in_minutes * 60000) / chunk_size_in_ms:
        #         if len(transcript.split()) < pause_limit:
        #             if len(video_chunks[thread]) > (
        #                     0.05 * interview_length_in_minutes * 60000) / chunk_size_in_ms:
        #                 sentence = get_transcript_for_video(video_chunks[thread][start:])
        #                 start = len(video_chunks[thread])
        # inputs = {"text": message,
        #           "thread": thread,
        #           "assistant": assistant,
        #           "endFlag": False}
        # response = get_interview_response(inputs)
        # response = json.dumps({'text':json.loads(response["response"][0])["ResponseToTheCandidate"]})
        response = "Hello how are you?"
        websocket.send(response)
        #     else:
        #         if ended is not False and len(transcript.split()) < pause_limit:
        #             # send fixed response and transcript
        #             ended = True
 
async def main():
    # Start the WebSocket server on port 3000
    async with websockets.serve(handle_client, "localhost", 7000):
        print("Server is listening on ws://localhost:7000")
        await asyncio.Future()  # Keep the server running indefinitely
 
# Run the WebSocket server
asyncio.run(main())
1 Like