samyakshrestha commited on
Commit
3f3fa7a
·
1 Parent(s): 9e156d3

Changed UI

Browse files
Files changed (1) hide show
  1. app.py +75 -33
app.py CHANGED
@@ -2,56 +2,98 @@ import gradio as gr
2
  import time
3
  from src.pipeline import generate_report
4
 
5
- # Pre-load models
 
 
6
  from src.tools_loader import get_tools
7
- _ = get_tools() # Load necessary tools/models at startup for faster inference
8
 
 
 
 
9
  def process_upload(image_path: str):
10
- # Handle case where no image is uploaded
 
 
 
11
  if image_path is None:
12
- yield "Please upload a chest X-ray image."
13
  return
14
 
15
- start = time.time() # Start timer for report generation
16
- yield "Analyzing image..." # Inform user that analysis is in progress
 
 
17
 
18
- report = generate_report(image_path) # Generate radiology report using pipeline
19
  elapsed = time.time() - start # Calculate elapsed time
20
 
21
- # Display the generated report along with time taken
22
- yield f"### Radiology Report\n{report}\n\n*Generated in {elapsed:.1f}s*"
 
 
 
 
 
23
 
24
- # Create enhanced Gradio interface with custom layout
25
- with gr.Blocks(title="Multi-Agent Radiology Assistant") as demo:
26
- gr.Markdown("# Multi-Agent Radiology Assistant") # App title
27
- gr.Markdown("Upload a chest X-ray image to receive an AI-generated radiology report") # App description
28
-
29
- with gr.Row(): # Layout row for input
30
- with gr.Column(scale=1): # Column for image upload and button
31
- input_image = gr.Image(
32
- type="filepath",
33
- label="Upload Chest X-ray",
34
- height=400 # Set image display height
35
- )
36
- process_btn = gr.Button("Generate Report", variant="primary") # Button to trigger report generation
37
-
38
- with gr.Row(): # Layout row for output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  output_report = gr.Markdown(
40
- label="Radiology Report",
41
- height=300 # Set output display height
42
  )
43
 
44
- # Connect the button to processing function
45
- process_btn.click(
46
  fn=process_upload, # Function to call on button click
47
- inputs=[input_image], # Input to the function
48
- outputs=[output_report] # Output from the function
 
 
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
- ) # Disclaimer and instructions
55
 
56
  if __name__ == "__main__":
57
- demo.launch() # Launch the Gradio app if run as main script
 
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