Added the code for the route✅✅
Browse files- mediSync/app.py +88 -77
mediSync/app.py
CHANGED
|
@@ -406,9 +406,10 @@ class MediSyncApp:
|
|
| 406 |
|
| 407 |
def create_interface():
|
| 408 |
"""Create and launch the Gradio interface with all fixes implemented."""
|
|
|
|
| 409 |
app = MediSyncApp()
|
| 410 |
|
| 411 |
-
# Example medical report for demo
|
| 412 |
example_report = """
|
| 413 |
CHEST X-RAY EXAMINATION
|
| 414 |
|
|
@@ -427,49 +428,54 @@ def create_interface():
|
|
| 427 |
RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
|
| 428 |
"""
|
| 429 |
|
| 430 |
-
# Get sample image path
|
| 431 |
sample_image_path = None
|
| 432 |
try:
|
| 433 |
sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
|
| 434 |
os.makedirs(sample_images_dir, exist_ok=True)
|
|
|
|
|
|
|
| 435 |
sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
|
| 436 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
sample_image_path = str(sample_images[0])
|
| 438 |
-
logging.info(f"Using local sample image: {sample_image_path}")
|
| 439 |
except Exception as e:
|
| 440 |
logging.error(f"Error setting up sample images: {str(e)}")
|
| 441 |
|
|
|
|
| 442 |
with gr.Blocks(
|
| 443 |
title="MediSync: Multi-Modal Medical Analysis System",
|
| 444 |
theme=gr.themes.Soft()
|
| 445 |
) as interface:
|
| 446 |
-
#
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
outputs=appointment_id,
|
| 454 |
-
_js="""
|
| 455 |
-
function() {
|
| 456 |
-
const params = new URLSearchParams(window.location.search);
|
| 457 |
-
const id = params.get('appointment_id') || '';
|
| 458 |
-
console.log('Captured appointment ID:', id);
|
| 459 |
-
return id;
|
| 460 |
-
}
|
| 461 |
-
"""
|
| 462 |
-
)
|
| 463 |
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
fn=lambda x: print(f"DEBUG - Captured Appointment ID: {x}") or x,
|
| 468 |
-
inputs=[appointment_id],
|
| 469 |
-
outputs=[debug_output]
|
| 470 |
)
|
| 471 |
|
| 472 |
-
# Unchanged interface sections
|
| 473 |
gr.Markdown("""
|
| 474 |
# MediSync: Multi-Modal Medical Analysis System
|
| 475 |
|
|
@@ -483,19 +489,23 @@ def create_interface():
|
|
| 483 |
4. Click "End Consultation" when finished to complete your appointment
|
| 484 |
""")
|
| 485 |
|
| 486 |
-
# Unchanged Multimodal Analysis tab
|
| 487 |
with gr.Tab("Multimodal Analysis"):
|
| 488 |
with gr.Row():
|
| 489 |
with gr.Column():
|
| 490 |
multi_img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
| 491 |
multi_img_enhance = gr.Button("Enhance Image")
|
|
|
|
| 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 |
with gr.Column():
|
| 500 |
multi_results = gr.HTML(label="Analysis Results")
|
| 501 |
multi_plot = gr.HTML(label="Visualization")
|
|
@@ -507,13 +517,13 @@ def create_interface():
|
|
| 507 |
label="Example X-ray and Report",
|
| 508 |
)
|
| 509 |
|
| 510 |
-
# Unchanged Image Analysis tab
|
| 511 |
with gr.Tab("Image Analysis"):
|
| 512 |
with gr.Row():
|
| 513 |
with gr.Column():
|
| 514 |
img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
| 515 |
img_enhance = gr.Button("Enhance Image")
|
| 516 |
img_analyze_btn = gr.Button("Analyze Image", variant="primary")
|
|
|
|
| 517 |
with gr.Column():
|
| 518 |
img_output = gr.Image(label="Processed Image")
|
| 519 |
img_results = gr.HTML(label="Analysis Results")
|
|
@@ -526,7 +536,6 @@ def create_interface():
|
|
| 526 |
label="Example X-ray Image",
|
| 527 |
)
|
| 528 |
|
| 529 |
-
# Unchanged Text Analysis tab
|
| 530 |
with gr.Tab("Text Analysis"):
|
| 531 |
with gr.Row():
|
| 532 |
with gr.Column():
|
|
@@ -537,6 +546,7 @@ def create_interface():
|
|
| 537 |
value=example_report,
|
| 538 |
)
|
| 539 |
text_analyze_btn = gr.Button("Analyze Text", variant="primary")
|
|
|
|
| 540 |
with gr.Column():
|
| 541 |
text_output = gr.Textbox(label="Processed Text")
|
| 542 |
text_results = gr.HTML(label="Analysis Results")
|
|
@@ -548,7 +558,6 @@ def create_interface():
|
|
| 548 |
label="Example Medical Report",
|
| 549 |
)
|
| 550 |
|
| 551 |
-
# Unchanged About tab
|
| 552 |
with gr.Tab("About"):
|
| 553 |
gr.Markdown("""
|
| 554 |
## About MediSync
|
|
@@ -571,7 +580,7 @@ def create_interface():
|
|
| 571 |
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.
|
| 572 |
""")
|
| 573 |
|
| 574 |
-
# Consultation completion section
|
| 575 |
with gr.Row():
|
| 576 |
with gr.Column():
|
| 577 |
end_consultation_btn = gr.Button(
|
|
@@ -581,45 +590,67 @@ def create_interface():
|
|
| 581 |
)
|
| 582 |
completion_status = gr.HTML()
|
| 583 |
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 595 |
|
| 596 |
try:
|
|
|
|
|
|
|
|
|
|
| 597 |
response = requests.post(
|
| 598 |
-
|
| 599 |
-
json={"appointment_id":
|
| 600 |
timeout=10
|
| 601 |
)
|
| 602 |
|
| 603 |
if response.status_code == 200:
|
| 604 |
return """
|
| 605 |
<div class='alert alert-success'>
|
| 606 |
-
Consultation completed successfully
|
| 607 |
<script>
|
| 608 |
-
setTimeout(()
|
| 609 |
-
window.location.href = "http://127.0.0.1:600/doctors
|
| 610 |
}, 2000);
|
| 611 |
</script>
|
| 612 |
</div>
|
| 613 |
"""
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
|
|
|
|
|
|
|
|
|
| 619 |
except Exception as e:
|
| 620 |
return f"""
|
| 621 |
<div class='alert alert-error'>
|
| 622 |
-
|
| 623 |
</div>
|
| 624 |
"""
|
| 625 |
|
|
@@ -629,33 +660,13 @@ def create_interface():
|
|
| 629 |
outputs=completion_status
|
| 630 |
)
|
| 631 |
|
| 632 |
-
# Unchanged event handlers
|
| 633 |
-
multi_img_enhance.click(
|
| 634 |
-
app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
|
| 635 |
-
)
|
| 636 |
-
multi_analyze_btn.click(
|
| 637 |
-
app.analyze_multimodal,
|
| 638 |
-
inputs=[multi_img_input, multi_text_input],
|
| 639 |
-
outputs=[multi_results, multi_plot],
|
| 640 |
-
)
|
| 641 |
-
img_enhance.click(app.enhance_image, inputs=img_input, outputs=img_output)
|
| 642 |
-
img_analyze_btn.click(
|
| 643 |
-
app.analyze_image,
|
| 644 |
-
inputs=img_input,
|
| 645 |
-
outputs=[img_output, img_results, img_plot],
|
| 646 |
-
)
|
| 647 |
-
text_analyze_btn.click(
|
| 648 |
-
app.analyze_text,
|
| 649 |
-
inputs=text_input,
|
| 650 |
-
outputs=[text_output, text_results, text_plot],
|
| 651 |
-
)
|
| 652 |
-
|
| 653 |
try:
|
| 654 |
interface.launch()
|
| 655 |
except Exception as e:
|
| 656 |
logging.error(f"Failed to launch interface: {str(e)}")
|
| 657 |
raise RuntimeError("Failed to launch MediSync interface") from e
|
| 658 |
|
|
|
|
| 659 |
if __name__ == "__main__":
|
| 660 |
logging.basicConfig(
|
| 661 |
level=logging.INFO,
|
|
|
|
| 406 |
|
| 407 |
def create_interface():
|
| 408 |
"""Create and launch the Gradio interface with all fixes implemented."""
|
| 409 |
+
|
| 410 |
app = MediSyncApp()
|
| 411 |
|
| 412 |
+
# Example medical report for demo
|
| 413 |
example_report = """
|
| 414 |
CHEST X-RAY EXAMINATION
|
| 415 |
|
|
|
|
| 428 |
RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
|
| 429 |
"""
|
| 430 |
|
| 431 |
+
# Get sample image path with robust error handling
|
| 432 |
sample_image_path = None
|
| 433 |
try:
|
| 434 |
sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
|
| 435 |
os.makedirs(sample_images_dir, exist_ok=True)
|
| 436 |
+
|
| 437 |
+
# Check for existing images first
|
| 438 |
sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
|
| 439 |
+
|
| 440 |
+
if not sample_images:
|
| 441 |
+
# Download fallback sample image if none exist
|
| 442 |
+
fallback_url = "https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr2_lrg-a.jpg"
|
| 443 |
+
sample_path = sample_images_dir / "sample_xray.jpg"
|
| 444 |
+
|
| 445 |
+
try:
|
| 446 |
+
response = requests.get(fallback_url, timeout=10)
|
| 447 |
+
if response.status_code == 200:
|
| 448 |
+
with open(sample_path, 'wb') as f:
|
| 449 |
+
f.write(response.content)
|
| 450 |
+
sample_image_path = str(sample_path)
|
| 451 |
+
logging.info("Successfully downloaded fallback sample image")
|
| 452 |
+
else:
|
| 453 |
+
logging.warning(f"Failed to download sample image. Status code: {response.status_code}")
|
| 454 |
+
except Exception as download_error:
|
| 455 |
+
logging.warning(f"Could not download sample image: {str(download_error)}")
|
| 456 |
+
else:
|
| 457 |
sample_image_path = str(sample_images[0])
|
|
|
|
| 458 |
except Exception as e:
|
| 459 |
logging.error(f"Error setting up sample images: {str(e)}")
|
| 460 |
|
| 461 |
+
# Define interface with robust parameter handling
|
| 462 |
with gr.Blocks(
|
| 463 |
title="MediSync: Multi-Modal Medical Analysis System",
|
| 464 |
theme=gr.themes.Soft()
|
| 465 |
) as interface:
|
| 466 |
+
# Get appointment ID from URL parameters
|
| 467 |
+
try:
|
| 468 |
+
from gradio.context import Context
|
| 469 |
+
appointment_id_value = Context.request.query_params.get("appointment_id", "") if hasattr(Context, 'request') else ""
|
| 470 |
+
except Exception as e:
|
| 471 |
+
logging.warning(f"Could not get URL parameters: {str(e)}")
|
| 472 |
+
appointment_id_value = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
|
| 474 |
+
appointment_id = gr.Textbox(
|
| 475 |
+
visible=False,
|
| 476 |
+
value=appointment_id_value
|
|
|
|
|
|
|
|
|
|
| 477 |
)
|
| 478 |
|
|
|
|
| 479 |
gr.Markdown("""
|
| 480 |
# MediSync: Multi-Modal Medical Analysis System
|
| 481 |
|
|
|
|
| 489 |
4. Click "End Consultation" when finished to complete your appointment
|
| 490 |
""")
|
| 491 |
|
|
|
|
| 492 |
with gr.Tab("Multimodal Analysis"):
|
| 493 |
with gr.Row():
|
| 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 |
+
|
| 498 |
multi_text_input = gr.Textbox(
|
| 499 |
label="Enter Medical Report Text",
|
| 500 |
placeholder="Enter the radiologist's report text here...",
|
| 501 |
lines=10,
|
| 502 |
value=example_report if not sample_image_path else None,
|
| 503 |
)
|
| 504 |
+
|
| 505 |
+
multi_analyze_btn = gr.Button(
|
| 506 |
+
"Analyze Image & Text", variant="primary"
|
| 507 |
+
)
|
| 508 |
+
|
| 509 |
with gr.Column():
|
| 510 |
multi_results = gr.HTML(label="Analysis Results")
|
| 511 |
multi_plot = gr.HTML(label="Visualization")
|
|
|
|
| 517 |
label="Example X-ray and Report",
|
| 518 |
)
|
| 519 |
|
|
|
|
| 520 |
with gr.Tab("Image Analysis"):
|
| 521 |
with gr.Row():
|
| 522 |
with gr.Column():
|
| 523 |
img_input = gr.Image(label="Upload X-ray Image", type="pil")
|
| 524 |
img_enhance = gr.Button("Enhance Image")
|
| 525 |
img_analyze_btn = gr.Button("Analyze Image", variant="primary")
|
| 526 |
+
|
| 527 |
with gr.Column():
|
| 528 |
img_output = gr.Image(label="Processed Image")
|
| 529 |
img_results = gr.HTML(label="Analysis Results")
|
|
|
|
| 536 |
label="Example X-ray Image",
|
| 537 |
)
|
| 538 |
|
|
|
|
| 539 |
with gr.Tab("Text Analysis"):
|
| 540 |
with gr.Row():
|
| 541 |
with gr.Column():
|
|
|
|
| 546 |
value=example_report,
|
| 547 |
)
|
| 548 |
text_analyze_btn = gr.Button("Analyze Text", variant="primary")
|
| 549 |
+
|
| 550 |
with gr.Column():
|
| 551 |
text_output = gr.Textbox(label="Processed Text")
|
| 552 |
text_results = gr.HTML(label="Analysis Results")
|
|
|
|
| 558 |
label="Example Medical Report",
|
| 559 |
)
|
| 560 |
|
|
|
|
| 561 |
with gr.Tab("About"):
|
| 562 |
gr.Markdown("""
|
| 563 |
## About MediSync
|
|
|
|
| 580 |
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.
|
| 581 |
""")
|
| 582 |
|
| 583 |
+
# Consultation completion section
|
| 584 |
with gr.Row():
|
| 585 |
with gr.Column():
|
| 586 |
end_consultation_btn = gr.Button(
|
|
|
|
| 590 |
)
|
| 591 |
completion_status = gr.HTML()
|
| 592 |
|
| 593 |
+
# Set up event handlers
|
| 594 |
+
multi_img_enhance.click(
|
| 595 |
+
app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
|
| 596 |
+
)
|
| 597 |
+
multi_analyze_btn.click(
|
| 598 |
+
app.analyze_multimodal,
|
| 599 |
+
inputs=[multi_img_input, multi_text_input],
|
| 600 |
+
outputs=[multi_results, multi_plot],
|
| 601 |
+
)
|
| 602 |
+
|
| 603 |
+
img_enhance.click(app.enhance_image, inputs=img_input, outputs=img_output)
|
| 604 |
+
img_analyze_btn.click(
|
| 605 |
+
app.analyze_image,
|
| 606 |
+
inputs=img_input,
|
| 607 |
+
outputs=[img_output, img_results, img_plot],
|
| 608 |
+
)
|
| 609 |
+
|
| 610 |
+
text_analyze_btn.click(
|
| 611 |
+
app.analyze_text,
|
| 612 |
+
inputs=text_input,
|
| 613 |
+
outputs=[text_output, text_results, text_plot],
|
| 614 |
+
)
|
| 615 |
+
|
| 616 |
+
def complete_consultation(appointment_id):
|
| 617 |
+
"""Handle consultation completion."""
|
| 618 |
+
if not appointment_id:
|
| 619 |
+
return "<div class='alert alert-error'>No appointment ID found. Please contact support.</div>"
|
| 620 |
|
| 621 |
try:
|
| 622 |
+
# Replace with your actual Flask app URL
|
| 623 |
+
flask_app_url = "http://127.0.0.1:600/complete_consultation"
|
| 624 |
+
|
| 625 |
response = requests.post(
|
| 626 |
+
flask_app_url,
|
| 627 |
+
json={"appointment_id": appointment_id},
|
| 628 |
timeout=10
|
| 629 |
)
|
| 630 |
|
| 631 |
if response.status_code == 200:
|
| 632 |
return """
|
| 633 |
<div class='alert alert-success'>
|
| 634 |
+
Consultation completed successfully. Redirecting...
|
| 635 |
<script>
|
| 636 |
+
setTimeout(function() {
|
| 637 |
+
window.location.href = "http://127.0.0.1:600/doctors";
|
| 638 |
}, 2000);
|
| 639 |
</script>
|
| 640 |
</div>
|
| 641 |
"""
|
| 642 |
+
else:
|
| 643 |
+
return f"""
|
| 644 |
+
<div class='alert alert-error'>
|
| 645 |
+
Error completing appointment (Status: {response.status_code}).
|
| 646 |
+
Please contact support.
|
| 647 |
+
</div>
|
| 648 |
+
"""
|
| 649 |
+
|
| 650 |
except Exception as e:
|
| 651 |
return f"""
|
| 652 |
<div class='alert alert-error'>
|
| 653 |
+
Error: {str(e)}
|
| 654 |
</div>
|
| 655 |
"""
|
| 656 |
|
|
|
|
| 660 |
outputs=completion_status
|
| 661 |
)
|
| 662 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 663 |
try:
|
| 664 |
interface.launch()
|
| 665 |
except Exception as e:
|
| 666 |
logging.error(f"Failed to launch interface: {str(e)}")
|
| 667 |
raise RuntimeError("Failed to launch MediSync interface") from e
|
| 668 |
|
| 669 |
+
|
| 670 |
if __name__ == "__main__":
|
| 671 |
logging.basicConfig(
|
| 672 |
level=logging.INFO,
|