Spaces:
Sleeping
Sleeping
File size: 2,452 Bytes
d939bae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
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.") |