Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Application principale pour Hugging Face Space
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import os
|
| 8 |
+
import tempfile
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
import sys
|
| 11 |
+
|
| 12 |
+
# Ajouter le dossier courant au path
|
| 13 |
+
sys.path.append(os.path.dirname(__file__))
|
| 14 |
+
|
| 15 |
+
# Importer les modules nécessaires
|
| 16 |
+
try:
|
| 17 |
+
from transcribe_audio import transcribe_file, load_whisper_model, get_audio_files
|
| 18 |
+
from analyze_bob_hf import analyze_files_hf
|
| 19 |
+
print("✅ Modules importés avec succès")
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"❌ Erreur d'import: {e}")
|
| 22 |
+
|
| 23 |
+
def process_audio_file(audio_file):
|
| 24 |
+
"""Traite un fichier audio et retourne les résultats"""
|
| 25 |
+
try:
|
| 26 |
+
# Créer des dossiers temporaires
|
| 27 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 28 |
+
temp_path = Path(temp_dir)
|
| 29 |
+
input_dir = temp_path / "input"
|
| 30 |
+
output_dir = temp_path / "output"
|
| 31 |
+
transcriptions_dir = output_dir / "transcriptions"
|
| 32 |
+
|
| 33 |
+
input_dir.mkdir(parents=True, exist_ok=True)
|
| 34 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 35 |
+
transcriptions_dir.mkdir(parents=True, exist_ok=True)
|
| 36 |
+
|
| 37 |
+
# Copier le fichier audio
|
| 38 |
+
audio_path = input_dir / os.path.basename(audio_file)
|
| 39 |
+
import shutil
|
| 40 |
+
shutil.copy2(audio_file, audio_path)
|
| 41 |
+
|
| 42 |
+
# Configurer les variables d'environnement
|
| 43 |
+
os.environ["BOB_INPUT_DIR"] = str(input_dir)
|
| 44 |
+
os.environ["BOB_TRANSCRIPTIONS_DIR"] = str(transcriptions_dir)
|
| 45 |
+
os.environ["BOB_OUTPUT_FILE"] = str(output_dir / "resume_bob.txt")
|
| 46 |
+
os.environ["WHISPER_MODEL"] = "small" # Modèle léger pour le Space
|
| 47 |
+
|
| 48 |
+
# Transcription
|
| 49 |
+
print("🔄 Transcription en cours...")
|
| 50 |
+
model = load_whisper_model("small")
|
| 51 |
+
success = transcribe_file(model, audio_path, transcriptions_dir)
|
| 52 |
+
|
| 53 |
+
if not success:
|
| 54 |
+
return "❌ Erreur de transcription", ""
|
| 55 |
+
|
| 56 |
+
# Analyse
|
| 57 |
+
print("🤖 Analyse en cours...")
|
| 58 |
+
result = analyze_files_hf(
|
| 59 |
+
transcriptions_dir=transcriptions_dir,
|
| 60 |
+
input_dir=input_dir,
|
| 61 |
+
output_file=output_dir / "resume_bob.txt",
|
| 62 |
+
log_fn=print
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Lire les résultats
|
| 66 |
+
resume_path = output_dir / "resume_bob.txt"
|
| 67 |
+
if resume_path.exists():
|
| 68 |
+
with open(resume_path, 'r', encoding='utf-8') as f:
|
| 69 |
+
resume_content = f.read()
|
| 70 |
+
else:
|
| 71 |
+
resume_content = "❌ Fichier de résultat non trouvé"
|
| 72 |
+
|
| 73 |
+
# Lire la transcription
|
| 74 |
+
transcription_files = list(transcriptions_dir.glob("*.txt"))
|
| 75 |
+
if transcription_files:
|
| 76 |
+
with open(transcription_files[0], 'r', encoding='utf-8') as f:
|
| 77 |
+
transcription_content = f.read()
|
| 78 |
+
else:
|
| 79 |
+
transcription_content = "❌ Transcription non trouvée"
|
| 80 |
+
|
| 81 |
+
return transcription_content, resume_content
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return f"❌ Erreur: {str(e)}", f"❌ Erreur: {str(e)}"
|
| 85 |
+
|
| 86 |
+
# Interface Gradio
|
| 87 |
+
with gr.Blocks(title="BOB Processor") as demo:
|
| 88 |
+
gr.Markdown("# 🎵 BOB Processor")
|
| 89 |
+
gr.Markdown("### Transcription et analyse automatique de fichiers audio")
|
| 90 |
+
|
| 91 |
+
with gr.Row():
|
| 92 |
+
with gr.Column():
|
| 93 |
+
audio_input = gr.Audio(
|
| 94 |
+
label="🎤 Fichier audio à traiter",
|
| 95 |
+
type="filepath"
|
| 96 |
+
)
|
| 97 |
+
process_btn = gr.Button("▶️ Traiter le fichier")
|
| 98 |
+
|
| 99 |
+
with gr.Column():
|
| 100 |
+
transcription_output = gr.Textbox(
|
| 101 |
+
label="📝 Transcription",
|
| 102 |
+
lines=10
|
| 103 |
+
)
|
| 104 |
+
resume_output = gr.Textbox(
|
| 105 |
+
label="📋 Résumé analysé",
|
| 106 |
+
lines=10
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
process_btn.click(
|
| 110 |
+
process_audio_file,
|
| 111 |
+
inputs=[audio_input],
|
| 112 |
+
outputs=[transcription_output, resume_output]
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
demo.launch()
|