Spaces:
Configuration error
Configuration error
import gradio as gr | |
import numpy as np | |
from PIL import Image | |
from tensorflow.keras.models import load_model | |
from huggingface_hub import hf_hub_download | |
model_path = hf_hub_download(repo_id="TaliZG03/kidney_normal_CT_classifier_model", filename="model.keras") | |
model = load_model(model_path) | |
def preprocess(image): | |
image = image.resize((300, 300)).convert("RGB") | |
image = np.array(image) / 255.0 | |
return np.expand_dims(image, axis=0) | |
def predict(image): | |
input_array = preprocess(image) | |
prediction = model.predict(input_array)[0][0] | |
label = "NORMAL" if prediction >= 0.5 else "ABNORMAL" | |
confidence = prediction if label == "NORMAL" else 1 - prediction | |
if label == "NORMAL" and confidence >= 0.7: | |
explanation = "β The kidney CT scan appears normal with high confidence." | |
attention_flag = "" | |
elif label == "NORMAL" and confidence < 0.7: | |
explanation = "β οΈ The scan appears normal, but the model's confidence is low. Consider radiologist review." | |
attention_flag = "π¨ FLAGGED FOR RADIOLOGIST REVIEW" | |
else: | |
explanation = "β οΈ The kidney CT scan shows signs of abnormality. Immediate radiologist attention is recommended." | |
attention_flag = "π¨ FLAGGED FOR RADIOLOGIST REVIEW" | |
return f""" Prediction: {label} | |
π Confidence: {confidence:.2%} | |
{explanation} | |
{attention_flag}""" | |
demo = gr.Interface( | |
fn=predict, | |
inputs=gr.Image(type="pil"), | |
outputs="text", | |
title="Kidney CT Classifier", | |
description="Upload a kidney CT image. The model will predict if it's NORMAL or ABNORMAL. Flagged results go to radiologist review.", | |
examples=["images/sample1.png", "images/sample2.png"] | |
) | |
if __name__ == "__main__": | |
demo.launch() | |