Afrinetwork7 commited on
Commit
920d6ef
1 Parent(s): 7826b7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -15
app.py CHANGED
@@ -61,28 +61,22 @@ async def transcribe_chunked_audio(audio_file: UploadFile, task: str = "transcri
61
  logger.debug(f"Audio file received: {audio_file.filename}")
62
 
63
  try:
64
- file_size_mb = os.stat(audio_file.filename).st_size / (1024 * 1024)
65
- logger.debug(f"File size: {file_size_mb:.2f}MB")
 
 
 
66
  except Exception as e:
67
- logger.error(f"Error getting file size: {str(e)}", exc_info=True)
68
- raise HTTPException(status_code=500, detail="Error checking file size")
69
 
70
  if file_size_mb > FILE_LIMIT_MB:
71
  logger.warning(f"Max file size exceeded: {file_size_mb:.2f}MB > {FILE_LIMIT_MB}MB")
72
  raise HTTPException(status_code=400, detail=f"File size exceeds file size limit. Got file of size {file_size_mb:.2f}MB for a limit of {FILE_LIMIT_MB}MB.")
73
 
74
- try:
75
- logger.debug(f"Opening audio file: {audio_file.filename}")
76
- with open(audio_file.filename, "rb") as f:
77
- inputs = f.read()
78
- logger.debug("Audio file read successfully")
79
- except Exception as e:
80
- logger.error(f"Error reading audio file: {str(e)}", exc_info=True)
81
- raise HTTPException(status_code=500, detail=f"Error reading audio file: {str(e)}")
82
-
83
  try:
84
  logger.debug("Performing ffmpeg read on audio file")
85
- inputs = ffmpeg_read(inputs, pipeline.feature_extractor.sampling_rate)
86
  inputs = {"array": inputs, "sampling_rate": pipeline.feature_extractor.sampling_rate}
87
  logger.debug("ffmpeg read completed successfully")
88
  except Exception as e:
@@ -100,7 +94,6 @@ async def transcribe_chunked_audio(audio_file: UploadFile, task: str = "transcri
100
  logger.debug("Transcribe_chunked_audio function completed successfully")
101
  return {"text": text, "runtime": runtime}
102
 
103
-
104
  @app.post("/transcribe_youtube")
105
  async def transcribe_youtube(yt_url: str = Form(...), task: str = "transcribe", return_timestamps: bool = False):
106
  logger.debug("Loading YouTube file...")
 
61
  logger.debug(f"Audio file received: {audio_file.filename}")
62
 
63
  try:
64
+ # Read the file content
65
+ file_content = await audio_file.read()
66
+ file_size = len(file_content)
67
+ file_size_mb = file_size / (1024 * 1024)
68
+ logger.debug(f"File size: {file_size} bytes ({file_size_mb:.2f}MB)")
69
  except Exception as e:
70
+ logger.error(f"Error reading file: {str(e)}", exc_info=True)
71
+ raise HTTPException(status_code=500, detail=f"Error reading file: {str(e)}")
72
 
73
  if file_size_mb > FILE_LIMIT_MB:
74
  logger.warning(f"Max file size exceeded: {file_size_mb:.2f}MB > {FILE_LIMIT_MB}MB")
75
  raise HTTPException(status_code=400, detail=f"File size exceeds file size limit. Got file of size {file_size_mb:.2f}MB for a limit of {FILE_LIMIT_MB}MB.")
76
 
 
 
 
 
 
 
 
 
 
77
  try:
78
  logger.debug("Performing ffmpeg read on audio file")
79
+ inputs = ffmpeg_read(file_content, pipeline.feature_extractor.sampling_rate)
80
  inputs = {"array": inputs, "sampling_rate": pipeline.feature_extractor.sampling_rate}
81
  logger.debug("ffmpeg read completed successfully")
82
  except Exception as e:
 
94
  logger.debug("Transcribe_chunked_audio function completed successfully")
95
  return {"text": text, "runtime": runtime}
96
 
 
97
  @app.post("/transcribe_youtube")
98
  async def transcribe_youtube(yt_url: str = Form(...), task: str = "transcribe", return_timestamps: bool = False):
99
  logger.debug("Loading YouTube file...")