Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
from fastapi import FastAPI, UploadFile, File, HTTPException, Request
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
| 5 |
-
import
|
| 6 |
from pathlib import Path
|
| 7 |
import tempfile, subprocess, os, shutil, base64
|
| 8 |
|
|
@@ -22,8 +22,7 @@ DEVICE = "cpu"
|
|
| 22 |
COMPUTE = "int8"
|
| 23 |
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
model = whisper.load_model(MODEL_NAME, device=DEVICE)
|
| 27 |
except Exception as e:
|
| 28 |
raise RuntimeError(f"Failed to load Whisper model: {e}")
|
| 29 |
|
|
@@ -54,8 +53,9 @@ def _cleanup(paths):
|
|
| 54 |
|
| 55 |
def _do_transcribe(wav_path: str):
|
| 56 |
try:
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
raise HTTPException(status_code=500, detail=f"ASR error: {e}")
|
| 61 |
|
|
@@ -107,4 +107,4 @@ async def transcribe_b64(payload: AudioB64):
|
|
| 107 |
async def echo(request: Request, file: UploadFile | None = File(default=None)):
|
| 108 |
ct = request.headers.get("content-type")
|
| 109 |
name = getattr(file, "filename", None) if file else None
|
| 110 |
-
return {"has_file": file is not None, "filename": name, "content_type": ct}
|
|
|
|
| 2 |
from fastapi import FastAPI, UploadFile, File, HTTPException, Request
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
| 5 |
+
from faster_whisper import WhisperModel
|
| 6 |
from pathlib import Path
|
| 7 |
import tempfile, subprocess, os, shutil, base64
|
| 8 |
|
|
|
|
| 22 |
COMPUTE = "int8"
|
| 23 |
|
| 24 |
try:
|
| 25 |
+
model = WhisperModel(MODEL_NAME, device=DEVICE, compute_type=COMPUTE)
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
raise RuntimeError(f"Failed to load Whisper model: {e}")
|
| 28 |
|
|
|
|
| 53 |
|
| 54 |
def _do_transcribe(wav_path: str):
|
| 55 |
try:
|
| 56 |
+
segments, info = model.transcribe(wav_path, vad_filter=True)
|
| 57 |
+
text = "".join(s.text for s in segments)
|
| 58 |
+
return {"text": text, "language": info.language, "duration": info.duration}
|
| 59 |
except Exception as e:
|
| 60 |
raise HTTPException(status_code=500, detail=f"ASR error: {e}")
|
| 61 |
|
|
|
|
| 107 |
async def echo(request: Request, file: UploadFile | None = File(default=None)):
|
| 108 |
ct = request.headers.get("content-type")
|
| 109 |
name = getattr(file, "filename", None) if file else None
|
| 110 |
+
return {"has_file": file is not None, "filename": name, "content_type": ct}
|