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 | |
# Download the model from Hugging Face Hub | |
model_path = hf_hub_download(repo_id="TaliZG03/kidney_normal_CT_classifier_model", filename="model.keras") | |
# Load the model | |
model = load_model(model_path) | |
# Preprocess function | |
def preprocess(image): | |
image = image.resize((300, 300)).convert("RGB") | |
image = np.array(image) / 255.0 | |
return np.expand_dims(image, axis=0) | |
# Prediction function with flagging | |
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": | |
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}""" | |
# Gradio Interface | |
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." | |
) | |
demo.launch() |