Spaces:
Configuration error
Configuration error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
|
7 |
+
# Download the model from Hugging Face Hub
|
8 |
+
model_path = hf_hub_download(repo_id="TaliZG03/kidney_normal_CT_classifier_model", filename="model.keras")
|
9 |
+
|
10 |
+
# Load the model
|
11 |
+
model = load_model(model_path)
|
12 |
+
|
13 |
+
# Preprocess function
|
14 |
+
def preprocess(image):
|
15 |
+
image = image.resize((300, 300)).convert("RGB")
|
16 |
+
image = np.array(image) / 255.0
|
17 |
+
return np.expand_dims(image, axis=0)
|
18 |
+
|
19 |
+
# Prediction function with flagging
|
20 |
+
def predict(image):
|
21 |
+
input_array = preprocess(image)
|
22 |
+
prediction = model.predict(input_array)[0][0]
|
23 |
+
|
24 |
+
label = "NORMAL" if prediction >= 0.5 else "ABNORMAL"
|
25 |
+
confidence = prediction if label == "NORMAL" else 1 - prediction
|
26 |
+
|
27 |
+
if label == "NORMAL" and confidence >= 0.7:
|
28 |
+
explanation = " The kidney CT scan appears normal with high confidence."
|
29 |
+
attention_flag = ""
|
30 |
+
elif label == "NORMAL":
|
31 |
+
explanation = " The scan appears normal, but the model's confidence is low. Consider radiologist review."
|
32 |
+
attention_flag = " FLAGGED FOR RADIOLOGIST REVIEW"
|
33 |
+
else:
|
34 |
+
explanation = " The kidney CT scan shows signs of abnormality. Immediate radiologist attention is recommended."
|
35 |
+
attention_flag = " FLAGGED FOR RADIOLOGIST REVIEW"
|
36 |
+
|
37 |
+
return f""" Prediction: {label}
|
38 |
+
Confidence: {confidence:.2%}
|
39 |
+
|
40 |
+
{explanation}
|
41 |
+
{attention_flag}"""
|
42 |
+
|
43 |
+
# Gradio Interface
|
44 |
+
demo = gr.Interface(
|
45 |
+
fn=predict,
|
46 |
+
inputs=gr.Image(type="pil"),
|
47 |
+
outputs="text",
|
48 |
+
title="Kidney CT Classifier",
|
49 |
+
description="Upload a kidney CT image. The model will predict if it's NORMAL or ABNORMAL. Flagged results go to radiologist review."
|
50 |
+
)
|
51 |
+
|
52 |
+
demo.launch()
|