Spaces:
Runtime error
Runtime error
updated the application UI ✅✅
Browse files385 unmodified lines38638738838939039139239339439539639739839911 unmodified lines4114124134144154164174184191 unmodified line421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644385 unmodified linesimport loggingimport osimport sysfrom pathlib import Pathimport requestsimport gradio as grimport matplotlib.pyplot as pltfrom PIL import Imageimport json# Import configurationtry:from .config import get_flask_urls, get_doctors_page_urls, TIMEOUT_SETTINGSexcept ImportError:11 unmodified lines}TIMEOUT_SETTINGS = {"connection_timeout": 5, "request_timeout": 10}parent_dir = os.path.dirname(os.path.abspath(__file__))sys.path.append(parent_dir)logging.basicConfig(level=logging.INFO,format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",1 unmodified line)logger = logging.getLogger(__name__)class MediSyncApp:def __init__(self):self.logger = logging.getLogger(__name__)self.logger.info("Initializing MediSync application")self._temp_files = []self.fusion_model = Noneself.image_model = Noneself.text_model = Nonedef __del__(self):self.cleanup_temp_files()def cleanup_temp_files(self):for temp_file in self._temp_files:try:if os.path.exists(temp_file):os.remove(temp_file)self.logger.debug(f"Cleaned up temporary file: {temp_file}")except Exception as e:self.logger.warning(f"Failed to clean up temporary file {temp_file}: {e}")self._temp_files = []def load_models(self):if self.fusion_model is not None:return Truetry:self.logger.info("Loading models...")self.logger.info("Models loaded successfully (mock implementation)")return Trueexcept Exception as e:self.logger.error(f"Error loading models: {e}")return Falsedef enhance_image(self, image):if image is None:return Nonetry:enhanced_image = imageself.logger.info("Image enhanced successfully")return enhanced_imageexcept Exception as e:self.logger.error(f"Error enhancing image: {e}")return imagedef analyze_image(self, image):if image is None:return None, "Please upload an image first.", Noneif not self.load_models():return image, "Error: Models not loaded properly.", Nonetry:self.logger.info("Analyzing image")results = {"primary_finding": "Normal chest X-ray","confidence": 0.85,"has_abnormality": False,"predictions": [("Normal", 0.85),("Pneumonia", 0.10),("Cardiomegaly", 0.05)]}fig = self.plot_image_prediction(image,results.get("predictions", []),f"Primary Finding: {results.get('primary_finding', 'Unknown')}")plot_html = self.fig_to_html(fig)plt.close(fig)html_result = self.format_image_results(results)return image, html_result, plot_htmlexcept Exception as e:self.logger.error(f"Error in image analysis: {e}")return image, f"Error analyzing image: {str(e)}", Nonedef analyze_text(self, text):if not text or text.strip() == "":return "", "Please enter medical report text.", Noneif not self.load_models():return text, "Error: Models not loaded properly.", Nonetry:self.logger.info("Analyzing text")results = {"entities": [{"text": "chest X-ray", "type": "PROCEDURE", "confidence": 0.95},{"text": "55-year-old male", "type": "PATIENT", "confidence": 0.90},{"text": "cough and fever", "type": "SYMPTOM", "confidence": 0.88}],"sentiment": "neutral","key_findings": ["Normal heart size", "Clear lungs", "8mm nodular opacity"]}html_result = self.format_text_results(results)plot_html = self.create_entity_visualization(results["entities"])return text, html_result, plot_htmlexcept Exception as e:self.logger.error(f"Error in text analysis: {e}")return text, f"Error analyzing text: {str(e)}", Nonedef analyze_multimodal(self, image, text):if image is None and (not text or text.strip() == ""):return "Please provide either an image or text for analysis.", Noneif not self.load_models():return "Error: Models not loaded properly.", Nonetry:self.logger.info("Performing multimodal analysis")results = {"combined_finding": "Normal chest X-ray with minor findings","confidence": 0.92,"image_contribution": "Normal cardiac silhouette and clear lung fields","text_contribution": "Clinical history supports normal findings","recommendations": ["Follow-up CT for the 8mm nodular opacity","Monitor for any changes in symptoms"]}html_result = self.format_multimodal_results(results)plot_html = self.create_multimodal_visualization(results)return html_result, plot_htmlexcept Exception as e:self.logger.error(f"Error in multimodal analysis: {e}")return f"Error in multimodal analysis: {str(e)}", Nonedef format_image_results(self, results):html_result = f"""<div class="medisync-card medisync-card-bg medisync-force-text"><h2 class="medisync-title medisync-blue">X-ray Analysis Results</h2><p><strong>Primary Finding:</strong> {results.get("primary_finding", "Unknown")}</p><p><strong>Confidence:</strong> {results.get("confidence", 0):.1%}</p><p><strong>Abnormality Detected:</strong> {"Yes" if results.get("has_abnormality", False) else "No"}</p><h3>Top Predictions:</h3><ul>"""for label, prob in results.get("predictions", [])[:5]:html_result += f"<li>{label}: {prob:.1%}</li>"html_result += "</ul></div>"return html_resultdef format_text_results(self, results):html_result = f"""<div class="medisync-card medisync-card-bg medisync-force-text"><h2 class="medisync-title medisync-green">Text Analysis Results</h2><p><strong>Sentiment:</strong> {results.get("sentiment", "Unknown").title()}</p><h3>Key Findings:</h3><ul>"""for finding in results.get("key_findings", []):html_result += f"<li>{finding}</li>"html_result += "</ul>"html_result += "<h3>Extracted Entities:</h3><ul>"for entity in results.get("entities", [])[:5]:html_result += f"<li><strong>{entity['text']}</strong> ({entity['type']}) - {entity['confidence']:.1%}</li>"html_result += "</ul></div>"return html_resultdef format_multimodal_results(self, results):html_result = f"""<div class="medisync-card medisync-card-bg medisync-force-text"><h2 class="medisync-title medisync-purple">Multimodal Analysis Results</h2><p><strong>Combined Finding:</strong> {results.get("combined_finding", "Unknown")}</p><p><strong>Overall Confidence:</strong> {results.get("confidence", 0):.1%}</p><h3>Image Contribution:</h3><p>{results.get("image_contribution", "No image analysis available")}</p><h3>Text Contribution:</h3><p>{results.get("text_contribution", "No text analysis available")}</p><h3>Recommendations:</h3><ul>"""for rec in results.get("recommendations", []):html_result += f"<li>{rec}</li>"html_result += "</ul></div>"return html_resultdef plot_image_prediction(self, image, predictions, title):fig, ax = plt.subplots(figsize=(10, 6))ax.imshow(image)ax.set_title(title, fontsize=14, fontweight='bold', color='#007bff')ax.axis('off')return figdef create_entity_visualization(self, entities):if not entities:return "<p>No entities found in text.</p>"fig, ax = plt.subplots(figsize=(10, 6))entity_types = {}for entity in entities:entity_type = entity['type']if entity_type not in entity_types:entity_types[entity_type] = 0entity_types[entity_type] += 1if entity_types:ax.bar(entity_types.keys(), entity_types.values(), color='#00bfae')ax.set_title('Entity Types Found in Text', fontsize=14, fontweight='bold', color='#00bfae')ax.set_ylabel('Count', color='#00bfae')plt.xticks(rotation=45, color='#222')plt.yticks(color='#222')return self.fig_to_html(fig)def create_multimodal_visualization(self, results):fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))confidence = results.get("confidence", 0)ax1.pie([confidence, 1-confidence], labels=['Confidence', 'Uncertainty'],colors=['#00bfae', '#ff7675'], autopct='%1.1f%%', textprops={'color': '#222'})ax1.set_title('Analysis Confidence', fontweight='bold', color='#00bfae')recommendations = results.get("recommendations", [])ax2.bar(['Recommendations'], [len(recommendations)], color='#6c63ff')ax2.set_title('Number of Recommendations', fontweight='bold', color='#6c63ff')ax2.set_ylabel('Count', color='#6c63ff')plt.tight_layout()return self.fig_to_html(fig)def fig_to_html(self, fig):import ioimport base64buf = io.BytesIO()fig.savefig(buf, format='png', bbox_inches='tight', dpi=100, facecolor=fig.get_facecolor())buf.seek(0)img_str = base64.b64encode(buf.read()).decode()buf.close()return f'<img src="data:image/png;base64,{img_str}" style="max-width: 100%; height: auto; background: transparent;"/>'def complete_appointment(appointment_id):try:385 unmodified lines38638738838939039139239339439539639739839911 unmodified lines4114124134144154164174184194204214224234244254264274284294301 unmodified line432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702385 unmodified linesimport loggingimport osimport sysimport tempfilefrom pathlib import Pathimport requestsimport gradio as grimport matplotlib.pyplot as pltfrom PIL import Image# Import configuration for end consultation logictry:from .config import get_flask_urls, get_doctors_page_urls, TIMEOUT_SETTINGSexcept ImportError:11 unmodified lines}TIMEOUT_SETTINGS = {"connection_timeout": 5, "request_timeout": 10}# Add parent directory to pathparent_dir = os.path.dirname(os.path.abspath(__file__))sys.path.append(parent_dir)# Import our modules for model and utility logicfrom models.multimodal_fusion import MultimodalFusionfrom utils.preprocessing import enhance_xray_image, normalize_report_textfrom utils.visualization import (plot_image_prediction,plot_multimodal_results,plot_report_entities,)# Set up logginglogging.basicConfig(level=logging.INFO,format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",1 unmodified line)logger = logging.getLogger(__name__)# Ensure sample data directory existsos.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 = Noneself.image_model = Noneself.text_model = Nonedef 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_analyzerself.text_model = self.fusion_model.text_analyzerself.logger.info("Models loaded successfully")return Trueexcept Exception as e:self.logger.error(f"Error loading models: {e}")return Falsedef analyze_image(self, image):"""Analyze a medical image.Args:image: Image file uploaded through GradioReturns:tuple: (image, image_results_html, plot_as_html)"""try:if image is None:return None, "Please upload an image first.", Noneif not self.load_models() or self.image_model is None:return image, "Error: Models not loaded properly.", Nonetemp_dir = tempfile.mkdtemp()temp_path = os.path.join(temp_dir, "upload.png")if isinstance(image, str):from shutil import copyfilecopyfile(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"""<div class="medisync-card medisync-card-bg medisync-force-text"><h2 class="medisync-title medisync-blue">X-ray Analysis Results</h2><p><strong>Primary Finding:</strong> {results.get("primary_finding", "Unknown")}</p><p><strong>Confidence:</strong> {results.get("confidence", 0):.1%}</p><p><strong>Abnormality Detected:</strong> {"Yes" if results.get("has_abnormality", False) else "No"}</p><h3>Top Predictions:</h3><ul>"""for label, prob in results.get("predictions", [])[:5]:html_result += f"<li>{label}: {prob:.1%}</li>"html_result += "</ul>"explanation = self.image_model.get_explanation(results)html_result += f"<h3>Analysis Explanation:</h3><p>{explanation}</p>"html_result += "</div>"return image, html_result, plot_htmlexcept Exception as e:self.logger.error(f"Error in image analysis: {e}")return image, f"Error analyzing image: {str(e)}", Nonedef analyze_text(self, text):"""Analyze a medical report text.Args:text: Report text input through GradioReturns:tuple: (text, text_results_html, entities_plot_html)"""try:if not text or text.strip() == "":return "", "Please enter medical report text.", Noneif not self.load_models() or self.text_model is None:return text, "Error: Models not loaded properly.", Noneif not text or len(text.strip()) < 10:return (text,"Error: Please enter a valid medical report text (at least 10 characters).",None,)normalized_text = normalize_report_text(text)self.logger.info("Analyzing medical report text")results = self.text_model.analyze(normalized_text)entities = results.get("entities", {})fig = plot_report_entities(normalized_text, entities)entities_plot_html = self.fig_to_html(fig)html_result = f"""<div class="medisync-card medisync-card-bg medisync-force-text"><h2 class="medisync-title medisync-green">Text Analysis Results</h2><p><strong>Severity Level:</strong> {results.get("severity", {}).get("level", "Unknown")}</p><p><strong>Severity Score:</strong> {results.get("severity", {}).get("score", 0)}/4</p><p><strong>Confidence:</strong> {results.get("severity", {}).get("confidence", 0):.1%}</p><h3>Key Findings:</h3><ul>"""findings = results.get("findings", [])if findings:for finding in findings:html_result += f"<li>{finding}</li>"else:html_result += "<li>No specific findings detailed.</li>"html_result += "</ul>"html_result += "<h3>Extracted Medical Entities:</h3>"for category, items in entities.items():if items:html_result += f"<p><strong>{category.capitalize()}:</strong> {', '.join(items)}</p>"html_result += "<h3>Follow-up Recommendations:</h3><ul>"followups = results.get("followup_recommendations", [])if followups:for rec in followups:html_result += f"<li>{rec}</li>"else:html_result += "<li>No specific follow-up recommendations.</li>"html_result += "</ul></div>"return text, html_result, entities_plot_htmlexcept Exception as e:self.logger.error(f"Error in text analysis: {e}")return text, f"Error analyzing text: {str(e)}", Nonedef analyze_multimodal(self, image, text):"""Perform multimodal analysis of image and text.Args:image: Image file uploaded through Gradiotext: Report text input through GradioReturns:tuple: (results_html, multimodal_plot_html)"""try:if not self.load_models() or self.fusion_model is None:return "Error: Models not loaded properly.", Noneif image is None:return "Error: Please upload an X-ray image for analysis.", Noneif not text or len(text.strip()) < 10:return ("Error: Please enter a valid medical report text (at least 10 characters).",None,)temp_dir = tempfile.mkdtemp()temp_path = os.path.join(temp_dir, "upload.png")if isinstance(image, str):from shutil import copyfilecopyfile(image, temp_path)else:image.save(temp_path)normalized_text = normalize_report_text(text)self.logger.info("Performing multimodal analysis")results = self.fusion_model.analyze(temp_path, normalized_text)fig = plot_multimodal_results(results, image, text)plot_html = self.fig_to_html(fig)explanation = self.fusion_model.get_explanation(results)html_result = f"""<div class="medisync-card medisync-card-bg medisync-force-text"><h2 class="medisync-title medisync-purple">Multimodal Analysis Results</h2><h3>Overview</h3><p><strong>Primary Finding:</strong> {results.get("primary_finding", "Unknown")}</p><p><strong>Severity Level:</strong> {results.get("severity", {}).get("level", "Unknown")}</p><p><strong>Severity Score:</strong> {results.get("severity", {}).get("score", 0)}/4</p><p><strong>Agreement Score:</strong> {results.get("agreement_score", 0):.0%}</p><h3>Detailed Findings</h3><ul>"""findings = results.get("findings", [])if findings:for finding in findings:html_result += f"<li>{finding}</li>"else:html_result += "<li>No specific findings detailed.</li>"html_result += "</ul>"html_result += "<h3>Recommended Follow-up</h3><ul>"followups = results.get("followup_recommendations", [])if followups:for rec in followups:html_result += f"<li>{rec}</li>"else:html_result += "<li>No specific follow-up recommendations provided.</li>"html_result += "</ul>"confidence = results.get("severity", {}).get("confidence", 0)html_result += f"""<p><em>Note: This analysis has a confidence level of {confidence:.0%}.Please consult with healthcare professionals for official diagnosis.</em></p><h3>Analysis Explanation:</h3><p>{explanation}</p></div>"""return html_result, plot_htmlexcept Exception as e:self.logger.error(f"Error in multimodal analysis: {e}")return f"Error in multimodal analysis: {str(e)}", Nonedef enhance_image(self, image):"""Enhance X-ray image contrast.Args:image: Image file uploaded through GradioReturns:PIL.Image: Enhanced image"""try:if image is None:return Nonetemp_dir = tempfile.mkdtemp()temp_path = os.path.join(temp_dir, "upload.png")if isinstance(image, str):from shutil import copyfilecopyfile(image, temp_path)else:image.save(temp_path)self.logger.info(f"Enhancing image: {temp_path}")output_path = os.path.join(temp_dir, "enhanced.png")enhance_xray_image(temp_path, output_path)enhanced = Image.open(output_path)return enhancedexcept Exception as e:self.logger.error(f"Error enhancing image: {e}")return imagedef fig_to_html(self, fig):"""Convert matplotlib figure to HTML for display in Gradio."""try:import base64import iobuf = io.BytesIO()fig.savefig(buf, format="png", bbox_inches="tight", dpi=100, facecolor=fig.get_facecolor())buf.seek(0)img_str = base64.b64encode(buf.read()).decode("utf-8")plt.close(fig)return f'<img src="data:image/png;base64,{img_str}" style="max-width: 100%; height: auto; background: transparent;"/>'except Exception as e:self.logger.error(f"Error converting figure to HTML: {e}")return "<p>Error displaying visualization.</p>"def complete_appointment(appointment_id):try:
mediSync/app.py+234-176