Spaces:
Sleeping
Sleeping
Update app.py
Browse files6 unmodified lines7891011121311 unmodified lines252627282930313 unmodified lines353637383940414213 unmodified lines5657585960616215 unmodified lines7879808182834 unmodified lines8889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691702 unmodified lines1731741751761771781791801811821831841851864 unmodified lines1911921931941951961979 unmodified lines2072082092102112122132142152166 unmodified lines# -----------------------------# Class labels for OCTMNIST# -----------------------------CLASS_NAMES = {0: "Choroidal Neovascularization",11 unmodified lines# -----------------------------# Image preprocessing# Must match your notebook preprocessing# -----------------------------transform = transforms.Compose([transforms.Resize((224, 224)),3 unmodified lines# -----------------------------# Load model# Must match your notebook model# -----------------------------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.", {}image = image.convert("RGB")15 unmodified linesresult_text = f"""<div class="result-card"><h2>Prediction Result</h2><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 = """Upload a retinal OCT image and the model will classify it into one of the OCTMNIST disease categories.Classes:0 = Choroidal Neovascularization1 = Diabetic Macular Edema2 = Drusen3 = Normal"""# -----------------------------# Custom CSS for beautiful UI# -----------------------------custom_css = """body {background: linear-gradient(135deg, #f7fbff 0%, #eef5ff 50%, #ffffff 100%);}.gradio-container {max-width: 1150px !important;margin: auto !important;}.main-title {text-align: center;font-size: 42px;font-weight: 800;color: #17324d;margin-bottom: 8px;}.subtitle {text-align: center;font-size: 18px;color: #4c6275;margin-bottom: 28px;}.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);}.result-card h2 {color: #17324d;margin-top: 0px;}.result-card p {font-size: 17px;color: #263849;}.footer-note {text-align: center;color: #68798a;font-size: 14px;margin-top: 25px;}"""# -----------------------------# Gradio Interface# -----------------------------with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:gr.HTML("""<div class="main-title">OCTMNIST Retinal Disease Classifier</div><div class="subtitle">EfficientNetV2-S based OCT image classification demo</div>""")2 unmodified linesinput_image = gr.Image(label="Upload OCT Image",type="pil",height=360)predict_button = gr.Button("Classify Image",variant="primary")gr.Markdown(description)with gr.Column(scale=1):4 unmodified lines)output_text = gr.HTML(label="Prediction")output_label = gr.Label(9 unmodified linesgr.HTML("""<div class="footer-note">Note: This is a research/educational demo and should not be used as a clinical diagnostic tool.</div>""")if __name__ == "__main__":demo.launch()6 unmodified lines7891011121311 unmodified lines252627282930313 unmodified lines353637383940414213 unmodified lines56575859606162636465666768697071727315 unmodified lines89909192939495969798994 unmodified lines1041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542 unmodified lines2572582592602612622632642652662672682692702712722732742752762774 unmodified lines2822832842852862872889 unmodified lines2982993003013023033043053063073083093103113123133146 unmodified lines# -----------------------------# OCTMNIST class labels# -----------------------------CLASS_NAMES = {0: "Choroidal Neovascularization",11 unmodified lines# -----------------------------# Image preprocessing# This should match your notebook preprocessing# -----------------------------transform = transforms.Compose([transforms.Resize((224, 224)),3 unmodified lines# -----------------------------# 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: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_probsimage = image.convert("RGB")15 unmodified linesresult_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# -----------------------------# Description text# -----------------------------description = """### About this demoUpload 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` — NormalThis app uses an EfficientNetV2-S model trained on OCTMNIST."""# -----------------------------# Custom CSS# -----------------------------custom_css = """body {background: linear-gradient(135deg, #f7fbff 0%, #eef5ff 45%, #ffffff 100%);}.gradio-container {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: 44px;font-weight: 900;color: #17324d;margin-bottom: 8px;letter-spacing: -0.5px;}.subtitle {text-align: center;font-size: 18px;color: #4c6275;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: 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: 24px;}.upload-note {background: #f8fbff;border-left: 4px solid #7aaef7;padding: 14px 16px;border-radius: 12px;color: #41576b;font-size: 15px;}"""# -----------------------------# Gradio UI# In Gradio 6, css and theme go inside launch()# -----------------------------with gr.Blocks() as demo:gr.HTML("""<div class="main-wrapper"><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 linesinput_image = gr.Image(label="Upload OCT Image",type="pil",height=380)predict_button = gr.Button("Classify Image",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 Result")output_label = gr.Label(9 unmodified linesgr.HTML("""<div class="footer-note">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(theme=gr.themes.Soft(),css=custom_css)
app.py+130-32