updated the code ✅✅ will see tomorrow
Browse files- mediSync/app.py +197 -116
mediSync/app.py
CHANGED
|
@@ -399,9 +399,11 @@ class MediSyncApp:
|
|
| 399 |
|
| 400 |
|
| 401 |
def create_interface():
|
| 402 |
-
"""Create and launch the Gradio interface with all fixes."""
|
|
|
|
| 403 |
app = MediSyncApp()
|
| 404 |
|
|
|
|
| 405 |
example_report = """
|
| 406 |
CHEST X-RAY EXAMINATION
|
| 407 |
|
|
@@ -420,74 +422,52 @@ def create_interface():
|
|
| 420 |
RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
|
| 421 |
"""
|
| 422 |
|
| 423 |
-
#
|
| 424 |
sample_image_path = None
|
| 425 |
try:
|
| 426 |
sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
|
| 427 |
os.makedirs(sample_images_dir, exist_ok=True)
|
| 428 |
|
| 429 |
-
#
|
| 430 |
sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
|
| 431 |
|
| 432 |
if not sample_images:
|
| 433 |
-
#
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
else:
|
| 441 |
sample_image_path = str(sample_images[0])
|
| 442 |
except Exception as e:
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
custom_css = """
|
| 446 |
-
.alert-box {
|
| 447 |
-
padding: 15px;
|
| 448 |
-
margin: 10px 0;
|
| 449 |
-
border-radius: 5px;
|
| 450 |
-
font-family: sans-serif;
|
| 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 |
-
|
| 471 |
-
|
| 472 |
-
'
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
# Initialize appointment ID from URL
|
| 477 |
-
def get_appointment_id():
|
| 478 |
-
try:
|
| 479 |
-
from gradio.context import Context
|
| 480 |
-
if hasattr(Context, 'request') and hasattr(Context.request, 'query_params'):
|
| 481 |
-
appointment_id = Context.request.query_params.get("appointment_id", "")
|
| 482 |
-
if appointment_id:
|
| 483 |
-
return {'recorded': False, 'appointment_id': appointment_id}
|
| 484 |
-
except Exception as e:
|
| 485 |
-
logger.warning(f"Could not get URL parameters: {str(e)}")
|
| 486 |
-
return {'recorded': False, 'appointment_id': ''}
|
| 487 |
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
)
|
| 492 |
|
| 493 |
gr.Markdown("""
|
|
@@ -495,94 +475,195 @@ def create_interface():
|
|
| 495 |
|
| 496 |
This AI-powered healthcare solution combines X-ray image analysis with patient report text processing
|
| 497 |
to provide comprehensive medical insights.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 498 |
""")
|
| 499 |
|
| 500 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 502 |
with gr.Row():
|
| 503 |
with gr.Column():
|
| 504 |
-
end_consultation_btn = gr.Button(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
completion_status = gr.HTML()
|
| 506 |
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 518 |
|
| 519 |
try:
|
| 520 |
-
#
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
<div class="alert-box alert-success">
|
| 524 |
-
<strong>Consultation Already Completed</strong>
|
| 525 |
-
<p>You will be redirected shortly.</p>
|
| 526 |
-
<script>
|
| 527 |
-
setTimeout(() => window.location.href = "http://127.0.0.1:600/doctors", 1000);
|
| 528 |
-
</script>
|
| 529 |
-
</div>
|
| 530 |
-
"""
|
| 531 |
-
return session_state, success_html
|
| 532 |
-
|
| 533 |
-
# Call your Flask endpoint to record completion
|
| 534 |
response = requests.post(
|
| 535 |
-
|
| 536 |
-
json={"appointment_id":
|
| 537 |
timeout=10
|
| 538 |
)
|
| 539 |
-
|
| 540 |
-
if response.
|
| 541 |
-
|
| 542 |
-
<div class=
|
| 543 |
-
|
| 544 |
-
<p>Your session has been properly recorded.</p>
|
| 545 |
-
<p>You will be redirected shortly.</p>
|
| 546 |
<script>
|
| 547 |
-
setTimeout(()
|
|
|
|
|
|
|
| 548 |
</script>
|
| 549 |
</div>
|
| 550 |
"""
|
| 551 |
-
|
| 552 |
-
return
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
<div class="alert-box alert-error">
|
| 560 |
-
<strong>Recording Failed</strong>
|
| 561 |
-
<p>{error_msg}</p>
|
| 562 |
-
<p>Please try again or contact support.</p>
|
| 563 |
-
</div>
|
| 564 |
-
"""
|
| 565 |
except Exception as e:
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
<strong>Connection Error</strong>
|
| 570 |
-
<p>{str(e)}</p>
|
| 571 |
-
<p>Please check your connection and try again.</p>
|
| 572 |
</div>
|
| 573 |
"""
|
| 574 |
|
| 575 |
end_consultation_btn.click(
|
| 576 |
fn=complete_consultation,
|
| 577 |
-
inputs=[
|
| 578 |
-
outputs=
|
| 579 |
)
|
| 580 |
-
|
| 581 |
try:
|
| 582 |
interface.launch()
|
| 583 |
except Exception as e:
|
| 584 |
-
|
| 585 |
-
raise
|
|
|
|
| 586 |
|
| 587 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 588 |
create_interface()
|
|
|
|
| 399 |
|
| 400 |
|
| 401 |
def create_interface():
|
| 402 |
+
"""Create and launch the Gradio interface with all fixes implemented."""
|
| 403 |
+
|
| 404 |
app = MediSyncApp()
|
| 405 |
|
| 406 |
+
# Example medical report for demo
|
| 407 |
example_report = """
|
| 408 |
CHEST X-RAY EXAMINATION
|
| 409 |
|
|
|
|
| 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 |
+
logging.info("Successfully downloaded fallback sample image")
|
| 446 |
+
else:
|
| 447 |
+
logging.warning(f"Failed to download sample image. Status code: {response.status_code}")
|
| 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 |
+
logging.error(f"Error setting up sample images: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Get appointment ID from URL parameters
|
| 461 |
+
try:
|
| 462 |
+
from gradio.context import Context
|
| 463 |
+
appointment_id_value = Context.request.query_params.get("appointment_id", "") if hasattr(Context, 'request') else ""
|
| 464 |
+
except Exception as e:
|
| 465 |
+
logging.warning(f"Could not get URL parameters: {str(e)}")
|
| 466 |
+
appointment_id_value = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
|
| 468 |
+
appointment_id = gr.Textbox(
|
| 469 |
+
visible=False,
|
| 470 |
+
value=appointment_id_value
|
| 471 |
)
|
| 472 |
|
| 473 |
gr.Markdown("""
|
|
|
|
| 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"):
|
| 487 |
+
with gr.Row():
|
| 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")
|
| 506 |
+
|
| 507 |
+
if sample_image_path:
|
| 508 |
+
gr.Examples(
|
| 509 |
+
examples=[[sample_image_path, example_report]],
|
| 510 |
+
inputs=[multi_img_input, multi_text_input],
|
| 511 |
+
label="Example X-ray and Report",
|
| 512 |
+
)
|
| 513 |
|
| 514 |
+
with gr.Tab("Image Analysis"):
|
| 515 |
+
with gr.Row():
|
| 516 |
+
with gr.Column():
|
| 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")
|
| 524 |
+
img_plot = gr.HTML(label="Visualization")
|
| 525 |
+
|
| 526 |
+
if sample_image_path:
|
| 527 |
+
gr.Examples(
|
| 528 |
+
examples=[[sample_image_path]],
|
| 529 |
+
inputs=[img_input],
|
| 530 |
+
label="Example X-ray Image",
|
| 531 |
+
)
|
| 532 |
+
|
| 533 |
+
with gr.Tab("Text Analysis"):
|
| 534 |
+
with gr.Row():
|
| 535 |
+
with gr.Column():
|
| 536 |
+
text_input = gr.Textbox(
|
| 537 |
+
label="Enter Medical Report Text",
|
| 538 |
+
placeholder="Enter the radiologist's report text here...",
|
| 539 |
+
lines=10,
|
| 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")
|
| 547 |
+
text_plot = gr.HTML(label="Entity Visualization")
|
| 548 |
+
|
| 549 |
+
gr.Examples(
|
| 550 |
+
examples=[[example_report]],
|
| 551 |
+
inputs=[text_input],
|
| 552 |
+
label="Example Medical Report",
|
| 553 |
+
)
|
| 554 |
+
|
| 555 |
+
with gr.Tab("About"):
|
| 556 |
+
gr.Markdown("""
|
| 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 |
+
# Set up event handlers
|
| 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,
|
| 607 |
+
outputs=[text_output, text_results, text_plot],
|
| 608 |
+
)
|
| 609 |
+
|
| 610 |
+
def complete_consultation(appointment_id):
|
| 611 |
+
"""Handle consultation completion."""
|
| 612 |
+
if not appointment_id:
|
| 613 |
+
return "<div class='alert alert-error'>No appointment ID found. Please contact support.</div>"
|
| 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 |
+
flask_app_url,
|
| 621 |
+
json={"appointment_id": appointment_id},
|
| 622 |
timeout=10
|
| 623 |
)
|
| 624 |
+
|
| 625 |
+
if response.status_code == 200:
|
| 626 |
+
return """
|
| 627 |
+
<div class='alert alert-success'>
|
| 628 |
+
Consultation completed successfully. Redirecting...
|
|
|
|
|
|
|
| 629 |
<script>
|
| 630 |
+
setTimeout(function() {
|
| 631 |
+
window.location.href = "http://127.0.0.1:600/doctors";
|
| 632 |
+
}, 2000);
|
| 633 |
</script>
|
| 634 |
</div>
|
| 635 |
"""
|
| 636 |
+
else:
|
| 637 |
+
return f"""
|
| 638 |
+
<div class='alert alert-error'>
|
| 639 |
+
Error completing appointment (Status: {response.status_code}).
|
| 640 |
+
Please contact support.
|
| 641 |
+
</div>
|
| 642 |
+
"""
|
| 643 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
except Exception as e:
|
| 645 |
+
return f"""
|
| 646 |
+
<div class='alert alert-error'>
|
| 647 |
+
Error: {str(e)}
|
|
|
|
|
|
|
|
|
|
| 648 |
</div>
|
| 649 |
"""
|
| 650 |
|
| 651 |
end_consultation_btn.click(
|
| 652 |
fn=complete_consultation,
|
| 653 |
+
inputs=[appointment_id],
|
| 654 |
+
outputs=completion_status
|
| 655 |
)
|
| 656 |
+
|
| 657 |
try:
|
| 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 |
+
logging.basicConfig(
|
| 666 |
+
level=logging.INFO,
|
| 667 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 668 |
+
)
|
| 669 |
create_interface()
|