updated code✅✅
Browse files- mediSync/app.py +60 -102
mediSync/app.py
CHANGED
|
@@ -399,11 +399,9 @@ class MediSyncApp:
|
|
| 399 |
|
| 400 |
|
| 401 |
def create_interface():
|
| 402 |
-
"""Create and launch the Gradio interface
|
| 403 |
-
|
| 404 |
app = MediSyncApp()
|
| 405 |
|
| 406 |
-
# Example medical report for demo
|
| 407 |
example_report = """
|
| 408 |
CHEST X-RAY EXAMINATION
|
| 409 |
|
|
@@ -422,52 +420,66 @@ def create_interface():
|
|
| 422 |
RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
|
| 423 |
"""
|
| 424 |
|
| 425 |
-
# Get sample image path with robust error handling
|
| 426 |
sample_image_path = None
|
| 427 |
try:
|
| 428 |
sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
|
| 429 |
os.makedirs(sample_images_dir, exist_ok=True)
|
| 430 |
-
|
| 431 |
-
# Check for existing images first
|
| 432 |
sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
|
| 433 |
|
| 434 |
if not sample_images:
|
| 435 |
-
# Download fallback sample image if none exist
|
| 436 |
fallback_url = "https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr2_lrg-a.jpg"
|
| 437 |
sample_path = sample_images_dir / "sample_xray.jpg"
|
| 438 |
-
|
| 439 |
try:
|
| 440 |
response = requests.get(fallback_url, timeout=10)
|
| 441 |
if response.status_code == 200:
|
| 442 |
with open(sample_path, 'wb') as f:
|
| 443 |
f.write(response.content)
|
| 444 |
sample_image_path = str(sample_path)
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
except Exception as download_error:
|
| 449 |
-
logging.warning(f"Could not download sample image: {str(download_error)}")
|
| 450 |
else:
|
| 451 |
sample_image_path = str(sample_images[0])
|
| 452 |
except Exception as e:
|
| 453 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
-
# Define interface with robust parameter handling
|
| 456 |
with gr.Blocks(
|
| 457 |
-
title="MediSync: Multi-Modal Medical Analysis System",
|
| 458 |
-
theme=gr.themes.Soft()
|
|
|
|
| 459 |
) as interface:
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
|
|
|
|
|
|
|
|
|
| 471 |
)
|
| 472 |
|
| 473 |
gr.Markdown("""
|
|
@@ -475,12 +487,6 @@ def create_interface():
|
|
| 475 |
|
| 476 |
This AI-powered healthcare solution combines X-ray image analysis with patient report text processing
|
| 477 |
to provide comprehensive medical insights.
|
| 478 |
-
|
| 479 |
-
## How to Use
|
| 480 |
-
1. Upload a chest X-ray image
|
| 481 |
-
2. Enter the corresponding medical report text
|
| 482 |
-
3. Choose the analysis type: image-only, text-only, or multimodal (combined)
|
| 483 |
-
4. Click "End Consultation" when finished to complete your appointment
|
| 484 |
""")
|
| 485 |
|
| 486 |
with gr.Tab("Multimodal Analysis"):
|
|
@@ -488,18 +494,13 @@ def create_interface():
|
|
| 488 |
with gr.Column():
|
| 489 |
multi_img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
| 490 |
multi_img_enhance = gr.Button("Enhance Image")
|
| 491 |
-
|
| 492 |
multi_text_input = gr.Textbox(
|
| 493 |
label="Enter Medical Report Text",
|
| 494 |
placeholder="Enter the radiologist's report text here...",
|
| 495 |
lines=10,
|
| 496 |
value=example_report if not sample_image_path else None,
|
| 497 |
)
|
| 498 |
-
|
| 499 |
-
multi_analyze_btn = gr.Button(
|
| 500 |
-
"Analyze Image & Text", variant="primary"
|
| 501 |
-
)
|
| 502 |
-
|
| 503 |
with gr.Column():
|
| 504 |
multi_results = gr.HTML(label="Analysis Results")
|
| 505 |
multi_plot = gr.HTML(label="Visualization")
|
|
@@ -517,7 +518,6 @@ def create_interface():
|
|
| 517 |
img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
| 518 |
img_enhance = gr.Button("Enhance Image")
|
| 519 |
img_analyze_btn = gr.Button("Analyze Image", variant="primary")
|
| 520 |
-
|
| 521 |
with gr.Column():
|
| 522 |
img_output = gr.Image(label="Processed Image")
|
| 523 |
img_results = gr.HTML(label="Analysis Results")
|
|
@@ -540,7 +540,6 @@ def create_interface():
|
|
| 540 |
value=example_report,
|
| 541 |
)
|
| 542 |
text_analyze_btn = gr.Button("Analyze Text", variant="primary")
|
| 543 |
-
|
| 544 |
with gr.Column():
|
| 545 |
text_output = gr.Textbox(label="Processed Text")
|
| 546 |
text_results = gr.HTML(label="Analysis Results")
|
|
@@ -557,50 +556,25 @@ def create_interface():
|
|
| 557 |
## About MediSync
|
| 558 |
|
| 559 |
MediSync is an AI-powered healthcare solution that uses multi-modal analysis to provide comprehensive insights from medical images and reports.
|
| 560 |
-
|
| 561 |
-
### Key Features
|
| 562 |
-
|
| 563 |
-
- **X-ray Image Analysis**: Detects abnormalities in chest X-rays using pre-trained vision models
|
| 564 |
-
- **Medical Report Processing**: Extracts key information from patient reports using NLP models
|
| 565 |
-
- **Multi-modal Integration**: Combines insights from both image and text data for more accurate analysis
|
| 566 |
-
|
| 567 |
-
### Models Used
|
| 568 |
-
|
| 569 |
-
- **X-ray Analysis**: facebook/deit-base-patch16-224-medical-cxr
|
| 570 |
-
- **Medical Text Analysis**: medicalai/ClinicalBERT
|
| 571 |
-
|
| 572 |
-
### Important Disclaimer
|
| 573 |
-
|
| 574 |
-
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.
|
| 575 |
""")
|
| 576 |
|
| 577 |
-
# Consultation completion section
|
| 578 |
with gr.Row():
|
| 579 |
with gr.Column():
|
| 580 |
-
end_consultation_btn = gr.Button(
|
| 581 |
-
"End Consultation",
|
| 582 |
-
variant="stop",
|
| 583 |
-
size="lg"
|
| 584 |
-
)
|
| 585 |
completion_status = gr.HTML()
|
| 586 |
|
| 587 |
-
|
| 588 |
-
multi_img_enhance.click(
|
| 589 |
-
app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
|
| 590 |
-
)
|
| 591 |
multi_analyze_btn.click(
|
| 592 |
app.analyze_multimodal,
|
| 593 |
inputs=[multi_img_input, multi_text_input],
|
| 594 |
outputs=[multi_results, multi_plot],
|
| 595 |
)
|
| 596 |
-
|
| 597 |
img_enhance.click(app.enhance_image, inputs=img_input, outputs=img_output)
|
| 598 |
img_analyze_btn.click(
|
| 599 |
app.analyze_image,
|
| 600 |
inputs=img_input,
|
| 601 |
outputs=[img_output, img_results, img_plot],
|
| 602 |
)
|
| 603 |
-
|
| 604 |
text_analyze_btn.click(
|
| 605 |
app.analyze_text,
|
| 606 |
inputs=text_input,
|
|
@@ -608,42 +582,35 @@ def create_interface():
|
|
| 608 |
)
|
| 609 |
|
| 610 |
def complete_consultation(appointment_id):
|
| 611 |
-
"""Handle consultation completion."""
|
| 612 |
if not appointment_id:
|
| 613 |
-
return "
|
| 614 |
-
|
|
|
|
|
|
|
|
|
|
| 615 |
try:
|
| 616 |
-
# Replace with your actual Flask app URL
|
| 617 |
-
flask_app_url = "http://127.0.0.1:600/complete_consultation"
|
| 618 |
-
|
| 619 |
response = requests.post(
|
| 620 |
-
|
| 621 |
json={"appointment_id": appointment_id},
|
| 622 |
timeout=10
|
| 623 |
)
|
| 624 |
-
|
| 625 |
if response.status_code == 200:
|
| 626 |
return """
|
| 627 |
-
<div class=
|
| 628 |
Consultation completed successfully. Redirecting...
|
| 629 |
<script>
|
| 630 |
-
setTimeout(
|
| 631 |
-
window.location.href = "http://127.0.0.1:600/doctors";
|
| 632 |
-
}, 2000);
|
| 633 |
</script>
|
| 634 |
</div>
|
| 635 |
"""
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
</div>
|
| 642 |
-
"""
|
| 643 |
-
|
| 644 |
except Exception as e:
|
| 645 |
return f"""
|
| 646 |
-
<div class=
|
| 647 |
Error: {str(e)}
|
| 648 |
</div>
|
| 649 |
"""
|
|
@@ -651,19 +618,10 @@ def create_interface():
|
|
| 651 |
end_consultation_btn.click(
|
| 652 |
fn=complete_consultation,
|
| 653 |
inputs=[appointment_id],
|
| 654 |
-
outputs=completion_status
|
| 655 |
)
|
| 656 |
|
| 657 |
-
|
| 658 |
-
interface.launch()
|
| 659 |
-
except Exception as e:
|
| 660 |
-
logging.error(f"Failed to launch interface: {str(e)}")
|
| 661 |
-
raise RuntimeError("Failed to launch MediSync interface") from e
|
| 662 |
-
|
| 663 |
|
| 664 |
if __name__ == "__main__":
|
| 665 |
-
|
| 666 |
-
level=logging.INFO,
|
| 667 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 668 |
-
)
|
| 669 |
-
create_interface()
|
|
|
|
| 399 |
|
| 400 |
|
| 401 |
def create_interface():
|
| 402 |
+
"""Create and launch the Gradio interface."""
|
|
|
|
| 403 |
app = MediSyncApp()
|
| 404 |
|
|
|
|
| 405 |
example_report = """
|
| 406 |
CHEST X-RAY EXAMINATION
|
| 407 |
|
|
|
|
| 420 |
RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
|
| 421 |
"""
|
| 422 |
|
|
|
|
| 423 |
sample_image_path = None
|
| 424 |
try:
|
| 425 |
sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
|
| 426 |
os.makedirs(sample_images_dir, exist_ok=True)
|
|
|
|
|
|
|
| 427 |
sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
|
| 428 |
|
| 429 |
if not sample_images:
|
|
|
|
| 430 |
fallback_url = "https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr2_lrg-a.jpg"
|
| 431 |
sample_path = sample_images_dir / "sample_xray.jpg"
|
|
|
|
| 432 |
try:
|
| 433 |
response = requests.get(fallback_url, timeout=10)
|
| 434 |
if response.status_code == 200:
|
| 435 |
with open(sample_path, 'wb') as f:
|
| 436 |
f.write(response.content)
|
| 437 |
sample_image_path = str(sample_path)
|
| 438 |
+
logger.info("Downloaded fallback sample image")
|
| 439 |
+
except Exception as e:
|
| 440 |
+
logger.warning(f"Could not download sample image: {str(e)}")
|
|
|
|
|
|
|
| 441 |
else:
|
| 442 |
sample_image_path = str(sample_images[0])
|
| 443 |
except Exception as e:
|
| 444 |
+
logger.error(f"Error handling sample images: {str(e)}")
|
| 445 |
+
|
| 446 |
+
custom_css = """
|
| 447 |
+
.alert-box {
|
| 448 |
+
padding: 15px;
|
| 449 |
+
margin: 10px 0;
|
| 450 |
+
border-radius: 5px;
|
| 451 |
+
}
|
| 452 |
+
.alert-error {
|
| 453 |
+
background-color: #ffebee;
|
| 454 |
+
color: #b71c1c;
|
| 455 |
+
border-left: 5px solid #b71c1c;
|
| 456 |
+
}
|
| 457 |
+
.alert-success {
|
| 458 |
+
background-color: #e8f5e9;
|
| 459 |
+
color: #1b5e20;
|
| 460 |
+
border-left: 5px solid #1b5e20;
|
| 461 |
+
}
|
| 462 |
+
"""
|
| 463 |
|
|
|
|
| 464 |
with gr.Blocks(
|
| 465 |
+
title="MediSync: Multi-Modal Medical Analysis System",
|
| 466 |
+
theme=gr.themes.Soft(),
|
| 467 |
+
css=custom_css
|
| 468 |
) as interface:
|
| 469 |
+
|
| 470 |
+
appointment_id = gr.Textbox(visible=False)
|
| 471 |
+
|
| 472 |
+
interface.load(
|
| 473 |
+
None,
|
| 474 |
+
None,
|
| 475 |
+
None,
|
| 476 |
+
_js="""
|
| 477 |
+
function getAppointmentId() {
|
| 478 |
+
const urlParams = new URLSearchParams(window.location.search);
|
| 479 |
+
return urlParams.get('appointment_id') || '';
|
| 480 |
+
}
|
| 481 |
+
document.getElementById('appointment-id').value = getAppointmentId();
|
| 482 |
+
"""
|
| 483 |
)
|
| 484 |
|
| 485 |
gr.Markdown("""
|
|
|
|
| 487 |
|
| 488 |
This AI-powered healthcare solution combines X-ray image analysis with patient report text processing
|
| 489 |
to provide comprehensive medical insights.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
""")
|
| 491 |
|
| 492 |
with gr.Tab("Multimodal Analysis"):
|
|
|
|
| 494 |
with gr.Column():
|
| 495 |
multi_img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
| 496 |
multi_img_enhance = gr.Button("Enhance Image")
|
|
|
|
| 497 |
multi_text_input = gr.Textbox(
|
| 498 |
label="Enter Medical Report Text",
|
| 499 |
placeholder="Enter the radiologist's report text here...",
|
| 500 |
lines=10,
|
| 501 |
value=example_report if not sample_image_path else None,
|
| 502 |
)
|
| 503 |
+
multi_analyze_btn = gr.Button("Analyze Image & Text", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
with gr.Column():
|
| 505 |
multi_results = gr.HTML(label="Analysis Results")
|
| 506 |
multi_plot = gr.HTML(label="Visualization")
|
|
|
|
| 518 |
img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
| 519 |
img_enhance = gr.Button("Enhance Image")
|
| 520 |
img_analyze_btn = gr.Button("Analyze Image", variant="primary")
|
|
|
|
| 521 |
with gr.Column():
|
| 522 |
img_output = gr.Image(label="Processed Image")
|
| 523 |
img_results = gr.HTML(label="Analysis Results")
|
|
|
|
| 540 |
value=example_report,
|
| 541 |
)
|
| 542 |
text_analyze_btn = gr.Button("Analyze Text", variant="primary")
|
|
|
|
| 543 |
with gr.Column():
|
| 544 |
text_output = gr.Textbox(label="Processed Text")
|
| 545 |
text_results = gr.HTML(label="Analysis Results")
|
|
|
|
| 556 |
## About MediSync
|
| 557 |
|
| 558 |
MediSync is an AI-powered healthcare solution that uses multi-modal analysis to provide comprehensive insights from medical images and reports.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 559 |
""")
|
| 560 |
|
|
|
|
| 561 |
with gr.Row():
|
| 562 |
with gr.Column():
|
| 563 |
+
end_consultation_btn = gr.Button("End Consultation", variant="stop", size="lg")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 564 |
completion_status = gr.HTML()
|
| 565 |
|
| 566 |
+
multi_img_enhance.click(app.enhance_image, inputs=multi_img_input, outputs=multi_img_input)
|
|
|
|
|
|
|
|
|
|
| 567 |
multi_analyze_btn.click(
|
| 568 |
app.analyze_multimodal,
|
| 569 |
inputs=[multi_img_input, multi_text_input],
|
| 570 |
outputs=[multi_results, multi_plot],
|
| 571 |
)
|
|
|
|
| 572 |
img_enhance.click(app.enhance_image, inputs=img_input, outputs=img_output)
|
| 573 |
img_analyze_btn.click(
|
| 574 |
app.analyze_image,
|
| 575 |
inputs=img_input,
|
| 576 |
outputs=[img_output, img_results, img_plot],
|
| 577 |
)
|
|
|
|
| 578 |
text_analyze_btn.click(
|
| 579 |
app.analyze_text,
|
| 580 |
inputs=text_input,
|
|
|
|
| 582 |
)
|
| 583 |
|
| 584 |
def complete_consultation(appointment_id):
|
|
|
|
| 585 |
if not appointment_id:
|
| 586 |
+
return """
|
| 587 |
+
<div class="alert-box alert-error">
|
| 588 |
+
No appointment ID found. Please contact support.
|
| 589 |
+
</div>
|
| 590 |
+
"""
|
| 591 |
try:
|
|
|
|
|
|
|
|
|
|
| 592 |
response = requests.post(
|
| 593 |
+
"http://127.0.0.1:600/complete_consultation",
|
| 594 |
json={"appointment_id": appointment_id},
|
| 595 |
timeout=10
|
| 596 |
)
|
|
|
|
| 597 |
if response.status_code == 200:
|
| 598 |
return """
|
| 599 |
+
<div class="alert-box alert-success">
|
| 600 |
Consultation completed successfully. Redirecting...
|
| 601 |
<script>
|
| 602 |
+
setTimeout(() => window.location.href = "http://127.0.0.1:600/doctors", 2000);
|
|
|
|
|
|
|
| 603 |
</script>
|
| 604 |
</div>
|
| 605 |
"""
|
| 606 |
+
return f"""
|
| 607 |
+
<div class="alert-box alert-error">
|
| 608 |
+
Error completing appointment (Status: {response.status_code}).
|
| 609 |
+
</div>
|
| 610 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 611 |
except Exception as e:
|
| 612 |
return f"""
|
| 613 |
+
<div class="alert-box alert-error">
|
| 614 |
Error: {str(e)}
|
| 615 |
</div>
|
| 616 |
"""
|
|
|
|
| 618 |
end_consultation_btn.click(
|
| 619 |
fn=complete_consultation,
|
| 620 |
inputs=[appointment_id],
|
| 621 |
+
outputs=[completion_status]
|
| 622 |
)
|
| 623 |
|
| 624 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
|
| 626 |
if __name__ == "__main__":
|
| 627 |
+
create_interface()
|
|
|
|
|
|
|
|
|
|
|
|