Fast_api / vers /vers.py
mulasagg's picture
API optimizations
aef3b1e
import numpy as np
def calc_ess(pitch_variation, vol_max_db, mean_volume_db, valence_scores):
"""
Emotional Stability Score(ESS) : Measures the consistency of the speaker's emotional tone, reflecting their ability to regulate emotions during speech.
Requires:
Tonal Steadiness: The lack of extreme fluctuations in emotional tone.
Absence of Sudden Loudness Spikes: Indicates controlled expression without abrupt emotional shifts.
Valence Stability: Consistency in the overall positive or negative tone across the speech.
"""
# calculate tonal steadiness
tonal_steadiness = max(0, 100 - (pitch_variation * 10))
# calculate loudness spikes
spike = max(0, vol_max_db - mean_volume_db - 15)
spike_ratio = min(spike / 30, 1.0) # Normalize with typical loudness range
stability = 1 - spike_ratio
loudness_stability = stability * 100
# calculate valence stability
valence_stability = 100 - (np.std(valence_scores) * 20)
ESS = (0.45 * float(tonal_steadiness)) + (0.35 * float(loudness_stability)) + (0.2 * float(valence_stability))
#print(f" tonal_steadiness: {tonal_steadiness}, loudness_stability: {loudness_stability}, valence_stability: {valence_stability}")
return ESS
def calc_lcs(volume_std, vol_max_db, mean_volume_db):
"""
Loudness Control Score (LCS): Evaluates how well the speaker manages volume
Requires:
- Volume Stability: Consistency in speech amplitude.
- Controlled Emphasis: The ability to modulate loudness smoothly for emphasis rather than abrupt changes.
"""
vol_stability = max(0, 100 - (volume_std * 5)) # Scale std for speech (5 dB std = 75)
# Controlled Emphasis (45%)
emphasis_spike = max(0, vol_max_db - mean_volume_db - 3)
spike_ratio = min(emphasis_spike / 15, 1.0) # Normalize to 15 dB range
emphasis_control = (1 - spike_ratio) * 100
# Combine scores
lcs = 0.55 * vol_stability + 0.45 * emphasis_control
print(f"vol_stability: {vol_stability}, emphasis_control: {emphasis_control}")
return min(100, max(0, lcs))
def calc_srs(wpm, filler_count, long_pause_count, pitch_variation):
"""
Speech Rate Stability (SRS): Reflects the consistency of the speaker's pace and rhythm.
Requires:
- Words per Minute Consistency: Regularity in speech speed.
- Absence of Sudden Speed Shifts: Smooth transitions without erratic tempo changes.
"""
ideal_wpm = 150
wpm_deviation = min(30, abs(wpm - ideal_wpm)) # Cap at 30 WPM deviation
wpm_consistency = max(0, 100 - (wpm_deviation * 1.67)) # 100-50 for max deviation
# Sudden Speech Shift Penalty
filler_penalty = min(filler_count / 10, 1.0)
pause_penalty = min(long_pause_count / 5, 1.0)
pitch_penalty = min(pitch_variation / 3.0, 1.0) # High variation → unstable
# Combine into absence of sudden shifts
stability = (1 - ((filler_penalty + pause_penalty + pitch_penalty) / 3)) * 100
# Final SRS Score
SRS = (0.45 * wpm_consistency) + (0.55 * stability)
#print(f"wpm_consistency: {wpm_consistency}, stability: {stability}")
return min(100, max(0, SRS))
def calc_vers(filler_count, long_pause_count, pitch_variation, mean_volume_db, vol_max_db, wpm, volume_std, valence_scores):
ESS = calc_ess(pitch_variation, vol_max_db, mean_volume_db, valence_scores)
LCS = calc_lcs(volume_std, vol_max_db, mean_volume_db)
SRS = calc_srs(wpm, filler_count, long_pause_count, pitch_variation)
# Calculate the VERS score using the formula
VERS = (0.5 * ESS) + (0.3 * LCS) + (0.2 * SRS) # This would be value from 0 to 100
if VERS > 0 and VERS < 50:
insight = """Poor regulation—noticeable swings in tone and uncontrolled
emotional expression. Feedback: Consider exercises and professional
coaching to stabilize your emotional delivery."""
elif VERS >= 50 and VERS < 80:
insight = """Moderate regulation—occasional fluctuations or abrupt changes.
Feedback: Work on smoothing out volume changes and maintaining a steady tone."""
elif VERS >= 80 and VERS <= 100:
insight = """Excellent regulation—steady tone and controlled volume dynamics.
Feedback: Continue using techniques that maintain emotional balance."""
else:
insight = "Invalid score calculated"
return {
"VERS": int(VERS),
# "ESS": round(ESS, 1),
# "LCS": round(LCS, 1),
# "SRS": round(SRS, 1),
# "insight": insight
}
# # Test input
# test_result = calc_vers(
# filler_count=4,
# long_pause_count=2,
# pitch_variation=3.2,
# mean_volume_db=65,
# vol_max_db=82,
# wpm=148,
# volume_std=4.1,
# valence_scores=[5.2, 5.5, 4.9]
# )
# print("VERS Score:", test_result["VERS"])
# print("ESS:", test_result["ESS"])
# print("LCS:", test_result["LCS"])
# print("SRS:", test_result["SRS"])
# print("Insight:", test_result["insight"])