Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,55 +2,50 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
from tensorflow.keras.models import load_model
|
4 |
from tensorflow.keras.preprocessing import image
|
|
|
5 |
|
6 |
# Load your custom model
|
7 |
model = load_model("lung_cancer_detection_model.h5", compile=False)
|
8 |
|
9 |
-
#
|
10 |
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
|
11 |
|
12 |
-
|
13 |
-
|
14 |
# Function to preprocess the uploaded image
|
15 |
-
def predict_single_image(
|
16 |
-
#
|
17 |
-
img =
|
18 |
-
img =
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
21 |
|
22 |
# Predict the class probabilities
|
23 |
-
probabilities = model.predict(
|
24 |
-
# display(probabilities)
|
25 |
|
26 |
# Determine the predicted class label
|
27 |
predicted_class = "positive" if probabilities[0][0] > 0.5 else "negative"
|
28 |
|
29 |
return predicted_class, probabilities[0][0]
|
30 |
|
31 |
-
|
32 |
# Function to classify the uploaded image
|
33 |
def classify_lung_cancer(img):
|
34 |
# Call the function to predict the class label for the single image
|
35 |
-
predicted_label, confidence = predict_single_image(
|
36 |
-
img, model, target_size=(512, 512)
|
37 |
-
)
|
38 |
|
39 |
-
#
|
40 |
-
# print('Predicted Label:', predicted_label)
|
41 |
-
# print('Confidence:', confidence)
|
42 |
return f"Prediction: {predicted_label}\n(Confidence: {confidence:.2f})"
|
43 |
|
44 |
-
|
45 |
# Define the Gradio interface
|
46 |
iface = gr.Interface(
|
47 |
fn=classify_lung_cancer,
|
48 |
-
inputs=gr.
|
49 |
-
outputs=gr.
|
50 |
live=True,
|
51 |
title="Lung Cancer Classification",
|
52 |
description="Upload an image and the model will classify it as positive or negative for lung cancer.",
|
53 |
)
|
54 |
|
55 |
# Create a Gradio interface
|
56 |
-
iface.launch()
|
|
|
2 |
import numpy as np
|
3 |
from tensorflow.keras.models import load_model
|
4 |
from tensorflow.keras.preprocessing import image
|
5 |
+
from PIL import Image
|
6 |
|
7 |
# Load your custom model
|
8 |
model = load_model("lung_cancer_detection_model.h5", compile=False)
|
9 |
|
10 |
+
# Compile your model using the optimizer
|
11 |
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
|
12 |
|
|
|
|
|
13 |
# Function to preprocess the uploaded image
|
14 |
+
def predict_single_image(img, model, target_size=(128, 128)):
|
15 |
+
# Convert the PIL image to grayscale and resize
|
16 |
+
img = img.convert("L") # Convert to grayscale
|
17 |
+
img = img.resize(target_size) # Resize to target size
|
18 |
+
|
19 |
+
# Convert image to array
|
20 |
+
img_array = np.array(img)
|
21 |
+
img_array = np.expand_dims(img_array, axis=0) # Expand dimensions for batch
|
22 |
+
img_array = img_array / 255.0 # Normalize the image
|
23 |
|
24 |
# Predict the class probabilities
|
25 |
+
probabilities = model.predict(img_array)
|
|
|
26 |
|
27 |
# Determine the predicted class label
|
28 |
predicted_class = "positive" if probabilities[0][0] > 0.5 else "negative"
|
29 |
|
30 |
return predicted_class, probabilities[0][0]
|
31 |
|
|
|
32 |
# Function to classify the uploaded image
|
33 |
def classify_lung_cancer(img):
|
34 |
# Call the function to predict the class label for the single image
|
35 |
+
predicted_label, confidence = predict_single_image(img, model, target_size=(128, 128))
|
|
|
|
|
36 |
|
37 |
+
# Return the formatted prediction
|
|
|
|
|
38 |
return f"Prediction: {predicted_label}\n(Confidence: {confidence:.2f})"
|
39 |
|
|
|
40 |
# Define the Gradio interface
|
41 |
iface = gr.Interface(
|
42 |
fn=classify_lung_cancer,
|
43 |
+
inputs=gr.Image(type="pil"), # Pass the PIL image directly
|
44 |
+
outputs=gr.Textbox(),
|
45 |
live=True,
|
46 |
title="Lung Cancer Classification",
|
47 |
description="Upload an image and the model will classify it as positive or negative for lung cancer.",
|
48 |
)
|
49 |
|
50 |
# Create a Gradio interface
|
51 |
+
iface.launch(share=True)
|