|
|
import os |
|
|
from fastapi import FastAPI, UploadFile, File, HTTPException |
|
|
from fastapi.middleware.cors import CORSMiddleware |
|
|
import whisper |
|
|
|
|
|
|
|
|
os.environ["XDG_CACHE_HOME"] = "/tmp/.cache" |
|
|
|
|
|
|
|
|
UPLOAD_DIR = "uploads" |
|
|
os.makedirs(UPLOAD_DIR, exist_ok=True) |
|
|
|
|
|
|
|
|
app = FastAPI(title="Whisper Audio Transcription API") |
|
|
|
|
|
|
|
|
app.add_middleware( |
|
|
CORSMiddleware, |
|
|
allow_origins=["*"], |
|
|
allow_credentials=True, |
|
|
allow_methods=["*"], |
|
|
allow_headers=["*"], |
|
|
) |
|
|
|
|
|
|
|
|
try: |
|
|
model = whisper.load_model("small") |
|
|
except Exception as e: |
|
|
raise RuntimeError(f"Error loading Whisper model: {str(e)}") |
|
|
|
|
|
|
|
|
@app.post("/transcribe/") |
|
|
async def transcribe_audio(file: UploadFile = File(...)): |
|
|
""" |
|
|
Upload an audio file and get the transcription. |
|
|
""" |
|
|
try: |
|
|
|
|
|
file_path = os.path.join(UPLOAD_DIR, file.filename) |
|
|
with open(file_path, "wb") as f: |
|
|
f.write(await file.read()) |
|
|
|
|
|
|
|
|
result = model.transcribe(file_path) |
|
|
|
|
|
|
|
|
os.remove(file_path) |
|
|
|
|
|
|
|
|
return {"filename": file.filename, "transcription": result.get("text", "")} |
|
|
|
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail=f"Transcription failed: {str(e)}") |
|
|
|