Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,27 +7,30 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 7 |
from fastapi.responses import JSONResponse
|
| 8 |
from pydantic import BaseModel
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
os.environ.setdefault("HF_HOME", "/
|
| 12 |
-
os.environ.setdefault("HUGGINGFACE_HUB_CACHE", "/
|
| 13 |
-
os.environ.setdefault("XDG_CACHE_HOME", "/
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
app = FastAPI(title="Nuvia Free Transcriber", version="1.
|
| 23 |
app.add_middleware(
|
| 24 |
CORSMiddleware,
|
| 25 |
allow_origins=["*"], allow_credentials=True,
|
| 26 |
allow_methods=["*"], allow_headers=["*"],
|
| 27 |
)
|
| 28 |
|
| 29 |
-
# Load
|
| 30 |
-
model = WhisperModel(MODEL_NAME, device="cpu", compute_type="int8")
|
| 31 |
|
| 32 |
class HealthOut(BaseModel):
|
| 33 |
ok: bool
|
|
@@ -61,13 +64,13 @@ def health():
|
|
| 61 |
@app.post("/transcribe", response_model=TranscribeOut)
|
| 62 |
async def transcribe(file: UploadFile = File(...)):
|
| 63 |
# Save upload
|
| 64 |
-
raw = await file.read()
|
| 65 |
ext = os.path.splitext(file.filename or "")[1].lower() or ".bin"
|
| 66 |
tmp_in = f"/tmp/in{ext}"
|
|
|
|
| 67 |
with open(tmp_in, "wb") as f:
|
| 68 |
f.write(raw)
|
| 69 |
|
| 70 |
-
# Convert to mono 16k
|
| 71 |
tmp_wav = "/tmp/in.wav"
|
| 72 |
try:
|
| 73 |
subprocess.check_call(
|
|
@@ -75,13 +78,13 @@ async def transcribe(file: UploadFile = File(...)):
|
|
| 75 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
| 76 |
)
|
| 77 |
except subprocess.CalledProcessError:
|
| 78 |
-
return JSONResponse(status_code=400, content={"error": "ffmpeg failed to decode
|
| 79 |
|
| 80 |
duration = ffprobe_duration(tmp_wav)
|
| 81 |
|
| 82 |
# Transcribe
|
| 83 |
segments, _ = model.transcribe(tmp_wav, language="en")
|
| 84 |
-
text = "".join(
|
| 85 |
|
| 86 |
wpm = None
|
| 87 |
if duration and duration > 0:
|
|
|
|
| 7 |
from fastapi.responses import JSONResponse
|
| 8 |
from pydantic import BaseModel
|
| 9 |
|
| 10 |
+
# ===== Writable caches (persist across restarts) =====
|
| 11 |
+
os.environ.setdefault("HF_HOME", "/data/hf")
|
| 12 |
+
os.environ.setdefault("HUGGINGFACE_HUB_CACHE", "/data/hf")
|
| 13 |
+
os.environ.setdefault("XDG_CACHE_HOME", "/data/.cache")
|
| 14 |
+
for p in ("/data", "/data/hf", "/data/.cache", "/tmp"):
|
| 15 |
+
try:
|
| 16 |
+
os.makedirs(p, exist_ok=True)
|
| 17 |
+
os.chmod(p, 0o777)
|
| 18 |
+
except Exception:
|
| 19 |
+
pass
|
| 20 |
|
| 21 |
+
# ===== Transcriber (CPU) =====
|
| 22 |
+
from faster_whisper import WhisperModel
|
| 23 |
+
MODEL_NAME = os.environ.get("WHISPER_MODEL", "tiny.en")
|
| 24 |
|
| 25 |
+
app = FastAPI(title="Nuvia Free Transcriber", version="1.2.0")
|
| 26 |
app.add_middleware(
|
| 27 |
CORSMiddleware,
|
| 28 |
allow_origins=["*"], allow_credentials=True,
|
| 29 |
allow_methods=["*"], allow_headers=["*"],
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# Load from /data to avoid runtime downloads
|
| 33 |
+
model = WhisperModel(MODEL_NAME, device="cpu", compute_type="int8", download_root="/data/hf")
|
| 34 |
|
| 35 |
class HealthOut(BaseModel):
|
| 36 |
ok: bool
|
|
|
|
| 64 |
@app.post("/transcribe", response_model=TranscribeOut)
|
| 65 |
async def transcribe(file: UploadFile = File(...)):
|
| 66 |
# Save upload
|
|
|
|
| 67 |
ext = os.path.splitext(file.filename or "")[1].lower() or ".bin"
|
| 68 |
tmp_in = f"/tmp/in{ext}"
|
| 69 |
+
raw = await file.read()
|
| 70 |
with open(tmp_in, "wb") as f:
|
| 71 |
f.write(raw)
|
| 72 |
|
| 73 |
+
# Convert to mono 16k WAV
|
| 74 |
tmp_wav = "/tmp/in.wav"
|
| 75 |
try:
|
| 76 |
subprocess.check_call(
|
|
|
|
| 78 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
| 79 |
)
|
| 80 |
except subprocess.CalledProcessError:
|
| 81 |
+
return JSONResponse(status_code=400, content={"error": "ffmpeg failed to decode audio"})
|
| 82 |
|
| 83 |
duration = ffprobe_duration(tmp_wav)
|
| 84 |
|
| 85 |
# Transcribe
|
| 86 |
segments, _ = model.transcribe(tmp_wav, language="en")
|
| 87 |
+
text = "".join(seg.text for seg in segments).strip()
|
| 88 |
|
| 89 |
wpm = None
|
| 90 |
if duration and duration > 0:
|