reupdated code ✅✅
Browse files- mediSync/app.py +91 -121
mediSync/app.py
CHANGED
|
@@ -399,10 +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 medical report
|
| 406 |
example_report = """
|
| 407 |
CHEST X-RAY EXAMINATION
|
| 408 |
|
|
@@ -421,73 +422,54 @@ def create_interface():
|
|
| 421 |
RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
|
| 422 |
"""
|
| 423 |
|
| 424 |
-
#
|
| 425 |
sample_image_path = None
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
sample_image_path = str(sample_images[0])
|
| 432 |
except Exception as e:
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
# Custom CSS for styling
|
| 436 |
-
custom_css = """
|
| 437 |
-
.alert-box {
|
| 438 |
-
padding: 15px;
|
| 439 |
-
margin: 10px 0;
|
| 440 |
-
border-radius: 5px;
|
| 441 |
-
font-family: sans-serif;
|
| 442 |
-
}
|
| 443 |
-
.alert-error {
|
| 444 |
-
background-color: #ffebee;
|
| 445 |
-
color: #b71c1c;
|
| 446 |
-
border-left: 5px solid #b71c1c;
|
| 447 |
-
}
|
| 448 |
-
.alert-success {
|
| 449 |
-
background-color: #e8f5e9;
|
| 450 |
-
color: #1b5e20;
|
| 451 |
-
border-left: 5px solid #1b5e20;
|
| 452 |
-
}
|
| 453 |
-
.fa-icon {
|
| 454 |
-
margin-right: 8px;
|
| 455 |
-
}
|
| 456 |
-
"""
|
| 457 |
|
|
|
|
| 458 |
with gr.Blocks(
|
| 459 |
-
title="MediSync: Multi-Modal Medical Analysis System",
|
| 460 |
-
theme=gr.themes.Soft()
|
| 461 |
-
css=custom_css
|
| 462 |
) as interface:
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
function setAppointmentId() {
|
| 471 |
-
const id = getAppointmentId();
|
| 472 |
-
if (id) {
|
| 473 |
-
document.getElementById('appointment-id').value = id;
|
| 474 |
-
console.log('Appointment ID set:', id);
|
| 475 |
-
}
|
| 476 |
-
}
|
| 477 |
-
window.onload = setAppointmentId;
|
| 478 |
-
"""
|
| 479 |
|
| 480 |
-
# Hidden appointment ID field
|
| 481 |
appointment_id = gr.Textbox(
|
| 482 |
-
elem_id="appointment-id",
|
| 483 |
visible=False,
|
| 484 |
-
|
| 485 |
)
|
| 486 |
|
| 487 |
-
# Initial JS to extract appointment ID
|
| 488 |
-
interface.load(None, None, None, _js=js_code)
|
| 489 |
-
|
| 490 |
-
# Main interface layout
|
| 491 |
gr.Markdown("""
|
| 492 |
# MediSync: Multi-Modal Medical Analysis System
|
| 493 |
|
|
@@ -497,8 +479,8 @@ def create_interface():
|
|
| 497 |
## How to Use
|
| 498 |
1. Upload a chest X-ray image
|
| 499 |
2. Enter the corresponding medical report text
|
| 500 |
-
3. Choose the analysis type
|
| 501 |
-
4. Click "End Consultation" when finished
|
| 502 |
""")
|
| 503 |
|
| 504 |
with gr.Tab("Multimodal Analysis"):
|
|
@@ -596,104 +578,92 @@ def create_interface():
|
|
| 596 |
with gr.Row():
|
| 597 |
with gr.Column():
|
| 598 |
end_consultation_btn = gr.Button(
|
| 599 |
-
"End Consultation",
|
| 600 |
variant="stop",
|
| 601 |
size="lg"
|
| 602 |
)
|
| 603 |
completion_status = gr.HTML()
|
| 604 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 605 |
def complete_consultation(appointment_id):
|
| 606 |
"""Handle consultation completion."""
|
| 607 |
if not appointment_id:
|
| 608 |
-
|
| 609 |
-
<div class="alert-box alert-error">
|
| 610 |
-
<i class="fas fa-exclamation-triangle fa-icon"></i>
|
| 611 |
-
<strong>No Appointment ID Found</strong>
|
| 612 |
-
<p>Your session may not be properly recorded.</p>
|
| 613 |
-
<p>You will be redirected back to the main page.</p>
|
| 614 |
-
</div>
|
| 615 |
-
<script>
|
| 616 |
-
setTimeout(() => window.location.href = "http://127.0.0.1:600/complete_appointment", 3000);
|
| 617 |
-
</script>
|
| 618 |
-
"""
|
| 619 |
-
logger.error("Completion attempted without appointment ID")
|
| 620 |
-
return error_html
|
| 621 |
|
| 622 |
try:
|
| 623 |
-
#
|
|
|
|
|
|
|
| 624 |
response = requests.post(
|
| 625 |
-
|
| 626 |
json={"appointment_id": appointment_id},
|
| 627 |
timeout=10
|
| 628 |
)
|
| 629 |
-
|
| 630 |
-
if response.
|
| 631 |
-
|
| 632 |
-
<div class=
|
| 633 |
-
|
| 634 |
-
<
|
| 635 |
-
|
|
|
|
|
|
|
|
|
|
| 636 |
</div>
|
| 637 |
-
<script>
|
| 638 |
-
setTimeout(() => window.location.href = "http://127.0.0.1:600/doctors", 2000);
|
| 639 |
-
</script>
|
| 640 |
"""
|
| 641 |
-
logger.info(f"Successfully completed appointment {appointment_id}")
|
| 642 |
-
return success_html
|
| 643 |
else:
|
| 644 |
-
error_msg = f"Server error: {response.status_code}"
|
| 645 |
-
if response.text:
|
| 646 |
-
error_msg += f" - {response.text[:200]}"
|
| 647 |
-
logger.error(error_msg)
|
| 648 |
return f"""
|
| 649 |
-
<div class=
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
<p>{error_msg}</p>
|
| 653 |
</div>
|
| 654 |
"""
|
|
|
|
| 655 |
except Exception as e:
|
| 656 |
-
logger.error(f"Completion error: {str(e)}")
|
| 657 |
return f"""
|
| 658 |
-
<div class=
|
| 659 |
-
|
| 660 |
-
<strong>Connection Error</strong>
|
| 661 |
-
<p>{str(e)}</p>
|
| 662 |
</div>
|
| 663 |
"""
|
| 664 |
|
| 665 |
end_consultation_btn.click(
|
| 666 |
fn=complete_consultation,
|
| 667 |
inputs=[appointment_id],
|
| 668 |
-
outputs=
|
| 669 |
-
)
|
| 670 |
-
|
| 671 |
-
# Set up all your existing analysis handlers here
|
| 672 |
-
multi_img_enhance.click(
|
| 673 |
-
app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
|
| 674 |
-
)
|
| 675 |
-
multi_analyze_btn.click(
|
| 676 |
-
app.analyze_multimodal,
|
| 677 |
-
inputs=[multi_img_input, multi_text_input],
|
| 678 |
-
outputs=[multi_results, multi_plot],
|
| 679 |
-
)
|
| 680 |
-
img_enhance.click(app.enhance_image, inputs=img_input, outputs=img_output)
|
| 681 |
-
img_analyze_btn.click(
|
| 682 |
-
app.analyze_image,
|
| 683 |
-
inputs=img_input,
|
| 684 |
-
outputs=[img_output, img_results, img_plot],
|
| 685 |
-
)
|
| 686 |
-
text_analyze_btn.click(
|
| 687 |
-
app.analyze_text,
|
| 688 |
-
inputs=text_input,
|
| 689 |
-
outputs=[text_output, text_results, text_plot],
|
| 690 |
)
|
| 691 |
|
| 692 |
try:
|
| 693 |
interface.launch()
|
| 694 |
except Exception as e:
|
| 695 |
-
|
| 696 |
-
raise
|
|
|
|
| 697 |
|
| 698 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 699 |
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("""
|
| 474 |
# MediSync: Multi-Modal Medical Analysis System
|
| 475 |
|
|
|
|
| 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"):
|
|
|
|
| 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()
|