Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +29 -36
- main.py +3 -12
- requirements.txt +4 -4
- start.sh +28 -0
Dockerfile
CHANGED
|
@@ -1,42 +1,35 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
cache = os.environ.get("WHISPER_CACHE_DIR", "/home/user/.cache/whisper")
|
| 31 |
-
model = os.environ.get("WHISPER_MODEL","large-v2")
|
| 32 |
-
whisper.load_model(model, download_root=cache)
|
| 33 |
-
print(f"Preloaded model {model} into {cache}")
|
| 34 |
-
PY
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
USER user
|
| 40 |
-
|
| 41 |
-
EXPOSE 7860
|
| 42 |
-
CMD ["uvicorn","main:app","--host","0.0.0.0","--port","7860","--timeout-keep-alive","75"]
|
|
|
|
| 1 |
+
FROM nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04
|
| 2 |
|
| 3 |
+
ENV DEBIAN_FRONTEND=noninteractive \
|
| 4 |
+
PIP_NO_CACHE_DIR=1 \
|
| 5 |
+
PYTHONUNBUFFERED=1 \
|
| 6 |
+
TRANSFORMERS_CACHE=/home/user/.cache/huggingface \
|
| 7 |
+
HF_HOME=/home/user/.cache/huggingface \
|
| 8 |
+
XDG_CACHE_HOME=/home/user/.cache \
|
| 9 |
+
WHISPER_CACHE_DIR=/home/user/.cache/whisper \
|
| 10 |
+
WHISPER_MODEL=tiny \
|
| 11 |
+
MAX_UPLOAD_BYTES=52428800 \
|
| 12 |
+
MAX_CONCURRENT=1
|
| 13 |
|
| 14 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 15 |
+
ffmpeg git python3 python3-pip && rm -rf /var/lib/apt/lists/*
|
| 16 |
|
| 17 |
+
# Non-root runtime user
|
| 18 |
+
RUN useradd -m -u 1000 user
|
| 19 |
+
WORKDIR /app
|
| 20 |
|
| 21 |
+
# ---- Python deps
|
| 22 |
+
COPY requirements.txt /app/requirements.txt
|
| 23 |
+
RUN python3 -m pip install --upgrade pip \
|
| 24 |
+
&& python3 -m pip install --no-cache-dir --index-url https://download.pytorch.org/whl/cu121 torch==2.3.1+cu121 \
|
| 25 |
+
&& python3 -m pip install --no-cache-dir -r /app/requirements.txt
|
| 26 |
|
| 27 |
+
# App code
|
| 28 |
+
COPY main.py /app/main.py
|
| 29 |
+
COPY start.sh /app/start.sh
|
| 30 |
+
RUN chmod +x /app/start.sh && chown -R user:user /app /home/user
|
| 31 |
+
USER user
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
EXPOSE 7860
|
| 34 |
+
# Warm model cache at runtime if missing, then launch
|
| 35 |
+
ENTRYPOINT ["/app/start.sh"]
|
|
|
|
|
|
|
|
|
|
|
|
main.py
CHANGED
|
@@ -49,7 +49,7 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
| 49 |
fp16 = device == "cuda"
|
| 50 |
log.info(f"Loading Whisper '%s' on %s (fp16=%s)", MODEL_NAME, device, fp16)
|
| 51 |
|
| 52 |
-
# Global model (loaded at process start
|
| 53 |
model = whisper.load_model(MODEL_NAME, device=device, download_root=CACHE_DIR)
|
| 54 |
|
| 55 |
# ------------------------
|
|
@@ -57,7 +57,7 @@ model = whisper.load_model(MODEL_NAME, device=device, download_root=CACHE_DIR)
|
|
| 57 |
# ------------------------
|
| 58 |
app = FastAPI(
|
| 59 |
title="Whisper API",
|
| 60 |
-
version="1.2.
|
| 61 |
openapi_url="/api/v1/stt/openapi.json",
|
| 62 |
docs_url="/api/v1/stt/docs"
|
| 63 |
)
|
|
@@ -86,40 +86,32 @@ def home():
|
|
| 86 |
|
| 87 |
@app.get("/healthz")
|
| 88 |
def healthz():
|
| 89 |
-
# cheap check: returns device and cuda mem if available
|
| 90 |
info = {"device": device}
|
| 91 |
if device == "cuda":
|
| 92 |
try:
|
| 93 |
-
info["cuda_mem_allocated"] = torch.cuda.memory_allocated()
|
| 94 |
info["cuda_mem_reserved"] = torch.cuda.memory_reserved()
|
| 95 |
except Exception:
|
| 96 |
pass
|
| 97 |
return {"ok": True, **info}
|
| 98 |
|
| 99 |
-
# ---- helpers ----
|
| 100 |
def _safe_tmp(suffix: str = ".wav") -> str:
|
| 101 |
fd, path = tempfile.mkstemp(prefix="aud_", suffix=suffix, dir="/tmp")
|
| 102 |
os.close(fd)
|
| 103 |
return path
|
| 104 |
|
| 105 |
def _ffmpeg_norm(src: str, dst: str):
|
| 106 |
-
# Force mono/16k PCM WAV for maximum stability
|
| 107 |
cmd = ["ffmpeg","-y","-i",src,"-ac","1","-ar","16000","-f","wav",dst]
|
| 108 |
r = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 109 |
if r.returncode != 0:
|
| 110 |
raise RuntimeError(r.stderr.decode(errors="ignore")[:400])
|
| 111 |
|
| 112 |
-
# ------------------------
|
| 113 |
-
# Transcription endpoint
|
| 114 |
-
# ------------------------
|
| 115 |
@app.post("/transcribe")
|
| 116 |
async def transcribir_audio(file: UploadFile = File(...), _: bool = Depends(verify_token)):
|
| 117 |
-
# Read whole body (keeps code simple, you can stream if needed)
|
| 118 |
raw = await file.read()
|
| 119 |
if len(raw) > MAX_BYTES:
|
| 120 |
raise HTTPException(413, f"Archivo excede {MAX_BYTES/1024/1024:.1f} MB")
|
| 121 |
|
| 122 |
-
# Use safe temp names (avoid path traversal using user filename)
|
| 123 |
src_suffix = os.path.splitext(file.filename or "")[1] or ".bin"
|
| 124 |
src = _safe_tmp(src_suffix)
|
| 125 |
with open(src, "wb") as f:
|
|
@@ -160,7 +152,6 @@ async def transcribir_audio(file: UploadFile = File(...), _: bool = Depends(veri
|
|
| 160 |
except Exception:
|
| 161 |
pass
|
| 162 |
|
| 163 |
-
# Local run: uvicorn main:app --host 0.0.0.0 --port 7860
|
| 164 |
if __name__ == "__main__":
|
| 165 |
import uvicorn
|
| 166 |
uvicorn.run("main:app", host="0.0.0.0", port=int(os.getenv("PORT", "7860")))
|
|
|
|
| 49 |
fp16 = device == "cuda"
|
| 50 |
log.info(f"Loading Whisper '%s' on %s (fp16=%s)", MODEL_NAME, device, fp16)
|
| 51 |
|
| 52 |
+
# Global model (loaded at process start; cache is prewarmed by start.sh)
|
| 53 |
model = whisper.load_model(MODEL_NAME, device=device, download_root=CACHE_DIR)
|
| 54 |
|
| 55 |
# ------------------------
|
|
|
|
| 57 |
# ------------------------
|
| 58 |
app = FastAPI(
|
| 59 |
title="Whisper API",
|
| 60 |
+
version="1.2.1",
|
| 61 |
openapi_url="/api/v1/stt/openapi.json",
|
| 62 |
docs_url="/api/v1/stt/docs"
|
| 63 |
)
|
|
|
|
| 86 |
|
| 87 |
@app.get("/healthz")
|
| 88 |
def healthz():
|
|
|
|
| 89 |
info = {"device": device}
|
| 90 |
if device == "cuda":
|
| 91 |
try:
|
| 92 |
+
info["cuda_mem_allocated"] = torch.cuda.memory_allocated()
|
| 93 |
info["cuda_mem_reserved"] = torch.cuda.memory_reserved()
|
| 94 |
except Exception:
|
| 95 |
pass
|
| 96 |
return {"ok": True, **info}
|
| 97 |
|
|
|
|
| 98 |
def _safe_tmp(suffix: str = ".wav") -> str:
|
| 99 |
fd, path = tempfile.mkstemp(prefix="aud_", suffix=suffix, dir="/tmp")
|
| 100 |
os.close(fd)
|
| 101 |
return path
|
| 102 |
|
| 103 |
def _ffmpeg_norm(src: str, dst: str):
|
|
|
|
| 104 |
cmd = ["ffmpeg","-y","-i",src,"-ac","1","-ar","16000","-f","wav",dst]
|
| 105 |
r = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 106 |
if r.returncode != 0:
|
| 107 |
raise RuntimeError(r.stderr.decode(errors="ignore")[:400])
|
| 108 |
|
|
|
|
|
|
|
|
|
|
| 109 |
@app.post("/transcribe")
|
| 110 |
async def transcribir_audio(file: UploadFile = File(...), _: bool = Depends(verify_token)):
|
|
|
|
| 111 |
raw = await file.read()
|
| 112 |
if len(raw) > MAX_BYTES:
|
| 113 |
raise HTTPException(413, f"Archivo excede {MAX_BYTES/1024/1024:.1f} MB")
|
| 114 |
|
|
|
|
| 115 |
src_suffix = os.path.splitext(file.filename or "")[1] or ".bin"
|
| 116 |
src = _safe_tmp(src_suffix)
|
| 117 |
with open(src, "wb") as f:
|
|
|
|
| 152 |
except Exception:
|
| 153 |
pass
|
| 154 |
|
|
|
|
| 155 |
if __name__ == "__main__":
|
| 156 |
import uvicorn
|
| 157 |
uvicorn.run("main:app", host="0.0.0.0", port=int(os.getenv("PORT", "7860")))
|
requirements.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
fastapi==0.
|
| 2 |
-
uvicorn==0.
|
| 3 |
python-multipart==0.0.9
|
| 4 |
-
torch>=2.0.0
|
| 5 |
openai-whisper==20231117
|
| 6 |
-
|
|
|
|
|
|
| 1 |
+
fastapi==0.111.0
|
| 2 |
+
uvicorn==0.30.6
|
| 3 |
python-multipart==0.0.9
|
|
|
|
| 4 |
openai-whisper==20231117
|
| 5 |
+
# torch is installed separately for CUDA 12.1 (cu121) in Dockerfile
|
| 6 |
+
# ffmpeg provided by OS package in Docker image
|
start.sh
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
set -euo pipefail
|
| 3 |
+
|
| 4 |
+
export HOME=/home/user
|
| 5 |
+
export TRANSFORMERS_CACHE=${TRANSFORMERS_CACHE:-/home/user/.cache/huggingface}
|
| 6 |
+
export HF_HOME=${HF_HOME:-/home/user/.cache/huggingface}
|
| 7 |
+
export XDG_CACHE_HOME=${XDG_CACHE_HOME:-/home/user/.cache}
|
| 8 |
+
export WHISPER_CACHE_DIR=${WHISPER_CACHE_DIR:-/home/user/.cache/whisper}
|
| 9 |
+
export WHISPER_MODEL=${WHISPER_MODEL:-large-v2}
|
| 10 |
+
|
| 11 |
+
mkdir -p "$TRANSFORMERS_CACHE" "$WHISPER_CACHE_DIR"
|
| 12 |
+
|
| 13 |
+
echo "[startup] Checking Whisper model cache for $WHISPER_MODEL ..."
|
| 14 |
+
python3 - <<'PY'
|
| 15 |
+
import os, glob, whisper
|
| 16 |
+
cache = os.environ.get("WHISPER_CACHE_DIR", "/home/user/.cache/whisper")
|
| 17 |
+
model = os.environ.get("WHISPER_MODEL", "large-v2")
|
| 18 |
+
# Simple presence check (whisper stores model files under cache dir)
|
| 19 |
+
existing = glob.glob(os.path.join(cache, "*"))
|
| 20 |
+
if existing:
|
| 21 |
+
print(f"[startup] Cache has {len(existing)} entries in {cache}")
|
| 22 |
+
else:
|
| 23 |
+
print(f"[startup] Preloading model {model} into {cache} (first run)...")
|
| 24 |
+
whisper.load_model(model, download_root=cache)
|
| 25 |
+
print("[startup] Preload done")
|
| 26 |
+
PY
|
| 27 |
+
|
| 28 |
+
exec uvicorn main:app --host 0.0.0.0 --port 7860 --timeout-keep-alive 75
|