updated code ✅✅
Browse files- mediSync/app.py +122 -34
mediSync/app.py
CHANGED
|
@@ -449,7 +449,8 @@ class MediSyncApp:
|
|
| 449 |
)
|
| 450 |
|
| 451 |
def create_interface():
|
| 452 |
-
"""Create and launch the Gradio interface
|
|
|
|
| 453 |
app = MediSyncApp()
|
| 454 |
|
| 455 |
# Example medical report for demo
|
|
@@ -471,24 +472,53 @@ def create_interface():
|
|
| 471 |
RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
|
| 472 |
"""
|
| 473 |
|
| 474 |
-
# Get sample image
|
| 475 |
sample_image_path = None
|
| 476 |
try:
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 483 |
except Exception as e:
|
| 484 |
-
logging.
|
| 485 |
|
| 486 |
-
# Define interface
|
| 487 |
with gr.Blocks(
|
| 488 |
title="MediSync: Multi-Modal Medical Analysis System",
|
| 489 |
-
theme=gr.themes.Soft()
|
| 490 |
-
css=".alert {padding: 10px; border-radius: 5px;} .alert-error {background-color: #ffebee;} .alert-success {background-color: #e8f5e9;}"
|
| 491 |
) as interface:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
gr.Markdown("""
|
| 493 |
# MediSync: Multi-Modal Medical Analysis System
|
| 494 |
|
|
@@ -499,20 +529,22 @@ def create_interface():
|
|
| 499 |
1. Upload a chest X-ray image
|
| 500 |
2. Enter the corresponding medical report text
|
| 501 |
3. Choose the analysis type: image-only, text-only, or multimodal (combined)
|
| 502 |
-
4.
|
| 503 |
""")
|
| 504 |
|
| 505 |
with gr.Tab("Multimodal Analysis"):
|
| 506 |
with gr.Row():
|
| 507 |
with gr.Column():
|
| 508 |
multi_img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
| 509 |
-
multi_img_enhance = gr.Button("Enhance Image"
|
|
|
|
| 510 |
multi_text_input = gr.Textbox(
|
| 511 |
label="Enter Medical Report Text",
|
| 512 |
placeholder="Enter the radiologist's report text here...",
|
| 513 |
lines=10,
|
| 514 |
-
value=example_report,
|
| 515 |
)
|
|
|
|
| 516 |
multi_analyze_btn = gr.Button(
|
| 517 |
"Analyze Image & Text", variant="primary"
|
| 518 |
)
|
|
@@ -532,7 +564,7 @@ def create_interface():
|
|
| 532 |
with gr.Row():
|
| 533 |
with gr.Column():
|
| 534 |
img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
| 535 |
-
img_enhance = gr.Button("Enhance Image"
|
| 536 |
img_analyze_btn = gr.Button("Analyze Image", variant="primary")
|
| 537 |
|
| 538 |
with gr.Column():
|
|
@@ -559,7 +591,7 @@ def create_interface():
|
|
| 559 |
text_analyze_btn = gr.Button("Analyze Text", variant="primary")
|
| 560 |
|
| 561 |
with gr.Column():
|
| 562 |
-
text_output = gr.Textbox(label="Processed Text"
|
| 563 |
text_results = gr.HTML(label="Analysis Results")
|
| 564 |
text_plot = gr.HTML(label="Entity Visualization")
|
| 565 |
|
|
@@ -577,20 +609,33 @@ def create_interface():
|
|
| 577 |
|
| 578 |
### Key Features
|
| 579 |
|
| 580 |
-
- **X-ray Image Analysis**: Detects abnormalities in chest X-rays
|
| 581 |
-
- **Medical Report Processing**: Extracts key information from patient reports
|
| 582 |
-
- **Multi-modal Integration**: Combines insights from both image and text data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 583 |
|
| 584 |
### Important Disclaimer
|
| 585 |
|
| 586 |
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.
|
| 587 |
""")
|
| 588 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 589 |
# Set up event handlers
|
| 590 |
multi_img_enhance.click(
|
| 591 |
-
app.enhance_image,
|
| 592 |
-
inputs=multi_img_input,
|
| 593 |
-
outputs=multi_img_input
|
| 594 |
)
|
| 595 |
multi_analyze_btn.click(
|
| 596 |
app.analyze_multimodal,
|
|
@@ -598,11 +643,7 @@ def create_interface():
|
|
| 598 |
outputs=[multi_results, multi_plot],
|
| 599 |
)
|
| 600 |
|
| 601 |
-
img_enhance.click(
|
| 602 |
-
app.enhance_image,
|
| 603 |
-
inputs=img_input,
|
| 604 |
-
outputs=img_output
|
| 605 |
-
)
|
| 606 |
img_analyze_btn.click(
|
| 607 |
app.analyze_image,
|
| 608 |
inputs=img_input,
|
|
@@ -615,12 +656,59 @@ def create_interface():
|
|
| 615 |
outputs=[text_output, text_results, text_plot],
|
| 616 |
)
|
| 617 |
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 624 |
|
| 625 |
if __name__ == "__main__":
|
| 626 |
logging.basicConfig(
|
|
|
|
| 449 |
)
|
| 450 |
|
| 451 |
def create_interface():
|
| 452 |
+
"""Create and launch the Gradio interface with all fixes implemented."""
|
| 453 |
+
|
| 454 |
app = MediSyncApp()
|
| 455 |
|
| 456 |
# Example medical report for demo
|
|
|
|
| 472 |
RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
|
| 473 |
"""
|
| 474 |
|
| 475 |
+
# Get sample image path with robust error handling
|
| 476 |
sample_image_path = None
|
| 477 |
try:
|
| 478 |
+
sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
|
| 479 |
+
os.makedirs(sample_images_dir, exist_ok=True)
|
| 480 |
+
|
| 481 |
+
# Check for existing images first
|
| 482 |
+
sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
|
| 483 |
+
|
| 484 |
+
if not sample_images:
|
| 485 |
+
# Download fallback sample image if none exist
|
| 486 |
+
fallback_url = "https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr2_lrg-a.jpg"
|
| 487 |
+
sample_path = sample_images_dir / "sample_xray.jpg"
|
| 488 |
+
|
| 489 |
+
try:
|
| 490 |
+
response = requests.get(fallback_url, timeout=10)
|
| 491 |
+
if response.status_code == 200:
|
| 492 |
+
with open(sample_path, 'wb') as f:
|
| 493 |
+
f.write(response.content)
|
| 494 |
+
sample_image_path = str(sample_path)
|
| 495 |
+
logging.info("Successfully downloaded fallback sample image")
|
| 496 |
+
else:
|
| 497 |
+
logging.warning(f"Failed to download sample image. Status code: {response.status_code}")
|
| 498 |
+
except Exception as download_error:
|
| 499 |
+
logging.warning(f"Could not download sample image: {str(download_error)}")
|
| 500 |
+
else:
|
| 501 |
+
sample_image_path = str(sample_images[0])
|
| 502 |
except Exception as e:
|
| 503 |
+
logging.error(f"Error setting up sample images: {str(e)}")
|
| 504 |
|
| 505 |
+
# Define interface with robust parameter handling
|
| 506 |
with gr.Blocks(
|
| 507 |
title="MediSync: Multi-Modal Medical Analysis System",
|
| 508 |
+
theme=gr.themes.Soft()
|
|
|
|
| 509 |
) as interface:
|
| 510 |
+
# Get appointment ID from URL parameters using JavaScript
|
| 511 |
+
appointment_id = gr.Textbox(
|
| 512 |
+
visible=False,
|
| 513 |
+
value="",
|
| 514 |
+
_js="""
|
| 515 |
+
function() {
|
| 516 |
+
const params = new URLSearchParams(window.location.search);
|
| 517 |
+
return params.get('appointment_id') || '';
|
| 518 |
+
}
|
| 519 |
+
"""
|
| 520 |
+
)
|
| 521 |
+
|
| 522 |
gr.Markdown("""
|
| 523 |
# MediSync: Multi-Modal Medical Analysis System
|
| 524 |
|
|
|
|
| 529 |
1. Upload a chest X-ray image
|
| 530 |
2. Enter the corresponding medical report text
|
| 531 |
3. Choose the analysis type: image-only, text-only, or multimodal (combined)
|
| 532 |
+
4. Click "End Consultation" when finished to complete your appointment
|
| 533 |
""")
|
| 534 |
|
| 535 |
with gr.Tab("Multimodal Analysis"):
|
| 536 |
with gr.Row():
|
| 537 |
with gr.Column():
|
| 538 |
multi_img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
| 539 |
+
multi_img_enhance = gr.Button("Enhance Image")
|
| 540 |
+
|
| 541 |
multi_text_input = gr.Textbox(
|
| 542 |
label="Enter Medical Report Text",
|
| 543 |
placeholder="Enter the radiologist's report text here...",
|
| 544 |
lines=10,
|
| 545 |
+
value=example_report if not sample_image_path else None,
|
| 546 |
)
|
| 547 |
+
|
| 548 |
multi_analyze_btn = gr.Button(
|
| 549 |
"Analyze Image & Text", variant="primary"
|
| 550 |
)
|
|
|
|
| 564 |
with gr.Row():
|
| 565 |
with gr.Column():
|
| 566 |
img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
| 567 |
+
img_enhance = gr.Button("Enhance Image")
|
| 568 |
img_analyze_btn = gr.Button("Analyze Image", variant="primary")
|
| 569 |
|
| 570 |
with gr.Column():
|
|
|
|
| 591 |
text_analyze_btn = gr.Button("Analyze Text", variant="primary")
|
| 592 |
|
| 593 |
with gr.Column():
|
| 594 |
+
text_output = gr.Textbox(label="Processed Text")
|
| 595 |
text_results = gr.HTML(label="Analysis Results")
|
| 596 |
text_plot = gr.HTML(label="Entity Visualization")
|
| 597 |
|
|
|
|
| 609 |
|
| 610 |
### Key Features
|
| 611 |
|
| 612 |
+
- **X-ray Image Analysis**: Detects abnormalities in chest X-rays using pre-trained vision models
|
| 613 |
+
- **Medical Report Processing**: Extracts key information from patient reports using NLP models
|
| 614 |
+
- **Multi-modal Integration**: Combines insights from both image and text data for more accurate analysis
|
| 615 |
+
|
| 616 |
+
### Models Used
|
| 617 |
+
|
| 618 |
+
- **X-ray Analysis**: facebook/deit-base-patch16-224-medical-cxr
|
| 619 |
+
- **Medical Text Analysis**: medicalai/ClinicalBERT
|
| 620 |
|
| 621 |
### Important Disclaimer
|
| 622 |
|
| 623 |
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.
|
| 624 |
""")
|
| 625 |
|
| 626 |
+
# Consultation completion section
|
| 627 |
+
with gr.Row():
|
| 628 |
+
with gr.Column():
|
| 629 |
+
end_consultation_btn = gr.Button(
|
| 630 |
+
"End Consultation",
|
| 631 |
+
variant="stop",
|
| 632 |
+
size="lg"
|
| 633 |
+
)
|
| 634 |
+
completion_status = gr.HTML()
|
| 635 |
+
|
| 636 |
# Set up event handlers
|
| 637 |
multi_img_enhance.click(
|
| 638 |
+
app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
|
|
|
|
|
|
|
| 639 |
)
|
| 640 |
multi_analyze_btn.click(
|
| 641 |
app.analyze_multimodal,
|
|
|
|
| 643 |
outputs=[multi_results, multi_plot],
|
| 644 |
)
|
| 645 |
|
| 646 |
+
img_enhance.click(app.enhance_image, inputs=img_input, outputs=img_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 647 |
img_analyze_btn.click(
|
| 648 |
app.analyze_image,
|
| 649 |
inputs=img_input,
|
|
|
|
| 656 |
outputs=[text_output, text_results, text_plot],
|
| 657 |
)
|
| 658 |
|
| 659 |
+
def complete_consultation(appointment_id):
|
| 660 |
+
"""Handle consultation completion."""
|
| 661 |
+
if not appointment_id:
|
| 662 |
+
return "<div class='alert alert-error'>No appointment ID found. Please contact support.</div>"
|
| 663 |
+
|
| 664 |
+
try:
|
| 665 |
+
# Replace with your actual Flask app URL
|
| 666 |
+
flask_app_url = "http://127.0.0.1:600/complete_consultation"
|
| 667 |
+
|
| 668 |
+
response = requests.post(
|
| 669 |
+
flask_app_url,
|
| 670 |
+
json={"appointment_id": appointment_id},
|
| 671 |
+
timeout=10
|
| 672 |
+
)
|
| 673 |
+
|
| 674 |
+
if response.status_code == 200:
|
| 675 |
+
return """
|
| 676 |
+
<div class='alert alert-success'>
|
| 677 |
+
Consultation completed successfully. Redirecting...
|
| 678 |
+
<script>
|
| 679 |
+
setTimeout(function() {
|
| 680 |
+
window.location.href = "http://127.0.0.1:600/doctors";
|
| 681 |
+
}, 2000);
|
| 682 |
+
</script>
|
| 683 |
+
</div>
|
| 684 |
+
"""
|
| 685 |
+
else:
|
| 686 |
+
return f"""
|
| 687 |
+
<div class='alert alert-error'>
|
| 688 |
+
Error completing appointment (Status: {response.status_code}).
|
| 689 |
+
Please contact support.
|
| 690 |
+
</div>
|
| 691 |
+
"""
|
| 692 |
+
|
| 693 |
+
except Exception as e:
|
| 694 |
+
return f"""
|
| 695 |
+
<div class='alert alert-error'>
|
| 696 |
+
Error: {str(e)}
|
| 697 |
+
</div>
|
| 698 |
+
"""
|
| 699 |
+
|
| 700 |
+
end_consultation_btn.click(
|
| 701 |
+
fn=complete_consultation,
|
| 702 |
+
inputs=[appointment_id],
|
| 703 |
+
outputs=completion_status
|
| 704 |
+
)
|
| 705 |
+
|
| 706 |
+
try:
|
| 707 |
+
interface.launch()
|
| 708 |
+
except Exception as e:
|
| 709 |
+
logging.error(f"Failed to launch interface: {str(e)}")
|
| 710 |
+
raise RuntimeError("Failed to launch MediSync interface") from e
|
| 711 |
+
|
| 712 |
|
| 713 |
if __name__ == "__main__":
|
| 714 |
logging.basicConfig(
|