Spaces:
Sleeping
Sleeping
AshDavid12
commited on
Commit
·
9bff659
1
Parent(s):
9b933e3
trying to get logs
Browse files
infer.py
CHANGED
@@ -126,7 +126,6 @@ async def read_root():
|
|
126 |
import tempfile
|
127 |
|
128 |
|
129 |
-
|
130 |
def transcribe_core_ws(audio_file, last_transcribed_time):
|
131 |
"""
|
132 |
Transcribe the audio file and return only the segments that have not been processed yet.
|
@@ -211,6 +210,7 @@ def validate_wav_file(wav_file_path):
|
|
211 |
logging.error(f"Error reading WAV file: {e}")
|
212 |
return False
|
213 |
|
|
|
214 |
@app.websocket("/wtranscribe")
|
215 |
async def websocket_transcribe(websocket: WebSocket):
|
216 |
logging.info("New WebSocket connection request received.")
|
@@ -227,7 +227,6 @@ async def websocket_transcribe(websocket: WebSocket):
|
|
227 |
pcm_audio_buffer = bytearray()
|
228 |
logging.info("im here, is it failing?.")
|
229 |
|
230 |
-
|
231 |
# Metadata for the incoming PCM data (sample rate, channels, and sample width should be consistent)
|
232 |
sample_rate = 16000 # 16kHz
|
233 |
channels = 1 # Mono
|
@@ -240,9 +239,13 @@ async def websocket_transcribe(websocket: WebSocket):
|
|
240 |
os.makedirs(tmp_directory)
|
241 |
logging.info("im here, is it failing?2.")
|
242 |
while True:
|
|
|
243 |
try:
|
244 |
# Receive the next chunk of PCM audio data
|
|
|
245 |
audio_chunk = await websocket.receive_bytes()
|
|
|
|
|
246 |
if not audio_chunk:
|
247 |
logging.warning("Received empty audio chunk, skipping processing.")
|
248 |
continue
|
@@ -259,7 +262,8 @@ async def websocket_transcribe(websocket: WebSocket):
|
|
259 |
# Estimate the duration of the chunk based on its size
|
260 |
chunk_duration = len(audio_chunk) / (sample_rate * channels * sample_width)
|
261 |
accumulated_audio_time += chunk_duration
|
262 |
-
logging.info(
|
|
|
263 |
|
264 |
# Transcribe when enough time (audio) is accumulated (e.g., at least 5 seconds of audio)
|
265 |
if accumulated_audio_time >= min_transcription_time:
|
@@ -270,10 +274,10 @@ async def websocket_transcribe(websocket: WebSocket):
|
|
270 |
logging.info(f"Temporary audio file created at {temp_wav_file.name}")
|
271 |
|
272 |
with wave.open(temp_wav_file.name, 'wb') as wav_file:
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
|
278 |
if not validate_wav_file(temp_wav_file.name):
|
279 |
logging.error(f"Invalid WAV file created: {temp_wav_file.name}")
|
@@ -291,7 +295,8 @@ async def websocket_transcribe(websocket: WebSocket):
|
|
291 |
raise Exception(f"Temporary WAV file {temp_wav_file.name} not found.")
|
292 |
|
293 |
# Call the transcription function with the WAV file path
|
294 |
-
partial_result, last_transcribed_time = transcribe_core_ws(temp_wav_file.name,
|
|
|
295 |
processed_segments.extend(partial_result['new_segments'])
|
296 |
|
297 |
# Clear the buffer after transcription
|
@@ -322,6 +327,7 @@ async def websocket_transcribe(websocket: WebSocket):
|
|
322 |
finally:
|
323 |
logging.info("Cleaning up and closing WebSocket connection.")
|
324 |
|
|
|
325 |
from fastapi.responses import FileResponse
|
326 |
|
327 |
|
@@ -459,12 +465,6 @@ async def download_audio(filename: str):
|
|
459 |
#
|
460 |
|
461 |
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
# @app.websocket("/wtranscribe")
|
469 |
# async def websocket_transcribe(websocket: WebSocket):
|
470 |
# logging.info("New WebSocket connection request received.")
|
|
|
126 |
import tempfile
|
127 |
|
128 |
|
|
|
129 |
def transcribe_core_ws(audio_file, last_transcribed_time):
|
130 |
"""
|
131 |
Transcribe the audio file and return only the segments that have not been processed yet.
|
|
|
210 |
logging.error(f"Error reading WAV file: {e}")
|
211 |
return False
|
212 |
|
213 |
+
|
214 |
@app.websocket("/wtranscribe")
|
215 |
async def websocket_transcribe(websocket: WebSocket):
|
216 |
logging.info("New WebSocket connection request received.")
|
|
|
227 |
pcm_audio_buffer = bytearray()
|
228 |
logging.info("im here, is it failing?.")
|
229 |
|
|
|
230 |
# Metadata for the incoming PCM data (sample rate, channels, and sample width should be consistent)
|
231 |
sample_rate = 16000 # 16kHz
|
232 |
channels = 1 # Mono
|
|
|
239 |
os.makedirs(tmp_directory)
|
240 |
logging.info("im here, is it failing?2.")
|
241 |
while True:
|
242 |
+
logging.info("in while true")
|
243 |
try:
|
244 |
# Receive the next chunk of PCM audio data
|
245 |
+
logging.info("in try before recive ")
|
246 |
audio_chunk = await websocket.receive_bytes()
|
247 |
+
logging.info("after recieve")
|
248 |
+
sys.stdout.flush()
|
249 |
if not audio_chunk:
|
250 |
logging.warning("Received empty audio chunk, skipping processing.")
|
251 |
continue
|
|
|
262 |
# Estimate the duration of the chunk based on its size
|
263 |
chunk_duration = len(audio_chunk) / (sample_rate * channels * sample_width)
|
264 |
accumulated_audio_time += chunk_duration
|
265 |
+
logging.info(
|
266 |
+
f"Received and buffered {len(audio_chunk)} bytes, total buffered: {len(pcm_audio_buffer)} bytes, total time: {accumulated_audio_time:.2f} seconds")
|
267 |
|
268 |
# Transcribe when enough time (audio) is accumulated (e.g., at least 5 seconds of audio)
|
269 |
if accumulated_audio_time >= min_transcription_time:
|
|
|
274 |
logging.info(f"Temporary audio file created at {temp_wav_file.name}")
|
275 |
|
276 |
with wave.open(temp_wav_file.name, 'wb') as wav_file:
|
277 |
+
wav_file.setnchannels(channels)
|
278 |
+
wav_file.setsampwidth(sample_width)
|
279 |
+
wav_file.setframerate(sample_rate)
|
280 |
+
wav_file.writeframes(pcm_audio_buffer)
|
281 |
|
282 |
if not validate_wav_file(temp_wav_file.name):
|
283 |
logging.error(f"Invalid WAV file created: {temp_wav_file.name}")
|
|
|
295 |
raise Exception(f"Temporary WAV file {temp_wav_file.name} not found.")
|
296 |
|
297 |
# Call the transcription function with the WAV file path
|
298 |
+
partial_result, last_transcribed_time = transcribe_core_ws(temp_wav_file.name,
|
299 |
+
last_transcribed_time)
|
300 |
processed_segments.extend(partial_result['new_segments'])
|
301 |
|
302 |
# Clear the buffer after transcription
|
|
|
327 |
finally:
|
328 |
logging.info("Cleaning up and closing WebSocket connection.")
|
329 |
|
330 |
+
|
331 |
from fastapi.responses import FileResponse
|
332 |
|
333 |
|
|
|
465 |
#
|
466 |
|
467 |
|
|
|
|
|
|
|
|
|
|
|
|
|
468 |
# @app.websocket("/wtranscribe")
|
469 |
# async def websocket_transcribe(websocket: WebSocket):
|
470 |
# logging.info("New WebSocket connection request received.")
|