AshDavid12 commited on
Commit
a94388a
·
1 Parent(s): f1bf1b3

empy seg -buffer size

Browse files
Files changed (2) hide show
  1. client.py +6 -30
  2. infer.py +17 -26
client.py CHANGED
@@ -1,18 +1,12 @@
1
  import asyncio
2
  import websockets
3
- import wave
4
  import requests
 
5
 
6
  # Parameters for reading and sending the audio
7
- SAMPLE_RATE = 16000
8
- CHUNK_SIZE = 8192 # Size of the audio chunk sent at a time
9
- AUDIO_FILE_URL = "https://raw.githubusercontent.com/AshDavid12/runpod_serverless_whisper/main/me-hebrew.wav" # Path to the mp3 file
10
-
11
 
12
  async def send_audio(websocket):
13
- buffer_size = 1024 * 1024 # Buffer 1MB of audio data before sending for transcription
14
- audio_buffer = bytearray() # Collect audio chunks directly in memory
15
-
16
  # Stream the audio file in real-time
17
  with requests.get(AUDIO_FILE_URL, stream=True, allow_redirects=False) as response:
18
  if response.status_code == 200:
@@ -20,42 +14,24 @@ async def send_audio(websocket):
20
 
21
  for chunk in response.iter_content(chunk_size=8192): # Stream in chunks of 8192 bytes
22
  if chunk:
23
- # Append each chunk to the in-memory buffer
24
- audio_buffer.extend(chunk)
25
- print(f"Received audio chunk of size {len(chunk)} bytes.")
26
-
27
- # Once we have buffered enough audio data, send it for transcription
28
- if len(audio_buffer) >= buffer_size:
29
- await websocket.send(audio_buffer) # Send buffered data directly
30
- print(f"Sent {len(audio_buffer)} bytes of audio data to the server for transcription.")
31
- audio_buffer.clear() # Clear buffer after sending
32
- await asyncio.sleep(0.01) # Simulate real-time streaming
33
 
34
  print("Finished sending audio.")
35
  else:
36
  print(f"Failed to download audio file. Status code: {response.status_code}")
37
 
38
- async def receive_transcription(websocket):
39
- while True:
40
- try:
41
- transcription = await websocket.recv()
42
- print(f"Received transcription: {transcription}")
43
- except Exception as e:
44
- print(f"Error receiving transcription: {e}")
45
- break
46
-
47
  async def receive_transcription(websocket):
48
  while True:
49
  try:
50
  transcription = await websocket.recv() # Receive transcription from the server
51
  print(f"Transcription: {transcription}")
52
  except Exception as e:
53
- print(f"Error: {e}")
54
  break
55
 
56
- import ssl
57
  async def run_client():
58
- uri = ("wss://gigaverse-ivrit-ai-streaming.hf.space/ws/transcribe") # Replace with your Hugging Face Space WebSocket URL
59
  ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
60
  ssl_context.check_hostname = False
61
  ssl_context.verify_mode = ssl.CERT_NONE
 
1
  import asyncio
2
  import websockets
 
3
  import requests
4
+ import ssl
5
 
6
  # Parameters for reading and sending the audio
7
+ AUDIO_FILE_URL = "https://raw.githubusercontent.com/AshDavid12/runpod_serverless_whisper/main/me-hebrew.wav" # Use WAV file
 
 
 
8
 
9
  async def send_audio(websocket):
 
 
 
10
  # Stream the audio file in real-time
11
  with requests.get(AUDIO_FILE_URL, stream=True, allow_redirects=False) as response:
12
  if response.status_code == 200:
 
14
 
15
  for chunk in response.iter_content(chunk_size=8192): # Stream in chunks of 8192 bytes
16
  if chunk:
17
+ await websocket.send(chunk) # Send each chunk over WebSocket
18
+ print(f"Sent audio chunk of size {len(chunk)} bytes")
 
 
 
 
 
 
 
 
19
 
20
  print("Finished sending audio.")
21
  else:
22
  print(f"Failed to download audio file. Status code: {response.status_code}")
23
 
 
 
 
 
 
 
 
 
 
24
  async def receive_transcription(websocket):
25
  while True:
26
  try:
27
  transcription = await websocket.recv() # Receive transcription from the server
28
  print(f"Transcription: {transcription}")
29
  except Exception as e:
30
+ print(f"Error receiving transcription: {e}")
31
  break
32
 
 
33
  async def run_client():
34
+ uri = ("wss://gigaverse-ivrit-ai-streaming.hf.space/ws/transcribe") # WebSocket URL
35
  ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
36
  ssl_context.check_hostname = False
37
  ssl_context.verify_mode = ssl.CERT_NONE
