CGAllenger commited on
Commit
ec53f2b
·
verified ·
1 Parent(s): ff5752c

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +84 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ # 1. Load the models
7
+ # Using try-except so the Space doesn't crash if models aren't fully uploaded yet
8
+ try:
9
+ mri_model = tf.keras.models.load_model("mri_model.keras")
10
+ xray_model = tf.keras.models.load_model("xray_model.h5")
11
+ except Exception as e:
12
+ print(f"Error loading models: {e}")
13
+ mri_model, xray_model = None, None
14
+
15
+ # 2. Define the labels based on your training data
16
+ mri_labels = ['Glioma', 'Meningioma', 'Pituitary tumor', 'no tumor']
17
+ xray_labels = [
18
+ 'Cardiomegaly', 'Emphysema', 'Effusion', 'Hernia', 'Infiltration',
19
+ 'Mass', 'Nodule', 'Atelectasis', 'Pneumothorax', 'Pleural_Thickening',
20
+ 'Pneumonia', 'Fibrosis', 'Edema', 'Consolidation'
21
+ ]
22
+
23
+ # 3. Prediction Function for MRI
24
+ def predict_mri(img):
25
+ if mri_model is None: return {"Error": 0.0}
26
+ if img is None: return {"No image": 0.0}
27
+
28
+ # Resize to 256x256
29
+ img = img.resize((256, 256))
30
+ img_array = np.array(img)
31
+
32
+ # Ensure image has 3 color channels (RGB)
33
+ if len(img_array.shape) == 2: # If grayscale
34
+ img_array = np.stack((img_array,)*3, axis=-1)
35
+
36
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
37
+
38
+ # NOTE: If you trained your model with normalization (e.g. dividing by 255), uncomment the next line:
39
+ # img_array = img_array / 255.0
40
+
41
+ prediction = mri_model.predict(img_array)[0]
42
+ confidences = {mri_labels[i]: float(prediction[i]) for i in range(len(mri_labels))}
43
+ return confidences
44
+
45
+ # 4. Prediction Function for X-Ray
46
+ def predict_xray(img):
47
+ if xray_model is None: return {"Error": 0.0}
48
+ if img is None: return {"No image": 0.0}
49
+
50
+ # Resize to 128x128
51
+ img = img.resize((128, 128))
52
+ img_array = np.array(img)
53
+
54
+ # Ensure image has 3 color channels (RGB)
55
+ if len(img_array.shape) == 2: # If grayscale
56
+ img_array = np.stack((img_array,)*3, axis=-1)
57
+
58
+ img_array = np.expand_dims(img_array, axis=0)
59
+
60
+ # NOTE: If you trained your model with normalization, uncomment the next line:
61
+ # img_array = img_array / 255.0
62
+
63
+ prediction = xray_model.predict(img_array)[0]
64
+ confidences = {xray_labels[i]: float(prediction[i]) for i in range(len(xray_labels))}
65
+ return confidences
66
+
67
+ # 5. Build the UI / API
68
+ with gr.Blocks() as demo:
69
+ gr.Markdown("# Medical Image Classification API")
70
+
71
+ with gr.Tab("MRI Classifier"):
72
+ mri_input = gr.Image(type="pil", label="Upload MRI Image")
73
+ # num_top_classes=1 ensures we only return the highest confidence score
74
+ mri_output = gr.Label(num_top_classes=1, label="Result")
75
+ mri_btn = gr.Button("Predict MRI")
76
+ mri_btn.click(fn=predict_mri, inputs=mri_input, outputs=mri_output, api_name="predict_mri")
77
+
78
+ with gr.Tab("X-Ray Classifier"):
79
+ xray_input = gr.Image(type="pil", label="Upload X-Ray Image")
80
+ xray_output = gr.Label(num_top_classes=1, label="Result")
81
+ xray_btn = gr.Button("Predict X-Ray")
82
+ xray_btn.click(fn=predict_xray, inputs=xray_input, outputs=xray_output, api_name="predict_xray")
83
+
84
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ tensorflow
2
+ numpy
3
+ pillow
4
+ gradio