|
|
|
|
|
""" |
|
|
Application principale pour Hugging Face Space |
|
|
""" |
|
|
|
|
|
import gradio as gr |
|
|
import os |
|
|
import tempfile |
|
|
from pathlib import Path |
|
|
import sys |
|
|
import traceback |
|
|
|
|
|
|
|
|
sys.path.append(os.path.dirname(__file__)) |
|
|
|
|
|
|
|
|
try: |
|
|
from transcribe_audio import transcribe_file, load_whisper_model, get_audio_files |
|
|
from analyze_bob_hf import analyze_files_hf |
|
|
print("✅ Modules importés avec succès") |
|
|
MODULES_AVAILABLE = True |
|
|
except Exception as e: |
|
|
print(f"❌ Erreur d'import: {e}") |
|
|
traceback.print_exc() |
|
|
MODULES_AVAILABLE = False |
|
|
|
|
|
def process_audio_file(audio_file): |
|
|
"""Traite un fichier audio et retourne les résultats""" |
|
|
try: |
|
|
|
|
|
with tempfile.TemporaryDirectory() as temp_dir: |
|
|
temp_path = Path(temp_dir) |
|
|
input_dir = temp_path / "input" |
|
|
output_dir = temp_path / "output" |
|
|
transcriptions_dir = output_dir / "transcriptions" |
|
|
|
|
|
input_dir.mkdir(parents=True, exist_ok=True) |
|
|
output_dir.mkdir(parents=True, exist_ok=True) |
|
|
transcriptions_dir.mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
print(f"📁 Dossiers créés: input={input_dir}, output={output_dir}") |
|
|
|
|
|
|
|
|
audio_path = input_dir / os.path.basename(audio_file) |
|
|
import shutil |
|
|
shutil.copy2(audio_file, audio_path) |
|
|
print(f"📁 Fichier audio copié: {audio_path}") |
|
|
|
|
|
|
|
|
os.environ["BOB_INPUT_DIR"] = str(input_dir) |
|
|
os.environ["BOB_TRANSCRIPTIONS_DIR"] = str(transcriptions_dir) |
|
|
os.environ["BOB_OUTPUT_FILE"] = str(output_dir / "resume_bob.txt") |
|
|
os.environ["WHISPER_MODEL"] = "small" |
|
|
os.environ["HF_MODEL"] = "google/gemma-3-4b-pt" |
|
|
|
|
|
print("🔄 Début de la transcription...") |
|
|
|
|
|
|
|
|
model = load_whisper_model("small") |
|
|
success = transcribe_file(model, audio_path, transcriptions_dir) |
|
|
|
|
|
if not success: |
|
|
return "❌ Erreur de transcription", "❌ Erreur de transcription" |
|
|
|
|
|
print("✅ Transcription terminée") |
|
|
|
|
|
|
|
|
transcription_files = list(transcriptions_dir.glob("*_transcription.txt")) |
|
|
print(f"📁 Fichiers de transcription trouvés: {len(transcription_files)}") |
|
|
|
|
|
if not transcription_files: |
|
|
return "✅ Transcription terminée", "❌ Aucun fichier de transcription généré" |
|
|
|
|
|
|
|
|
with open(transcription_files[0], 'r', encoding='utf-8') as f: |
|
|
transcription_content = f.read() |
|
|
print(f"📝 Transcription lue ({len(transcription_content)} caractères)") |
|
|
|
|
|
print("🤖 Début de l'analyse...") |
|
|
|
|
|
|
|
|
print(f"🔍 Dossiers avant analyse:") |
|
|
print(f" Input: {list(input_dir.iterdir())}") |
|
|
print(f" Transcriptions: {list(transcriptions_dir.iterdir())}") |
|
|
print(f" Output: {list(output_dir.iterdir())}") |
|
|
|
|
|
|
|
|
try: |
|
|
print("🔄 Appel de analyze_files_hf...") |
|
|
result = analyze_files_hf( |
|
|
transcriptions_dir=transcriptions_dir, |
|
|
input_dir=input_dir, |
|
|
output_file=output_dir / "resume_bob.txt", |
|
|
log_fn=print |
|
|
) |
|
|
print(f"📊 Résultat de analyze_files_hf: {result}") |
|
|
|
|
|
|
|
|
print(f"🔍 Dossiers après analyse:") |
|
|
print(f" Output: {list(output_dir.iterdir())}") |
|
|
|
|
|
|
|
|
resume_path = output_dir / "resume_bob.txt" |
|
|
print(f"🔍 Recherche fichier: {resume_path}") |
|
|
print(f"📁 Fichier existe: {resume_path.exists()}") |
|
|
|
|
|
if resume_path.exists(): |
|
|
with open(resume_path, 'r', encoding='utf-8') as f: |
|
|
resume_content = f.read() |
|
|
print("✅ Fichier de résultat trouvé et lu") |
|
|
else: |
|
|
resume_content = f"❌ Fichier de résultat non trouvé\nChemin attendu: {resume_path}" |
|
|
print("❌ Fichier de résultat non trouvé") |
|
|
|
|
|
print("📄 Contenu détaillé:") |
|
|
for root, dirs, files in os.walk(temp_path): |
|
|
for file in files: |
|
|
full_path = os.path.join(root, file) |
|
|
print(f" {full_path}") |
|
|
|
|
|
return transcription_content, resume_content |
|
|
|
|
|
except Exception as analyze_error: |
|
|
error_details = f"❌ Erreur pendant l'analyse:\n{str(analyze_error)}\n\nTraceback:\n{traceback.format_exc()}" |
|
|
print(error_details) |
|
|
return transcription_content, error_details |
|
|
|
|
|
except Exception as e: |
|
|
error_msg = f"❌ Erreur générale:\n{str(e)}\n\nTraceback:\n{traceback.format_exc()}" |
|
|
print(error_msg) |
|
|
return error_msg, error_msg |
|
|
|
|
|
|
|
|
with gr.Blocks(title="BOB Processor") as demo: |
|
|
gr.Markdown("# 🎵 BOB Processor") |
|
|
gr.Markdown("### Transcription et analyse automatique de fichiers audio") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
audio_input = gr.Audio( |
|
|
label="🎤 Fichier audio à traiter", |
|
|
type="filepath" |
|
|
) |
|
|
process_btn = gr.Button("▶️ Traiter le fichier") |
|
|
|
|
|
with gr.Column(): |
|
|
transcription_output = gr.Textbox( |
|
|
label="📝 Transcription", |
|
|
lines=10 |
|
|
) |
|
|
resume_output = gr.Textbox( |
|
|
label="📋 Résumé analysé", |
|
|
lines=10 |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
process_btn.click( |
|
|
process_audio_file, |
|
|
inputs=[audio_input], |
|
|
outputs=[transcription_output, resume_output], |
|
|
show_progress=True, |
|
|
queue=True |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch( |
|
|
max_threads=1, |
|
|
show_error=True |
|
|
) |
|
|
|