Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,16 @@ import tensorflow as tf
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image, ImageOps
|
| 5 |
import socket
|
|
|
|
| 6 |
def find_free_port():
|
| 7 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
| 8 |
-
s.bind(('', 0))
|
| 9 |
return s.getsockname()[1]
|
| 10 |
|
| 11 |
free_port = find_free_port()
|
| 12 |
print(f"Launching Gradio on free port: {free_port}")
|
| 13 |
-
|
|
|
|
| 14 |
model = tf.keras.models.load_model('best_cnnmodelf&n.h5')
|
| 15 |
|
| 16 |
class_names = ['glioma_tumor', 'meningioma_tumor', 'no_tumor', 'pituitary_tumor']
|
|
@@ -18,8 +20,8 @@ class_names = ['glioma_tumor', 'meningioma_tumor', 'no_tumor', 'pituitary_tumor'
|
|
| 18 |
def preprocess(image):
|
| 19 |
size = (150, 150)
|
| 20 |
image = ImageOps.fit(image, size, Image.ANTIALIAS)
|
| 21 |
-
image = np.array(image) / 255.0
|
| 22 |
-
image = np.expand_dims(image, axis=0)
|
| 23 |
return image
|
| 24 |
|
| 25 |
def classify(image):
|
|
@@ -31,25 +33,20 @@ def classify(image):
|
|
| 31 |
confidence = scores[top_idx] * 100
|
| 32 |
return f"Prediction: {predicted_class}", f"Confidence: {confidence:.2f}%"
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
Upload a brain MRI scan image (jpg or png). The model will classify the tumor type as:
|
| 38 |
- Glioma Tumor
|
| 39 |
- Meningioma Tumor
|
| 40 |
- No Tumor
|
| 41 |
- Pituitary Tumor
|
| 42 |
-
"""
|
| 43 |
-
|
| 44 |
-
iface = gr.Interface(
|
| 45 |
-
fn=classify,
|
| 46 |
-
inputs=gr.Image(type="pil", label="Upload MRI scan"),
|
| 47 |
-
outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Confidence")],
|
| 48 |
-
title=title,
|
| 49 |
-
description=description,
|
| 50 |
allow_flagging="never",
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
-
iface.launch()
|
| 55 |
-
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image, ImageOps
|
| 5 |
import socket
|
| 6 |
+
|
| 7 |
def find_free_port():
|
| 8 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
| 9 |
+
s.bind(('', 0))
|
| 10 |
return s.getsockname()[1]
|
| 11 |
|
| 12 |
free_port = find_free_port()
|
| 13 |
print(f"Launching Gradio on free port: {free_port}")
|
| 14 |
+
|
| 15 |
+
# Make sure this filename is correct!
|
| 16 |
model = tf.keras.models.load_model('best_cnnmodelf&n.h5')
|
| 17 |
|
| 18 |
class_names = ['glioma_tumor', 'meningioma_tumor', 'no_tumor', 'pituitary_tumor']
|
|
|
|
| 20 |
def preprocess(image):
|
| 21 |
size = (150, 150)
|
| 22 |
image = ImageOps.fit(image, size, Image.ANTIALIAS)
|
| 23 |
+
image = np.array(image) / 255.0
|
| 24 |
+
image = np.expand_dims(image, axis=0)
|
| 25 |
return image
|
| 26 |
|
| 27 |
def classify(image):
|
|
|
|
| 33 |
confidence = scores[top_idx] * 100
|
| 34 |
return f"Prediction: {predicted_class}", f"Confidence: {confidence:.2f}%"
|
| 35 |
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=classify,
|
| 38 |
+
inputs=gr.Image(type="pil", label="Upload MRI scan"),
|
| 39 |
+
outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Confidence")],
|
| 40 |
+
title="Brain Tumor Classification",
|
| 41 |
+
description="""
|
| 42 |
Upload a brain MRI scan image (jpg or png). The model will classify the tumor type as:
|
| 43 |
- Glioma Tumor
|
| 44 |
- Meningioma Tumor
|
| 45 |
- No Tumor
|
| 46 |
- Pituitary Tumor
|
| 47 |
+
""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
allow_flagging="never",
|
| 49 |
)
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
| 52 |
+
iface.launch(server_port=free_port)
|
|
|