File size: 4,076 Bytes
d8e0712 6763be4 d8e0712 3f3fa7a 6763be4 3f3fa7a 5c7cdbe 3f3fa7a 5c7cdbe 3f3fa7a 5c7cdbe 3f3fa7a 6763be4 5c7cdbe 3f3fa7a 6eb737d 9e156d3 6eb737d 3f3fa7a 6763be4 3f3fa7a 9e156d3 3f3fa7a 9e156d3 3f3fa7a 9e156d3 3f3fa7a 9e156d3 3f3fa7a 9e156d3 27d2cd5 e16418a 3f3fa7a d8e0712 3f3fa7a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
import gradio as gr
import time
from src.pipeline import generate_report
# ------------------------------------------------------------------
# 1. Pre-load models (unchanged)
# ------------------------------------------------------------------
from src.tools_loader import get_tools
_ = get_tools() # Pre-load necessary models/tools for report generation
# ------------------------------------------------------------------
# 2. Helper: streaming generator with progress
# ------------------------------------------------------------------
def process_upload(image_path: str):
"""
Streamed generator: yields loading states then final report.
Gradio shows spinner automatically during execution.
"""
if image_path is None:
yield "**Please upload a chest X-ray image to begin analysis.**" # Prompt user to upload image if none provided
return
start = time.time() # Record start time for performance measurement
# Generate the actual report
report = generate_report(image_path) # Call pipeline to generate radiology report
elapsed = time.time() - start # Calculate elapsed time
# Return final formatted report
yield f"""### Radiology Report
{report}
---
*Generated in {elapsed:.1f} seconds*""" # Display report and time taken
# ------------------------------------------------------------------
# 3. Gradio UI - Vertical Layout
# ------------------------------------------------------------------
with gr.Blocks(
theme=gr.themes.Soft(), # Use Gradio's Soft theme for UI
title="Multi-Agent Radiology Assistant", # Set page title
css="""
.image-container { max-width: 600px; margin: 0 auto; }
.report-container { margin-top: 40px; padding-top: 20px; }
.generate-btn { margin: 30px auto; display: block; }
.progress-bar { z-index: 1000 !important; position: relative; }
.gradio-container .wrap { z-index: auto; }
""" # Custom CSS for layout and styling
) as demo:
# Header
gr.Markdown(
"# Multi-Agent Radiology Assistant\n"
"Upload a chest X-ray image to receive an AI-powered radiology report"
) # Display main header and instructions
# Image upload section (centered, top)
with gr.Column():
input_image = gr.Image(
type="filepath", # Accept image file path
label="Upload Chest X-ray Image", # Label for upload box
height=400, # Set image box height
elem_classes=["image-container"] # Apply custom CSS class
)
# Generate button (centered with more spacing)
generate_btn = gr.Button(
"Generate Report", # Button text
variant="primary", # Primary button style
size="lg", # Large button size
elem_classes=["generate-btn"] # Apply custom CSS class
)
# Report output section (bottom)
with gr.Column(elem_classes=["report-container"]):
output_report = gr.Markdown(
value="**Ready to analyze**\n\nUpload an X-ray image above and click 'Generate Report' to begin.", # Initial output message
label="Analysis Results" # Output label
)
# Event handler with progress settings
generate_btn.click(
fn=process_upload, # Function to call on button click
inputs=input_image, # Pass uploaded image as input
outputs=output_report, # Display output in Markdown box
show_progress="full", # Shows Gradio's built-in progress bar
concurrency_limit=1 # Prevent multiple simultaneous requests
)
# Footer with example hint
gr.Markdown(
"Download and use any frontal chest X-ray PNG/JPG file from the internet and click **Generate Report**.\n"
"### NOTE: This is just a demo. It is not intended to diagnose or suggest treatment of any disease or condition, and should not be used for medical advice."
) # Display disclaimer and usage hint
if __name__ == "__main__":
demo.launch() # Launch Gradio app if script is run directly |