ielts-grader-api / src /explore_speaking.py
diminch's picture
Deploy V15 Clean (Removed binary files history)
d939bae
import whisper
import librosa
import numpy as np
import os
import warnings
warnings.filterwarnings("ignore")
def analyze_speaking_audio(audio_path):
print(f"🎤 Đang phân tích file: {audio_path}")
# --- 1. Load Model Whisper (ASR) ---
print("⏳ Đang tải model Whisper (có thể lâu lần đầu)...")
model = whisper.load_model("base")
# --- 2. Transcribe (Chuyển giọng thành chữ) ---
print("📝 Đang chuyển đổi giọng nói...")
result = model.transcribe(audio_path, fp16=False)
transcript = result["text"].strip()
print("\n" + "="*40)
print("TRANSCRIPT:")
print(f"'{transcript}'")
print("="*40 + "\n")
# --- 3. Phân tích Fluency (Trôi chảy) ---
# Dùng librosa để phân tích tín hiệu âm thanh
y, sr = librosa.load(audio_path)
duration = librosa.get_duration(y=y, sr=sr)
# Đếm số từ
word_count = len(transcript.split())
# Tính tốc độ nói (Words Per Minute - WPM)
wpm = (word_count / duration) * 60
# Phát hiện khoảng lặng (Pauses)
# top_db: ngưỡng decibel để coi là im lặng
non_silent_intervals = librosa.effects.split(y, top_db=20)
silent_duration = duration - sum([ (end-start)/sr for start, end in non_silent_intervals ])
pause_ratio = silent_duration / duration
print("ACOUSTIC METRICS:")
print(f"- Thời lượng (Duration): {duration:.2f} giây")
print(f"- Số từ (Word Count): {word_count}")
print(f"- Tốc độ (Speed): {wpm:.2f} WPM (Chuẩn IELTS 6.0+ thường > 100 WPM)")
print(f"- Thời gian im lặng: {silent_duration:.2f} giây ({pause_ratio*100:.1f}%)")
# --- 4. Đánh giá sơ bộ ---
fluency_score_est = 0
if wpm > 120: fluency_score_est = 7.0
elif wpm > 100: fluency_score_est = 6.0
elif wpm > 80: fluency_score_est = 5.0
else: fluency_score_est = 4.0
print(f"\n💡 Đánh giá sơ bộ Fluency: ~{fluency_score_est}")
return {
"transcript": transcript,
"wpm": wpm,
"pause_ratio": pause_ratio
}
if __name__ == "__main__":
sample_audio = "data/test_speaking.m4a"
if os.path.exists(sample_audio):
analyze_speaking_audio(sample_audio)
else:
print(f"Không tìm thấy file '{sample_audio}'.")
print("Hãy tạo một file ghi âm tiếng Anh, lưu vào đó và chạy lại.")