| from resemblyzer import VoiceEncoder, preprocess_wav | |
| import numpy as np | |
| import os | |
| encoder = VoiceEncoder() | |
| def verify_voices(audio_path1, audio_path2, threshold=0.75): | |
| """ | |
| Compares two audio files and returns whether they belong to the same speaker. | |
| """ | |
| try: | |
| audio1 = preprocess_wav(audio_path1) | |
| audio2 = preprocess_wav(audio_path2) | |
| embedding1 = encoder.embed_utterance(audio1) | |
| embedding2 = encoder.embed_utterance(audio2) | |
| similarity = np.dot(embedding1, embedding2) / ( | |
| np.linalg.norm(embedding1) * np.linalg.norm(embedding2) | |
| ) | |
| match = similarity > threshold | |
| return { | |
| "match": match, | |
| "similarity": round(float(similarity), 4), | |
| "threshold": threshold, | |
| } | |
| except Exception as e: | |
| return {"error": str(e)} | |