VRS1503 commited on
Commit
f617c02
·
verified ·
1 Parent(s): 3787626

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ models/efficientnetb0_best/savedmodel/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ RUN apt-get update && apt-get install -y --no-install-recommends \
4
+ libjpeg62-turbo \
5
+ && rm -rf /var/lib/apt/lists/*
6
+
7
+ WORKDIR /app
8
+
9
+ COPY requirements.txt /app/requirements.txt
10
+ RUN pip install --no-cache-dir -r requirements.txt
11
+
12
+ COPY app_gradio.py /app/app_gradio.py
13
+ COPY models /app/models
14
+
15
+ EXPOSE 7860
16
+
17
+ ENV TF_CPP_MIN_LOG_LEVEL=2
18
+ ENV TF_FORCE_GPU_ALLOW_GROWTH=true
19
+
20
+ CMD ["python", "app_gradio.py"]
app_gradio.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io, numpy as np, tensorflow as tf
2
+ from PIL import Image, UnidentifiedImageError
3
+ import gradio as gr
4
+
5
+ SAVEDMODEL_DIR = "/content/drive/MyDrive/Pneumonia_Detection/models/efficientnetb0_best/savedmodel"
6
+ INPUT_SIZE = (224, 224)
7
+ THRESHOLD = 0.61
8
+
9
+ loaded = tf.saved_model.load(SAVEDMODEL_DIR)
10
+ infer = loaded.signatures["serving_default"]
11
+
12
+ def _read_dicom_bytes(b: bytes):
13
+ import pydicom
14
+ dcm = pydicom.dcmread(io.BytesIO(b), force=True)
15
+ arr = dcm.pixel_array.astype("float32")
16
+ photometric = str(getattr(dcm, "PhotometricInterpretation", "")).upper()
17
+ if "MONOCHROME1" in photometric:
18
+ arr = arr.max() - arr
19
+ if arr.max() > 0:
20
+ arr = arr / arr.max()
21
+ if arr.ndim == 2:
22
+ arr = np.stack([arr]*3, axis=-1)
23
+ elif arr.ndim == 3 and arr.shape[-1] == 1:
24
+ arr = np.concatenate([arr]*3, axis=-1)
25
+ return arr
26
+
27
+ def _read_image_bytes(b: bytes):
28
+ img = Image.open(io.BytesIO(b)).convert("RGB")
29
+ return (np.asarray(img).astype("float32") / 255.0)
30
+
31
+ def _prep_01_rgb(arr):
32
+ x = tf.image.resize(arr, INPUT_SIZE).numpy().astype("float32")
33
+ x = np.clip(x, 0.0, 1.0)
34
+ xb = np.expand_dims(x, 0)
35
+ return tf.constant(xb), x
36
+
37
+ def _get_prob(out_dict):
38
+ t = next(iter(out_dict.values()))
39
+ v = t.numpy()
40
+ return float(v.reshape(-1)[0]) if v.shape[-1] == 1 else float(v[0, 1])
41
+
42
+ def predict(file):
43
+ if file is None:
44
+ return None, {"NORMAL": 0.0, "PNEUMONIA": 0.0}, "No file uploaded."
45
+ raw = file.read()
46
+ name = getattr(file, "name", "upload")
47
+ try:
48
+ if name.lower().endswith((".dcm", ".dicom")):
49
+ arr = _read_dicom_bytes(raw)
50
+ else:
51
+ arr = _read_image_bytes(raw)
52
+ except Exception:
53
+ try:
54
+ arr = _read_image_bytes(raw)
55
+ except Exception:
56
+ try:
57
+ arr = _read_dicom_bytes(raw)
58
+ except Exception as e:
59
+ return None, {"NORMAL": 0.0, "PNEUMONIA": 0.0}, f"Unreadable file: {e}"
60
+
61
+ xb, x_disp = _prep_01_rgb(arr)
62
+ out = infer(xb)
63
+ prob = _get_prob(out)
64
+ decision = "PNEUMONIA" if prob >= THRESHOLD else "NORMAL"
65
+ return x_disp, {"NORMAL": 1 - prob, "PNEUMONIA": prob}, f"Decision @ {THRESHOLD:.2f}: {decision} (p={prob:.3f})"
66
+
67
+ with gr.Blocks(title="Pneumonia Detector – EfficientNetB0") as demo:
68
+ gr.Markdown("# Pneumonia Detector – EfficientNetB0")
69
+ gr.Markdown(
70
+ "Upload **PNG/JPG** or **DICOM**. Input is resized to **224×224 RGB [0,1]**; "
71
+ "the SavedModel contains its own preprocessing. Threshold **0.61**."
72
+ )
73
+ inp = gr.File(label="Upload chest X-ray (.png, .jpg, .dcm)")
74
+ with gr.Row():
75
+ out_img = gr.Image(label="Model Input (224×224)")
76
+ out_lbl = gr.Label(label="Probabilities")
77
+ out_txt = gr.Textbox(label="Decision")
78
+ btn = gr.Button("Predict")
79
+ btn.click(fn=predict, inputs=[inp], outputs=[out_img, out_lbl, out_txt])
80
+
81
+ if __name__ == "__main__":
82
+ demo.launch(Share=True)
models/efficientnetb0_best/savedmodel/fingerprint.pb ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9d92ad8dce5965ff747819adabd1ec0439864e20bf0c5e2491134d66c27a63b5
3
+ size 81
models/efficientnetb0_best/savedmodel/saved_model.pb ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:89e8e3eaf661c9ab0d606b82045c8c74baa32d6e12d654164f37d92bdb36524c
3
+ size 2084046
models/efficientnetb0_best/savedmodel/variables/variables.data-00000-of-00001 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5b635b2fb60d6c7214b32d3278a7854825badb441a014f923fdb0de1a9d9dba0
3
+ size 192971525
models/efficientnetb0_best/savedmodel/variables/variables.index ADDED
Binary file (36.5 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ gradio>=4.0.0
2
+ pillow
3
+ numpy
4
+ tensorflow-cpu==2.15.0
5
+ pydicom
6
+ pylibjpeg
7
+ pylibjpeg-libjpeg
8
+ pylibjpeg-openjpeg