-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlanguage_detector.py
22 lines (21 loc) · 954 Bytes
/
language_detector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import whisper
from langdetect import detect, LangDetectException
def detect_language(model, audio, max_duration=300):
start_duration = 60
while start_duration <= max_duration:
print(f"Tentando detectar idioma com {start_duration} segundos de áudio...")
segment = whisper.pad_or_trim(audio[:start_duration * whisper.audio.SAMPLE_RATE])
mel = whisper.log_mel_spectrogram(segment).to(model.device)
options = whisper.DecodingOptions(without_timestamps=True)
result = whisper.decode(model, mel, options)
text = result.text.strip()
if text:
try:
detected_language = detect(text)
print(f"Idioma detectado: {detected_language}")
return detected_language
except LangDetectException:
pass
start_duration += 30
print("Não foi possível detectar o idioma. Definindo como inglês.")
return 'en'