Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import json
|
| 4 |
-
import tempfile
|
| 5 |
-
import threading
|
| 6 |
-
from typing import Optional
|
| 7 |
-
from datetime import datetime, timezone
|
| 8 |
-
|
| 9 |
-
import httpx
|
| 10 |
-
import whisper
|
| 11 |
-
import nltk
|
| 12 |
-
from nltk.tokenize import sent_tokenize
|
| 13 |
-
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
-
|
| 16 |
|
| 17 |
# ---------------- Fix Whisper cache issue ----------------
|
| 18 |
os.environ["XDG_CACHE_HOME"] = "/tmp/.cache"
|
|
@@ -22,7 +11,7 @@ UPLOAD_DIR = "uploads"
|
|
| 22 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 23 |
|
| 24 |
# Initialize FastAPI
|
| 25 |
-
app = FastAPI()
|
| 26 |
|
| 27 |
# Allow CORS for all origins
|
| 28 |
app.add_middleware(
|
|
@@ -33,12 +22,18 @@ app.add_middleware(
|
|
| 33 |
allow_headers=["*"],
|
| 34 |
)
|
| 35 |
|
| 36 |
-
# Load Whisper model (small for speed)
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
@app.post("/transcribe/")
|
| 41 |
async def transcribe_audio(file: UploadFile = File(...)):
|
|
|
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
# Save uploaded file temporarily
|
| 44 |
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
|
@@ -48,8 +43,11 @@ async def transcribe_audio(file: UploadFile = File(...)):
|
|
| 48 |
# Transcribe with Whisper
|
| 49 |
result = model.transcribe(file_path)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
# Return text output
|
| 52 |
-
return {"filename": file.filename, "transcription": result
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 1 |
import os
|
| 2 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
import whisper
|
| 5 |
|
| 6 |
# ---------------- Fix Whisper cache issue ----------------
|
| 7 |
os.environ["XDG_CACHE_HOME"] = "/tmp/.cache"
|
|
|
|
| 11 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 12 |
|
| 13 |
# Initialize FastAPI
|
| 14 |
+
app = FastAPI(title="Whisper Audio Transcription API")
|
| 15 |
|
| 16 |
# Allow CORS for all origins
|
| 17 |
app.add_middleware(
|
|
|
|
| 22 |
allow_headers=["*"],
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# Load Whisper model (use "small" for speed; can change to "base" or "medium")
|
| 26 |
+
try:
|
| 27 |
+
model = whisper.load_model("small")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
raise RuntimeError(f"Error loading Whisper model: {str(e)}")
|
| 30 |
|
| 31 |
|
| 32 |
@app.post("/transcribe/")
|
| 33 |
async def transcribe_audio(file: UploadFile = File(...)):
|
| 34 |
+
"""
|
| 35 |
+
Upload an audio file and get the transcription.
|
| 36 |
+
"""
|
| 37 |
try:
|
| 38 |
# Save uploaded file temporarily
|
| 39 |
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
|
|
|
| 43 |
# Transcribe with Whisper
|
| 44 |
result = model.transcribe(file_path)
|
| 45 |
|
| 46 |
+
# Cleanup (optional: remove after processing)
|
| 47 |
+
os.remove(file_path)
|
| 48 |
+
|
| 49 |
# Return text output
|
| 50 |
+
return {"filename": file.filename, "transcription": result.get("text", "")}
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
+
raise HTTPException(status_code=500, detail=f"Transcription failed: {str(e)}")
|