Spaces:
Running
Running
Upload 3 files
Browse files- Dockerfile +23 -0
- app.py +32 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use official Python image
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Install dependencies for ffmpeg and WhisperX
|
| 5 |
+
RUN apt-get update && \
|
| 6 |
+
apt-get install -y git ffmpeg libsndfile1 && \
|
| 7 |
+
rm -rf /var/lib/apt/lists/*
|
| 8 |
+
|
| 9 |
+
# Set working directory
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
# Copy requirements and install
|
| 13 |
+
COPY requirements.txt .
|
| 14 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 15 |
+
|
| 16 |
+
# Copy app code
|
| 17 |
+
COPY . .
|
| 18 |
+
|
| 19 |
+
# Expose port
|
| 20 |
+
EXPOSE 8000
|
| 21 |
+
|
| 22 |
+
# Run the FastAPI app
|
| 23 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import whisperx
|
| 4 |
+
import torch
|
| 5 |
+
import tempfile
|
| 6 |
+
import shutil
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
# Load model globally to avoid reloading for every request
|
| 12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
+
model = whisperx.load_model("medium", device)
|
| 14 |
+
|
| 15 |
+
@app.post("/transcribe")
|
| 16 |
+
async def transcribe_audio(file: UploadFile = File(...)):
|
| 17 |
+
try:
|
| 18 |
+
# Save uploaded audio to temp file
|
| 19 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
| 20 |
+
shutil.copyfileobj(file.file, tmp)
|
| 21 |
+
temp_audio_path = tmp.name
|
| 22 |
+
|
| 23 |
+
# Load and process audio
|
| 24 |
+
audio = whisperx.load_audio(temp_audio_path)
|
| 25 |
+
result = model.transcribe(audio, batch_size=16, return_word_timestamps=True)
|
| 26 |
+
|
| 27 |
+
# Clean up temp file
|
| 28 |
+
os.remove(temp_audio_path)
|
| 29 |
+
|
| 30 |
+
return JSONResponse(content=result)
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.110.0
|
| 2 |
+
uvicorn[standard]==0.29.0
|
| 3 |
+
torch==2.2.2
|
| 4 |
+
torchaudio==2.2.2
|
| 5 |
+
whisperx @ git+https://github.com/m-bain/whisperx.git
|