Quantumstride commited on
Commit
a43ae14
Β·
verified Β·
1 Parent(s): 18b6137

Upload 5 files

Browse files
Files changed (5) hide show
  1. app.py +77 -0
  2. config.json +39 -0
  3. norm_stats.json +29 -0
  4. requirements.txt +0 -0
  5. swintiny_fold3.pt +3 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import numpy as np
4
+ import pydicom
5
+
6
+ # ── Load model ─────────────────────────
7
+ model = torch.load("swintiny_fold3.pt", map_location="cpu")
8
+ model.eval()
9
+
10
+ # ── Basic DICOM processing ─────────────
11
+ def process_dicom(files):
12
+ if files is None or len(files) == 0:
13
+ return torch.randn(1, 16, 224, 224) # fallback
14
+
15
+ slices = []
16
+ for f in files:
17
+ ds = pydicom.dcmread(f.name)
18
+ img = ds.pixel_array.astype(np.float32)
19
+ slices.append(img)
20
+
21
+ volume = np.stack(slices)
22
+
23
+ # normalize
24
+ volume = (volume - volume.min()) / (volume.max() - volume.min() + 1e-5)
25
+
26
+ # take first 16 slices
27
+ if volume.shape[0] < 16:
28
+ pad = 16 - volume.shape[0]
29
+ volume = np.pad(volume, ((0, pad), (0,0), (0,0)))
30
+ else:
31
+ volume = volume[:16]
32
+
33
+ return torch.tensor(volume).unsqueeze(0).float()
34
+
35
+
36
+ # ── Tabular processing ─────────────
37
+ def process_tabular(age, sex, fvc, percent, smoking):
38
+ sex_val = 1 if sex == "Male" else 0
39
+ smoke_map = {"Never": 0, "Ex-smoker": 1, "Currently": 2}
40
+ smoke_val = smoke_map.get(smoking, 0)
41
+
42
+ return torch.tensor([[age, fvc, percent, sex_val, smoke_val]], dtype=torch.float32)
43
+
44
+
45
+ # ── Prediction ─────────────
46
+ def predict(age, sex, fvc, percent, smoking, dicoms):
47
+
48
+ ct = process_dicom(dicoms)
49
+ tab = process_tabular(age, sex, fvc, percent, smoking)
50
+
51
+ with torch.no_grad():
52
+ weeks = torch.arange(0, 145).float().unsqueeze(0)
53
+ mu, sigma = model(ct, tab, weeks)
54
+
55
+ fvc_1yr = mu[0][52].item()
56
+
57
+ return f"Predicted FVC after 1 year: {fvc_1yr:.2f} ml"
58
+
59
+
60
+ # ── UI ─────────────
61
+ demo = gr.Interface(
62
+ fn=predict,
63
+ inputs=[
64
+ gr.Number(label="Age", value=65),
65
+ gr.Radio(["Male", "Female"], value="Male", label="Sex"),
66
+ gr.Number(label="Baseline FVC", value=2500),
67
+ gr.Number(label="Percent Predicted", value=70),
68
+ gr.Dropdown(["Never", "Ex-smoker", "Currently"], value="Ex-smoker", label="Smoking"),
69
+ gr.File(file_count="multiple", label="Upload DICOM files")
70
+ ],
71
+ outputs="text",
72
+ title="🫁 FibrosisNet (Fast Demo)",
73
+ description="Upload CT DICOM + patient data to predict fibrosis progression"
74
+ )
75
+
76
+ # ── Launch (important for HF) ─────────────
77
+ demo.launch(server_name="0.0.0.0", server_port=7860)
config.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "IMG_SIZE": 224,
3
+ "NUM_SLICES": 16,
4
+ "HU_MIN": -1000,
5
+ "HU_MAX": 400,
6
+ "WEEK_MIN": -12,
7
+ "WEEK_MAX": 133,
8
+ "N_FOLDS": 5,
9
+ "TAB_FEATURES": [
10
+ "BaselineFVC",
11
+ "Percent",
12
+ "Age",
13
+ "Sex_enc",
14
+ "Smoking_enc",
15
+ "FVC_height_ratio",
16
+ "percent_age_interaction"
17
+ ],
18
+ "SIN_PERIODS": [
19
+ 10.0,
20
+ 26.0,
21
+ 52.0,
22
+ 104.0
23
+ ],
24
+ "SIN_DIM": 8,
25
+ "TAB_DIM": 15,
26
+ "FUSION_DIM": 512,
27
+ "FUSION_HEADS": 8,
28
+ "FUSION_DEPTH": 1,
29
+ "BACKBONES": {
30
+ "vit": "vit_base_patch16_224",
31
+ "effnetv2m": "efficientnetv2_m",
32
+ "swintiny": "swin_tiny_patch4_window7_224"
33
+ },
34
+ "BACKBONE_PRETRAINED": {
35
+ "vit": true,
36
+ "effnetv2m": false,
37
+ "swintiny": true
38
+ }
39
+ }
norm_stats.json ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "features": [
3
+ "BaselineFVC",
4
+ "Percent",
5
+ "Age",
6
+ "Sex_enc",
7
+ "Smoking_enc",
8
+ "FVC_height_ratio",
9
+ "percent_age_interaction"
10
+ ],
11
+ "t_mean": [
12
+ 2773.836181640625,
13
+ 80.28543853759766,
14
+ 67.24561309814453,
15
+ 0.7894737124443054,
16
+ 0.7719298005104065,
17
+ 0.8028555512428284,
18
+ 0.7729322910308838
19
+ ],
20
+ "t_std": [
21
+ 842.7239990234375,
22
+ 19.473363876342773,
23
+ 6.880639553070068,
24
+ 0.40768343210220337,
25
+ 0.5303945541381836,
26
+ 0.1947346031665802,
27
+ 0.20774446427822113
28
+ ]
29
+ }
requirements.txt ADDED
File without changes
swintiny_fold3.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cd379814d4ddb6c65cee0e064e71b3ef95b373f026b8499ed7b66114232f90e4
3
+ size 131766030