updated code ✅✅`
Browse files- mediSync/app.py +72 -184
mediSync/app.py
CHANGED
|
@@ -400,7 +400,6 @@ class MediSyncApp:
|
|
| 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
|
|
@@ -427,263 +426,152 @@ def create_interface():
|
|
| 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 |
-
#
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
#
|
| 469 |
-
appointment_id = gr.Textbox(
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
value=appointment_id_value if appointment_id_value else ""
|
| 473 |
-
)
|
| 474 |
-
|
| 475 |
-
# Function to load appointment ID from URL parameters
|
| 476 |
-
def load_appointment_id():
|
| 477 |
-
try:
|
| 478 |
-
from gradio.context import Context
|
| 479 |
-
if hasattr(Context, 'request') and Context.request:
|
| 480 |
-
return Context.request.query_params.get("appointment_id", "")
|
| 481 |
-
except:
|
| 482 |
-
pass
|
| 483 |
-
return ""
|
| 484 |
-
|
| 485 |
-
# Load appointment ID when interface loads
|
| 486 |
interface.load(
|
| 487 |
-
fn=
|
|
|
|
| 488 |
outputs=appointment_id
|
| 489 |
)
|
| 490 |
|
| 491 |
gr.Markdown("""
|
| 492 |
# MediSync: Multi-Modal Medical Analysis System
|
| 493 |
-
|
| 494 |
-
This AI-powered healthcare solution combines X-ray image analysis with patient report text processing
|
| 495 |
-
to provide comprehensive medical insights.
|
| 496 |
-
|
| 497 |
## How to Use
|
| 498 |
1. Upload a chest X-ray image
|
| 499 |
-
2. Enter
|
| 500 |
-
3. Choose
|
| 501 |
-
4. Click "End Consultation" when finished
|
| 502 |
""")
|
| 503 |
|
| 504 |
with gr.Tab("Multimodal Analysis"):
|
| 505 |
with gr.Row():
|
| 506 |
with gr.Column():
|
| 507 |
multi_img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
| 508 |
-
multi_img_enhance = gr.Button("Enhance Image")
|
| 509 |
-
|
| 510 |
multi_text_input = gr.Textbox(
|
| 511 |
-
label="
|
| 512 |
-
|
| 513 |
-
lines=10
|
| 514 |
-
value=example_report if not sample_image_path else None,
|
| 515 |
-
)
|
| 516 |
-
|
| 517 |
-
multi_analyze_btn = gr.Button(
|
| 518 |
-
"Analyze Image & Text", variant="primary"
|
| 519 |
)
|
| 520 |
-
|
| 521 |
with gr.Column():
|
| 522 |
-
multi_results = gr.HTML(label="
|
| 523 |
multi_plot = gr.HTML(label="Visualization")
|
| 524 |
|
| 525 |
-
if sample_image_path:
|
| 526 |
-
gr.Examples(
|
| 527 |
-
examples=[[sample_image_path, example_report]],
|
| 528 |
-
inputs=[multi_img_input, multi_text_input],
|
| 529 |
-
label="Example X-ray and Report",
|
| 530 |
-
)
|
| 531 |
-
|
| 532 |
with gr.Tab("Image Analysis"):
|
| 533 |
with gr.Row():
|
| 534 |
with gr.Column():
|
| 535 |
img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
| 536 |
-
|
| 537 |
-
img_analyze_btn = gr.Button("Analyze Image", variant="primary")
|
| 538 |
-
|
| 539 |
with gr.Column():
|
| 540 |
img_output = gr.Image(label="Processed Image")
|
| 541 |
-
img_results = gr.HTML(label="
|
| 542 |
-
img_plot = gr.HTML(label="Visualization")
|
| 543 |
-
|
| 544 |
-
if sample_image_path:
|
| 545 |
-
gr.Examples(
|
| 546 |
-
examples=[[sample_image_path]],
|
| 547 |
-
inputs=[img_input],
|
| 548 |
-
label="Example X-ray Image",
|
| 549 |
-
)
|
| 550 |
|
| 551 |
with gr.Tab("Text Analysis"):
|
| 552 |
with gr.Row():
|
| 553 |
with gr.Column():
|
| 554 |
text_input = gr.Textbox(
|
| 555 |
-
label="
|
| 556 |
-
placeholder="Enter the radiologist's report text here...",
|
| 557 |
-
lines=10,
|
| 558 |
value=example_report,
|
|
|
|
| 559 |
)
|
| 560 |
-
text_analyze_btn = gr.Button("Analyze
|
| 561 |
-
|
| 562 |
with gr.Column():
|
| 563 |
-
|
| 564 |
-
text_results = gr.HTML(label="Analysis Results")
|
| 565 |
-
text_plot = gr.HTML(label="Entity Visualization")
|
| 566 |
-
|
| 567 |
-
gr.Examples(
|
| 568 |
-
examples=[[example_report]],
|
| 569 |
-
inputs=[text_input],
|
| 570 |
-
label="Example Medical Report",
|
| 571 |
-
)
|
| 572 |
-
|
| 573 |
-
with gr.Tab("About"):
|
| 574 |
-
gr.Markdown("""
|
| 575 |
-
## About MediSync
|
| 576 |
-
|
| 577 |
-
MediSync is an AI-powered healthcare solution that uses multi-modal analysis to provide comprehensive insights from medical images and reports.
|
| 578 |
-
|
| 579 |
-
### Key Features
|
| 580 |
-
|
| 581 |
-
- **X-ray Image Analysis**: Detects abnormalities in chest X-rays using pre-trained vision models
|
| 582 |
-
- **Medical Report Processing**: Extracts key information from patient reports using NLP models
|
| 583 |
-
- **Multi-modal Integration**: Combines insights from both image and text data for more accurate analysis
|
| 584 |
-
|
| 585 |
-
### Models Used
|
| 586 |
-
|
| 587 |
-
- **X-ray Analysis**: facebook/deit-base-patch16-224-medical-cxr
|
| 588 |
-
- **Medical Text Analysis**: medicalai/ClinicalBERT
|
| 589 |
-
|
| 590 |
-
### Important Disclaimer
|
| 591 |
-
|
| 592 |
-
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.
|
| 593 |
-
""")
|
| 594 |
-
|
| 595 |
-
|
| 596 |
|
| 597 |
# Consultation completion section
|
| 598 |
with gr.Row():
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
completion_status = gr.HTML()
|
| 606 |
-
|
| 607 |
-
# Set up event handlers
|
| 608 |
-
multi_img_enhance.click(
|
| 609 |
-
app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
|
| 610 |
-
)
|
| 611 |
-
multi_analyze_btn.click(
|
| 612 |
-
app.analyze_multimodal,
|
| 613 |
-
inputs=[multi_img_input, multi_text_input],
|
| 614 |
-
outputs=[multi_results, multi_plot],
|
| 615 |
-
)
|
| 616 |
-
|
| 617 |
-
img_enhance.click(app.enhance_image, inputs=img_input, outputs=img_output)
|
| 618 |
-
img_analyze_btn.click(
|
| 619 |
-
app.analyze_image,
|
| 620 |
-
inputs=img_input,
|
| 621 |
-
outputs=[img_output, img_results, img_plot],
|
| 622 |
-
)
|
| 623 |
-
|
| 624 |
-
text_analyze_btn.click(
|
| 625 |
-
app.analyze_text,
|
| 626 |
-
inputs=text_input,
|
| 627 |
-
outputs=[text_output, text_results, text_plot],
|
| 628 |
-
)
|
| 629 |
|
| 630 |
-
def complete_consultation(
|
| 631 |
-
"
|
| 632 |
-
|
| 633 |
-
|
|
|
|
| 634 |
|
| 635 |
try:
|
| 636 |
-
# Replace with your actual Flask app URL
|
| 637 |
-
flask_app_url = "http://127.0.0.1:600/complete_consultation"
|
| 638 |
-
|
| 639 |
response = requests.post(
|
| 640 |
-
|
| 641 |
-
json={"appointment_id":
|
| 642 |
timeout=10
|
| 643 |
)
|
| 644 |
|
| 645 |
if response.status_code == 200:
|
| 646 |
return """
|
| 647 |
<div class='alert alert-success'>
|
| 648 |
-
|
| 649 |
<script>
|
| 650 |
-
setTimeout(
|
| 651 |
-
window.location.href = "http://127.0.0.1:600/doctors";
|
| 652 |
}, 2000);
|
| 653 |
</script>
|
| 654 |
</div>
|
| 655 |
"""
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
</div>
|
| 662 |
-
"""
|
| 663 |
-
|
| 664 |
except Exception as e:
|
| 665 |
return f"""
|
| 666 |
<div class='alert alert-error'>
|
| 667 |
-
|
| 668 |
</div>
|
| 669 |
"""
|
| 670 |
|
| 671 |
end_consultation_btn.click(
|
| 672 |
fn=complete_consultation,
|
| 673 |
-
inputs=
|
| 674 |
outputs=completion_status
|
| 675 |
)
|
| 676 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 677 |
try:
|
| 678 |
interface.launch()
|
| 679 |
except Exception as e:
|
| 680 |
logging.error(f"Failed to launch interface: {str(e)}")
|
| 681 |
-
raise
|
| 682 |
-
|
| 683 |
|
| 684 |
if __name__ == "__main__":
|
| 685 |
-
logging.basicConfig(
|
| 686 |
-
|
| 687 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 688 |
-
)
|
| 689 |
-
create_interface()
|
|
|
|
| 400 |
|
| 401 |
def create_interface():
|
| 402 |
"""Create and launch the Gradio interface with all fixes implemented."""
|
|
|
|
| 403 |
app = MediSyncApp()
|
| 404 |
|
| 405 |
# Example medical report for demo
|
|
|
|
| 426 |
try:
|
| 427 |
sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
|
| 428 |
os.makedirs(sample_images_dir, exist_ok=True)
|
|
|
|
|
|
|
| 429 |
sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
|
| 430 |
+
if sample_images:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
sample_image_path = str(sample_images[0])
|
| 432 |
except Exception as e:
|
| 433 |
logging.error(f"Error setting up sample images: {str(e)}")
|
| 434 |
|
|
|
|
| 435 |
with gr.Blocks(
|
| 436 |
title="MediSync: Multi-Modal Medical Analysis System",
|
| 437 |
theme=gr.themes.Soft()
|
| 438 |
) as interface:
|
| 439 |
+
# JavaScript to extract appointment ID from URL
|
| 440 |
+
js_extract_id = """
|
| 441 |
+
function() {
|
| 442 |
+
const params = new URLSearchParams(window.location.search);
|
| 443 |
+
return params.get('appointment_id') || '';
|
| 444 |
+
}
|
| 445 |
+
"""
|
| 446 |
+
|
| 447 |
+
# Hidden appointment ID component
|
| 448 |
+
appointment_id = gr.Textbox(visible=False)
|
| 449 |
+
|
| 450 |
+
# Load appointment ID from URL when interface loads
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 451 |
interface.load(
|
| 452 |
+
fn=None,
|
| 453 |
+
_js=js_extract_id,
|
| 454 |
outputs=appointment_id
|
| 455 |
)
|
| 456 |
|
| 457 |
gr.Markdown("""
|
| 458 |
# MediSync: Multi-Modal Medical Analysis System
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
## How to Use
|
| 460 |
1. Upload a chest X-ray image
|
| 461 |
+
2. Enter medical report text
|
| 462 |
+
3. Choose analysis type
|
| 463 |
+
4. Click "End Consultation" when finished
|
| 464 |
""")
|
| 465 |
|
| 466 |
with gr.Tab("Multimodal Analysis"):
|
| 467 |
with gr.Row():
|
| 468 |
with gr.Column():
|
| 469 |
multi_img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
|
|
|
|
|
|
| 470 |
multi_text_input = gr.Textbox(
|
| 471 |
+
label="Medical Report Text",
|
| 472 |
+
value=example_report,
|
| 473 |
+
lines=10
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
)
|
| 475 |
+
multi_analyze_btn = gr.Button("Analyze", variant="primary")
|
| 476 |
with gr.Column():
|
| 477 |
+
multi_results = gr.HTML(label="Results")
|
| 478 |
multi_plot = gr.HTML(label="Visualization")
|
| 479 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
with gr.Tab("Image Analysis"):
|
| 481 |
with gr.Row():
|
| 482 |
with gr.Column():
|
| 483 |
img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
| 484 |
+
img_analyze_btn = gr.Button("Analyze", variant="primary")
|
|
|
|
|
|
|
| 485 |
with gr.Column():
|
| 486 |
img_output = gr.Image(label="Processed Image")
|
| 487 |
+
img_results = gr.HTML(label="Results")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 488 |
|
| 489 |
with gr.Tab("Text Analysis"):
|
| 490 |
with gr.Row():
|
| 491 |
with gr.Column():
|
| 492 |
text_input = gr.Textbox(
|
| 493 |
+
label="Medical Report Text",
|
|
|
|
|
|
|
| 494 |
value=example_report,
|
| 495 |
+
lines=10
|
| 496 |
)
|
| 497 |
+
text_analyze_btn = gr.Button("Analyze", variant="primary")
|
|
|
|
| 498 |
with gr.Column():
|
| 499 |
+
text_results = gr.HTML(label="Results")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
|
| 501 |
# Consultation completion section
|
| 502 |
with gr.Row():
|
| 503 |
+
end_consultation_btn = gr.Button(
|
| 504 |
+
"End Consultation",
|
| 505 |
+
variant="stop",
|
| 506 |
+
size="lg"
|
| 507 |
+
)
|
| 508 |
+
completion_status = gr.HTML()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
|
| 510 |
+
def complete_consultation(app_id):
|
| 511 |
+
print(f"Processing completion for appointment: {app_id}")
|
| 512 |
+
|
| 513 |
+
if not app_id:
|
| 514 |
+
return "<div class='alert alert-error'>No appointment ID found</div>"
|
| 515 |
|
| 516 |
try:
|
|
|
|
|
|
|
|
|
|
| 517 |
response = requests.post(
|
| 518 |
+
"http://127.0.0.1:600/complete_appointment",
|
| 519 |
+
json={"appointment_id": app_id},
|
| 520 |
timeout=10
|
| 521 |
)
|
| 522 |
|
| 523 |
if response.status_code == 200:
|
| 524 |
return """
|
| 525 |
<div class='alert alert-success'>
|
| 526 |
+
Completed! Redirecting...
|
| 527 |
<script>
|
| 528 |
+
setTimeout(() => {
|
| 529 |
+
window.location.href = "http://127.0.0.1:600/doctors#consultations";
|
| 530 |
}, 2000);
|
| 531 |
</script>
|
| 532 |
</div>
|
| 533 |
"""
|
| 534 |
+
return f"""
|
| 535 |
+
<div class='alert alert-error'>
|
| 536 |
+
Error: {response.json().get('message', 'Unknown error')}
|
| 537 |
+
</div>
|
| 538 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 539 |
except Exception as e:
|
| 540 |
return f"""
|
| 541 |
<div class='alert alert-error'>
|
| 542 |
+
Connection error: {str(e)}
|
| 543 |
</div>
|
| 544 |
"""
|
| 545 |
|
| 546 |
end_consultation_btn.click(
|
| 547 |
fn=complete_consultation,
|
| 548 |
+
inputs=appointment_id,
|
| 549 |
outputs=completion_status
|
| 550 |
)
|
| 551 |
|
| 552 |
+
# Analysis button handlers
|
| 553 |
+
multi_analyze_btn.click(
|
| 554 |
+
app.analyze_multimodal,
|
| 555 |
+
inputs=[multi_img_input, multi_text_input],
|
| 556 |
+
outputs=[multi_results, multi_plot]
|
| 557 |
+
)
|
| 558 |
+
img_analyze_btn.click(
|
| 559 |
+
app.analyze_image,
|
| 560 |
+
inputs=img_input,
|
| 561 |
+
outputs=[img_output, img_results]
|
| 562 |
+
)
|
| 563 |
+
text_analyze_btn.click(
|
| 564 |
+
app.analyze_text,
|
| 565 |
+
inputs=text_input,
|
| 566 |
+
outputs=text_results
|
| 567 |
+
)
|
| 568 |
+
|
| 569 |
try:
|
| 570 |
interface.launch()
|
| 571 |
except Exception as e:
|
| 572 |
logging.error(f"Failed to launch interface: {str(e)}")
|
| 573 |
+
raise
|
|
|
|
| 574 |
|
| 575 |
if __name__ == "__main__":
|
| 576 |
+
logging.basicConfig(level=logging.INFO)
|
| 577 |
+
create_interface()
|
|
|
|
|
|
|
|
|