Update app.py
Browse files
app.py
CHANGED
|
@@ -40,6 +40,8 @@ def convert_to_wav(audio_path):
|
|
| 40 |
try:
|
| 41 |
audio = AudioSegment.from_file(audio_path)
|
| 42 |
audio = audio.set_channels(1)
|
|
|
|
|
|
|
| 43 |
wav_path = os.path.splitext(audio_path)[0] + ".wav"
|
| 44 |
audio.export(wav_path, format="wav")
|
| 45 |
return wav_path
|
|
@@ -47,37 +49,57 @@ def convert_to_wav(audio_path):
|
|
| 47 |
print(f"Erreur lors de la conversion en WAV : {e}")
|
| 48 |
return None
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
def detect_language_on_upload(filepath):
|
| 51 |
-
"""Détecte la langue d'un fichier audio en utilisant Whisper + LangDetect"""
|
| 52 |
if filepath is None:
|
| 53 |
return "auto"
|
| 54 |
-
|
| 55 |
try:
|
| 56 |
wav_filepath = convert_to_wav(filepath)
|
| 57 |
if not wav_filepath:
|
| 58 |
return "auto"
|
| 59 |
-
|
| 60 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
outputs = pipe(
|
| 62 |
-
|
| 63 |
-
chunk_length_s=15,
|
| 64 |
-
batch_size=8,
|
| 65 |
return_timestamps=False
|
| 66 |
)
|
| 67 |
-
|
| 68 |
-
transcribed_text = outputs
|
| 69 |
-
|
| 70 |
-
# Si
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
if len(transcribed_text) < 10:
|
| 72 |
return "auto"
|
| 73 |
-
|
| 74 |
# Utilise LangDetect sur le texte transcrit
|
| 75 |
detected_lang = detect(transcribed_text)
|
| 76 |
-
|
| 77 |
# Mapping des codes de langue LangDetect vers les codes Whisper
|
| 78 |
lang_mapping = {
|
| 79 |
'fr': 'fr',
|
| 80 |
-
'en': 'en',
|
| 81 |
'es': 'es',
|
| 82 |
'de': 'de',
|
| 83 |
'it': 'it',
|
|
@@ -90,9 +112,9 @@ def detect_language_on_upload(filepath):
|
|
| 90 |
'zh-cn': 'zh',
|
| 91 |
'zh': 'zh'
|
| 92 |
}
|
| 93 |
-
|
| 94 |
return lang_mapping.get(detected_lang, "auto")
|
| 95 |
-
|
| 96 |
except (LangDetectException, Exception) as e:
|
| 97 |
print(f"Erreur lors de la détection de langue : {e}")
|
| 98 |
return "auto"
|
|
@@ -159,7 +181,6 @@ def transcribe_audio(filepath, diarize, language_choice):
|
|
| 159 |
|
| 160 |
with gr.Blocks() as demo:
|
| 161 |
gr.HTML("<div style='text-align:center;'><h1>Application de Transcription et Diarisation Audio</h1></div>")
|
| 162 |
-
# gr.Markdown("## Objectif")
|
| 163 |
gr.Markdown("Transcrivez et diarisez automatiquement vos fichiers audio (WhatsApp, réunions, interviews, etc.) grâce à Whisper et pyannote, directement dans ce Space.")
|
| 164 |
|
| 165 |
gr.Markdown("""
|
|
|
|
| 40 |
try:
|
| 41 |
audio = AudioSegment.from_file(audio_path)
|
| 42 |
audio = audio.set_channels(1)
|
| 43 |
+
# Assure un sample rate standard pour Whisper (16 kHz), utile pour vitesse/stabilité
|
| 44 |
+
audio = audio.set_frame_rate(16000)
|
| 45 |
wav_path = os.path.splitext(audio_path)[0] + ".wav"
|
| 46 |
audio.export(wav_path, format="wav")
|
| 47 |
return wav_path
|
|
|
|
| 49 |
print(f"Erreur lors de la conversion en WAV : {e}")
|
| 50 |
return None
|
| 51 |
|
| 52 |
+
def make_short_wav(input_wav_path, max_seconds=12):
|
| 53 |
+
"""Crée un court extrait (début) du WAV pour la détection de langue."""
|
| 54 |
+
try:
|
| 55 |
+
audio = AudioSegment.from_wav(input_wav_path)
|
| 56 |
+
clip = audio[: max_seconds * 1000] # millisecondes
|
| 57 |
+
short_path = os.path.splitext(input_wav_path)[0] + f"_head{max_seconds}s.wav"
|
| 58 |
+
clip.export(short_path, format="wav")
|
| 59 |
+
return short_path
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"Erreur lors de la création de l'extrait court : {e}")
|
| 62 |
+
return None
|
| 63 |
+
|
| 64 |
def detect_language_on_upload(filepath):
|
| 65 |
+
"""Détecte la langue d'un fichier audio en n'utilisant qu'un court extrait (Whisper + LangDetect)."""
|
| 66 |
if filepath is None:
|
| 67 |
return "auto"
|
| 68 |
+
|
| 69 |
try:
|
| 70 |
wav_filepath = convert_to_wav(filepath)
|
| 71 |
if not wav_filepath:
|
| 72 |
return "auto"
|
| 73 |
+
|
| 74 |
+
# Utiliser uniquement les premières secondes pour la détection (plus rapide)
|
| 75 |
+
short_wav = make_short_wav(wav_filepath, max_seconds=12)
|
| 76 |
+
if not short_wav:
|
| 77 |
+
short_wav = wav_filepath # fallback si l'extrait échoue
|
| 78 |
+
|
| 79 |
+
# Transcription rapide d'un échantillon court (pas besoin de batch ni timestamps)
|
| 80 |
outputs = pipe(
|
| 81 |
+
short_wav,
|
| 82 |
+
chunk_length_s=15,
|
|
|
|
| 83 |
return_timestamps=False
|
| 84 |
)
|
| 85 |
+
|
| 86 |
+
transcribed_text = outputs.get("text", "").strip()
|
| 87 |
+
|
| 88 |
+
# Si Whisper renvoie déjà une langue
|
| 89 |
+
whisper_lang = outputs.get("language")
|
| 90 |
+
if whisper_lang and isinstance(whisper_lang, str) and len(whisper_lang) <= 5:
|
| 91 |
+
return whisper_lang
|
| 92 |
+
|
| 93 |
if len(transcribed_text) < 10:
|
| 94 |
return "auto"
|
| 95 |
+
|
| 96 |
# Utilise LangDetect sur le texte transcrit
|
| 97 |
detected_lang = detect(transcribed_text)
|
| 98 |
+
|
| 99 |
# Mapping des codes de langue LangDetect vers les codes Whisper
|
| 100 |
lang_mapping = {
|
| 101 |
'fr': 'fr',
|
| 102 |
+
'en': 'en',
|
| 103 |
'es': 'es',
|
| 104 |
'de': 'de',
|
| 105 |
'it': 'it',
|
|
|
|
| 112 |
'zh-cn': 'zh',
|
| 113 |
'zh': 'zh'
|
| 114 |
}
|
| 115 |
+
|
| 116 |
return lang_mapping.get(detected_lang, "auto")
|
| 117 |
+
|
| 118 |
except (LangDetectException, Exception) as e:
|
| 119 |
print(f"Erreur lors de la détection de langue : {e}")
|
| 120 |
return "auto"
|
|
|
|
| 181 |
|
| 182 |
with gr.Blocks() as demo:
|
| 183 |
gr.HTML("<div style='text-align:center;'><h1>Application de Transcription et Diarisation Audio</h1></div>")
|
|
|
|
| 184 |
gr.Markdown("Transcrivez et diarisez automatiquement vos fichiers audio (WhatsApp, réunions, interviews, etc.) grâce à Whisper et pyannote, directement dans ce Space.")
|
| 185 |
|
| 186 |
gr.Markdown("""
|