Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import whisper
|
| 4 |
+
import spacy
|
| 5 |
+
import torch
|
| 6 |
+
import os
|
| 7 |
+
from pyannote.audio import Pipeline
|
| 8 |
+
|
| 9 |
+
# Chargement des modèles
|
| 10 |
+
whisper_model = whisper.load_model("base") # medium ou large possible
|
| 11 |
+
nlp = spacy.load("fr_core_news_md")
|
| 12 |
+
|
| 13 |
+
# Diarisation avec PyAnnote (nécessite un token HF dans les Secrets du Space)
|
| 14 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 15 |
+
if hf_token:
|
| 16 |
+
diar_pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization-3.1", use_auth_token=hf_token)
|
| 17 |
+
else:
|
| 18 |
+
diar_pipeline = None
|
| 19 |
+
|
| 20 |
+
def process_audio(file):
|
| 21 |
+
result = whisper_model.transcribe(file, language="fr", verbose=False)
|
| 22 |
+
transcription = result["text"]
|
| 23 |
+
|
| 24 |
+
# Diarisation
|
| 25 |
+
if diar_pipeline:
|
| 26 |
+
diar_result = diar_pipeline(file)
|
| 27 |
+
diar_str = "\n".join([
|
| 28 |
+
f"{turn.start:.1f}s - {turn.end:.1f}s : {speaker}"
|
| 29 |
+
for turn, _, speaker in diar_result.itertracks(yield_label=True)
|
| 30 |
+
])
|
| 31 |
+
else:
|
| 32 |
+
diar_str = "Diarisation non disponible (ajoutez votre HF_TOKEN dans les secrets)"
|
| 33 |
+
|
| 34 |
+
# NER
|
| 35 |
+
doc = nlp(transcription)
|
| 36 |
+
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 37 |
+
ent_str = "\n".join([f"{text} ({label})" for text, label in entities]) if entities else "Aucune entité détectée"
|
| 38 |
+
|
| 39 |
+
return transcription, diar_str, ent_str
|
| 40 |
+
|
| 41 |
+
demo = gr.Interface(
|
| 42 |
+
fn=process_audio,
|
| 43 |
+
inputs=gr.Audio(type="filepath", label="Audio (.mp3/.wav)"),
|
| 44 |
+
outputs=[
|
| 45 |
+
gr.Textbox(label="📝 Transcription Whisper"),
|
| 46 |
+
gr.Textbox(label="🗣️ Diarisation (PyAnnote)"),
|
| 47 |
+
gr.Textbox(label="🧠 Entités Nommées (spaCy)")
|
| 48 |
+
],
|
| 49 |
+
title="🔎 Pipeline Audio Intelligent",
|
| 50 |
+
description="Transcription, Diarisation, et Extraction d'Entités Nommées sur un fichier audio français."
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
demo.launch()
|