Modification of the STT structure

This commit is contained in:
2026-09-22 11:56:16 +03:30
parent 42db5919e1
commit 41f0025883
2 changed files with 47 additions and 16 deletions
+26 -15
View File
@@ -177,27 +177,38 @@ def _wav_to_float32(wav_bytes: bytes) -> tuple[np.ndarray, int]:
return audio, rate
def _resample_audio(audio: np.ndarray, src_rate: int, dst_rate: int = 16000) -> np.ndarray:
if src_rate == dst_rate or audio.size == 0:
return audio.astype(np.float32, copy=False)
duration = audio.shape[0] / float(src_rate)
new_len = max(1, int(round(duration * dst_rate)))
x_old = np.linspace(0.0, 1.0, num=audio.shape[0], endpoint=False)
x_new = np.linspace(0.0, 1.0, num=new_len, endpoint=False)
return np.interp(x_new, x_old, audio).astype(np.float32)
def _ensure_16k(wav_bytes: bytes) -> np.ndarray:
audio, rate = _wav_to_float32(wav_bytes)
if rate == 16000:
return audio
# Prefer ffmpeg when available; otherwise pure-numpy resample (browser often sends 44.1/48 kHz)
ffmpeg = shutil.which("ffmpeg")
if not ffmpeg:
raise HTTPException(status_code=400, detail="نرخ نمونه‌برداری صوت پشتیبانی نشد")
with tempfile.TemporaryDirectory() as tmp:
src = Path(tmp) / "in.wav"
dst = Path(tmp) / "out.wav"
src.write_bytes(wav_bytes)
result = subprocess.run(
[ffmpeg, "-y", "-i", str(src), "-ac", "1", "-ar", "16000", "-f", "wav", str(dst)],
capture_output=True,
check=False,
)
if result.returncode != 0 or not dst.exists():
raise HTTPException(status_code=400, detail="نرمال‌سازی صوت ناموفق بود")
audio, _ = _wav_to_float32(dst.read_bytes())
return audio
if ffmpeg:
with tempfile.TemporaryDirectory() as tmp:
src = Path(tmp) / "in.wav"
dst = Path(tmp) / "out.wav"
src.write_bytes(wav_bytes)
result = subprocess.run(
[ffmpeg, "-y", "-i", str(src), "-ac", "1", "-ar", "16000", "-f", "wav", str(dst)],
capture_output=True,
check=False,
)
if result.returncode == 0 and dst.exists():
audio, _ = _wav_to_float32(dst.read_bytes())
return audio
return _resample_audio(audio, rate, 16000)
def _transcribe_local(wav_bytes: bytes) -> str: