Spaces:
Sleeping
Sleeping
Update app.py
Browse files6 unmodified lines789101011121311 unmodified lines25262728282930313 unmodified lines3536373839383940414213 unmodified lines5657585959606162636465666768697071727315 unmodified lines89909192939495969798994 unmodified lines1041051069110710810911011194112113114115116117118119120969798991001211221231241251051261271281291091301311321331131341351361371381391401411421431441451461471481191201491501511521531541551561571581591291601611621631641651661671681691701711721731741751761771781791801811821351361371831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172181542192202212222232242252262272282292302312322331602342352361622372382392401651661672412422432442452462472482492502512522532542 unmodified lines2572582591762602612622632641812652662672682692702712722732742752762774 unmodified lines2822832841942852862872889 unmodified lines2982993002103013023033043053063073083093102163113123133146 unmodified lines
# -----------------------------# Class labels for OCTMNIST# OCTMNIST class labels# -----------------------------CLASS_NAMES = { 0: "Choroidal Neovascularization",11 unmodified lines
# -----------------------------# Image preprocessing# Must match your notebook preprocessing# This should match your notebook preprocessing# -----------------------------transform = transforms.Compose([ transforms.Resize((224, 224)),3 unmodified lines
# -----------------------------# Load model# Must match your notebook model# Load EfficientNetV2-S model# This must match the architecture used during training# -----------------------------model = timm.create_model( "efficientnetv2_s",13 unmodified lines# -----------------------------def predict_oct(image): if image is None: return None, "Please upload an OCT image first.", {} empty_probs = { CLASS_NAMES[i]: 0.0 for i in range(len(CLASS_NAMES)) }
message = """ <div class="result-card"> <h2>No image uploaded</h2> <p>Please upload an OCT image first.</p> </div> """
return None, message, empty_probs
image = image.convert("RGB")
15 unmodified lines result_text = f""" <div class="result-card"> <h2>Prediction Result</h2>
<div class="prediction-main"> <span class="prediction-label">{pred_name}</span> </div>
<p><b>Predicted Class ID:</b> {pred_idx}</p> <p><b>Predicted Class Name:</b> {pred_name}</p> <p><b>Confidence:</b> {confidence:.4f}</p>4 unmodified lines
# -----------------------------# Example description# Description text# -----------------------------description = """### About this demo
Upload a retinal OCT image and the model will classify it into one of the OCTMNIST disease categories.Upload a retinal OCT image and the model will classify it into one of the OCTMNIST categories.
**Classes:**
- `0` — Choroidal Neovascularization - `1` — Diabetic Macular Edema - `2` — Drusen - `3` — Normal
Classes:0 = Choroidal Neovascularization 1 = Diabetic Macular Edema 2 = Drusen 3 = NormalThis app uses an EfficientNetV2-S model trained on OCTMNIST."""
# -----------------------------# Custom CSS for beautiful UI# Custom CSS# -----------------------------custom_css = """body { background: linear-gradient(135deg, #f7fbff 0%, #eef5ff 50%, #ffffff 100%); background: linear-gradient(135deg, #f7fbff 0%, #eef5ff 45%, #ffffff 100%);}
.gradio-container { max-width: 1150px !important; max-width: 1180px !important; margin: auto !important;}
.main-wrapper { background: linear-gradient(135deg, #ffffff, #f4f8ff); border: 1px solid #dbe8ff; border-radius: 28px; padding: 32px; box-shadow: 0 14px 40px rgba(23, 50, 77, 0.10); margin-bottom: 25px;}
.main-title { text-align: center; font-size: 42px; font-weight: 800; font-size: 44px; font-weight: 900; color: #17324d; margin-bottom: 8px; letter-spacing: -0.5px;}
.subtitle { text-align: center; font-size: 18px; color: #4c6275; margin-bottom: 28px; margin-bottom: 10px;}
.badge-row { text-align: center; margin-top: 18px;}
.badge { display: inline-block; background: #e9f2ff; color: #25527a; border: 1px solid #cfe2ff; border-radius: 999px; padding: 8px 14px; font-weight: 600; font-size: 14px; margin: 4px;}
.result-card { background: linear-gradient(135deg, #ffffff, #f4f8ff); border: 1px solid #dbe8ff; border-radius: 18px; padding: 22px; box-shadow: 0 8px 28px rgba(23, 50, 77, 0.10); border-radius: 22px; padding: 24px; box-shadow: 0 10px 30px rgba(23, 50, 77, 0.10);}
.result-card h2 { color: #17324d; margin-top: 0px; margin-bottom: 16px;}
.result-card p { font-size: 17px; color: #263849; margin: 10px 0px;}
.prediction-main { background: linear-gradient(135deg, #e9f2ff, #ffffff); border: 1px solid #cfe2ff; border-radius: 18px; padding: 18px; margin-bottom: 18px; text-align: center;}
.prediction-label { font-size: 25px; font-weight: 900; color: #17324d;}
.footer-note { text-align: center; color: #68798a; font-size: 14px; margin-top: 25px; margin-top: 24px;}
.upload-note { background: #f8fbff; border-left: 4px solid #7aaef7; padding: 14px 16px; border-radius: 12px; color: #41576b; font-size: 15px;}"""
# -----------------------------# Gradio Interface# Gradio UI# In Gradio 6, css and theme go inside launch()# -----------------------------with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:with gr.Blocks() as demo:
gr.HTML(""" <div class="main-wrapper"> <div class="main-title">OCTMNIST Retinal Disease Classifier</div> <div class="subtitle"> EfficientNetV2-S based OCT image classification demo <div class="main-title">OCTMNIST Retinal Disease Classifier</div> <div class="subtitle"> Upload an OCT image and get the predicted disease class using EfficientNetV2-S </div>
<div class="badge-row"> <span class="badge">EfficientNetV2-S</span> <span class="badge">OCTMNIST</span> <span class="badge">4-Class Classification</span> <span class="badge">PyTorch + Gradio</span> </div> </div> """)
2 unmodified lines input_image = gr.Image( label="Upload OCT Image", type="pil", height=360 height=380 )
predict_button = gr.Button( "Classify Image", variant="primary" variant="primary", size="lg" )
gr.HTML(""" <div class="upload-note"> Upload a retinal OCT image. The app will return both the class ID and the medical class name. </div> """)
gr.Markdown(description)
with gr.Column(scale=1):4 unmodified lines )
output_text = gr.HTML( label="Prediction" label="Prediction Result" )
output_label = gr.Label(9 unmodified lines
gr.HTML(""" <div class="footer-note"> Note: This is a research/educational demo and should not be used as a clinical diagnostic tool. This demo is for educational and research purposes only. It should not be used as a clinical diagnostic tool. </div> """)
# -----------------------------# Launch app# For Hugging Face Spaces, do not need share=True# -----------------------------if __name__ == "__main__": demo.launch() demo.launch( theme=gr.themes.Soft(), css=custom_css )app.py+130-32