Spaces:
Sleeping
Sleeping
Download src/models/speech_gate.py from ConsulawTechnologies/NarratifAI: direct link, hf CLI and curl.
- Browser
- Download file 1 kB
-
https://huggingface.co/spaces/ConsulawTechnologies/NarratifAI/resolve/main/src/models/speech_gate.py
- Command line
-
hf download hf://spaces/ConsulawTechnologies/NarratifAI/src/models/speech_gate.py
-
curl -L -o speech_gate.py https://huggingface.co/spaces/ConsulawTechnologies/NarratifAI/resolve/main/src/models/speech_gate.py
1 kB
| """Conservative speech detection for the opt-in staging candidate. | |
| No absolute loudness cutoff: normalize a detection-only copy so quiet speech | |
| is assessed by its shape. Decode the original waveform, not boosted noise. | |
| """ | |
| import numpy as np | |
| def speech_regions(audio, sr=16000): | |
| if sr != 16000: | |
| raise ValueError('Speech gate requires 16 kHz audio') | |
| audio = np.asarray(audio, dtype=np.float32) | |
| if audio.ndim != 1 or not np.isfinite(audio).all(): | |
| raise ValueError('Speech gate requires finite mono audio') | |
| if not audio.size or not np.any(audio): | |
| return [] | |
| from faster_whisper.vad import get_speech_timestamps, VadOptions | |
| peak = float(np.max(np.abs(audio))) | |
| detection_audio = audio * (0.5 / peak) if peak < 0.5 else audio | |
| return get_speech_timestamps(detection_audio, VadOptions( | |
| threshold=0.35, min_speech_duration_ms=100, | |
| min_silence_duration_ms=500, speech_pad_ms=250, | |
| max_speech_duration_s=28, | |
| ), sampling_rate=sr) | |