MalikIbrar commited on
Commit
d604b45
·
1 Parent(s): e723a9c
Files changed (2) hide show
  1. Dockerfile +1 -0
  2. main.py +7 -8
Dockerfile CHANGED
@@ -4,6 +4,7 @@
4
  FROM python:3.9
5
 
6
  RUN useradd -m -u 1000 user
 
7
  USER user
8
  ENV PATH="/home/user/.local/bin:$PATH"
9
 
 
4
  FROM python:3.9
5
 
6
  RUN useradd -m -u 1000 user
7
+ RUN apt-get update && apt-get install -y ffmpeg
8
  USER user
9
  ENV PATH="/home/user/.local/bin:$PATH"
10
 
main.py CHANGED
@@ -2,7 +2,6 @@ from fastapi import FastAPI, File, UploadFile, HTTPException
2
  from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
3
  import torch
4
  import uvicorn
5
- import librosa
6
  import soundfile as sf
7
  from fastapi.middleware.cors import CORSMiddleware
8
  import os
@@ -52,15 +51,15 @@ async def transcribe_audio(file: UploadFile = File(...)):
52
  temp_file.write(await file.read())
53
  temp_path = temp_file.name
54
 
55
- # Load the audio file using librosa
56
- audio, sr = librosa.load(temp_path, sr=16000)
57
 
58
- # Convert to a format that the model can process (in case the file needs reformatting)
59
- processed_path = temp_path # Reuse temp file if format is already correct
60
- sf.write(processed_path, audio, 16000)
61
 
62
  # Pass the processed audio to the pipeline
63
- result = pipe(processed_path)
64
 
65
  # Remove the temp file after processing
66
  os.remove(temp_path)
@@ -70,7 +69,7 @@ async def transcribe_audio(file: UploadFile = File(...)):
70
 
71
  except Exception as e:
72
  # Clean up temp file in case of error
73
- if os.path.exists(temp_path):
74
  os.remove(temp_path)
75
  raise HTTPException(status_code=500, detail=f"Error occurred: {str(e)}")
76
 
 
2
  from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
3
  import torch
4
  import uvicorn
 
5
  import soundfile as sf
6
  from fastapi.middleware.cors import CORSMiddleware
7
  import os
 
51
  temp_file.write(await file.read())
52
  temp_path = temp_file.name
53
 
54
+ # Load the audio file using soundfile
55
+ audio, sr = sf.read(temp_path)
56
 
57
+ # Ensure the sample rate is 16000 Hz
58
+ if sr != 16000:
59
+ raise HTTPException(status_code=400, detail="Sample rate must be 16000 Hz.")
60
 
61
  # Pass the processed audio to the pipeline
62
+ result = pipe(temp_path)
63
 
64
  # Remove the temp file after processing
65
  os.remove(temp_path)
 
69
 
70
  except Exception as e:
71
  # Clean up temp file in case of error
72
+ if 'temp_path' in locals() and os.path.exists(temp_path):
73
  os.remove(temp_path)
74
  raise HTTPException(status_code=500, detail=f"Error occurred: {str(e)}")
75