ALYYAN commited on
Commit
33f2535
·
unverified ·
1 Parent(s): 02f30a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -37
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py (The Final Polished Version)
2
 
3
  import gradio as gr
4
  from pathlib import Path
@@ -19,7 +19,7 @@ try:
19
  except FileNotFoundError:
20
  print("Warning: 'sample_images' directory not found."); SAMPLE_IMAGES = []
21
 
22
- # --- Core Logic Functions (Unchanged and Correct) ---
23
  async def process_analysis(patient_name, patient_age, image_list, is_sample=False):
24
  if not is_sample and (not patient_name or patient_age is None):
25
  raise gr.Error("Patient Name and Age are required.")
@@ -59,17 +59,17 @@ css = """
59
  /* --- Professional Dark Theme & Fonts --- */
60
  :root { --primary-hue: 220 !important; --secondary-hue: 210 !important; --neutral-hue: 210 !important; --body-background-fill: #111827 !important; --block-background-fill: #1F2937 !important; --block-border-width: 1px !important; --border-color-accent: #374151 !important; --background-fill-secondary: #1F2937 !important;}
61
  /* --- Header & Title Styling --- */
62
- #app_header { text-align: center; max-width: 900px; margin: 0 auto; } /* --- FIX: Center the header column --- */
63
  #app_title { font-size: 2.8rem !important; font-weight: 700 !important; color: #FFFFFF !important; padding-top: 1rem; }
64
  #app_subtitle { font-size: 1.2rem !important; color: #9CA3AF !important; margin-bottom: 2rem; }
65
- /* --- Layout, Spacing, and Component Styling --- */
66
- #main_container { gap: 2rem; max-width: 900px; margin: 0 auto; } /* Center the main content */
67
  #results_gallery .gallery-item { padding: 0.25rem !important; background-color: #374151; border: 1px solid #374151 !important; }
68
  #bottom_controls { max-width: 500px; margin: 2.5rem auto 1rem auto; }
69
- #sample_gallery .gallery-item { border: 4px solid transparent; transition: border-color 0.3s ease; }
70
  """
71
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="blue"), css=css, title="Pneumonia Detection AI") as demo:
72
 
 
73
  with gr.Column() as main_app:
74
  with gr.Column(elem_id="app_header"):
75
  gr.Markdown("# 🩺 Pneumonia Detection AI", elem_id="app_title")
@@ -104,20 +104,10 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="blue")
104
  refresh_history_btn = gr.Button("Refresh History")
105
  history_df = gr.DataFrame(headers=["Name", "Age", "Prediction", "Confidence", "Date"], row_count=10, interactive=False)
106
 
107
- # --- SAMPLES PAGE (THE DEFINITIVE FIX) ---
108
  with gr.Column(visible=False) as samples_page:
109
  gr.Markdown("# 🖼️ Sample Image Library", elem_classes="app_title")
110
  gr.Markdown("Click an image to run an anonymous analysis.")
111
-
112
- # We will use the gallery's native .select() event.
113
- sample_gallery = gr.Gallery(
114
- value=SAMPLE_IMAGES,
115
- label="Sample Images",
116
- columns=5, height=400,
117
- allow_preview=True, # Allows the nice popup view
118
- elem_id="sample_gallery"
119
- )
120
- # We add a hidden button that our code will "click"
121
  hidden_sample_analyze_btn = gr.Button("Analyze Sample", visible=False)
122
  back_to_main_btn_samp = gr.Button("⬅️ Back to Main App")
123
 
@@ -133,50 +123,38 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="blue")
133
  start_over_btn.click(fn=None, js="() => { window.location.reload(); }")
134
 
135
  # --- SAMPLE PAGE LOGIC (THE DEFINITIVE FIX) ---
136
-
137
- # When a sample image is clicked, this function runs.
138
- # It takes the event data, which contains the path of the clicked image.
139
- # Its ONLY job is to programmatically "click" the hidden analysis button.
140
  def on_sample_select(evt: gr.SelectData):
141
- # We return the path of the selected image. This value will become the input
142
- # for the hidden_sample_analyze_btn's click event.
143
  return evt.value
144
 
145
- # The .select() event's output is now the INPUT to the hidden button's .click() event.
146
  sample_gallery.select(
147
  fn=on_sample_select,
148
- None,
149
- hidden_sample_analyze_btn
150
  )
151
 
152
- # The hidden button's click event runs the actual analysis
153
  async def handle_sample_analysis(selected_image_path: str):
154
- if not selected_image_path: # This handles the case where nothing is selected
155
  raise gr.Error("Sample image path is missing.")
156
-
157
  analysis_results = await process_analysis("Sample User", 0, [selected_image_path], is_sample=True)
158
-
159
- return [
160
- gr.update(visible=True), # main_app
161
- gr.update(visible=False), # samples_page
162
- *analysis_results
163
- ]
164
 
165
  hidden_sample_analyze_btn.click(
166
  fn=handle_sample_analysis,
167
- inputs=[hidden_sample_analyze_btn], # The button's value is the path
168
  outputs=[main_app, samples_page, uploader_column, results_column, result_images, result_label]
169
  )
170
 
171
- # --- Page Navigation (Unchanged and Correct) ---
172
  all_pages = [main_app, history_page, samples_page]
173
  async def show_history_page_and_refresh(): records_update = await refresh_history_table(); return [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), records_update]
174
  def show_samples_page(): return [gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)]
175
  def show_main_page(): return [gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)]
 
