Create app.py (#1)
Browse files- Create app.py (f34132fa5c1eed8470e015af04da155985463fca)
Co-authored-by: MUHAMMAD IBRAHIM <[email protected]>
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import os, glob
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import gradio as gr
|
6 |
+
import tensorflow as tf
|
7 |
+
from functools import lru_cache
|
8 |
+
from huggingface_hub import hf_hub_download
|
9 |
+
|
10 |
+
HF_MODEL_ID = "Vedag812/xray_cnn"
|
11 |
+
CLASS_NAMES = ["NORMAL", "PNEUMONIA"]
|
12 |
+
|
13 |
+
@lru_cache(maxsize=1)
|
14 |
+
def load_model():
|
15 |
+
model_path = hf_hub_download(repo_id=HF_MODEL_ID, filename="xray_cnn.keras")
|
16 |
+
model = tf.keras.models.load_model(model_path, compile=False)
|
17 |
+
return model
|
18 |
+
|
19 |
+
def preprocess(pil_img: Image.Image):
|
20 |
+
img = pil_img.convert("L").resize((150, 150))
|
21 |
+
arr = np.array(img).astype("float32") / 255.0
|
22 |
+
arr = np.expand_dims(arr, axis=(0, -1)) # shape (1,150,150,1)
|
23 |
+
return arr
|
24 |
+
|
25 |
+
def predict_fn(pil_img: Image.Image):
|
26 |
+
model = load_model()
|
27 |
+
x = preprocess(pil_img)
|
28 |
+
prob = float(model.predict(x, verbose=0)[0][0]) # sigmoid
|
29 |
+
pred_idx = int(prob > 0.5)
|
30 |
+
confidence = prob if pred_idx == 1 else 1 - prob
|
31 |
+
probs = {CLASS_NAMES[0]: 1 - prob, CLASS_NAMES[1]: prob}
|
32 |
+
msg = f"Prediction: {CLASS_NAMES[pred_idx]} | Confidence: {confidence*100:.2f}%"
|
33 |
+
return probs, msg
|
34 |
+
|
35 |
+
def list_examples():
|
36 |
+
files = []
|
37 |
+
for pattern in ["images/*.jpeg", "images/*.jpg", "images/*.png"]:
|
38 |
+
files.extend(glob.glob(pattern))
|
39 |
+
files = sorted(files)
|
40 |
+
return [[p] for p in files] # gr.Examples expects list of [path]
|
41 |
+
|
42 |
+
with gr.Blocks(css="""
|
43 |
+
.gradio-container {max-width: 980px !important; margin: auto;}
|
44 |
+
#title {text-align:center;}
|
45 |
+
.card {border:1px solid #e5e7eb; border-radius:16px; padding:16px;}
|
46 |
+
""") as demo:
|
47 |
+
gr.Markdown("<h1 id='title'>Chest X-Ray Classification</h1>")
|
48 |
+
gr.Markdown("Upload an image or click a sample from the gallery. The model predicts NORMAL or PNEUMONIA.")
|
49 |
+
|
50 |
+
with gr.Row():
|
51 |
+
with gr.Column(scale=2):
|
52 |
+
inp = gr.Image(type="pil", image_mode="L", label="Upload X-ray")
|
53 |
+
with gr.Row():
|
54 |
+
btn = gr.Button("Predict", variant="primary")
|
55 |
+
clr = gr.ClearButton(components=[inp], value="Clear")
|
56 |
+
gr.Markdown("### Samples")
|
57 |
+
gr.Examples(
|
58 |
+
examples=list_examples(),
|
59 |
+
inputs=inp,
|
60 |
+
examples_per_page=12,
|
61 |
+
)
|
62 |
+
with gr.Column(scale=1):
|
63 |
+
probs = gr.Label(num_top_classes=2, label="Class probabilities")
|
64 |
+
out_text = gr.Markdown()
|
65 |
+
|
66 |
+
# Run on click
|
67 |
+
btn.click(predict_fn, inputs=inp, outputs=[probs, out_text])
|
68 |
+
# Also auto-run when image changes (from upload or example click)
|
69 |
+
inp.change(predict_fn, inputs=inp, outputs=[probs, out_text])
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
+
demo.launch()
|