Grinding commited on
Commit
7ce9c0c
·
verified ·
1 Parent(s): 646c461

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +7 -0
  2. app.py +39 -0
  3. requirements.txt +8 -0
Dockerfile ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ # [cite: 173-178]
2
+ FROM python:3.9-slim
3
+ WORKDIR /code
4
+ COPY ./requirements.txt /code/requirements.txt
5
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
6
+ COPY ./app.py /code/app.py
7
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from source [cite: 147-169]
2
+ from fastapi import FastAPI, UploadFile, File, HTTPException
3
+ from transformers import pipeline
4
+ import torch
5
+ import librosa
6
+ import io
7
+
8
+ app = FastAPI()
9
+
10
+ # Load the ASR pipeline on startup
11
+ try:
12
+ asr_pipeline = pipeline(
13
+ "automatic-speech-recognition",
14
+ model="distil-whisper/distil-large-v3",
15
+ torch_dtype=torch.float32,
16
+ device="cpu",
17
+ )
18
+ except Exception as e:
19
+ asr_pipeline = None
20
+ print(f"Error loading ASR model: {e}")
21
+
22
+ @app.post("/transcribe")
23
+ async def transcribe_audio(audio_file: UploadFile = File(...)):
24
+ if not asr_pipeline:
25
+ raise HTTPException(status_code=503, detail="ASR model is not available.")
26
+
27
+ # Read audio file into memory
28
+ audio_bytes = await audio_file.read()
29
+
30
+ # Use librosa to load and resample the audio to 16kHz mono
31
+ try:
32
+ speech, sr = librosa.load(io.BytesIO(audio_bytes), sr=16000, mono=True)
33
+ except Exception as e:
34
+ raise HTTPException(status_code=400, detail=f"Could not process audio file: {e}")
35
+
36
+ # Perform transcription with chunking for long audio
37
+ result = asr_pipeline(speech, chunk_length_s=30, stride_length_s=5)
38
+
39
+ return {"transcription": result["text"]}
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ torch
4
+ transformers
5
+ accelerate
6
+ python-multipart
7
+ librosa
8
+ pydub