176
  history_btn.click(fn=show_history_page_and_refresh, outputs=all_pages + [history_df])
177
  samples_btn.click(fn=show_samples_page, outputs=all_pages)
178
  back_to_main_btn_hist.click(fn=show_main_page, outputs=all_pages)
179
  back_to_main_btn_samp.click(fn=show_main_page, outputs=all_pages)
 
180
  refresh_history_btn.click(fn=refresh_history_table, outputs=history_df)
181
  demo.load(fn=refresh_history_table, outputs=history_df)
182
 
 
1
+ # app.py (Definitive Final Version with Syntax Fix)
2
 
3
  import gradio as gr
4
  from pathlib import Path
 
19
  except FileNotFoundError:
20
  print("Warning: 'sample_images' directory not found."); SAMPLE_IMAGES = []
21
 
22
+ # --- Core Logic Functions (Unchanged) ---
23
  async def process_analysis(patient_name, patient_age, image_list, is_sample=False):
24
  if not is_sample and (not patient_name or patient_age is None):
25
  raise gr.Error("Patient Name and Age are required.")
 
59
  /* --- Professional Dark Theme & Fonts --- */
60
  :root { --primary-hue: 220 !important; --secondary-hue: 210 !important; --neutral-hue: 210 !important; --body-background-fill: #111827 !important; --block-background-fill: #1F2937 !important; --block-border-width: 1px !important; --border-color-accent: #374151 !important; --background-fill-secondary: #1F2937 !important;}
61
  /* --- Header & Title Styling --- */
62
+ #app_header { text-align: center; max-width: 900px; margin: 0 auto; }
63
  #app_title { font-size: 2.8rem !important; font-weight: 700 !important; color: #FFFFFF !important; padding-top: 1rem; }
64
  #app_subtitle { font-size: 1.2rem !important; color: #9CA3AF !important; margin-bottom: 2rem; }
65
+ /* --- Layout and Spacing --- */
66
+ #main_container { gap: 2rem; max-width: 900px; margin: 0 auto; }
67
  #results_gallery .gallery-item { padding: 0.25rem !important; background-color: #374151; border: 1px solid #374151 !important; }
68
  #bottom_controls { max-width: 500px; margin: 2.5rem auto 1rem auto; }
 
69
  """
70
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="blue"), css=css, title="Pneumonia Detection AI") as demo:
71
 
72
+ # --- UI Layout (Unchanged) ---
73
  with gr.Column() as main_app:
74
  with gr.Column(elem_id="app_header"):
75
  gr.Markdown("# 🩺 Pneumonia Detection AI", elem_id="app_title")
 
104
  refresh_history_btn = gr.Button("Refresh History")
105
  history_df = gr.DataFrame(headers=["Name", "Age", "Prediction", "Confidence", "Date"], row_count=10, interactive=False)
106
 
 
107
  with gr.Column(visible=False) as samples_page:
108
  gr.Markdown("# 🖼️ Sample Image Library", elem_classes="app_title")
109
  gr.Markdown("Click an image to run an anonymous analysis.")
110
+ sample_gallery = gr.Gallery(value=SAMPLE_IMAGES, label="Sample Images", columns=5, height=400, allow_preview=True, elem_id="sample_gallery")
 
 
 
 
 
 
 
 
 
111
  hidden_sample_analyze_btn = gr.Button("Analyze Sample", visible=False)
112
  back_to_main_btn_samp = gr.Button("⬅️ Back to Main App")
113
 
 
123
  start_over_btn.click(fn=None, js="() => { window.location.reload(); }")
124
 
125
  # --- SAMPLE PAGE LOGIC (THE DEFINITIVE FIX) ---
 
 
 
 
126
  def on_sample_select(evt: gr.SelectData):
 
 
127
  return evt.value
128
 
 
129
  sample_gallery.select(
130
  fn=on_sample_select,
131
+ inputs=None, # The event data is passed automatically
132
+ outputs=[hidden_sample_analyze_btn]
133
  )
134
 
 
135
  async def handle_sample_analysis(selected_image_path: str):
136
+ if not selected_image_path:
137
  raise gr.Error("Sample image path is missing.")
 
138
  analysis_results = await process_analysis("Sample User", 0, [selected_image_path], is_sample=True)
139
+ return [gr.update(visible=True), gr.update(visible=False), *analysis_results]
 
 
 
 
 
140
 
141
  hidden_sample_analyze_btn.click(
142
  fn=handle_sample_analysis,
143
+ inputs=[hidden_sample_analyze_btn],
144
  outputs=[main_app, samples_page, uploader_column, results_column, result_images, result_label]
145
  )
146
 
147
+ # --- Page Navigation ---
148
  all_pages = [main_app, history_page, samples_page]
149
  async def show_history_page_and_refresh(): records_update = await refresh_history_table(); return [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), records_update]
150
  def show_samples_page(): return [gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)]
151
  def show_main_page(): return [gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)]
152
+
153
  history_btn.click(fn=show_history_page_and_refresh, outputs=all_pages + [history_df])
154
  samples_btn.click(fn=show_samples_page, outputs=all_pages)
155
  back_to_main_btn_hist.click(fn=show_main_page, outputs=all_pages)
156
  back_to_main_btn_samp.click(fn=show_main_page, outputs=all_pages)
157
+
158
  refresh_history_btn.click(fn=refresh_history_table, outputs=history_df)
159
  demo.load(fn=refresh_history_table, outputs=history_df)
160