Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def predict_image(image):
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
from keras.models import load_model
|
5 |
+
|
6 |
+
# Load your pre-trained model (make sure the model file is in the same directory)
|
7 |
+
model = load_model('brain_tumor_model.h5')
|
8 |
+
|
9 |
+
# Function to process image and make predictions
|
10 |
def predict_image(image):
|
11 |
+
# Resize the image
|
12 |
+
img = image.resize((128, 128))
|
13 |
+
|
14 |
+
# Convert the image to a NumPy array
|
15 |
+
img = np.array(img)
|
16 |
+
|
17 |
+
# Check if the image has 3 color channels
|
18 |
+
if img.shape == (128, 128): # If grayscale, convert to RGB
|
19 |
+
img = np.stack((img,) * 3, axis=-1)
|
20 |
+
|
21 |
+
# Add a batch dimension
|
22 |
+
img = np.expand_dims(img, axis=0)
|
23 |
+
|
24 |
+
# Make the prediction
|
25 |
+
prediction = model.predict(img)
|
26 |
+
|
27 |
+
# Get the predicted class and confidence level
|
28 |
+
predicted_class = np.argmax(prediction)
|
29 |
+
confidence = np.max(prediction)
|
30 |
+
|
31 |
+
# Return the results
|
32 |
+
if predicted_class == 0:
|
33 |
+
return f'No tumor detected. Confidence: {confidence:.2f}'
|
34 |
+
else:
|
35 |
+
return f'Tumor detected. Confidence: {confidence:.2f}'
|
36 |
+
|
37 |
+
# Create custom CSS for background color
|
38 |
+
css = """
|
39 |
+
body {
|
40 |
+
background-color: #f0f4f7;
|
41 |
+
}
|
42 |
+
"""
|
43 |
+
|
44 |
+
|
45 |
+
# Create the Gradio interface
|
46 |
+
iface = gr.Interface(
|
47 |
+
fn=predict_image,
|
48 |
+
inputs=gr.Image(type="pil"),
|
49 |
+
outputs=gr.Textbox(),
|
50 |
+
title="Brain Tumor Detection AI App",
|
51 |
+
description="Upload an image to detect brain tumors.",
|
52 |
+
css=css, # Apply the custom background color
|
53 |
+
theme="dark", # Apply a dark theme to the interface
|
54 |
+
flagging_options=["Incorrect Diagnosis", "Image Not Clear", "Model Error"], # Add flagging options
|
55 |
+
)
|
56 |
+
|
57 |
+
# Launch the interface
|
58 |
+
iface.launch()
|