import logging import os import sys import tempfile from pathlib import Path import requests import gradio as gr import matplotlib.pyplot as plt from PIL import Image # Import configuration for end consultation logic try: from .config import get_flask_urls, get_doctors_page_urls, TIMEOUT_SETTINGS except ImportError: def get_flask_urls(): return [ "http://127.0.0.1:600/complete_appointment", "http://localhost:600/complete_appointment", "https://your-flask-app-domain.com/complete_appointment", "http://your-flask-app-ip:600/complete_appointment" ] def get_doctors_page_urls(): return { "local": "http://127.0.0.1:600/doctors", "production": "https://your-flask-app-domain.com/doctors" } TIMEOUT_SETTINGS = {"connection_timeout": 5, "request_timeout": 10} # Add parent directory to path parent_dir = os.path.dirname(os.path.abspath(__file__)) sys.path.append(parent_dir) # Import our modules for model and utility logic from models.multimodal_fusion import MultimodalFusion from utils.preprocessing import enhance_xray_image, normalize_report_text from utils.visualization import ( plot_image_prediction, plot_multimodal_results, plot_report_entities, ) # Set up logging logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", handlers=[logging.StreamHandler(), logging.FileHandler("mediSync.log")], ) logger = logging.getLogger(__name__) # Ensure sample data directory exists os.makedirs(os.path.join(parent_dir, "data", "sample"), exist_ok=True) class MediSyncApp: """ Main application class for the MediSync multi-modal medical analysis system. """ def __init__(self): """Initialize the application and load models.""" self.logger = logging.getLogger(__name__) self.logger.info("Initializing MediSync application") self.fusion_model = None self.image_model = None self.text_model = None def load_models(self): """ Load models if not already loaded. Returns: bool: True if models loaded successfully, False otherwise """ try: if self.fusion_model is None: self.logger.info("Loading models...") self.fusion_model = MultimodalFusion() self.image_model = self.fusion_model.image_analyzer self.text_model = self.fusion_model.text_analyzer self.logger.info("Models loaded successfully") return True except Exception as e: self.logger.error(f"Error loading models: {e}") return False def analyze_image(self, image): """ Analyze a medical image. Args: image: Image file uploaded through Gradio Returns: tuple: (image, image_results_html, plot_as_html) """ try: if image is None: return None, "Please upload an image first.", None if not self.load_models() or self.image_model is None: return image, "Error: Models not loaded properly.", None temp_dir = tempfile.mkdtemp() temp_path = os.path.join(temp_dir, "upload.png") if isinstance(image, str): from shutil import copyfile copyfile(image, temp_path) else: image.save(temp_path) self.logger.info(f"Analyzing image: {temp_path}") results = self.image_model.analyze(temp_path) fig = plot_image_prediction( image, results.get("predictions", []), f"Primary Finding: {results.get('primary_finding', 'Unknown')}", ) plot_html = self.fig_to_html(fig) html_result = f"""
Primary Finding: {results.get("primary_finding", "Unknown")}
Confidence: {results.get("confidence", 0):.1%}
Abnormality Detected: {"Yes" if results.get("has_abnormality", False) else "No"}
{explanation}
" html_result += "Severity Level: {results.get("severity", {}).get("level", "Unknown")}
Severity Score: {results.get("severity", {}).get("score", 0)}/4
Confidence: {results.get("severity", {}).get("confidence", 0):.1%}
{category.capitalize()}: {', '.join(items)}
" html_result += "Primary Finding: {results.get("primary_finding", "Unknown")}
Severity Level: {results.get("severity", {}).get("level", "Unknown")}
Severity Score: {results.get("severity", {}).get("score", 0)}/4
Agreement Score: {results.get("agreement_score", 0):.0%}
Note: This analysis has a confidence level of {confidence:.0%}. Please consult with healthcare professionals for official diagnosis.
{explanation}
Error displaying visualization.
" def complete_appointment(appointment_id): try: flask_urls = get_flask_urls() payload = {"appointment_id": appointment_id} for flask_api_url in flask_urls: try: logger.info(f"Trying to connect to: {flask_api_url}") response = requests.post(flask_api_url, json=payload, timeout=TIMEOUT_SETTINGS["connection_timeout"]) if response.status_code == 200: return {"status": "success", "message": "Appointment completed successfully"} elif response.status_code == 404: return {"status": "error", "message": "Appointment not found"} else: logger.warning(f"Unexpected response from {flask_api_url}: {response.status_code}") continue except requests.exceptions.ConnectionError: logger.warning(f"Connection failed to {flask_api_url}") continue except requests.exceptions.Timeout: logger.warning(f"Timeout connecting to {flask_api_url}") continue except Exception as e: logger.warning(f"Error with {flask_api_url}: {e}") continue return { "status": "error", "message": "Cannot connect to Flask app. Please ensure the Flask app is running and accessible." } except Exception as e: logger.error(f"Error completing appointment: {e}") return {"status": "error", "message": f"Error: {str(e)}"} def create_interface(): import urllib.parse app = MediSyncApp() example_report = """ CHEST X-RAY EXAMINATION CLINICAL HISTORY: 55-year-old male with cough and fever. FINDINGS: The heart size is at the upper limits of normal. The lungs are clear without focal consolidation, effusion, or pneumothorax. There is mild prominence of the pulmonary vasculature. No pleural effusion is seen. There is a small nodular opacity noted in the right lower lobe measuring approximately 8mm, which is suspicious and warrants further investigation. The mediastinum is unremarkable. The visualized bony structures show no acute abnormalities. IMPRESSION: 1. Mild cardiomegaly. 2. 8mm nodular opacity in the right lower lobe, recommend follow-up CT for further evaluation. 3. No acute pulmonary parenchymal abnormality. RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe. """ sample_images_dir = Path(parent_dir) / "data" / "sample" sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg")) sample_image_path = str(sample_images[0]) if sample_images else None with gr.Blocks( title="MediSync: Multi-Modal Medical Analysis System", theme=gr.themes.Default(), css=""" /* Modern neumorphic card style for all result containers */ .medisync-card { border-radius: 18px; box-shadow: 0 4px 24px 0 rgba(0,0,0,0.10), 0 1.5px 4px 0 rgba(0,191,174,0.08); margin: 18px 0; padding: 24px 24px 18px 24px; font-size: 1.08rem; transition: background 0.2s, color 0.2s; } .medisync-card-bg { background: var(--background-fill-primary, #f8f9fa); color: var(--body-text-color, #222); } .medisync-title { font-weight: 900; font-size: 1.45em; margin-bottom: 0.7em; letter-spacing: 1px; text-shadow: 0 2px 8px #00bfae33, 0 1px 0 #fff; /* Remove display:flex and gap for simple bold text */ } .medisync-blue { color: #00bfae; } .medisync-green { color: #28a745; } .medisync-purple { color: #6c63ff; } .medisync-card ul, .medisync-card ol { margin-left: 1.2em; } .medisync-card li { margin-bottom: 0.2em; } /* Button and input styling for modern look */ .gr-button, .end-consultation-btn { border-radius: 8px !important; font-weight: 600 !important; font-size: 1rem !important; padding: 8px 18px !important; min-width: 120px !important; min-height: 38px !important; transition: background 0.2s, color 0.2s; } .end-consultation-btn { background: linear-gradient(90deg, #dc3545 60%, #ff7675 100%) !important; border: none !important; color: #fff !important; box-shadow: 0 2px 8px 0 rgba(220,53,69,0.10); font-size: 1.05rem !important; padding: 10px 24px !important; min-width: 160px !important; min-height: 40px !important; } .end-consultation-btn:hover { background: linear-gradient(90deg, #c82333 60%, #ff7675 100%) !important; } /* Responsive tweaks */ @media (max-width: 900px) { .medisync-card { padding: 16px 8px 12px 8px; } .medisync-title { font-size: 1.1em; } } /* Ensure text is visible in dark mode */ html[data-theme="dark"] .medisync-card-bg, html[data-theme="dark"] .medisync-card-bg.medisync-force-text { background: #23272f !important; color: #f8fafc !important; } html[data-theme="dark"] .medisync-title { color: #00bfae !important; text-shadow: 0 2px 8px #00bfae33, 0 1px 0 #23272f; } html[data-theme="dark"] .medisync-blue { color: #00bfae !important; } html[data-theme="dark"] .medisync-green { color: #00e676 !important; } html[data-theme="dark"] .medisync-purple { color: #a385ff !important; } /* Make sure all gradio labels and text are visible */ label, .gr-label, .gr-text, .gr-html, .gr-markdown { color: var(--body-text-color, #222) !important; } html[data-theme="dark"] label, html[data-theme="dark"] .gr-label, html[data-theme="dark"] .gr-text, html[data-theme="dark"] .gr-html, html[data-theme="dark"] .gr-markdown { color: #f8fafc !important; } /* Force all text in medisync-card and status outputs to be visible in all themes */ .medisync-force-text, .medisync-force-text * { color: var(--body-text-color, #222) !important; } html[data-theme="dark"] .medisync-force-text, html[data-theme="dark"] .medisync-force-text * { color: #f8fafc !important; } /* End consultation status output: remove color and theme, keep text black and simple */ #end_consultation_status, #end_consultation_status * { color: #000 !important; background: #fff !important; font-size: 1.12rem !important; font-weight: 600 !important; } /* Style the buttons inside the end consultation status popup */ #end_consultation_status button { font-size: 1rem !important; font-weight: 600 !important; border-radius: 6px !important; padding: 8px 18px !important; margin-top: 8px !important; margin-bottom: 4px !important; min-width: 120px !important; min-height: 36px !important; box-shadow: 0 1.5px 4px 0 rgba(0,191,174,0.08); } #end_consultation_status button:active, #end_consultation_status button:focus { outline: 2px solid #00bfae !important; } #end_consultation_status .btn-green { background-color: #00bfae !important; color: #fff !important; } #end_consultation_status .btn-purple { background-color: #6c63ff !important; color: #fff !important; } #end_consultation_status .btn-dark { background-color: #23272f !important; color: #fff !important; } #end_consultation_status .btn-orange { background-color: #ff9800 !important; color: #fff !important; } #end_consultation_status .btn-red { background-color: #dc3545 !important; color: #fff !important; } """ ) as interface: gr.Markdown( """MediSync is an AI-powered healthcare solution that uses multi-modal analysis to provide comprehensive insights from medical images and reports.
This tool is for educational and research purposes only. It is not intended to provide medical advice or replace professional healthcare. Always consult with qualified healthcare providers for medical decisions.
âī¸ {result['message']}
Your appointment has been marked as completed.
Your consultation analysis is complete! However, we cannot automatically mark your appointment as completed because the Flask app is not accessible from this environment.
Appointment ID: {appointment_id.strip()}
Next Steps:
{appointment_id.strip()}{result['message']}
Please try again or contact support if the problem persists.