Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,12 +5,30 @@ import soundfile as sf
|
|
5 |
from sklearn.preprocessing import StandardScaler
|
6 |
from sklearn.cluster import KMeans
|
7 |
from transformers import pipeline
|
8 |
-
import noisereduce as nr
|
|
|
9 |
|
10 |
print("Chargement du modèle Wav2Vec2...")
|
11 |
stt_pipeline = pipeline("automatic-speech-recognition", model="boumehdi/wav2vec2-large-xlsr-moroccan-darija")
|
12 |
print("Modèle chargé avec succès !")
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def process_audio(audio_path):
|
15 |
print(f"Fichier reçu : {audio_path}")
|
16 |
|
@@ -19,11 +37,11 @@ def process_audio(audio_path):
|
|
19 |
audio, sr = librosa.load(audio_path, sr=None, duration=30)
|
20 |
print(f"Audio chargé : {len(audio)} échantillons à {sr} Hz")
|
21 |
|
22 |
-
# Réduction du bruit (
|
23 |
audio_denoised = nr.reduce_noise(y=audio, sr=sr)
|
24 |
print("Bruit réduit.")
|
25 |
|
26 |
-
# Extraction des MFCC
|
27 |
mfccs = librosa.feature.mfcc(y=audio_denoised, sr=sr, n_mfcc=13)
|
28 |
print(f"MFCC extrait, shape: {mfccs.shape}")
|
29 |
|
@@ -32,19 +50,13 @@ def process_audio(audio_path):
|
|
32 |
mfccs_scaled = scaler.fit_transform(mfccs.T)
|
33 |
print("MFCC normalisé.")
|
34 |
|
35 |
-
#
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
speaker_labels = kmeans.fit_predict(mfccs_scaled)
|
38 |
-
print(f"Clustering terminé, {len(set(speaker_labels))} locuteurs détectés.")
|
39 |
-
|
40 |
-
# Vérification du nombre de locuteurs
|
41 |
-
num_speakers = len(set(speaker_labels))
|
42 |
-
if num_speakers == 1:
|
43 |
-
print("Un seul locuteur détecté.")
|
44 |
-
elif num_speakers == 2:
|
45 |
-
print("Deux locuteurs détectés.")
|
46 |
-
else:
|
47 |
-
print(f"Plus de deux locuteurs détectés : {num_speakers} locuteurs.")
|
48 |
|
49 |
# Regrouper les segments audio par speaker
|
50 |
speaker_audio = {speaker: [] for speaker in set(speaker_labels)}
|
|
|
5 |
from sklearn.preprocessing import StandardScaler
|
6 |
from sklearn.cluster import KMeans
|
7 |
from transformers import pipeline
|
8 |
+
import noisereduce as nr
|
9 |
+
from sklearn.metrics import silhouette_score
|
10 |
|
11 |
print("Chargement du modèle Wav2Vec2...")
|
12 |
stt_pipeline = pipeline("automatic-speech-recognition", model="boumehdi/wav2vec2-large-xlsr-moroccan-darija")
|
13 |
print("Modèle chargé avec succès !")
|
14 |
|
15 |
+
def find_optimal_clusters(mfccs_scaled):
|
16 |
+
"""Trouve le nombre optimal de locuteurs en utilisant la méthode du score silhouette"""
|
17 |
+
best_score = -1
|
18 |
+
best_n_clusters = 1 # Au moins 1 cluster (1 locuteur)
|
19 |
+
|
20 |
+
for n_clusters in range(1, 3): # On teste pour 1 ou 2 locuteurs
|
21 |
+
kmeans = KMeans(n_clusters=n_clusters, random_state=42, n_init=10)
|
22 |
+
labels = kmeans.fit_predict(mfccs_scaled)
|
23 |
+
|
24 |
+
if n_clusters > 1:
|
25 |
+
score = silhouette_score(mfccs_scaled, labels) # Score d’évaluation
|
26 |
+
if score > best_score:
|
27 |
+
best_score = score
|
28 |
+
best_n_clusters = n_clusters
|
29 |
+
|
30 |
+
return best_n_clusters
|
31 |
+
|
32 |
def process_audio(audio_path):
|
33 |
print(f"Fichier reçu : {audio_path}")
|
34 |
|
|
|
37 |
audio, sr = librosa.load(audio_path, sr=None, duration=30)
|
38 |
print(f"Audio chargé : {len(audio)} échantillons à {sr} Hz")
|
39 |
|
40 |
+
# Réduction du bruit (amélioration du SNR)
|
41 |
audio_denoised = nr.reduce_noise(y=audio, sr=sr)
|
42 |
print("Bruit réduit.")
|
43 |
|
44 |
+
# Extraction des MFCC après réduction du bruit
|
45 |
mfccs = librosa.feature.mfcc(y=audio_denoised, sr=sr, n_mfcc=13)
|
46 |
print(f"MFCC extrait, shape: {mfccs.shape}")
|
47 |
|
|
|
50 |
mfccs_scaled = scaler.fit_transform(mfccs.T)
|
51 |
print("MFCC normalisé.")
|
52 |
|
53 |
+
# Trouver le nombre optimal de locuteurs
|
54 |
+
optimal_clusters = find_optimal_clusters(mfccs_scaled)
|
55 |
+
print(f"Nombre optimal de locuteurs détecté : {optimal_clusters}")
|
56 |
+
|
57 |
+
# Appliquer KMeans avec le bon nombre de locuteurs
|
58 |
+
kmeans = KMeans(n_clusters=optimal_clusters, random_state=42, n_init=10)
|
59 |
speaker_labels = kmeans.fit_predict(mfccs_scaled)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
# Regrouper les segments audio par speaker
|
62 |
speaker_audio = {speaker: [] for speaker in set(speaker_labels)}
|