File size: 1,745 Bytes
752a8c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()