Spaces:
Running
Running
eaysu
commited on
Commit
·
c57481a
1
Parent(s):
182c104
initial commit
Browse files- app.py +89 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import psutil
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from resemblyzer import VoiceEncoder, preprocess_wav
|
| 7 |
+
|
| 8 |
+
# Initialize the VoiceEncoder
|
| 9 |
+
encoder = VoiceEncoder()
|
| 10 |
+
|
| 11 |
+
def analyze_voice_similarity(audio_file1, audio_file2):
|
| 12 |
+
start_time = time.time() # Record start time
|
| 13 |
+
|
| 14 |
+
# Get current process information
|
| 15 |
+
process = psutil.Process(os.getpid())
|
| 16 |
+
|
| 17 |
+
# Preprocess audio files
|
| 18 |
+
try:
|
| 19 |
+
wav1 = preprocess_wav(audio_file1)
|
| 20 |
+
wav2 = preprocess_wav(audio_file2)
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return f"Error processing audio files: {str(e)}"
|
| 23 |
+
|
| 24 |
+
# Extract speaker embeddings
|
| 25 |
+
embed1 = encoder.embed_utterance(wav1)
|
| 26 |
+
embed2 = encoder.embed_utterance(wav2)
|
| 27 |
+
|
| 28 |
+
# Calculate cosine similarity between embeddings
|
| 29 |
+
similarity = np.dot(embed1, embed2) / (np.linalg.norm(embed1) * np.linalg.norm(embed2))
|
| 30 |
+
|
| 31 |
+
# Determine if voices are from the same source
|
| 32 |
+
result = "SAME PERSON" if similarity >= 0.80 else "DIFFERENT PEOPLE"
|
| 33 |
+
|
| 34 |
+
# Get memory usage
|
| 35 |
+
memory_usage = process.memory_info().rss / (1024 * 1024) # in MB
|
| 36 |
+
|
| 37 |
+
# Calculate execution time
|
| 38 |
+
execution_time = time.time() - start_time
|
| 39 |
+
|
| 40 |
+
# Format the output
|
| 41 |
+
output = f"""
|
| 42 |
+
### Voice Similarity Analysis Results
|
| 43 |
+
|
| 44 |
+
**Similarity Score**: {similarity:.4f}
|
| 45 |
+
**Conclusion**: {result}
|
| 46 |
+
|
| 47 |
+
### Performance Metrics
|
| 48 |
+
**Memory Usage**: {memory_usage:.2f} MB
|
| 49 |
+
**Execution Time**: {execution_time:.4f} seconds
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
return output
|
| 53 |
+
|
| 54 |
+
# Create Gradio interface
|
| 55 |
+
with gr.Blocks(title="Voice Similarity Analyzer") as demo:
|
| 56 |
+
gr.Markdown("# 🎤 Voice Similarity Analyzer")
|
| 57 |
+
gr.Markdown("Upload two audio files to check if they're from the same person. A similarity score >= 0.80 indicates the same speaker.")
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
with gr.Column():
|
| 61 |
+
audio_input1 = gr.Audio(label="Voice Sample 1", type="filepath")
|
| 62 |
+
with gr.Column():
|
| 63 |
+
audio_input2 = gr.Audio(label="Voice Sample 2", type="filepath")
|
| 64 |
+
|
| 65 |
+
analyze_button = gr.Button("Analyze Voice Similarity", variant="primary")
|
| 66 |
+
output_text = gr.Markdown(label="Results")
|
| 67 |
+
|
| 68 |
+
analyze_button.click(
|
| 69 |
+
fn=analyze_voice_similarity,
|
| 70 |
+
inputs=[audio_input1, audio_input2],
|
| 71 |
+
outputs=output_text
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
gr.Markdown("""
|
| 75 |
+
## How It Works
|
| 76 |
+
1. Upload two voice recordings
|
| 77 |
+
2. Click "Analyze Voice Similarity"
|
| 78 |
+
3. The app will extract voice embeddings using Resemblyzer
|
| 79 |
+
4. The similarity score is calculated using cosine similarity
|
| 80 |
+
5. A score >= 0.80 indicates the same speaker
|
| 81 |
+
|
| 82 |
+
## Performance Metrics
|
| 83 |
+
- Memory usage shows how much RAM is being used
|
| 84 |
+
- Execution time measures how long the comparison takes
|
| 85 |
+
""")
|
| 86 |
+
|
| 87 |
+
# Launch the app
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
resemblyzer
|
| 3 |
+
numpy
|
| 4 |
+
psutil
|