Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,88 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
-
import spacy
|
| 4 |
import torch
|
| 5 |
import os
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
fn=process_audio,
|
| 46 |
-
inputs=gr.Audio(type="filepath", label="Audio (.mp3
|
| 47 |
-
outputs=
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
gr.Textbox(label="🧠 Entités Nommées (spaCy)")
|
| 51 |
-
],
|
| 52 |
-
title="🔎 Pipeline Audio Intelligent",
|
| 53 |
-
description="Transcription, Diarisation, et Extraction d'Entités Nommées sur un fichier audio français."
|
| 54 |
)
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|
| 57 |
-
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
import os
|
| 4 |
+
from whisperx import load_model, load_align_model, align
|
| 5 |
+
from resemblyzer import preprocess_wav, VoiceEncoder
|
| 6 |
+
from sklearn.cluster import AgglomerativeClustering
|
| 7 |
+
import librosa
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
compute_type = "float16" if device == "cuda" else "int8"
|
| 12 |
+
|
| 13 |
+
whisper_model = load_model("medium", device=device, compute_type=compute_type)
|
| 14 |
+
align_model, metadata = load_align_model(language_code="fr", device=device)
|
| 15 |
+
voice_encoder = VoiceEncoder()
|
| 16 |
+
|
| 17 |
+
def get_speaker_segments(audio_path, window_size=1.0, step_size=0.5, num_speakers=2):
|
| 18 |
+
wav, sr = librosa.load(audio_path, sr=16000, mono=True)
|
| 19 |
+
wav = librosa.util.normalize(wav)
|
| 20 |
+
duration = librosa.get_duration(y=wav, sr=sr)
|
| 21 |
+
|
| 22 |
+
segments = []
|
| 23 |
+
embeddings = []
|
| 24 |
+
|
| 25 |
+
for start in np.arange(0, duration - window_size, step_size):
|
| 26 |
+
end = start + window_size
|
| 27 |
+
clip = wav[int(start * sr):int(end * sr)]
|
| 28 |
+
if len(clip) == 0:
|
| 29 |
+
continue
|
| 30 |
+
try:
|
| 31 |
+
embed = voice_encoder.embed_utterance(clip)
|
| 32 |
+
embeddings.append(embed)
|
| 33 |
+
segments.append((start, end))
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"⚠️ Skipped segment {start}-{end}s: {e}")
|
| 36 |
+
|
| 37 |
+
if len(embeddings) < 2:
|
| 38 |
+
print("⚠️ Pas assez de segments pour la diarisation. Diarisation annulée.")
|
| 39 |
+
return [{"start": 0, "end": duration, "speaker": "speaker_00"}]
|
| 40 |
+
|
| 41 |
+
clustering = AgglomerativeClustering(n_clusters=num_speakers)
|
| 42 |
+
labels = clustering.fit_predict(embeddings)
|
| 43 |
+
|
| 44 |
+
speaker_segments = []
|
| 45 |
+
for (start, end), label in zip(segments, labels):
|
| 46 |
+
speaker_segments.append({"start": start, "end": end, "speaker": f"speaker_{label:02d}"})
|
| 47 |
+
|
| 48 |
+
return speaker_segments
|
| 49 |
+
|
| 50 |
+
def process_audio(audio_file):
|
| 51 |
+
tmp_path = audio_file
|
| 52 |
+
|
| 53 |
+
# Step 1: Transcription
|
| 54 |
+
result = whisper_model.transcribe(tmp_path, language="fr", word_timestamps=False, verbose=False)
|
| 55 |
+
|
| 56 |
+
# Step 2: Diarisation via resemblyzer
|
| 57 |
+
speaker_segments = get_speaker_segments(tmp_path)
|
| 58 |
+
|
| 59 |
+
# Step 3: Alignement mot à mot
|
| 60 |
+
result_aligned = align(result["segments"], align_model, metadata, tmp_path, return_char_alignments=False)
|
| 61 |
+
|
| 62 |
+
# Attribution speaker
|
| 63 |
+
for segment in result_aligned["segments"]:
|
| 64 |
+
segment_start = segment["start"]
|
| 65 |
+
speaker_found = next((sp["speaker"] for sp in speaker_segments if sp["start"] <= segment_start <= sp["end"]), "speaker_??")
|
| 66 |
+
segment["speaker"] = speaker_found
|
| 67 |
+
|
| 68 |
+
# Format final
|
| 69 |
+
final_output = ""
|
| 70 |
+
for seg in result_aligned["segments"]:
|
| 71 |
+
speaker = seg["speaker"]
|
| 72 |
+
start = f"{seg['start']:.2f}s"
|
| 73 |
+
end = f"{seg['end']:.2f}s"
|
| 74 |
+
text = seg['text'].strip()
|
| 75 |
+
final_output += f"[{start} - {end}] {speaker}: {text}\n"
|
| 76 |
+
|
| 77 |
+
return final_output
|
| 78 |
+
|
| 79 |
+
iface = gr.Interface(
|
| 80 |
fn=process_audio,
|
| 81 |
+
inputs=gr.Audio(type="filepath", label="Audio (.wav, .mp3...)"),
|
| 82 |
+
outputs=gr.Textbox(label="Transcription + Diarisation + Alignement"),
|
| 83 |
+
title="🎙️ Transcription enrichie avec WhisperX + Resemblyzer",
|
| 84 |
+
description="Transcription française, diarisation légère (sans token), alignement mot à mot."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
)
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
+
iface.launch()
|