Kennethdot commited on
Commit
00a2f64
Β·
verified Β·
1 Parent(s): 157f981

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +8 -6
api.py CHANGED
@@ -15,6 +15,7 @@ from fastapi.responses import FileResponse, JSONResponse
15
  from fastapi.staticfiles import StaticFiles
16
  from huggingface_hub import HfApi, create_repo, upload_file
17
  from transformers import WhisperForConditionalGeneration, WhisperProcessor
 
18
 
19
  # ── Env ────────────────────────────────────────────────────────────────────
20
  load_dotenv()
@@ -79,12 +80,10 @@ app = FastAPI(title="Kasanoma ASR", version="2.0.0")
79
  _csv_lock = threading.Lock()
80
 
81
  # ── Helpers ────────────────────────────────────────────────────────────────
82
-
83
  def audio_duration(path: Path) -> float:
84
- """Return audio duration in seconds; falls back to 0.0 on error."""
85
  try:
86
- data, sr = sf.read(str(path), always_2d=False)
87
- return round(len(data) / sr, 2)
88
  except Exception:
89
  return 0.0
90
 
@@ -140,8 +139,11 @@ def transcribe_path(audio_path: Path) -> tuple[str, str]:
140
  """
141
  model, processor, device = get_model()
142
 
143
- # Read + convert
144
- audio_data, sampling_rate = sf.read(str(audio_path))
 
 
 
145
 
146
  # Stereo β†’ mono
147
  if len(audio_data.shape) > 1:
 
15
  from fastapi.staticfiles import StaticFiles
16
  from huggingface_hub import HfApi, create_repo, upload_file
17
  from transformers import WhisperForConditionalGeneration, WhisperProcessor
18
+ import librosa
19
 
20
  # ── Env ────────────────────────────────────────────────────────────────────
21
  load_dotenv()
 
80
  _csv_lock = threading.Lock()
81
 
82
  # ── Helpers ────────────────────────────────────────────────────────────────
 
83
  def audio_duration(path: Path) -> float:
 
84
  try:
85
+ y, sr = librosa.load(str(path), sr=None, mono=True)
86
+ return round(len(y) / sr, 2)
87
  except Exception:
88
  return 0.0
89
 
 
139
  """
140
  model, processor, device = get_model()
141
 
142
+ audio_data, sampling_rate = librosa.load(str(audio_path), sr=16000, mono=True)
143
+
144
+ # Normalise
145
+ if np.max(np.abs(audio_data)) > 0:
146
+ audio_data = audio_data / np.max(np.abs(audio_data))
147
 
148
  # Stereo β†’ mono
149
  if len(audio_data.shape) > 1: