AshDavid12 commited on
Commit
55b37bf
·
1 Parent(s): f4a3257

adding dowload wav temp files

Browse files
Files changed (2) hide show
  1. client.py +11 -0
  2. infer.py +16 -2
client.py CHANGED
@@ -35,6 +35,17 @@ async def receive_transcription(websocket):
35
  try:
36
  transcription = await websocket.recv() # Receive transcription from the server
37
  print(f"Transcription: {transcription}")
 
 
 
 
 
 
 
 
 
 
 
38
  except Exception as e:
39
  print(f"Error receiving transcription: {e}")
40
  break
 
35
  try:
36
  transcription = await websocket.recv() # Receive transcription from the server
37
  print(f"Transcription: {transcription}")
38
+ download_url = transcription.get('download_url')
39
+ if download_url:
40
+ print(f"Download URL: {download_url}")
41
+ # Download the audio file
42
+ response = requests.get(download_url)
43
+ if response.status_code == 200:
44
+ with open("downloaded_audio.wav", "wb") as f:
45
+ f.write(response.content)
46
+ print("File downloaded successfully")
47
+ else:
48
+ print(f"Failed to download file. Status code: {response.status_code}")
49
  except Exception as e:
50
  print(f"Error receiving transcription: {e}")
51
  break
infer.py CHANGED
@@ -1,4 +1,6 @@
1
  import base64
 
 
2
  import faster_whisper
3
  import tempfile
4
 
@@ -188,8 +190,9 @@ async def websocket_transcribe(websocket: WebSocket):
188
  #min_transcription_time = 5.0 # Minimum duration of audio in seconds before transcription starts
189
 
190
  # A temporary file to store the growing audio data
191
- with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio_file:
192
  logging.info(f"Temporary audio file created at {temp_audio_file.name}")
 
193
 
194
  while True:
195
  try:
@@ -225,7 +228,8 @@ async def websocket_transcribe(websocket: WebSocket):
225
  # Send the transcription result back to the client with both new and all processed segments
226
  response = {
227
  "new_segments": partial_result['new_segments'],
228
- "processed_segments": processed_segments
 
229
  }
230
  logging.info(f"Sending {len(partial_result['new_segments'])} new segments to the client.")
231
  await websocket.send_json(response)
@@ -242,8 +246,18 @@ async def websocket_transcribe(websocket: WebSocket):
242
  logging.info("Cleaning up and closing WebSocket connection.")
243
 
244
 
 
 
245
 
 
 
 
246
 
 
 
 
 
 
247
 
248
  # @app.websocket("/ws")
249
  # async def websocket_endpoint(websocket: WebSocket):
 
1
  import base64
2
+ import os
3
+
4
  import faster_whisper
5
  import tempfile
6
 
 
190
  #min_transcription_time = 5.0 # Minimum duration of audio in seconds before transcription starts
191
 
192
  # A temporary file to store the growing audio data
193
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False,dir= "/tmp") as temp_audio_file:
194
  logging.info(f"Temporary audio file created at {temp_audio_file.name}")
195
+ temp_audio_filename = os.path.basename(temp_audio_file.name)
196
 
197
  while True:
198
  try:
 
228
  # Send the transcription result back to the client with both new and all processed segments
229
  response = {
230
  "new_segments": partial_result['new_segments'],
231
+ "processed_segments": processed_segments,
232
+ "download_url": f"wss://gigaverse-ivrit-ai-streaming.hf.space/download_audio/{temp_audio_filename}"
233
  }
234
  logging.info(f"Sending {len(partial_result['new_segments'])} new segments to the client.")
235
  await websocket.send_json(response)
 
246
  logging.info("Cleaning up and closing WebSocket connection.")
247
 
248
 
249
+ from fastapi.responses import FileResponse
250
+
251
 
252
+ @app.get("/download_audio/{filename}")
253
+ async def download_audio(filename: str):
254
+ file_path = f"/tmp/{filename}"
255
 
256
+ # Ensure the file exists before serving it
257
+ if os.path.exists(file_path):
258
+ return FileResponse(file_path, media_type='audio/wav', filename=filename)
259
+ else:
260
+ return {"error": "File not found"}
261
 
262
  # @app.websocket("/ws")
263
  # async def websocket_endpoint(websocket: WebSocket):