v-e-n-o-m commited on
Commit
d405224
·
1 Parent(s): c4b6df5

Fix cache permission error for Whisper API

Browse files
Files changed (4) hide show
  1. .dockerignore +9 -0
  2. Dockerfile +6 -0
  3. app.py +9 -3
  4. requirements.txt +2 -1
.dockerignore ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ __pycache__
2
+ *.pyc
3
+ *.pyo
4
+ *.pyd
5
+ .Python
6
+ env/
7
+ venv/
8
+ .env
9
+ *.log
Dockerfile CHANGED
@@ -2,6 +2,12 @@ FROM python:3.10-slim
2
 
3
  WORKDIR /app
4
 
 
 
 
 
 
 
5
  COPY requirements.txt .
6
  RUN pip install --no-cache-dir -r requirements.txt
7
 
 
2
 
3
  WORKDIR /app
4
 
5
+ # Install ffmpeg for audio processing
6
+ RUN apt-get update && apt-get install -y ffmpeg && rm -rf /var/lib/apt/lists/*
7
+
8
+ # Create cache directory
9
+ RUN mkdir -p /app/cache && chmod -R 777 /app/cache
10
+
11
  COPY requirements.txt .
12
  RUN pip install --no-cache-dir -r requirements.txt
13
 
app.py CHANGED
@@ -4,15 +4,21 @@ import torch
4
  import soundfile as sf
5
  import io
6
  import numpy as np
 
7
 
8
  app = FastAPI()
9
 
10
- # Initialize Whisper pipeline (loaded once at startup)
 
 
 
 
11
  pipe = pipeline(
12
  "automatic-speech-recognition",
13
  model="openai/whisper-large-v3",
14
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
15
- device="cuda" if torch.cuda.is_available() else "cpu",
 
16
  )
17
 
18
  @app.post("/transcribe")
 
4
  import soundfile as sf
5
  import io
6
  import numpy as np
7
+ import os
8
 
9
  app = FastAPI()
10
 
11
+ # Set cache directory
12
+ os.environ["TRANSFORMERS_CACHE"] = "/app/cache"
13
+ os.environ["HF_HOME"] = "/app/cache"
14
+
15
+ # Initialize Whisper pipeline (CPU, lower memory)
16
  pipe = pipeline(
17
  "automatic-speech-recognition",
18
  model="openai/whisper-large-v3",
19
+ torch_dtype=torch.float32, # CPU compatibility
20
+ device="cpu",
21
+ model_kwargs={"use_safetensors": True},
22
  )
23
 
24
  @app.post("/transcribe")
requirements.txt CHANGED
@@ -3,4 +3,5 @@ uvicorn==0.23.2
3
  transformers==4.38.2
4
  torch==2.0.1
5
  soundfile==0.12.1
6
- numpy==1.24.3
 
 
3
  transformers==4.38.2
4
  torch==2.0.1
5
  soundfile==0.12.1
6
+ numpy==1.24.3
7
+ librosa==0.10.1