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