Spaces:
Sleeping
Sleeping
File size: 8,692 Bytes
9818f66 b383602 65ab7ab b383602 65ab7ab b383602 48b3884 b383602 9ae30e6 b383602 4c814c5 9818f66 9ae30e6 9818f66 b383602 9818f66 48b3884 9818f66 48b3884 9818f66 48b3884 a84b21c 9818f66 a84b21c 9818f66 a84b21c 9818f66 a84b21c 4c814c5 b383602 48b3884 1063b82 48b3884 1063b82 48b3884 1063b82 48b3884 1063b82 48b3884 1063b82 9818f66 1063b82 9818f66 48b3884 1063b82 48b3884 1063b82 9818f66 48b3884 9818f66 9ae30e6 48b3884 1063b82 9818f66 65ab7ab 4c814c5 9818f66 4c814c5 9818f66 1063b82 4c814c5 9818f66 4c814c5 9818f66 48b3884 65ab7ab 48b3884 b383602 4f56e85 |
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# app.py (The Final Polished Version)
import gradio as gr
from pathlib import Path
import asyncio
from PIL import Image
# Import backend components
from app.prediction import PredictionPipeline
from app.database import add_patient_record, get_all_records
# --- Initialization ---
prediction_pipeline = PredictionPipeline()
SAMPLE_IMAGE_DIR = Path("sample_images")
try:
if SAMPLE_IMAGE_DIR.is_dir():
SAMPLE_IMAGES = [str(p) for p in sorted(list(SAMPLE_IMAGE_DIR.glob('*/*.jpeg')))]
else: raise FileNotFoundError
except FileNotFoundError:
print("Warning: 'sample_images' directory not found."); SAMPLE_IMAGES = []
# --- Core Logic Functions (Unchanged and Correct) ---
async def process_analysis(patient_name, patient_age, image_list, is_sample=False):
# ... (code is the same)
async def refresh_history_table():
# ... (code is the same)
# --- Gradio UI Definition ---
css = """
/* --- Professional Dark Theme & Fonts --- */
: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;}
/* --- Header & Title Styling --- */
#app_header { text-align: center; max-width: 900px; margin: 0 auto; } /* --- FIX: Center the header column --- */
#app_title { font-size: 2.8rem !important; font-weight: 700 !important; color: #FFFFFF !important; padding-top: 1rem; }
#app_subtitle { font-size: 1.2rem !important; color: #9CA3AF !important; margin-bottom: 2rem; }
/* --- Layout, Spacing, and Component Styling --- */
#main_container { gap: 2rem; max-width: 900px; margin: 0 auto; } /* Center the main content */
#results_gallery .gallery-item { padding: 0.25rem !important; background-color: #374151; border: 1px solid #374151 !important; }
#bottom_controls { max-width: 500px; margin: 2.5rem auto 1rem auto; }
#sample_gallery .gallery-item { border: 4px solid transparent; transition: border-color 0.3s ease; }
"""
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="blue"), css=css, title="Pneumonia Detection AI") as demo:
with gr.Column() as main_app:
with gr.Column(elem_id="app_header"):
gr.Markdown("# 🩺 Pneumonia Detection AI", elem_id="app_title")
gr.Markdown("An AI-powered tool to assist in the diagnosis of pneumonia.", elem_id="app_subtitle")
with gr.Row(elem_id="main_container"):
with gr.Column(scale=1) as uploader_column:
gr.Markdown("### Upload Patient X-Rays")
image_input = gr.File(label="Upload up to 3 Images", file_count="multiple", file_types=["image"], type="filepath")
with gr.Column(scale=2, visible=False) as results_column:
gr.Markdown("### Analysis Results")
result_images = gr.Gallery(label="Analyzed Images", columns=3, object_fit="contain", height=350, elem_id="results_gallery")
result_label = gr.Label(label="Overall Prediction", num_top_classes=2)
start_over_btn = gr.Button("Start New Analysis", variant="secondary")
with gr.Group(visible=False) as patient_info_modal:
gr.Markdown("## Enter Patient Details", elem_classes="text-center")
patient_name_modal = gr.Textbox(label="Patient Name", placeholder="e.g., John Doe")
patient_age_modal = gr.Number(label="Patient Age", minimum=0, maximum=120, step=1)
with gr.Row():
submit_analysis_btn = gr.Button("Analyze Images", variant="primary")
cancel_btn = gr.Button("Cancel", variant="stop")
with gr.Column(elem_id="bottom_controls"):
with gr.Accordion("About this Tool", open=False):
gr.Markdown("...") # Professional description here
with gr.Row():
samples_btn = gr.Button("Try Sample Images")
history_btn = gr.Button("View Patient History")
with gr.Column(visible=False) as history_page:
gr.Markdown("# 📜 Patient Record History", elem_classes="app_title")
with gr.Row():
back_to_main_btn_hist = gr.Button("⬅️ Back to Main App")
refresh_history_btn = gr.Button("Refresh History")
history_df = gr.DataFrame(headers=["Name", "Age", "Prediction", "Confidence", "Date"], row_count=10, interactive=False)
# --- SAMPLES PAGE (THE DEFINITIVE FIX) ---
with gr.Column(visible=False) as samples_page:
gr.Markdown("# 🖼️ Sample Image Library", elem_classes="app_title")
gr.Markdown("Click an image to run an anonymous analysis.")
# We will use the gallery's native .select() event.
sample_gallery = gr.Gallery(
value=SAMPLE_IMAGES,
label="Sample Images",
columns=5, height=400,
allow_preview=True, # Allows the nice popup view
elem_id="sample_gallery"
)
# We add a hidden button that our code will "click"
hidden_sample_analyze_btn = gr.Button("Analyze Sample", visible=False)
back_to_main_btn_samp = gr.Button("⬅️ Back to Main App")
# --- Event Handling Logic ---
# ... (upload, modal, start over handlers are correct)
def show_patient_info(files): return gr.update(visible=True) if files else gr.update(visible=False)
image_input.upload(fn=show_patient_info, inputs=image_input, outputs=patient_info_modal)
async def submit_and_hide_modal(name, age, files):
analysis_results = await process_analysis(name, age, files); return [*analysis_results, gr.update(visible=False)]
submit_analysis_btn.click(fn=submit_and_hide_modal, inputs=[patient_name_modal, patient_age_modal, image_input], outputs=[uploader_column, results_column, result_images, result_label, patient_info_modal])
cancel_btn.click(lambda: (gr.update(visible=False), None), None, [patient_info_modal, image_input])
start_over_btn.click(fn=None, js="() => { window.location.reload(); }")
# --- SAMPLE PAGE LOGIC (THE DEFINITIVE FIX) ---
# When a sample image is clicked, this function runs.
# It takes the event data, which contains the path of the clicked image.
# Its ONLY job is to programmatically "click" the hidden analysis button.
def on_sample_select(evt: gr.SelectData):
# We return the path of the selected image. This value will become the input
# for the hidden_sample_analyze_btn's click event.
return evt.value
# The .select() event's output is now the INPUT to the hidden button's .click() event.
sample_gallery.select(
fn=on_sample_select,
None,
hidden_sample_analyze_btn
)
# The hidden button's click event runs the actual analysis
async def handle_sample_analysis(selected_image_path: str):
if not selected_image_path: # This handles the case where nothing is selected
raise gr.Error("Sample image path is missing.")
analysis_results = await process_analysis("Sample User", 0, [selected_image_path], is_sample=True)
return [
gr.update(visible=True), # main_app
gr.update(visible=False), # samples_page
*analysis_results
]
hidden_sample_analyze_btn.click(
fn=handle_sample_analysis,
inputs=[hidden_sample_analyze_btn], # The button's value is the path
outputs=[main_app, samples_page, uploader_column, results_column, result_images, result_label]
)
# --- Page Navigation (Unchanged and Correct) ---
all_pages = [main_app, history_page, samples_page]
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]
def show_samples_page(): return [gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)]
def show_main_page(): return [gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)]
history_btn.click(fn=show_history_page_and_refresh, outputs=all_pages + [history_df])
samples_btn.click(fn=show_samples_page, outputs=all_pages)
back_to_main_btn_hist.click(fn=show_main_page, outputs=all_pages)
back_to_main_btn_samp.click(fn=show_main_page, outputs=all_pages)
refresh_history_btn.click(fn=refresh_history_table, outputs=history_df)
demo.load(fn=refresh_history_table, outputs=history_df)
# --- Launch the App ---
if __name__ == "__main__":
demo.launch()
|