Fast_api / vers /vers_api.py
mulasagg's picture
API optimizations
aef3b1e
import whisper
import numpy as np
from .compute_vers_score import compute_vers_score
def convert_numpy_types(obj):
"""Convert NumPy types to Python native types for JSON serialization."""
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
elif isinstance(obj, dict):
return {k: convert_numpy_types(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [convert_numpy_types(i) for i in obj]
else:
return obj
def main(file_path: str, model_size: str = "base", filler_count = None) -> dict:
try:
# Load whisper model
whisper_model = whisper.load_model(model_size)
# Compute VERS score
results = compute_vers_score(file_path, whisper_model, filler_count)
# Convert any NumPy types to native Python types
results = convert_numpy_types(results)
# Structure response with rounded values
# (using Python's built-in round function which returns Python native float)
response = {
"VERS Score": round(results['VERS'], 2)
# "ESS": round(results['ESS'], 2),
# "LCS": round(results['LCS'], 2),
# "SRS": round(results['SRS'], 2),
# "Insight": results['insight'],
}
return response
except Exception as e:
raise RuntimeError(f"Error during analysis: {str(e)}")