infer.py CHANGED
@@ -193,59 +193,50 @@ async def websocket_transcribe(websocket: WebSocket):
193
 
194
  try:
195
  processed_segments = [] # Keeps track of the segments already transcribed
196
- audio_data = bytearray() # Buffer for audio chunks
197
- logging.info("Initialized processed_segments and audio_data buffer.")
198
 
199
  # A temporary file to store the growing audio data
200
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio_file:
201
  logging.info(f"Temporary audio file created at {temp_audio_file.name}")
202
 
203
- # Continuously receive and process audio chunks
204
  while True:
205
  try:
206
- logging.info("Waiting to receive the next chunk of audio data from WebSocket.")
207
-
208
  # Receive the next chunk of audio data
209
  audio_chunk = await websocket.receive_bytes()
210
- logging.info(f"Received an audio chunk of size {len(audio_chunk)} bytes.")
211
-
212
  if not audio_chunk:
213
  logging.warning("Received empty audio chunk, skipping processing.")
214
  continue
215
 
 
216
  temp_audio_file.write(audio_chunk)
217
  temp_audio_file.flush()
218
- logging.debug(f"Written audio chunk to temporary file: {temp_audio_file.name}")
 
 
219
 
220
- audio_data.extend(audio_chunk) # In-memory data buffer (if needed)
221
- #logging.debug(f"Audio data buffer extended to size {len(audio_data)} bytes.")
 
222
 
223
- # Perform transcription and track new segments
224
- logging.info(
225
- f"Transcribing audio from {temp_audio_file.name}. Processed segments: {len(processed_segments)}")
226
- partial_result, processed_segments = transcribe_core_ws(temp_audio_file.name, processed_segments)
227
 
228
- logging.info(
229
- f"Transcription completed. Sending {len(partial_result['new_segments'])} new segments to the client.")
230
- # Send the new transcription result back to the client
231
- logging.info(
232
- f"partial result{partial_result}")
233
- await websocket.send_json(partial_result)
234
 
235
  except WebSocketDisconnect:
236
- logging.info("WebSocket connection closed by the client. Ending transcription session.")
237
- break
238
- except Exception as e:
239
- logging.error(f"Error processing audio chunk: {e}")
240
- await websocket.send_json({"error": str(e)})
241
  break
242
 
243
  except Exception as e:
244
  logging.error(f"Unexpected error during WebSocket transcription: {e}")
245
  await websocket.send_json({"error": str(e)})
 
246
  finally:
247
  logging.info("Cleaning up and closing WebSocket connection.")
248
 
249
 
250
 
251
-
 
193
 
194
  try:
195
  processed_segments = [] # Keeps track of the segments already transcribed
196
+ accumulated_audio_size = 0 # Track how much audio data has been buffered
 
197
 
198
  # A temporary file to store the growing audio data
199
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio_file:
200
  logging.info(f"Temporary audio file created at {temp_audio_file.name}")
201
 
 
202
  while True:
203
  try:
 
 
204
  # Receive the next chunk of audio data
205
  audio_chunk = await websocket.receive_bytes()
 
 
206
  if not audio_chunk:
207
  logging.warning("Received empty audio chunk, skipping processing.")
208
  continue
209
 
210
+ # Write audio chunk to file and accumulate size
211
  temp_audio_file.write(audio_chunk)
212
  temp_audio_file.flush()
213
+ accumulated_audio_size += len(audio_chunk)
214
+ logging.info(
215
+ f"Received and buffered {len(audio_chunk)} bytes, total buffered: {accumulated_audio_size} bytes")
216
 
217
+ # Buffer at least 512KB before transcription
218
+ if accumulated_audio_size >= (512 * 1024): # Adjust this size as needed
219
+ logging.info("Buffered enough data, starting transcription.")
220
 
221
+ partial_result, processed_segments = transcribe_core_ws(temp_audio_file.name,
222
+ processed_segments)
223
+ accumulated_audio_size = 0 # Reset the accumulated audio size
 
224
 
225
+ # Send the transcription result back to the client
226
+ logging.info(f"Sending {len(partial_result['new_segments'])} new segments to the client.")
227
+ logging.info(f"partial result {partial_result}")
228
+ await websocket.send_json(partial_result)
 
 
229
 
230
  except WebSocketDisconnect:
231
+ logging.info("WebSocket connection closed by the client.")
 
 
 
 
232
  break
233
 
234
  except Exception as e:
235
  logging.error(f"Unexpected error during WebSocket transcription: {e}")
236
  await websocket.send_json({"error": str(e)})
237
+
238
  finally:
239
  logging.info("Cleaning up and closing WebSocket connection.")
240
 
241
 
242