|
|
|
|
|
""" |
|
|
Interface Gradio : Transcription médicale → Correction → Matching → Rapport |
|
|
""" |
|
|
import gradio as gr |
|
|
import sys |
|
|
import os |
|
|
|
|
|
|
|
|
import template_db_creation |
|
|
sys.modules['__main__'].TemplateInfo = template_db_creation.TemplateInfo |
|
|
|
|
|
from template_db_creation import MedicalTemplateParser |
|
|
from smart_match import TranscriptionMatcher |
|
|
from correcteur import MedicalTranscriptionProcessor, AZURE_OPENAI_DEPLOYMENT |
|
|
|
|
|
|
|
|
DB_PATH = "/Users/macbook/medical-agent/medical-agent/templates/medical_templates.pkl" |
|
|
|
|
|
|
|
|
parser = None |
|
|
matcher = None |
|
|
|
|
|
def initialize_system(): |
|
|
"""Initialise le système au démarrage""" |
|
|
global parser, matcher |
|
|
|
|
|
try: |
|
|
print(f"📂 Chargement de la base de données: {DB_PATH}") |
|
|
parser = MedicalTemplateParser() |
|
|
parser.load_database(DB_PATH) |
|
|
|
|
|
matcher = TranscriptionMatcher(parser) |
|
|
|
|
|
print(f"✅ Système initialisé avec {len(parser.templates)} templates") |
|
|
return True |
|
|
except Exception as e: |
|
|
print(f"❌ Erreur initialisation: {e}") |
|
|
return False |
|
|
|
|
|
def process_transcription(transcription: str): |
|
|
""" |
|
|
Traite la transcription médicale complète |
|
|
|
|
|
Args: |
|
|
transcription: Texte de la transcription |
|
|
|
|
|
Returns: |
|
|
Tuple (transcription_corrigée, template_vide, rapport_final) |
|
|
""" |
|
|
try: |
|
|
|
|
|
print("🔧 Étape 1: Correction de la transcription...") |
|
|
processor = MedicalTranscriptionProcessor(AZURE_OPENAI_DEPLOYMENT) |
|
|
result = processor.process_transcription(transcription) |
|
|
corrected_transcription = result.final_corrected_text |
|
|
|
|
|
|
|
|
print("🔍 Étape 2: Recherche du template approprié...") |
|
|
results = matcher.match_and_fill(corrected_transcription, return_top_k=1) |
|
|
|
|
|
if not results: |
|
|
return ( |
|
|
corrected_transcription, |
|
|
"❌ Aucun template approprié trouvé", |
|
|
"❌ Impossible de générer le rapport" |
|
|
) |
|
|
|
|
|
best_result = results[0] |
|
|
|
|
|
|
|
|
template_vide = f"{best_result.template_id}\n" |
|
|
template_vide += "=" * len(best_result.template_id) + "\n" |
|
|
template_vide += best_result.template_content |
|
|
|
|
|
|
|
|
rapport_final = f"{best_result.template_id}\n" |
|
|
rapport_final += "=" * len(best_result.template_id) + "\n" |
|
|
|
|
|
|
|
|
rapport_final += best_result.filled_template |
|
|
|
|
|
|
|
|
print(f"✅ Traitement terminé - Template: {best_result.template_id}") |
|
|
|
|
|
return corrected_transcription, template_vide, rapport_final |
|
|
|
|
|
except Exception as e: |
|
|
error_msg = f"❌ Erreur: {str(e)}" |
|
|
print(error_msg) |
|
|
return error_msg, "", "" |
|
|
|
|
|
|
|
|
print("🚀 Initialisation du système...") |
|
|
if not initialize_system(): |
|
|
print("⚠️ Erreur lors de l'initialisation - vérifiez le chemin de la DB") |
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=process_transcription, |
|
|
inputs=gr.Textbox( |
|
|
lines=15, |
|
|
label="📝 Transcription médicale", |
|
|
placeholder="Collez ici la transcription de l'examen médical..." |
|
|
), |
|
|
outputs=[ |
|
|
gr.Textbox(lines=20, label="✅ Transcription corrigée", show_copy_button=True), |
|
|
gr.Textbox(lines=20, label="📋 Rapport à remplir (Template)", show_copy_button=True), |
|
|
gr.Textbox(lines=20, label="📄 Compte-rendu structuré final", show_copy_button=True), |
|
|
], |
|
|
title="🏥 Génération de comptes-rendus structurés", |
|
|
|
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(share=True) |